• 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 #include <stdarg.h>               // va_list
8 
9 /* Error handling definitions */
10 
11 PyAPI_FUNC(void) PyErr_SetNone(PyObject *);
12 PyAPI_FUNC(void) PyErr_SetObject(PyObject *, PyObject *);
13 PyAPI_FUNC(void) PyErr_SetString(
14     PyObject *exception,
15     const char *string   /* decoded from utf-8 */
16     );
17 PyAPI_FUNC(PyObject *) PyErr_Occurred(void);
18 PyAPI_FUNC(void) PyErr_Clear(void);
19 PyAPI_FUNC(void) PyErr_Fetch(PyObject **, PyObject **, PyObject **);
20 PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, PyObject *);
21 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
22 PyAPI_FUNC(void) PyErr_GetExcInfo(PyObject **, PyObject **, PyObject **);
23 PyAPI_FUNC(void) PyErr_SetExcInfo(PyObject *, PyObject *, PyObject *);
24 #endif
25 
26 /* Defined in Python/pylifecycle.c
27 
28    The Py_FatalError() function is replaced with a macro which logs
29    automatically the name of the current function, unless the Py_LIMITED_API
30    macro is defined. */
31 PyAPI_FUNC(void) _Py_NO_RETURN Py_FatalError(const char *message);
32 
33 /* Error testing and normalization */
34 PyAPI_FUNC(int) PyErr_GivenExceptionMatches(PyObject *, PyObject *);
35 PyAPI_FUNC(int) PyErr_ExceptionMatches(PyObject *);
36 PyAPI_FUNC(void) PyErr_NormalizeException(PyObject**, PyObject**, PyObject**);
37 
38 /* Traceback manipulation (PEP 3134) */
39 PyAPI_FUNC(int) PyException_SetTraceback(PyObject *, PyObject *);
40 PyAPI_FUNC(PyObject *) PyException_GetTraceback(PyObject *);
41 
42 /* Cause manipulation (PEP 3134) */
43 PyAPI_FUNC(PyObject *) PyException_GetCause(PyObject *);
44 PyAPI_FUNC(void) PyException_SetCause(PyObject *, PyObject *);
45 
46 /* Context manipulation (PEP 3134) */
47 PyAPI_FUNC(PyObject *) PyException_GetContext(PyObject *);
48 PyAPI_FUNC(void) PyException_SetContext(PyObject *, PyObject *);
49 
50 /* */
51 
52 #define PyExceptionClass_Check(x)                                       \
53     (PyType_Check((x)) &&                                               \
54      PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
55 
56 #define PyExceptionInstance_Check(x)                    \
57     PyType_FastSubclass(Py_TYPE(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)
58 
59 PyAPI_FUNC(const char *) PyExceptionClass_Name(PyObject *);
60 
61 #define PyExceptionInstance_Class(x) ((PyObject*)Py_TYPE(x))
62 
63 
64 /* Predefined exceptions */
65 
66 PyAPI_DATA(PyObject *) PyExc_BaseException;
67 PyAPI_DATA(PyObject *) PyExc_Exception;
68 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
69 PyAPI_DATA(PyObject *) PyExc_StopAsyncIteration;
70 #endif
71 PyAPI_DATA(PyObject *) PyExc_StopIteration;
72 PyAPI_DATA(PyObject *) PyExc_GeneratorExit;
73 PyAPI_DATA(PyObject *) PyExc_ArithmeticError;
74 PyAPI_DATA(PyObject *) PyExc_LookupError;
75 
76 PyAPI_DATA(PyObject *) PyExc_AssertionError;
77 PyAPI_DATA(PyObject *) PyExc_AttributeError;
78 PyAPI_DATA(PyObject *) PyExc_BufferError;
79 PyAPI_DATA(PyObject *) PyExc_EOFError;
80 PyAPI_DATA(PyObject *) PyExc_FloatingPointError;
81 PyAPI_DATA(PyObject *) PyExc_OSError;
82 PyAPI_DATA(PyObject *) PyExc_ImportError;
83 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060000
84 PyAPI_DATA(PyObject *) PyExc_ModuleNotFoundError;
85 #endif
86 PyAPI_DATA(PyObject *) PyExc_IndexError;
87 PyAPI_DATA(PyObject *) PyExc_KeyError;
88 PyAPI_DATA(PyObject *) PyExc_KeyboardInterrupt;
89 PyAPI_DATA(PyObject *) PyExc_MemoryError;
90 PyAPI_DATA(PyObject *) PyExc_NameError;
91 PyAPI_DATA(PyObject *) PyExc_OverflowError;
92 PyAPI_DATA(PyObject *) PyExc_RuntimeError;
93 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
94 PyAPI_DATA(PyObject *) PyExc_RecursionError;
95 #endif
96 PyAPI_DATA(PyObject *) PyExc_NotImplementedError;
97 PyAPI_DATA(PyObject *) PyExc_SyntaxError;
98 PyAPI_DATA(PyObject *) PyExc_IndentationError;
99 PyAPI_DATA(PyObject *) PyExc_TabError;
100 PyAPI_DATA(PyObject *) PyExc_ReferenceError;
101 PyAPI_DATA(PyObject *) PyExc_SystemError;
102 PyAPI_DATA(PyObject *) PyExc_SystemExit;
103 PyAPI_DATA(PyObject *) PyExc_TypeError;
104 PyAPI_DATA(PyObject *) PyExc_UnboundLocalError;
105 PyAPI_DATA(PyObject *) PyExc_UnicodeError;
106 PyAPI_DATA(PyObject *) PyExc_UnicodeEncodeError;
107 PyAPI_DATA(PyObject *) PyExc_UnicodeDecodeError;
108 PyAPI_DATA(PyObject *) PyExc_UnicodeTranslateError;
109 PyAPI_DATA(PyObject *) PyExc_ValueError;
110 PyAPI_DATA(PyObject *) PyExc_ZeroDivisionError;
111 
112 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
113 PyAPI_DATA(PyObject *) PyExc_BlockingIOError;
114 PyAPI_DATA(PyObject *) PyExc_BrokenPipeError;
115 PyAPI_DATA(PyObject *) PyExc_ChildProcessError;
116 PyAPI_DATA(PyObject *) PyExc_ConnectionError;
117 PyAPI_DATA(PyObject *) PyExc_ConnectionAbortedError;
118 PyAPI_DATA(PyObject *) PyExc_ConnectionRefusedError;
119 PyAPI_DATA(PyObject *) PyExc_ConnectionResetError;
120 PyAPI_DATA(PyObject *) PyExc_FileExistsError;
121 PyAPI_DATA(PyObject *) PyExc_FileNotFoundError;
122 PyAPI_DATA(PyObject *) PyExc_InterruptedError;
123 PyAPI_DATA(PyObject *) PyExc_IsADirectoryError;
124 PyAPI_DATA(PyObject *) PyExc_NotADirectoryError;
125 PyAPI_DATA(PyObject *) PyExc_PermissionError;
126 PyAPI_DATA(PyObject *) PyExc_ProcessLookupError;
127 PyAPI_DATA(PyObject *) PyExc_TimeoutError;
128 #endif
129 
130 
131 /* Compatibility aliases */
132 PyAPI_DATA(PyObject *) PyExc_EnvironmentError;
133 PyAPI_DATA(PyObject *) PyExc_IOError;
134 #ifdef MS_WINDOWS
135 PyAPI_DATA(PyObject *) PyExc_WindowsError;
136 #endif
137 
138 /* Predefined warning categories */
139 PyAPI_DATA(PyObject *) PyExc_Warning;
140 PyAPI_DATA(PyObject *) PyExc_UserWarning;
141 PyAPI_DATA(PyObject *) PyExc_DeprecationWarning;
142 PyAPI_DATA(PyObject *) PyExc_PendingDeprecationWarning;
143 PyAPI_DATA(PyObject *) PyExc_SyntaxWarning;
144 PyAPI_DATA(PyObject *) PyExc_RuntimeWarning;
145 PyAPI_DATA(PyObject *) PyExc_FutureWarning;
146 PyAPI_DATA(PyObject *) PyExc_ImportWarning;
147 PyAPI_DATA(PyObject *) PyExc_UnicodeWarning;
148 PyAPI_DATA(PyObject *) PyExc_BytesWarning;
149 PyAPI_DATA(PyObject *) PyExc_EncodingWarning;
150 PyAPI_DATA(PyObject *) PyExc_ResourceWarning;
151 
152 
153 /* Convenience functions */
154 
155 PyAPI_FUNC(int) PyErr_BadArgument(void);
156 PyAPI_FUNC(PyObject *) PyErr_NoMemory(void);
157 PyAPI_FUNC(PyObject *) PyErr_SetFromErrno(PyObject *);
158 PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObject(
159     PyObject *, PyObject *);
160 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
161 PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObjects(
162     PyObject *, PyObject *, PyObject *);
163 #endif
164 PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilename(
165     PyObject *exc,
166     const char *filename   /* decoded from the filesystem encoding */
167     );
168 
169 PyAPI_FUNC(PyObject *) PyErr_Format(
170     PyObject *exception,
171     const char *format,   /* ASCII-encoded string  */
172     ...
173     );
174 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
175 PyAPI_FUNC(PyObject *) PyErr_FormatV(
176     PyObject *exception,
177     const char *format,
178     va_list vargs);
179 #endif
180 
181 #ifdef MS_WINDOWS
182 PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(
183     int ierr,
184     const char *filename        /* decoded from the filesystem encoding */
185     );
186 PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErr(int);
187 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObject(
188     PyObject *,int, PyObject *);
189 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
190 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObjects(
191     PyObject *,int, PyObject *, PyObject *);
192 #endif
193 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilename(
194     PyObject *exc,
195     int ierr,
196     const char *filename        /* decoded from the filesystem encoding */
197     );
198 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);
199 #endif /* MS_WINDOWS */
200 
201 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060000
202 PyAPI_FUNC(PyObject *) PyErr_SetImportErrorSubclass(PyObject *, PyObject *,
203     PyObject *, PyObject *);
204 #endif
205 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
206 PyAPI_FUNC(PyObject *) PyErr_SetImportError(PyObject *, PyObject *,
207     PyObject *);
208 #endif
209 
210 /* Export the old function so that the existing API remains available: */
211 PyAPI_FUNC(void) PyErr_BadInternalCall(void);
212 PyAPI_FUNC(void) _PyErr_BadInternalCall(const char *filename, int lineno);
213 /* Mask the old API with a call to the new API for code compiled under
214    Python 2.0: */
215 #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
216 
217 /* Function to create a new exception */
218 PyAPI_FUNC(PyObject *) PyErr_NewException(
219     const char *name, PyObject *base, PyObject *dict);
220 PyAPI_FUNC(PyObject *) PyErr_NewExceptionWithDoc(
221     const char *name, const char *doc, PyObject *base, PyObject *dict);
222 PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *);
223 
224 
225 /* In signalmodule.c */
226 PyAPI_FUNC(int) PyErr_CheckSignals(void);
227 PyAPI_FUNC(void) PyErr_SetInterrupt(void);
228 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
229 PyAPI_FUNC(int) PyErr_SetInterruptEx(int signum);
230 #endif
231 
232 /* Support for adding program text to SyntaxErrors */
233 PyAPI_FUNC(void) PyErr_SyntaxLocation(
234     const char *filename,       /* decoded from the filesystem encoding */
235     int lineno);
236 PyAPI_FUNC(void) PyErr_SyntaxLocationEx(
237     const char *filename,       /* decoded from the filesystem encoding */
238     int lineno,
239     int col_offset);
240 PyAPI_FUNC(PyObject *) PyErr_ProgramText(
241     const char *filename,       /* decoded from the filesystem encoding */
242     int lineno);
243 
244 /* The following functions are used to create and modify unicode
245    exceptions from C */
246 
247 /* create a UnicodeDecodeError object */
248 PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_Create(
249     const char *encoding,       /* UTF-8 encoded string */
250     const char *object,
251     Py_ssize_t length,
252     Py_ssize_t start,
253     Py_ssize_t end,
254     const char *reason          /* UTF-8 encoded string */
255     );
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 *exc,
299     const char *reason          /* UTF-8 encoded string */
300     );
301 PyAPI_FUNC(int) PyUnicodeDecodeError_SetReason(
302     PyObject *exc,
303     const char *reason          /* UTF-8 encoded string */
304     );
305 PyAPI_FUNC(int) PyUnicodeTranslateError_SetReason(
306     PyObject *exc,
307     const char *reason          /* UTF-8 encoded string */
308     );
309 
310 PyAPI_FUNC(int) PyOS_snprintf(char *str, size_t size, const char  *format, ...)
311                         Py_GCC_ATTRIBUTE((format(printf, 3, 4)));
312 PyAPI_FUNC(int) PyOS_vsnprintf(char *str, size_t size, const char  *format, va_list va)
313                         Py_GCC_ATTRIBUTE((format(printf, 3, 0)));
314 
315 #ifndef Py_LIMITED_API
316 #  define Py_CPYTHON_ERRORS_H
317 #  include  "cpython/pyerrors.h"
318 #  undef Py_CPYTHON_ERRORS_H
319 #endif
320 
321 #ifdef __cplusplus
322 }
323 #endif
324 #endif /* !Py_ERRORS_H */
325