1#ifndef Py_ERRORS_H
2#define Py_ERRORS_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7/* Error objects */
8
9#ifndef Py_LIMITED_API
10/* PyException_HEAD defines the initial segment of every exception class. */
11#define PyException_HEAD PyObject_HEAD PyObject *dict;\
12 PyObject *args; PyObject *traceback;\
13 PyObject *context; PyObject *cause;\
14 char suppress_context;
15
16typedef struct {
17 PyException_HEAD
18} PyBaseExceptionObject;
19
20typedef struct {
21 PyException_HEAD
22 PyObject *msg;
23 PyObject *filename;
24 PyObject *lineno;
25 PyObject *offset;
26 PyObject *text;
27 PyObject *print_file_and_line;
28} PySyntaxErrorObject;
29
30typedef struct {
31 PyException_HEAD
32 PyObject *msg;
33 PyObject *name;
34 PyObject *path;
35} PyImportErrorObject;
36
37typedef struct {
38 PyException_HEAD
39 PyObject *encoding;
40 PyObject *object;
41 Py_ssize_t start;
42 Py_ssize_t end;
43 PyObject *reason;
44} PyUnicodeErrorObject;
45
46typedef struct {
47 PyException_HEAD
48 PyObject *code;
49} PySystemExitObject;
50
51typedef struct {
52 PyException_HEAD
53 PyObject *myerrno;
54 PyObject *strerror;
55 PyObject *filename;
56 PyObject *filename2;
57#ifdef MS_WINDOWS
58 PyObject *winerror;
59#endif
60 Py_ssize_t written; /* only for BlockingIOError, -1 otherwise */
61} PyOSErrorObject;
62
63typedef struct {
64 PyException_HEAD
65 PyObject *value;
66} PyStopIterationObject;
67
68/* Compatibility typedefs */
69typedef PyOSErrorObject PyEnvironmentErrorObject;
70#ifdef MS_WINDOWS
71typedef PyOSErrorObject PyWindowsErrorObject;
72#endif
73#endif /* !Py_LIMITED_API */
74
75/* Error handling definitions */
76
77PyAPI_FUNC(void) PyErr_SetNone(PyObject *);
78PyAPI_FUNC(void) PyErr_SetObject(PyObject *, PyObject *);
79#ifndef Py_LIMITED_API
80PyAPI_FUNC(void) _PyErr_SetKeyError(PyObject *);
81#endif
82PyAPI_FUNC(void) PyErr_SetString(
83 PyObject *exception,
84 const char *string /* decoded from utf-8 */
85 );
86PyAPI_FUNC(PyObject *) PyErr_Occurred(void);
87PyAPI_FUNC(void) PyErr_Clear(void);
88PyAPI_FUNC(void) PyErr_Fetch(PyObject **, PyObject **, PyObject **);
89PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, PyObject *);
90PyAPI_FUNC(void) PyErr_GetExcInfo(PyObject **, PyObject **, PyObject **);
91PyAPI_FUNC(void) PyErr_SetExcInfo(PyObject *, PyObject *, PyObject *);
92
93#if defined(__clang__) || \
94 (defined(__GNUC_MAJOR__) && \
95 ((__GNUC_MAJOR__ >= 3) || \
96 (__GNUC_MAJOR__ == 2) && (__GNUC_MINOR__ >= 5)))
97#define _Py_NO_RETURN __attribute__((__noreturn__))
98#else
99#define _Py_NO_RETURN
100#endif
101
102/* Defined in Python/pylifecycle.c */
103PyAPI_FUNC(void) Py_FatalError(const char *message) _Py_NO_RETURN;
104
105#if defined(Py_DEBUG) || defined(Py_LIMITED_API)
106#define _PyErr_OCCURRED() PyErr_Occurred()
107#else
108#define _PyErr_OCCURRED() (PyThreadState_GET()->curexc_type)
109#endif
110
111/* Error testing and normalization */
112PyAPI_FUNC(int) PyErr_GivenExceptionMatches(PyObject *, PyObject *);
113PyAPI_FUNC(int) PyErr_ExceptionMatches(PyObject *);
114PyAPI_FUNC(void) PyErr_NormalizeException(PyObject**, PyObject**, PyObject**);
115
116/* Traceback manipulation (PEP 3134) */
117PyAPI_FUNC(int) PyException_SetTraceback(PyObject *, PyObject *);
118PyAPI_FUNC(PyObject *) PyException_GetTraceback(PyObject *);
119
120/* Cause manipulation (PEP 3134) */
121PyAPI_FUNC(PyObject *) PyException_GetCause(PyObject *);
122PyAPI_FUNC(void) PyException_SetCause(PyObject *, PyObject *);
123
124/* Context manipulation (PEP 3134) */
125PyAPI_FUNC(PyObject *) PyException_GetContext(PyObject *);
126PyAPI_FUNC(void) PyException_SetContext(PyObject *, PyObject *);
127#ifndef Py_LIMITED_API
128PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject *);
129#endif
130
131/* */
132
133#define PyExceptionClass_Check(x) \
134 (PyType_Check((x)) && \
135 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
136
137#define PyExceptionInstance_Check(x) \
138 PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
139
140#define PyExceptionClass_Name(x) \
141 ((char *)(((PyTypeObject*)(x))->tp_name))
142
143#define PyExceptionInstance_Class(x) ((PyObject*)((x)->ob_type))
144
145
146/* Predefined exceptions */
147
148PyAPI_DATA(PyObject *) PyExc_BaseException;
149PyAPI_DATA(PyObject *) PyExc_Exception;
150PyAPI_DATA(PyObject *) PyExc_StopAsyncIteration;
151PyAPI_DATA(PyObject *) PyExc_StopIteration;
152PyAPI_DATA(PyObject *) PyExc_GeneratorExit;
153PyAPI_DATA(PyObject *) PyExc_ArithmeticError;
154PyAPI_DATA(PyObject *) PyExc_LookupError;
155
156PyAPI_DATA(PyObject *) PyExc_AssertionError;
157PyAPI_DATA(PyObject *) PyExc_AttributeError;
158PyAPI_DATA(PyObject *) PyExc_BufferError;
159PyAPI_DATA(PyObject *) PyExc_EOFError;
160PyAPI_DATA(PyObject *) PyExc_FloatingPointError;
161PyAPI_DATA(PyObject *) PyExc_OSError;
162PyAPI_DATA(PyObject *) PyExc_ImportError;
163PyAPI_DATA(PyObject *) PyExc_IndexError;
164PyAPI_DATA(PyObject *) PyExc_KeyError;
165PyAPI_DATA(PyObject *) PyExc_KeyboardInterrupt;
166PyAPI_DATA(PyObject *) PyExc_MemoryError;
167PyAPI_DATA(PyObject *) PyExc_NameError;
168PyAPI_DATA(PyObject *) PyExc_OverflowError;
169PyAPI_DATA(PyObject *) PyExc_RuntimeError;
170PyAPI_DATA(PyObject *) PyExc_RecursionError;
171PyAPI_DATA(PyObject *) PyExc_NotImplementedError;
172PyAPI_DATA(PyObject *) PyExc_SyntaxError;
173PyAPI_DATA(PyObject *) PyExc_IndentationError;
174PyAPI_DATA(PyObject *) PyExc_TabError;
175PyAPI_DATA(PyObject *) PyExc_ReferenceError;
176PyAPI_DATA(PyObject *) PyExc_SystemError;
177PyAPI_DATA(PyObject *) PyExc_SystemExit;
178PyAPI_DATA(PyObject *) PyExc_TypeError;
179PyAPI_DATA(PyObject *) PyExc_UnboundLocalError;
180PyAPI_DATA(PyObject *) PyExc_UnicodeError;
181PyAPI_DATA(PyObject *) PyExc_UnicodeEncodeError;
182PyAPI_DATA(PyObject *) PyExc_UnicodeDecodeError;
183PyAPI_DATA(PyObject *) PyExc_UnicodeTranslateError;
184PyAPI_DATA(PyObject *) PyExc_ValueError;
185PyAPI_DATA(PyObject *) PyExc_ZeroDivisionError;
186
187PyAPI_DATA(PyObject *) PyExc_BlockingIOError;
188PyAPI_DATA(PyObject *) PyExc_BrokenPipeError;
189PyAPI_DATA(PyObject *) PyExc_ChildProcessError;
190PyAPI_DATA(PyObject *) PyExc_ConnectionError;
191PyAPI_DATA(PyObject *) PyExc_ConnectionAbortedError;
192PyAPI_DATA(PyObject *) PyExc_ConnectionRefusedError;
193PyAPI_DATA(PyObject *) PyExc_ConnectionResetError;
194PyAPI_DATA(PyObject *) PyExc_FileExistsError;
195PyAPI_DATA(PyObject *) PyExc_FileNotFoundError;
196PyAPI_DATA(PyObject *) PyExc_InterruptedError;
197PyAPI_DATA(PyObject *) PyExc_IsADirectoryError;
198PyAPI_DATA(PyObject *) PyExc_NotADirectoryError;
199PyAPI_DATA(PyObject *) PyExc_PermissionError;
200PyAPI_DATA(PyObject *) PyExc_ProcessLookupError;
201PyAPI_DATA(PyObject *) PyExc_TimeoutError;
202
203
204/* Compatibility aliases */
205PyAPI_DATA(PyObject *) PyExc_EnvironmentError;
206PyAPI_DATA(PyObject *) PyExc_IOError;
207#ifdef MS_WINDOWS
208PyAPI_DATA(PyObject *) PyExc_WindowsError;
209#endif
210
211PyAPI_DATA(PyObject *) PyExc_RecursionErrorInst;
212
213/* Predefined warning categories */
214PyAPI_DATA(PyObject *) PyExc_Warning;
215PyAPI_DATA(PyObject *) PyExc_UserWarning;
216PyAPI_DATA(PyObject *) PyExc_DeprecationWarning;
217PyAPI_DATA(PyObject *) PyExc_PendingDeprecationWarning;
218PyAPI_DATA(PyObject *) PyExc_SyntaxWarning;
219PyAPI_DATA(PyObject *) PyExc_RuntimeWarning;
220PyAPI_DATA(PyObject *) PyExc_FutureWarning;
221PyAPI_DATA(PyObject *) PyExc_ImportWarning;
222PyAPI_DATA(PyObject *) PyExc_UnicodeWarning;
223PyAPI_DATA(PyObject *) PyExc_BytesWarning;
224PyAPI_DATA(PyObject *) PyExc_ResourceWarning;
225
226
227/* Convenience functions */
228
229PyAPI_FUNC(int) PyErr_BadArgument(void);
230PyAPI_FUNC(PyObject *) PyErr_NoMemory(void);
231PyAPI_FUNC(PyObject *) PyErr_SetFromErrno(PyObject *);
232PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObject(
233 PyObject *, PyObject *);
234PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObjects(
235 PyObject *, PyObject *, PyObject *);
236PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilename(
237 PyObject *exc,
238 const char *filename /* decoded from the filesystem encoding */
239 );
240#if defined(MS_WINDOWS) && !defined(Py_LIMITED_API)
241PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithUnicodeFilename(
242 PyObject *, const Py_UNICODE *);
243#endif /* MS_WINDOWS */
244
245PyAPI_FUNC(PyObject *) PyErr_Format(
246 PyObject *exception,
247 const char *format, /* ASCII-encoded string */
248 ...
249 );
250#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
251PyAPI_FUNC(PyObject *) PyErr_FormatV(
252 PyObject *exception,
253 const char *format,
254 va_list vargs);
255#endif
256
257#ifdef MS_WINDOWS
258PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(
259 int ierr,
260 const char *filename /* decoded from the filesystem encoding */
261 );
262#ifndef Py_LIMITED_API
263/* XXX redeclare to use WSTRING */
264PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithUnicodeFilename(
265 int, const Py_UNICODE *);
266#endif
267PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErr(int);
268PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObject(
269 PyObject *,int, PyObject *);
270PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObjects(
271 PyObject *,int, PyObject *, PyObject *);
272PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilename(
273 PyObject *exc,
274 int ierr,
275 const char *filename /* decoded from the filesystem encoding */
276 );
277#ifndef Py_LIMITED_API
278PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithUnicodeFilename(
279 PyObject *,int, const Py_UNICODE *);
280#endif
281PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);
282#endif /* MS_WINDOWS */
283
284PyAPI_FUNC(PyObject *) PyErr_SetExcWithArgsKwargs(PyObject *, PyObject *,
285 PyObject *);
286PyAPI_FUNC(PyObject *) PyErr_SetImportError(PyObject *, PyObject *,
287 PyObject *);
288
289/* Export the old function so that the existing API remains available: */
290PyAPI_FUNC(void) PyErr_BadInternalCall(void);
291PyAPI_FUNC(void) _PyErr_BadInternalCall(const char *filename, int lineno);
292/* Mask the old API with a call to the new API for code compiled under
293 Python 2.0: */
294#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
295
296/* Function to create a new exception */
297PyAPI_FUNC(PyObject *) PyErr_NewException(
298 const char *name, PyObject *base, PyObject *dict);
299PyAPI_FUNC(PyObject *) PyErr_NewExceptionWithDoc(
300 const char *name, const char *doc, PyObject *base, PyObject *dict);
301PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *);
302
303/* In exceptions.c */
304#ifndef Py_LIMITED_API
305/* Helper that attempts to replace the current exception with one of the
306 * same type but with a prefix added to the exception text. The resulting
307 * exception description looks like:
308 *
309 * prefix (exc_type: original_exc_str)
310 *
311 * Only some exceptions can be safely replaced. If the function determines
312 * it isn't safe to perform the replacement, it will leave the original
313 * unmodified exception in place.
314 *
315 * Returns a borrowed reference to the new exception (if any), NULL if the
316 * existing exception was left in place.
317 */
318PyAPI_FUNC(PyObject *) _PyErr_TrySetFromCause(
319 const char *prefix_format, /* ASCII-encoded string */
320 ...
321 );
322#endif
323
324
325/* In sigcheck.c or signalmodule.c */
326PyAPI_FUNC(int) PyErr_CheckSignals(void);
327PyAPI_FUNC(void) PyErr_SetInterrupt(void);
328
329/* In signalmodule.c */
330#ifndef Py_LIMITED_API
331int PySignal_SetWakeupFd(int fd);
332#endif
333
334/* Support for adding program text to SyntaxErrors */
335PyAPI_FUNC(void) PyErr_SyntaxLocation(
336 const char *filename, /* decoded from the filesystem encoding */
337 int lineno);
338PyAPI_FUNC(void) PyErr_SyntaxLocationEx(
339 const char *filename, /* decoded from the filesystem encoding */
340 int lineno,
341 int col_offset);
342#ifndef Py_LIMITED_API
343PyAPI_FUNC(void) PyErr_SyntaxLocationObject(
344 PyObject *filename,
345 int lineno,
346 int col_offset);
347#endif
348PyAPI_FUNC(PyObject *) PyErr_ProgramText(
349 const char *filename, /* decoded from the filesystem encoding */
350 int lineno);
351#ifndef Py_LIMITED_API
352PyAPI_FUNC(PyObject *) PyErr_ProgramTextObject(
353 PyObject *filename,
354 int lineno);
355#endif
356
357/* The following functions are used to create and modify unicode
358 exceptions from C */
359
360/* create a UnicodeDecodeError object */
361PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_Create(
362 const char *encoding, /* UTF-8 encoded string */
363 const char *object,
364 Py_ssize_t length,
365 Py_ssize_t start,
366 Py_ssize_t end,
367 const char *reason /* UTF-8 encoded string */
368 );
369
370/* create a UnicodeEncodeError object */
371#ifndef Py_LIMITED_API
372PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_Create(
373 const char *encoding, /* UTF-8 encoded string */
374 const Py_UNICODE *object,
375 Py_ssize_t length,
376 Py_ssize_t start,
377 Py_ssize_t end,
378 const char *reason /* UTF-8 encoded string */
379 );
380#endif
381
382/* create a UnicodeTranslateError object */
383#ifndef Py_LIMITED_API
384PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_Create(
385 const Py_UNICODE *object,
386 Py_ssize_t length,
387 Py_ssize_t start,
388 Py_ssize_t end,
389 const char *reason /* UTF-8 encoded string */
390 );
391PyAPI_FUNC(PyObject *) _PyUnicodeTranslateError_Create(
392 PyObject *object,
393 Py_ssize_t start,
394 Py_ssize_t end,
395 const char *reason /* UTF-8 encoded string */
396 );
397#endif
398
399/* get the encoding attribute */
400PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetEncoding(PyObject *);
401PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetEncoding(PyObject *);
402
403/* get the object attribute */
404PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetObject(PyObject *);
405PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetObject(PyObject *);
406PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetObject(PyObject *);
407
408/* get the value of the start attribute (the int * may not be NULL)
409 return 0 on success, -1 on failure */
410PyAPI_FUNC(int) PyUnicodeEncodeError_GetStart(PyObject *, Py_ssize_t *);
411PyAPI_FUNC(int) PyUnicodeDecodeError_GetStart(PyObject *, Py_ssize_t *);
412PyAPI_FUNC(int) PyUnicodeTranslateError_GetStart(PyObject *, Py_ssize_t *);
413
414/* assign a new value to the start attribute
415 return 0 on success, -1 on failure */
416PyAPI_FUNC(int) PyUnicodeEncodeError_SetStart(PyObject *, Py_ssize_t);
417PyAPI_FUNC(int) PyUnicodeDecodeError_SetStart(PyObject *, Py_ssize_t);
418PyAPI_FUNC(int) PyUnicodeTranslateError_SetStart(PyObject *, Py_ssize_t);
419
420/* get the value of the end attribute (the int *may not be NULL)
421 return 0 on success, -1 on failure */
422PyAPI_FUNC(int) PyUnicodeEncodeError_GetEnd(PyObject *, Py_ssize_t *);
423PyAPI_FUNC(int) PyUnicodeDecodeError_GetEnd(PyObject *, Py_ssize_t *);
424PyAPI_FUNC(int) PyUnicodeTranslateError_GetEnd(PyObject *, Py_ssize_t *);
425
426/* assign a new value to the end attribute
427 return 0 on success, -1 on failure */
428PyAPI_FUNC(int) PyUnicodeEncodeError_SetEnd(PyObject *, Py_ssize_t);
429PyAPI_FUNC(int) PyUnicodeDecodeError_SetEnd(PyObject *, Py_ssize_t);
430PyAPI_FUNC(int) PyUnicodeTranslateError_SetEnd(PyObject *, Py_ssize_t);
431
432/* get the value of the reason attribute */
433PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetReason(PyObject *);
434PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetReason(PyObject *);
435PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetReason(PyObject *);
436
437/* assign a new value to the reason attribute
438 return 0 on success, -1 on failure */
439PyAPI_FUNC(int) PyUnicodeEncodeError_SetReason(
440 PyObject *exc,
441 const char *reason /* UTF-8 encoded string */
442 );
443PyAPI_FUNC(int) PyUnicodeDecodeError_SetReason(
444 PyObject *exc,
445 const char *reason /* UTF-8 encoded string */
446 );
447PyAPI_FUNC(int) PyUnicodeTranslateError_SetReason(
448 PyObject *exc,
449 const char *reason /* UTF-8 encoded string */
450 );
451
452/* These APIs aren't really part of the error implementation, but
453 often needed to format error messages; the native C lib APIs are
454 not available on all platforms, which is why we provide emulations
455 for those platforms in Python/mysnprintf.c,
456 WARNING: The return value of snprintf varies across platforms; do
457 not rely on any particular behavior; eventually the C99 defn may
458 be reliable.
459*/
460#if defined(MS_WIN32) && !defined(HAVE_SNPRINTF)
461# define HAVE_SNPRINTF
462# define snprintf _snprintf
463# define vsnprintf _vsnprintf
464#endif
465
466#include <stdarg.h>
467PyAPI_FUNC(int) PyOS_snprintf(char *str, size_t size, const char *format, ...)
468 Py_GCC_ATTRIBUTE((format(printf, 3, 4)));
469PyAPI_FUNC(int) PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
470 Py_GCC_ATTRIBUTE((format(printf, 3, 0)));
471
472#ifdef __cplusplus
473}
474#endif
475#endif /* !Py_ERRORS_H */
476