1 #ifndef Py_ERRORS_H
2 #define Py_ERRORS_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 /* Error handling definitions */
8 
9 PyAPI_FUNC(void) PyErr_SetNone(PyObject *);
10 PyAPI_FUNC(void) PyErr_SetObject(PyObject *, PyObject *);
11 PyAPI_FUNC(void) PyErr_SetString(
12     PyObject *exception,
13     const char *string   /* decoded from utf-8 */
14     );
15 PyAPI_FUNC(PyObject *) PyErr_Occurred(void);
16 PyAPI_FUNC(void) PyErr_Clear(void);
17 PyAPI_FUNC(void) PyErr_Fetch(PyObject **, PyObject **, PyObject **);
18 PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, PyObject *);
19 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
20 PyAPI_FUNC(void) PyErr_GetExcInfo(PyObject **, PyObject **, PyObject **);
21 PyAPI_FUNC(void) PyErr_SetExcInfo(PyObject *, PyObject *, PyObject *);
22 #endif
23 
24 /* Defined in Python/pylifecycle.c */
25 PyAPI_FUNC(void) _Py_NO_RETURN Py_FatalError(const char *message);
26 
27 #if defined(Py_DEBUG) || defined(Py_LIMITED_API)
28 #define _PyErr_OCCURRED() PyErr_Occurred()
29 #else
30 #define _PyErr_OCCURRED() (PyThreadState_GET()->curexc_type)
31 #endif
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((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
58 
59 PyAPI_FUNC(const char *) PyExceptionClass_Name(PyObject *);
60 
61 #define PyExceptionInstance_Class(x) ((PyObject*)((x)->ob_type))
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_ResourceWarning;
150 
151 
152 /* Convenience functions */
153 
154 PyAPI_FUNC(int) PyErr_BadArgument(void);
155 PyAPI_FUNC(PyObject *) PyErr_NoMemory(void);
156 PyAPI_FUNC(PyObject *) PyErr_SetFromErrno(PyObject *);
157 PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObject(
158     PyObject *, PyObject *);
159 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
160 PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObjects(
161     PyObject *, PyObject *, PyObject *);
162 #endif
163 PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilename(
164     PyObject *exc,
165     const char *filename   /* decoded from the filesystem encoding */
166     );
167 
168 PyAPI_FUNC(PyObject *) PyErr_Format(
169     PyObject *exception,
170     const char *format,   /* ASCII-encoded string  */
171     ...
172     );
173 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
174 PyAPI_FUNC(PyObject *) PyErr_FormatV(
175     PyObject *exception,
176     const char *format,
177     va_list vargs);
178 #endif
179 
180 #ifdef MS_WINDOWS
181 PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(
182     int ierr,
183     const char *filename        /* decoded from the filesystem encoding */
184     );
185 PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErr(int);
186 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObject(
187     PyObject *,int, PyObject *);
188 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
189 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObjects(
190     PyObject *,int, PyObject *, PyObject *);
191 #endif
192 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilename(
193     PyObject *exc,
194     int ierr,
195     const char *filename        /* decoded from the filesystem encoding */
196     );
197 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);
198 #endif /* MS_WINDOWS */
199 
200 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060000
201 PyAPI_FUNC(PyObject *) PyErr_SetImportErrorSubclass(PyObject *, PyObject *,
202     PyObject *, PyObject *);
203 #endif
204 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
205 PyAPI_FUNC(PyObject *) PyErr_SetImportError(PyObject *, PyObject *,
206     PyObject *);
207 #endif
208 
209 /* Export the old function so that the existing API remains available: */
210 PyAPI_FUNC(void) PyErr_BadInternalCall(void);
211 PyAPI_FUNC(void) _PyErr_BadInternalCall(const char *filename, int lineno);
212 /* Mask the old API with a call to the new API for code compiled under
213    Python 2.0: */
214 #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
215 
216 /* Function to create a new exception */
217 PyAPI_FUNC(PyObject *) PyErr_NewException(
218     const char *name, PyObject *base, PyObject *dict);
219 PyAPI_FUNC(PyObject *) PyErr_NewExceptionWithDoc(
220     const char *name, const char *doc, PyObject *base, PyObject *dict);
221 PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *);
222 
223 
224 /* In signalmodule.c */
225 PyAPI_FUNC(int) PyErr_CheckSignals(void);
226 PyAPI_FUNC(void) PyErr_SetInterrupt(void);
227 
228 /* Support for adding program text to SyntaxErrors */
229 PyAPI_FUNC(void) PyErr_SyntaxLocation(
230     const char *filename,       /* decoded from the filesystem encoding */
231     int lineno);
232 PyAPI_FUNC(void) PyErr_SyntaxLocationEx(
233     const char *filename,       /* decoded from the filesystem encoding */
234     int lineno,
235     int col_offset);
236 PyAPI_FUNC(PyObject *) PyErr_ProgramText(
237     const char *filename,       /* decoded from the filesystem encoding */
238     int lineno);
239 
240 /* The following functions are used to create and modify unicode
241    exceptions from C */
242 
243 /* create a UnicodeDecodeError object */
244 PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_Create(
245     const char *encoding,       /* UTF-8 encoded string */
246     const char *object,
247     Py_ssize_t length,
248     Py_ssize_t start,
249     Py_ssize_t end,
250     const char *reason          /* UTF-8 encoded string */
251     );
252 
253 /* get the encoding attribute */
254 PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetEncoding(PyObject *);
255 PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetEncoding(PyObject *);
256 
257 /* get the object attribute */
258 PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetObject(PyObject *);
259 PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetObject(PyObject *);
260 PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetObject(PyObject *);
261 
262 /* get the value of the start attribute (the int * may not be NULL)
263    return 0 on success, -1 on failure */
264 PyAPI_FUNC(int) PyUnicodeEncodeError_GetStart(PyObject *, Py_ssize_t *);
265 PyAPI_FUNC(int) PyUnicodeDecodeError_GetStart(PyObject *, Py_ssize_t *);
266 PyAPI_FUNC(int) PyUnicodeTranslateError_GetStart(PyObject *, Py_ssize_t *);
267 
268 /* assign a new value to the start attribute
269    return 0 on success, -1 on failure */
270 PyAPI_FUNC(int) PyUnicodeEncodeError_SetStart(PyObject *, Py_ssize_t);
271 PyAPI_FUNC(int) PyUnicodeDecodeError_SetStart(PyObject *, Py_ssize_t);
272 PyAPI_FUNC(int) PyUnicodeTranslateError_SetStart(PyObject *, Py_ssize_t);
273 
274 /* get the value of the end attribute (the int *may not be NULL)
275  return 0 on success, -1 on failure */
276 PyAPI_FUNC(int) PyUnicodeEncodeError_GetEnd(PyObject *, Py_ssize_t *);
277 PyAPI_FUNC(int) PyUnicodeDecodeError_GetEnd(PyObject *, Py_ssize_t *);
278 PyAPI_FUNC(int) PyUnicodeTranslateError_GetEnd(PyObject *, Py_ssize_t *);
279 
280 /* assign a new value to the end attribute
281    return 0 on success, -1 on failure */
282 PyAPI_FUNC(int) PyUnicodeEncodeError_SetEnd(PyObject *, Py_ssize_t);
283 PyAPI_FUNC(int) PyUnicodeDecodeError_SetEnd(PyObject *, Py_ssize_t);
284 PyAPI_FUNC(int) PyUnicodeTranslateError_SetEnd(PyObject *, Py_ssize_t);
285 
286 /* get the value of the reason attribute */
287 PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetReason(PyObject *);
288 PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetReason(PyObject *);
289 PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetReason(PyObject *);
290 
291 /* assign a new value to the reason attribute
292    return 0 on success, -1 on failure */
293 PyAPI_FUNC(int) PyUnicodeEncodeError_SetReason(
294     PyObject *exc,
295     const char *reason          /* UTF-8 encoded string */
296     );
297 PyAPI_FUNC(int) PyUnicodeDecodeError_SetReason(
298     PyObject *exc,
299     const char *reason          /* UTF-8 encoded string */
300     );
301 PyAPI_FUNC(int) PyUnicodeTranslateError_SetReason(
302     PyObject *exc,
303     const char *reason          /* UTF-8 encoded string */
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 #ifndef Py_LIMITED_API
327 #  define Py_CPYTHON_ERRORS_H
328 #  include  "cpython/pyerrors.h"
329 #  undef Py_CPYTHON_ERRORS_H
330 #endif
331 
332 #ifdef __cplusplus
333 }
334 #endif
335 #endif /* !Py_ERRORS_H */
336