1 #ifndef Py_CPYTHON_ERRORS_H
2 #  error "this header file must not be included directly"
3 #endif
4 
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8 
9 /* Error objects */
10 
11 /* PyException_HEAD defines the initial segment of every exception class. */
12 #define PyException_HEAD PyObject_HEAD PyObject *dict;\
13              PyObject *args; PyObject *traceback;\
14              PyObject *context; PyObject *cause;\
15              char suppress_context;
16 
17 typedef struct {
18     PyException_HEAD
19 } PyBaseExceptionObject;
20 
21 typedef struct {
22     PyException_HEAD
23     PyObject *msg;
24     PyObject *filename;
25     PyObject *lineno;
26     PyObject *offset;
27     PyObject *text;
28     PyObject *print_file_and_line;
29 } PySyntaxErrorObject;
30 
31 typedef struct {
32     PyException_HEAD
33     PyObject *msg;
34     PyObject *name;
35     PyObject *path;
36 } PyImportErrorObject;
37 
38 typedef struct {
39     PyException_HEAD
40     PyObject *encoding;
41     PyObject *object;
42     Py_ssize_t start;
43     Py_ssize_t end;
44     PyObject *reason;
45 } PyUnicodeErrorObject;
46 
47 typedef struct {
48     PyException_HEAD
49     PyObject *code;
50 } PySystemExitObject;
51 
52 typedef struct {
53     PyException_HEAD
54     PyObject *myerrno;
55     PyObject *strerror;
56     PyObject *filename;
57     PyObject *filename2;
58 #ifdef MS_WINDOWS
59     PyObject *winerror;
60 #endif
61     Py_ssize_t written;   /* only for BlockingIOError, -1 otherwise */
62 } PyOSErrorObject;
63 
64 typedef struct {
65     PyException_HEAD
66     PyObject *value;
67 } PyStopIterationObject;
68 
69 /* Compatibility typedefs */
70 typedef PyOSErrorObject PyEnvironmentErrorObject;
71 #ifdef MS_WINDOWS
72 typedef PyOSErrorObject PyWindowsErrorObject;
73 #endif
74 
75 /* Error handling definitions */
76 
77 PyAPI_FUNC(void) _PyErr_SetKeyError(PyObject *);
78 PyAPI_FUNC(_PyErr_StackItem*) _PyErr_GetTopmostException(PyThreadState *tstate);
79 PyAPI_FUNC(void) _PyErr_GetExcInfo(PyThreadState *, PyObject **, PyObject **, PyObject **);
80 
81 /* Context manipulation (PEP 3134) */
82 
83 PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject *);
84 
85 /* */
86 
87 #define PyExceptionClass_Name(x)  (((PyTypeObject*)(x))->tp_name)
88 
89 /* Convenience functions */
90 
91 #ifdef MS_WINDOWS
92 Py_DEPRECATED(3.3)
93 PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithUnicodeFilename(
94     PyObject *, const Py_UNICODE *);
95 #endif /* MS_WINDOWS */
96 
97 /* Like PyErr_Format(), but saves current exception as __context__ and
98    __cause__.
99  */
100 PyAPI_FUNC(PyObject *) _PyErr_FormatFromCause(
101     PyObject *exception,
102     const char *format,   /* ASCII-encoded string  */
103     ...
104     );
105 
106 #ifdef MS_WINDOWS
107 /* XXX redeclare to use WSTRING */
108 Py_DEPRECATED(3.3)
109 PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithUnicodeFilename(
110     int, const Py_UNICODE *);
111 Py_DEPRECATED(3.3)
112 PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithUnicodeFilename(
113     PyObject *,int, const Py_UNICODE *);
114 #endif
115 
116 /* In exceptions.c */
117 
118 /* Helper that attempts to replace the current exception with one of the
119  * same type but with a prefix added to the exception text. The resulting
120  * exception description looks like:
121  *
122  *     prefix (exc_type: original_exc_str)
123  *
124  * Only some exceptions can be safely replaced. If the function determines
125  * it isn't safe to perform the replacement, it will leave the original
126  * unmodified exception in place.
127  *
128  * Returns a borrowed reference to the new exception (if any), NULL if the
129  * existing exception was left in place.
130  */
131 PyAPI_FUNC(PyObject *) _PyErr_TrySetFromCause(
132     const char *prefix_format,   /* ASCII-encoded string  */
133     ...
134     );
135 
136 /* In signalmodule.c */
137 
138 int PySignal_SetWakeupFd(int fd);
139 PyAPI_FUNC(int) _PyErr_CheckSignals(void);
140 
141 /* Support for adding program text to SyntaxErrors */
142 
143 PyAPI_FUNC(void) PyErr_SyntaxLocationObject(
144     PyObject *filename,
145     int lineno,
146     int col_offset);
147 
148 PyAPI_FUNC(PyObject *) PyErr_ProgramTextObject(
149     PyObject *filename,
150     int lineno);
151 
152 /* Create a UnicodeEncodeError object.
153  *
154  * TODO: This API will be removed in Python 3.11.
155  */
156 Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_Create(
157     const char *encoding,       /* UTF-8 encoded string */
158     const Py_UNICODE *object,
159     Py_ssize_t length,
160     Py_ssize_t start,
161     Py_ssize_t end,
162     const char *reason          /* UTF-8 encoded string */
163     );
164 
165 /* Create a UnicodeTranslateError object.
166  *
167  * TODO: This API will be removed in Python 3.11.
168  */
169 Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_Create(
170     const Py_UNICODE *object,
171     Py_ssize_t length,
172     Py_ssize_t start,
173     Py_ssize_t end,
174     const char *reason          /* UTF-8 encoded string */
175     );
176 PyAPI_FUNC(PyObject *) _PyUnicodeTranslateError_Create(
177     PyObject *object,
178     Py_ssize_t start,
179     Py_ssize_t end,
180     const char *reason          /* UTF-8 encoded string */
181     );
182 
183 PyAPI_FUNC(void) _PyErr_WriteUnraisableMsg(
184     const char *err_msg,
185     PyObject *obj);
186 
187 PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalErrorFunc(
188     const char *func,
189     const char *message);
190 
191 PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalErrorFormat(
192     const char *func,
193     const char *format,
194     ...);
195 
196 #define Py_FatalError(message) _Py_FatalErrorFunc(__func__, message)
197 
198 #ifdef __cplusplus
199 }
200 #endif
201