• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef Py_ERRORS_H
2 #define Py_ERRORS_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 /* Error objects */
8 
9 typedef struct {
10     PyObject_HEAD
11     PyObject *dict;
12     PyObject *args;
13     PyObject *message;
14 } PyBaseExceptionObject;
15 
16 typedef struct {
17     PyObject_HEAD
18     PyObject *dict;
19     PyObject *args;
20     PyObject *message;
21     PyObject *msg;
22     PyObject *filename;
23     PyObject *lineno;
24     PyObject *offset;
25     PyObject *text;
26     PyObject *print_file_and_line;
27 } PySyntaxErrorObject;
28 
29 #ifdef Py_USING_UNICODE
30 typedef struct {
31     PyObject_HEAD
32     PyObject *dict;
33     PyObject *args;
34     PyObject *message;
35     PyObject *encoding;
36     PyObject *object;
37     Py_ssize_t start;
38     Py_ssize_t end;
39     PyObject *reason;
40 } PyUnicodeErrorObject;
41 #endif
42 
43 typedef struct {
44     PyObject_HEAD
45     PyObject *dict;
46     PyObject *args;
47     PyObject *message;
48     PyObject *code;
49 } PySystemExitObject;
50 
51 typedef struct {
52     PyObject_HEAD
53     PyObject *dict;
54     PyObject *args;
55     PyObject *message;
56     PyObject *myerrno;
57     PyObject *strerror;
58     PyObject *filename;
59 } PyEnvironmentErrorObject;
60 
61 #ifdef MS_WINDOWS
62 typedef struct {
63     PyObject_HEAD
64     PyObject *dict;
65     PyObject *args;
66     PyObject *message;
67     PyObject *myerrno;
68     PyObject *strerror;
69     PyObject *filename;
70     PyObject *winerror;
71 } PyWindowsErrorObject;
72 #endif
73 
74 /* Error handling definitions */
75 
76 PyAPI_FUNC(void) PyErr_SetNone(PyObject *);
77 PyAPI_FUNC(void) PyErr_SetObject(PyObject *, PyObject *);
78 PyAPI_FUNC(void) PyErr_SetString(PyObject *, const char *);
79 PyAPI_FUNC(PyObject *) PyErr_Occurred(void);
80 PyAPI_FUNC(void) PyErr_Clear(void);
81 PyAPI_FUNC(void) PyErr_Fetch(PyObject **, PyObject **, PyObject **);
82 PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, PyObject *);
83 
84 #ifdef Py_DEBUG
85 #define _PyErr_OCCURRED() PyErr_Occurred()
86 #else
87 #define _PyErr_OCCURRED() (_PyThreadState_Current->curexc_type)
88 #endif
89 
90 /* Error testing and normalization */
91 PyAPI_FUNC(int) PyErr_GivenExceptionMatches(PyObject *, PyObject *);
92 PyAPI_FUNC(int) PyErr_ExceptionMatches(PyObject *);
93 PyAPI_FUNC(void) PyErr_NormalizeException(PyObject**, PyObject**, PyObject**);
94 PyAPI_FUNC(void) _PyErr_ReplaceException(PyObject *, PyObject *, PyObject *);
95 
96 /* */
97 
98 #define PyExceptionClass_Check(x)                                       \
99     (PyClass_Check((x)) || (PyType_Check((x)) &&                        \
100       PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)))
101 
102 #define PyExceptionInstance_Check(x)                    \
103     (PyInstance_Check((x)) ||                           \
104      PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS))
105 
106 #define PyExceptionClass_Name(x)                                   \
107     (PyClass_Check((x))                                            \
108      ? PyString_AS_STRING(((PyClassObject*)(x))->cl_name)          \
109      : (char *)(((PyTypeObject*)(x))->tp_name))
110 
111 #define PyExceptionInstance_Class(x)                                    \
112     ((PyInstance_Check((x))                                             \
113       ? (PyObject*)((PyInstanceObject*)(x))->in_class                   \
114       : (PyObject*)((x)->ob_type)))
115 
116 
117 /* Predefined exceptions */
118 
119 PyAPI_DATA(PyObject *) PyExc_BaseException;
120 PyAPI_DATA(PyObject *) PyExc_Exception;
121 PyAPI_DATA(PyObject *) PyExc_StopIteration;
122 PyAPI_DATA(PyObject *) PyExc_GeneratorExit;
123 PyAPI_DATA(PyObject *) PyExc_StandardError;
124 PyAPI_DATA(PyObject *) PyExc_ArithmeticError;
125 PyAPI_DATA(PyObject *) PyExc_LookupError;
126 
127 PyAPI_DATA(PyObject *) PyExc_AssertionError;
128 PyAPI_DATA(PyObject *) PyExc_AttributeError;
129 PyAPI_DATA(PyObject *) PyExc_EOFError;
130 PyAPI_DATA(PyObject *) PyExc_FloatingPointError;
131 PyAPI_DATA(PyObject *) PyExc_EnvironmentError;
132 PyAPI_DATA(PyObject *) PyExc_IOError;
133 PyAPI_DATA(PyObject *) PyExc_OSError;
134 PyAPI_DATA(PyObject *) PyExc_ImportError;
135 PyAPI_DATA(PyObject *) PyExc_IndexError;
136 PyAPI_DATA(PyObject *) PyExc_KeyError;
137 PyAPI_DATA(PyObject *) PyExc_KeyboardInterrupt;
138 PyAPI_DATA(PyObject *) PyExc_MemoryError;
139 PyAPI_DATA(PyObject *) PyExc_NameError;
140 PyAPI_DATA(PyObject *) PyExc_OverflowError;
141 PyAPI_DATA(PyObject *) PyExc_RuntimeError;
142 PyAPI_DATA(PyObject *) PyExc_NotImplementedError;
143 PyAPI_DATA(PyObject *) PyExc_SyntaxError;
144 PyAPI_DATA(PyObject *) PyExc_IndentationError;
145 PyAPI_DATA(PyObject *) PyExc_TabError;
146 PyAPI_DATA(PyObject *) PyExc_ReferenceError;
147 PyAPI_DATA(PyObject *) PyExc_SystemError;
148 PyAPI_DATA(PyObject *) PyExc_SystemExit;
149 PyAPI_DATA(PyObject *) PyExc_TypeError;
150 PyAPI_DATA(PyObject *) PyExc_UnboundLocalError;
151 PyAPI_DATA(PyObject *) PyExc_UnicodeError;
152 PyAPI_DATA(PyObject *) PyExc_UnicodeEncodeError;
153 PyAPI_DATA(PyObject *) PyExc_UnicodeDecodeError;
154 PyAPI_DATA(PyObject *) PyExc_UnicodeTranslateError;
155 PyAPI_DATA(PyObject *) PyExc_ValueError;
156 PyAPI_DATA(PyObject *) PyExc_ZeroDivisionError;
157 #ifdef MS_WINDOWS
158 PyAPI_DATA(PyObject *) PyExc_WindowsError;
159 #endif
160 #ifdef __VMS
161 PyAPI_DATA(PyObject *) PyExc_VMSError;
162 #endif
163 
164 PyAPI_DATA(PyObject *) PyExc_BufferError;
165 
166 PyAPI_DATA(PyObject *) PyExc_MemoryErrorInst;
167 PyAPI_DATA(PyObject *) PyExc_RecursionErrorInst;
168 
169 /* Predefined warning categories */
170 PyAPI_DATA(PyObject *) PyExc_Warning;
171 PyAPI_DATA(PyObject *) PyExc_UserWarning;
172 PyAPI_DATA(PyObject *) PyExc_DeprecationWarning;
173 PyAPI_DATA(PyObject *) PyExc_PendingDeprecationWarning;
174 PyAPI_DATA(PyObject *) PyExc_SyntaxWarning;
175 PyAPI_DATA(PyObject *) PyExc_RuntimeWarning;
176 PyAPI_DATA(PyObject *) PyExc_FutureWarning;
177 PyAPI_DATA(PyObject *) PyExc_ImportWarning;
178 PyAPI_DATA(PyObject *) PyExc_UnicodeWarning;
179 PyAPI_DATA(PyObject *) PyExc_BytesWarning;
180 
181 
182 /* Convenience functions */
183 
184 PyAPI_FUNC(int) PyErr_BadArgument(void);
185 PyAPI_FUNC(PyObject *) PyErr_NoMemory(void);
186 PyAPI_FUNC(PyObject *) PyErr_SetFromErrno(PyObject *);
187 PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObject(
188     PyObject *, PyObject *);
189 PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilename(
190     PyObject *, const char *);
191 #ifdef MS_WINDOWS
192 PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithUnicodeFilename(
193     PyObject *, const Py_UNICODE *);
194 #endif /* MS_WINDOWS */
195 
196 PyAPI_FUNC(PyObject *) PyErr_Format(PyObject *, const char *, ...)
197                         Py_GCC_ATTRIBUTE((format(printf, 2, 3)));
198 
199 #ifdef MS_WINDOWS
200 PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilenameObject(
201     int, const char *);
202 PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(
203     int, const char *);
204 PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithUnicodeFilename(
205     int, const Py_UNICODE *);
206 PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErr(int);
207 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObject(
208     PyObject *,int, PyObject *);
209 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilename(
210     PyObject *,int, const char *);
211 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithUnicodeFilename(
212     PyObject *,int, const Py_UNICODE *);
213 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);
214 #endif /* MS_WINDOWS */
215 
216 /* Export the old function so that the existing API remains available: */
217 PyAPI_FUNC(void) PyErr_BadInternalCall(void);
218 PyAPI_FUNC(void) _PyErr_BadInternalCall(const char *filename, int lineno);
219 /* Mask the old API with a call to the new API for code compiled under
220    Python 2.0: */
221 #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
222 
223 /* Function to create a new exception */
224 PyAPI_FUNC(PyObject *) PyErr_NewException(
225     char *name, PyObject *base, PyObject *dict);
226 PyAPI_FUNC(PyObject *) PyErr_NewExceptionWithDoc(
227     char *name, char *doc, PyObject *base, PyObject *dict);
228 PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *);
229 
230 /* In sigcheck.c or signalmodule.c */
231 PyAPI_FUNC(int) PyErr_CheckSignals(void);
232 PyAPI_FUNC(void) PyErr_SetInterrupt(void);
233 
234 /* In signalmodule.c */
235 int PySignal_SetWakeupFd(int fd);
236 
237 /* Support for adding program text to SyntaxErrors */
238 PyAPI_FUNC(void) PyErr_SyntaxLocation(const char *, int);
239 PyAPI_FUNC(PyObject *) PyErr_ProgramText(const char *, int);
240 
241 #ifdef Py_USING_UNICODE
242 /* The following functions are used to create and modify unicode
243    exceptions from C */
244 
245 /* create a UnicodeDecodeError object */
246 PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_Create(
247     const char *, const char *, Py_ssize_t, Py_ssize_t, Py_ssize_t, const char *);
248 
249 /* create a UnicodeEncodeError object */
250 PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_Create(
251     const char *, const Py_UNICODE *, Py_ssize_t, Py_ssize_t, Py_ssize_t, const char *);
252 
253 /* create a UnicodeTranslateError object */
254 PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_Create(
255     const Py_UNICODE *, Py_ssize_t, Py_ssize_t, Py_ssize_t, const char *);
256 
257 /* get the encoding attribute */
258 PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetEncoding(PyObject *);
259 PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetEncoding(PyObject *);
260 
261 /* get the object attribute */
262 PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetObject(PyObject *);
263 PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetObject(PyObject *);
264 PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetObject(PyObject *);
265 
266 /* get the value of the start attribute (the int * may not be NULL)
267    return 0 on success, -1 on failure */
268 PyAPI_FUNC(int) PyUnicodeEncodeError_GetStart(PyObject *, Py_ssize_t *);
269 PyAPI_FUNC(int) PyUnicodeDecodeError_GetStart(PyObject *, Py_ssize_t *);
270 PyAPI_FUNC(int) PyUnicodeTranslateError_GetStart(PyObject *, Py_ssize_t *);
271 
272 /* assign a new value to the start attribute
273    return 0 on success, -1 on failure */
274 PyAPI_FUNC(int) PyUnicodeEncodeError_SetStart(PyObject *, Py_ssize_t);
275 PyAPI_FUNC(int) PyUnicodeDecodeError_SetStart(PyObject *, Py_ssize_t);
276 PyAPI_FUNC(int) PyUnicodeTranslateError_SetStart(PyObject *, Py_ssize_t);
277 
278 /* get the value of the end attribute (the int *may not be NULL)
279  return 0 on success, -1 on failure */
280 PyAPI_FUNC(int) PyUnicodeEncodeError_GetEnd(PyObject *, Py_ssize_t *);
281 PyAPI_FUNC(int) PyUnicodeDecodeError_GetEnd(PyObject *, Py_ssize_t *);
282 PyAPI_FUNC(int) PyUnicodeTranslateError_GetEnd(PyObject *, Py_ssize_t *);
283 
284 /* assign a new value to the end attribute
285    return 0 on success, -1 on failure */
286 PyAPI_FUNC(int) PyUnicodeEncodeError_SetEnd(PyObject *, Py_ssize_t);
287 PyAPI_FUNC(int) PyUnicodeDecodeError_SetEnd(PyObject *, Py_ssize_t);
288 PyAPI_FUNC(int) PyUnicodeTranslateError_SetEnd(PyObject *, Py_ssize_t);
289 
290 /* get the value of the reason attribute */
291 PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetReason(PyObject *);
292 PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetReason(PyObject *);
293 PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetReason(PyObject *);
294 
295 /* assign a new value to the reason attribute
296    return 0 on success, -1 on failure */
297 PyAPI_FUNC(int) PyUnicodeEncodeError_SetReason(
298     PyObject *, const char *);
299 PyAPI_FUNC(int) PyUnicodeDecodeError_SetReason(
300     PyObject *, const char *);
301 PyAPI_FUNC(int) PyUnicodeTranslateError_SetReason(
302     PyObject *, const char *);
303 #endif
304 
305 
306 /* These APIs aren't really part of the error implementation, but
307    often needed to format error messages; the native C lib APIs are
308    not available on all platforms, which is why we provide emulations
309    for those platforms in Python/mysnprintf.c,
310    WARNING:  The return value of snprintf varies across platforms; do
311    not rely on any particular behavior; eventually the C99 defn may
312    be reliable.
313 */
314 #if defined(MS_WIN32) && !defined(HAVE_SNPRINTF)
315 # define HAVE_SNPRINTF
316 # define snprintf _snprintf
317 # define vsnprintf _vsnprintf
318 #endif
319 
320 #include <stdarg.h>
321 PyAPI_FUNC(int) PyOS_snprintf(char *str, size_t size, const char  *format, ...)
322                         Py_GCC_ATTRIBUTE((format(printf, 3, 4)));
323 PyAPI_FUNC(int) PyOS_vsnprintf(char *str, size_t size, const char  *format, va_list va)
324                         Py_GCC_ATTRIBUTE((format(printf, 3, 0)));
325 
326 #ifdef __cplusplus
327 }
328 #endif
329 #endif /* !Py_ERRORS_H */
330