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