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