Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 2 | /* Error handling */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5 | #include "pycore_initconfig.h" |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 6 | #include "pycore_pyerrors.h" |
Victor Stinner | e5014be | 2020-04-14 17:52:15 +0200 | [diff] [blame] | 7 | #include "pycore_pystate.h" // _PyThreadState_GET() |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 8 | #include "pycore_sysmodule.h" |
Victor Stinner | df22c03 | 2019-05-23 01:00:58 +0200 | [diff] [blame] | 9 | #include "pycore_traceback.h" |
Guido van Rossum | f22120a | 1990-12-20 23:05:40 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 11 | #ifndef __STDC__ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 12 | #ifndef MS_WINDOWS |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 13 | extern char *strerror(int); |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 14 | #endif |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 15 | #endif |
Guido van Rossum | f5401bd | 1990-11-02 17:50:28 +0000 | [diff] [blame] | 16 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 17 | #ifdef MS_WINDOWS |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 18 | #include <windows.h> |
| 19 | #include <winbase.h> |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 22 | #include <ctype.h> |
| 23 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 24 | #ifdef __cplusplus |
| 25 | extern "C" { |
| 26 | #endif |
| 27 | |
Hai Shi | 46874c2 | 2020-01-30 17:20:25 -0600 | [diff] [blame] | 28 | _Py_IDENTIFIER(__module__); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 29 | _Py_IDENTIFIER(builtins); |
| 30 | _Py_IDENTIFIER(stderr); |
Victor Stinner | a85a1d3 | 2019-05-28 16:01:17 +0200 | [diff] [blame] | 31 | _Py_IDENTIFIER(flush); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 32 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 33 | /* Forward declarations */ |
| 34 | static PyObject * |
| 35 | _PyErr_FormatV(PyThreadState *tstate, PyObject *exception, |
| 36 | const char *format, va_list vargs); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 37 | |
| 38 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 39 | void |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 40 | _PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value, |
| 41 | PyObject *traceback) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 42 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | PyObject *oldtype, *oldvalue, *oldtraceback; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 44 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | if (traceback != NULL && !PyTraceBack_Check(traceback)) { |
| 46 | /* XXX Should never happen -- fatal error instead? */ |
| 47 | /* Well, it could be None. */ |
| 48 | Py_DECREF(traceback); |
| 49 | traceback = NULL; |
| 50 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 51 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | /* Save these in locals to safeguard against recursive |
| 53 | invocation through Py_XDECREF */ |
| 54 | oldtype = tstate->curexc_type; |
| 55 | oldvalue = tstate->curexc_value; |
| 56 | oldtraceback = tstate->curexc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 57 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | tstate->curexc_type = type; |
| 59 | tstate->curexc_value = value; |
| 60 | tstate->curexc_traceback = traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 61 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | Py_XDECREF(oldtype); |
| 63 | Py_XDECREF(oldvalue); |
| 64 | Py_XDECREF(oldtraceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 67 | void |
| 68 | PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback) |
| 69 | { |
| 70 | PyThreadState *tstate = _PyThreadState_GET(); |
| 71 | _PyErr_Restore(tstate, type, value, traceback); |
| 72 | } |
| 73 | |
| 74 | |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 75 | _PyErr_StackItem * |
| 76 | _PyErr_GetTopmostException(PyThreadState *tstate) |
| 77 | { |
| 78 | _PyErr_StackItem *exc_info = tstate->exc_info; |
| 79 | while ((exc_info->exc_type == NULL || exc_info->exc_type == Py_None) && |
| 80 | exc_info->previous_item != NULL) |
| 81 | { |
| 82 | exc_info = exc_info->previous_item; |
| 83 | } |
| 84 | return exc_info; |
| 85 | } |
| 86 | |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 87 | static PyObject* |
| 88 | _PyErr_CreateException(PyObject *exception, PyObject *value) |
| 89 | { |
| 90 | if (value == NULL || value == Py_None) { |
| 91 | return _PyObject_CallNoArg(exception); |
| 92 | } |
| 93 | else if (PyTuple_Check(value)) { |
| 94 | return PyObject_Call(exception, value, NULL); |
| 95 | } |
| 96 | else { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 97 | return PyObject_CallOneArg(exception, value); |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 101 | void |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 102 | _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | PyObject *exc_value; |
| 105 | PyObject *tb = NULL; |
Guido van Rossum | b4fb6e4 | 2008-06-14 20:20:24 +0000 | [diff] [blame] | 106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | if (exception != NULL && |
| 108 | !PyExceptionClass_Check(exception)) { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 109 | _PyErr_Format(tstate, PyExc_SystemError, |
Victor Stinner | b0be6b3 | 2020-05-05 17:07:41 +0200 | [diff] [blame] | 110 | "_PyErr_SetObject: " |
| 111 | "exception %R is not a BaseException subclass", |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 112 | exception); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | return; |
| 114 | } |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | Py_XINCREF(value); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 117 | exc_value = _PyErr_GetTopmostException(tstate)->exc_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | if (exc_value != NULL && exc_value != Py_None) { |
| 119 | /* Implicit exception chaining */ |
| 120 | Py_INCREF(exc_value); |
| 121 | if (value == NULL || !PyExceptionInstance_Check(value)) { |
| 122 | /* We must normalize the value right now */ |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 123 | PyObject *fixed_value; |
Victor Stinner | de821be | 2015-03-24 12:41:23 +0100 | [diff] [blame] | 124 | |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 125 | /* Issue #23571: functions must not be called with an |
Victor Stinner | de821be | 2015-03-24 12:41:23 +0100 | [diff] [blame] | 126 | exception set */ |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 127 | _PyErr_Clear(tstate); |
Victor Stinner | de821be | 2015-03-24 12:41:23 +0100 | [diff] [blame] | 128 | |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 129 | fixed_value = _PyErr_CreateException(exception, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | Py_XDECREF(value); |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 131 | if (fixed_value == NULL) { |
Alexey Izbyshev | a2eefa6 | 2018-08-26 19:31:25 +0300 | [diff] [blame] | 132 | Py_DECREF(exc_value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | return; |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | value = fixed_value; |
| 137 | } |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | /* Avoid reference cycles through the context chain. |
| 140 | This is O(chain length) but context chains are |
| 141 | usually very short. Sensitive readers may try |
| 142 | to inline the call to PyException_GetContext. */ |
| 143 | if (exc_value != value) { |
| 144 | PyObject *o = exc_value, *context; |
| 145 | while ((context = PyException_GetContext(o))) { |
| 146 | Py_DECREF(context); |
| 147 | if (context == value) { |
| 148 | PyException_SetContext(o, NULL); |
| 149 | break; |
| 150 | } |
| 151 | o = context; |
| 152 | } |
| 153 | PyException_SetContext(value, exc_value); |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 154 | } |
| 155 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | Py_DECREF(exc_value); |
| 157 | } |
| 158 | } |
| 159 | if (value != NULL && PyExceptionInstance_Check(value)) |
| 160 | tb = PyException_GetTraceback(value); |
| 161 | Py_XINCREF(exception); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 162 | _PyErr_Restore(tstate, exception, value, tb); |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | PyErr_SetObject(PyObject *exception, PyObject *value) |
| 167 | { |
| 168 | PyThreadState *tstate = _PyThreadState_GET(); |
| 169 | _PyErr_SetObject(tstate, exception, value); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Raymond Hettinger | 69492da | 2013-09-02 15:59:26 -0700 | [diff] [blame] | 172 | /* Set a key error with the specified argument, wrapping it in a |
| 173 | * tuple automatically so that tuple keys are not unpacked as the |
| 174 | * exception arguments. */ |
| 175 | void |
| 176 | _PyErr_SetKeyError(PyObject *arg) |
| 177 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 178 | PyThreadState *tstate = _PyThreadState_GET(); |
| 179 | PyObject *tup = PyTuple_Pack(1, arg); |
| 180 | if (!tup) { |
| 181 | /* caller will expect error to be set anyway */ |
| 182 | return; |
| 183 | } |
| 184 | _PyErr_SetObject(tstate, PyExc_KeyError, tup); |
Raymond Hettinger | 69492da | 2013-09-02 15:59:26 -0700 | [diff] [blame] | 185 | Py_DECREF(tup); |
| 186 | } |
| 187 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 188 | void |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 189 | _PyErr_SetNone(PyThreadState *tstate, PyObject *exception) |
| 190 | { |
| 191 | _PyErr_SetObject(tstate, exception, (PyObject *)NULL); |
| 192 | } |
| 193 | |
| 194 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 196 | PyErr_SetNone(PyObject *exception) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 197 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 198 | PyThreadState *tstate = _PyThreadState_GET(); |
| 199 | _PyErr_SetNone(tstate, exception); |
| 200 | } |
| 201 | |
| 202 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 203 | void |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 204 | _PyErr_SetString(PyThreadState *tstate, PyObject *exception, |
| 205 | const char *string) |
| 206 | { |
| 207 | PyObject *value = PyUnicode_FromString(string); |
| 208 | _PyErr_SetObject(tstate, exception, value); |
| 209 | Py_XDECREF(value); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 213 | PyErr_SetString(PyObject *exception, const char *string) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 215 | PyThreadState *tstate = _PyThreadState_GET(); |
| 216 | _PyErr_SetString(tstate, exception, string); |
| 217 | } |
| 218 | |
| 219 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 220 | PyObject* _Py_HOT_FUNCTION |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 221 | PyErr_Occurred(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | { |
Victor Stinner | d12d0e7 | 2019-11-07 12:42:07 +0100 | [diff] [blame] | 223 | /* The caller must hold the GIL. */ |
| 224 | assert(PyGILState_Check()); |
| 225 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 226 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 227 | return _PyErr_Occurred(tstate); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 230 | |
| 231 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 232 | PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 233 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | if (err == NULL || exc == NULL) { |
| 235 | /* maybe caused by "import exceptions" that failed early on */ |
| 236 | return 0; |
| 237 | } |
| 238 | if (PyTuple_Check(exc)) { |
| 239 | Py_ssize_t i, n; |
| 240 | n = PyTuple_Size(exc); |
| 241 | for (i = 0; i < n; i++) { |
| 242 | /* Test recursively */ |
| 243 | if (PyErr_GivenExceptionMatches( |
| 244 | err, PyTuple_GET_ITEM(exc, i))) |
| 245 | { |
| 246 | return 1; |
| 247 | } |
| 248 | } |
| 249 | return 0; |
| 250 | } |
| 251 | /* err might be an instance, so check its class. */ |
| 252 | if (PyExceptionInstance_Check(err)) |
| 253 | err = PyExceptionInstance_Class(err); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) { |
scoder | e4c06bc | 2017-07-31 22:27:46 +0200 | [diff] [blame] | 256 | return PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | return err == exc; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 260 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 261 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 262 | |
| 263 | int |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 264 | _PyErr_ExceptionMatches(PyThreadState *tstate, PyObject *exc) |
| 265 | { |
| 266 | return PyErr_GivenExceptionMatches(_PyErr_Occurred(tstate), exc); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 271 | PyErr_ExceptionMatches(PyObject *exc) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 272 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 273 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 274 | return _PyErr_ExceptionMatches(tstate, exc); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 278 | #ifndef Py_NORMALIZE_RECURSION_LIMIT |
| 279 | #define Py_NORMALIZE_RECURSION_LIMIT 32 |
| 280 | #endif |
| 281 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 282 | /* Used in many places to normalize a raised exception, including in |
| 283 | eval_code2(), do_raise(), and PyErr_Print() |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 284 | |
| 285 | XXX: should PyErr_NormalizeException() also call |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | PyException_SetTraceback() with the resulting value and tb? |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 287 | */ |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 288 | void |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 289 | _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc, |
| 290 | PyObject **val, PyObject **tb) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 291 | { |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 292 | int recursion_depth = 0; |
| 293 | PyObject *type, *value, *initial_tb; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 294 | |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 295 | restart: |
| 296 | type = *exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | if (type == NULL) { |
| 298 | /* There was no exception, so nothing to do. */ |
| 299 | return; |
| 300 | } |
Guido van Rossum | ed473a4 | 2000-08-07 19:18:27 +0000 | [diff] [blame] | 301 | |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 302 | value = *val; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | /* If PyErr_SetNone() was used, the value will have been actually |
| 304 | set to NULL. |
| 305 | */ |
| 306 | if (!value) { |
| 307 | value = Py_None; |
| 308 | Py_INCREF(value); |
| 309 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | /* Normalize the exception so that if the type is a class, the |
| 312 | value will be an instance. |
| 313 | */ |
| 314 | if (PyExceptionClass_Check(type)) { |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 315 | PyObject *inclass = NULL; |
| 316 | int is_subclass = 0; |
Victor Stinner | 74a7fa6 | 2013-07-17 00:44:53 +0200 | [diff] [blame] | 317 | |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 318 | if (PyExceptionInstance_Check(value)) { |
| 319 | inclass = PyExceptionInstance_Class(value); |
| 320 | is_subclass = PyObject_IsSubclass(inclass, type); |
| 321 | if (is_subclass < 0) { |
| 322 | goto error; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* If the value was not an instance, or is not an instance |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | whose class is (or is derived from) type, then use the |
| 328 | value as an argument to instantiation of the type |
| 329 | class. |
| 330 | */ |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 331 | if (!is_subclass) { |
| 332 | PyObject *fixed_value = _PyErr_CreateException(type, value); |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 333 | if (fixed_value == NULL) { |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 334 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | Py_DECREF(value); |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 337 | value = fixed_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | } |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 339 | /* If the class of the instance doesn't exactly match the |
| 340 | class of the type, believe the instance. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | */ |
| 342 | else if (inclass != type) { |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 343 | Py_INCREF(inclass); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | Py_DECREF(type); |
| 345 | type = inclass; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | *exc = type; |
| 349 | *val = value; |
| 350 | return; |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 351 | |
| 352 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | Py_DECREF(type); |
| 354 | Py_DECREF(value); |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 355 | recursion_depth++; |
| 356 | if (recursion_depth == Py_NORMALIZE_RECURSION_LIMIT) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 357 | _PyErr_SetString(tstate, PyExc_RecursionError, |
| 358 | "maximum recursion depth exceeded " |
| 359 | "while normalizing an exception"); |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 360 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | /* If the new exception doesn't set a traceback and the old |
| 362 | exception had a traceback, use the old traceback for the |
| 363 | new exception. It's better than nothing. |
| 364 | */ |
| 365 | initial_tb = *tb; |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 366 | _PyErr_Fetch(tstate, exc, val, tb); |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 367 | assert(*exc != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | if (initial_tb != NULL) { |
| 369 | if (*tb == NULL) |
| 370 | *tb = initial_tb; |
| 371 | else |
| 372 | Py_DECREF(initial_tb); |
| 373 | } |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 374 | /* Abort when Py_NORMALIZE_RECURSION_LIMIT has been exceeded, and the |
| 375 | corresponding RecursionError could not be normalized, and the |
| 376 | MemoryError raised when normalize this RecursionError could not be |
| 377 | normalized. */ |
| 378 | if (recursion_depth >= Py_NORMALIZE_RECURSION_LIMIT + 2) { |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 379 | if (PyErr_GivenExceptionMatches(*exc, PyExc_MemoryError)) { |
| 380 | Py_FatalError("Cannot recover from MemoryErrors " |
| 381 | "while normalizing exceptions."); |
| 382 | } |
| 383 | else { |
| 384 | Py_FatalError("Cannot recover from the recursive normalization " |
| 385 | "of an exception."); |
| 386 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | } |
Serhiy Storchaka | cf29653 | 2017-11-05 11:27:48 +0200 | [diff] [blame] | 388 | goto restart; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 392 | void |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 393 | PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 394 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 395 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 396 | _PyErr_NormalizeException(tstate, exc, val, tb); |
| 397 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 398 | |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 399 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 400 | void |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 401 | _PyErr_Fetch(PyThreadState *tstate, PyObject **p_type, PyObject **p_value, |
| 402 | PyObject **p_traceback) |
| 403 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | *p_type = tstate->curexc_type; |
| 405 | *p_value = tstate->curexc_value; |
| 406 | *p_traceback = tstate->curexc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | tstate->curexc_type = NULL; |
| 409 | tstate->curexc_value = NULL; |
| 410 | tstate->curexc_traceback = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 413 | |
| 414 | void |
| 415 | PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) |
| 416 | { |
| 417 | PyThreadState *tstate = _PyThreadState_GET(); |
| 418 | _PyErr_Fetch(tstate, p_type, p_value, p_traceback); |
| 419 | } |
| 420 | |
| 421 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 422 | void |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 423 | _PyErr_Clear(PyThreadState *tstate) |
| 424 | { |
| 425 | _PyErr_Restore(tstate, NULL, NULL, NULL); |
| 426 | } |
| 427 | |
| 428 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 429 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 430 | PyErr_Clear(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 431 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 432 | PyThreadState *tstate = _PyThreadState_GET(); |
| 433 | _PyErr_Clear(tstate); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 434 | } |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 435 | |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 436 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 437 | void |
Julien Danjou | 3430c55 | 2020-01-13 17:30:14 +0100 | [diff] [blame] | 438 | _PyErr_GetExcInfo(PyThreadState *tstate, |
| 439 | PyObject **p_type, PyObject **p_value, PyObject **p_traceback) |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 440 | { |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 441 | _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); |
| 442 | *p_type = exc_info->exc_type; |
| 443 | *p_value = exc_info->exc_value; |
| 444 | *p_traceback = exc_info->exc_traceback; |
| 445 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 446 | Py_XINCREF(*p_type); |
| 447 | Py_XINCREF(*p_value); |
| 448 | Py_XINCREF(*p_traceback); |
| 449 | } |
| 450 | |
Julien Danjou | 3430c55 | 2020-01-13 17:30:14 +0100 | [diff] [blame] | 451 | |
| 452 | void |
| 453 | PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) |
| 454 | { |
| 455 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 3f12ac1 | 2020-01-15 11:23:25 +0100 | [diff] [blame] | 456 | _PyErr_GetExcInfo(tstate, p_type, p_value, p_traceback); |
Julien Danjou | 3430c55 | 2020-01-13 17:30:14 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 459 | void |
| 460 | PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback) |
| 461 | { |
| 462 | PyObject *oldtype, *oldvalue, *oldtraceback; |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 463 | PyThreadState *tstate = _PyThreadState_GET(); |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 464 | |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 465 | oldtype = tstate->exc_info->exc_type; |
| 466 | oldvalue = tstate->exc_info->exc_value; |
| 467 | oldtraceback = tstate->exc_info->exc_traceback; |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 468 | |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 469 | tstate->exc_info->exc_type = p_type; |
| 470 | tstate->exc_info->exc_value = p_value; |
| 471 | tstate->exc_info->exc_traceback = p_traceback; |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 472 | |
| 473 | Py_XDECREF(oldtype); |
| 474 | Py_XDECREF(oldvalue); |
| 475 | Py_XDECREF(oldtraceback); |
| 476 | } |
| 477 | |
Serhiy Storchaka | e2bd2a7 | 2014-10-08 22:31:52 +0300 | [diff] [blame] | 478 | /* Like PyErr_Restore(), but if an exception is already set, |
| 479 | set the context associated with it. |
| 480 | */ |
| 481 | void |
| 482 | _PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb) |
| 483 | { |
| 484 | if (exc == NULL) |
| 485 | return; |
| 486 | |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 487 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | b0be6b3 | 2020-05-05 17:07:41 +0200 | [diff] [blame] | 488 | |
| 489 | if (!PyExceptionClass_Check(exc)) { |
| 490 | _PyErr_Format(tstate, PyExc_SystemError, |
| 491 | "_PyErr_ChainExceptions: " |
| 492 | "exception %R is not a BaseException subclass", |
| 493 | exc); |
| 494 | return; |
| 495 | } |
| 496 | |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 497 | if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | e2bd2a7 | 2014-10-08 22:31:52 +0300 | [diff] [blame] | 498 | PyObject *exc2, *val2, *tb2; |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 499 | _PyErr_Fetch(tstate, &exc2, &val2, &tb2); |
| 500 | _PyErr_NormalizeException(tstate, &exc, &val, &tb); |
Serhiy Storchaka | 9e373be | 2016-10-21 16:19:59 +0300 | [diff] [blame] | 501 | if (tb != NULL) { |
| 502 | PyException_SetTraceback(val, tb); |
| 503 | Py_DECREF(tb); |
| 504 | } |
Serhiy Storchaka | e2bd2a7 | 2014-10-08 22:31:52 +0300 | [diff] [blame] | 505 | Py_DECREF(exc); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 506 | _PyErr_NormalizeException(tstate, &exc2, &val2, &tb2); |
Serhiy Storchaka | e2bd2a7 | 2014-10-08 22:31:52 +0300 | [diff] [blame] | 507 | PyException_SetContext(val2, val); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 508 | _PyErr_Restore(tstate, exc2, val2, tb2); |
Serhiy Storchaka | e2bd2a7 | 2014-10-08 22:31:52 +0300 | [diff] [blame] | 509 | } |
| 510 | else { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 511 | _PyErr_Restore(tstate, exc, val, tb); |
Serhiy Storchaka | e2bd2a7 | 2014-10-08 22:31:52 +0300 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 515 | static PyObject * |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 516 | _PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception, |
| 517 | const char *format, va_list vargs) |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 518 | { |
| 519 | PyObject *exc, *val, *val2, *tb; |
| 520 | |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 521 | assert(_PyErr_Occurred(tstate)); |
| 522 | _PyErr_Fetch(tstate, &exc, &val, &tb); |
| 523 | _PyErr_NormalizeException(tstate, &exc, &val, &tb); |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 524 | if (tb != NULL) { |
| 525 | PyException_SetTraceback(val, tb); |
| 526 | Py_DECREF(tb); |
| 527 | } |
| 528 | Py_DECREF(exc); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 529 | assert(!_PyErr_Occurred(tstate)); |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 530 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 531 | _PyErr_FormatV(tstate, exception, format, vargs); |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 532 | |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 533 | _PyErr_Fetch(tstate, &exc, &val2, &tb); |
| 534 | _PyErr_NormalizeException(tstate, &exc, &val2, &tb); |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 535 | Py_INCREF(val); |
| 536 | PyException_SetCause(val2, val); |
| 537 | PyException_SetContext(val2, val); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 538 | _PyErr_Restore(tstate, exc, val2, tb); |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 539 | |
| 540 | return NULL; |
| 541 | } |
| 542 | |
| 543 | PyObject * |
Victor Stinner | 1726909 | 2019-11-05 01:22:12 +0100 | [diff] [blame] | 544 | _PyErr_FormatFromCauseTstate(PyThreadState *tstate, PyObject *exception, |
| 545 | const char *format, ...) |
| 546 | { |
| 547 | va_list vargs; |
| 548 | #ifdef HAVE_STDARG_PROTOTYPES |
| 549 | va_start(vargs, format); |
| 550 | #else |
| 551 | va_start(vargs); |
| 552 | #endif |
| 553 | _PyErr_FormatVFromCause(tstate, exception, format, vargs); |
| 554 | va_end(vargs); |
| 555 | return NULL; |
| 556 | } |
| 557 | |
| 558 | PyObject * |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 559 | _PyErr_FormatFromCause(PyObject *exception, const char *format, ...) |
| 560 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 561 | PyThreadState *tstate = _PyThreadState_GET(); |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 562 | va_list vargs; |
| 563 | #ifdef HAVE_STDARG_PROTOTYPES |
| 564 | va_start(vargs, format); |
| 565 | #else |
| 566 | va_start(vargs); |
| 567 | #endif |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 568 | _PyErr_FormatVFromCause(tstate, exception, format, vargs); |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 569 | va_end(vargs); |
| 570 | return NULL; |
| 571 | } |
| 572 | |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 573 | /* Convenience functions to set a type error exception and return 0 */ |
| 574 | |
| 575 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 576 | PyErr_BadArgument(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 577 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 578 | PyThreadState *tstate = _PyThreadState_GET(); |
| 579 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 580 | "bad argument type for built-in operation"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | return 0; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 584 | PyObject * |
Victor Stinner | 838f264 | 2019-06-13 22:41:23 +0200 | [diff] [blame] | 585 | _PyErr_NoMemory(PyThreadState *tstate) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 586 | { |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 587 | if (Py_IS_TYPE(PyExc_MemoryError, NULL)) { |
Victor Stinner | f54a574 | 2013-07-22 22:28:37 +0200 | [diff] [blame] | 588 | /* PyErr_NoMemory() has been called before PyExc_MemoryError has been |
| 589 | initialized by _PyExc_Init() */ |
| 590 | Py_FatalError("Out of memory and PyExc_MemoryError is not " |
| 591 | "initialized yet"); |
| 592 | } |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 593 | _PyErr_SetNone(tstate, PyExc_MemoryError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | return NULL; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 597 | PyObject * |
Victor Stinner | 838f264 | 2019-06-13 22:41:23 +0200 | [diff] [blame] | 598 | PyErr_NoMemory(void) |
| 599 | { |
| 600 | PyThreadState *tstate = _PyThreadState_GET(); |
| 601 | return _PyErr_NoMemory(tstate); |
| 602 | } |
| 603 | |
| 604 | PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 605 | PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 606 | { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 607 | return PyErr_SetFromErrnoWithFilenameObjects(exc, filenameObject, NULL); |
| 608 | } |
| 609 | |
| 610 | PyObject * |
| 611 | PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2) |
| 612 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 613 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | PyObject *message; |
Antoine Pitrou | 5d6fbe8 | 2011-10-12 19:39:57 +0200 | [diff] [blame] | 615 | PyObject *v, *args; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | int i = errno; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 617 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | WCHAR *s_buf = NULL; |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 619 | #endif /* Unix/Windows */ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 620 | |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 621 | #ifdef EINTR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | if (i == EINTR && PyErr_CheckSignals()) |
| 623 | return NULL; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 624 | #endif |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 625 | |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 626 | #ifndef MS_WINDOWS |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 627 | if (i != 0) { |
Andy Lester | 7386a70 | 2020-02-13 22:42:56 -0600 | [diff] [blame] | 628 | const char *s = strerror(i); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 629 | message = PyUnicode_DecodeLocale(s, "surrogateescape"); |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 630 | } |
| 631 | else { |
| 632 | /* Sometimes errno didn't get set */ |
| 633 | message = PyUnicode_FromString("Error"); |
| 634 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 635 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | if (i == 0) |
| 637 | message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */ |
| 638 | else |
| 639 | { |
| 640 | /* Note that the Win32 errors do not lineup with the |
| 641 | errno error. So if the error is in the MSVC error |
| 642 | table, we use it, otherwise we assume it really _is_ |
| 643 | a Win32 error code |
| 644 | */ |
| 645 | if (i > 0 && i < _sys_nerr) { |
| 646 | message = PyUnicode_FromString(_sys_errlist[i]); |
| 647 | } |
| 648 | else { |
| 649 | int len = FormatMessageW( |
| 650 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 651 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 652 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 653 | NULL, /* no message source */ |
| 654 | i, |
| 655 | MAKELANGID(LANG_NEUTRAL, |
| 656 | SUBLANG_DEFAULT), |
| 657 | /* Default language */ |
| 658 | (LPWSTR) &s_buf, |
| 659 | 0, /* size not used */ |
| 660 | NULL); /* no args */ |
| 661 | if (len==0) { |
| 662 | /* Only ever seen this in out-of-mem |
| 663 | situations */ |
| 664 | s_buf = NULL; |
Serhiy Storchaka | f41f8f9 | 2015-04-02 09:47:27 +0300 | [diff] [blame] | 665 | message = PyUnicode_FromFormat("Windows Error 0x%x", i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | } else { |
| 667 | /* remove trailing cr/lf and dots */ |
| 668 | while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) |
| 669 | s_buf[--len] = L'\0'; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 670 | message = PyUnicode_FromWideChar(s_buf, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 674 | #endif /* Unix/Windows */ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | if (message == NULL) |
| 677 | { |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 678 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | LocalFree(s_buf); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 680 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | return NULL; |
| 682 | } |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 683 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 684 | if (filenameObject != NULL) { |
| 685 | if (filenameObject2 != NULL) |
| 686 | args = Py_BuildValue("(iOOiO)", i, message, filenameObject, 0, filenameObject2); |
| 687 | else |
| 688 | args = Py_BuildValue("(iOO)", i, message, filenameObject); |
| 689 | } else { |
| 690 | assert(filenameObject2 == NULL); |
Antoine Pitrou | 5d6fbe8 | 2011-10-12 19:39:57 +0200 | [diff] [blame] | 691 | args = Py_BuildValue("(iO)", i, message); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 692 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 693 | Py_DECREF(message); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 694 | |
Antoine Pitrou | 5d6fbe8 | 2011-10-12 19:39:57 +0200 | [diff] [blame] | 695 | if (args != NULL) { |
| 696 | v = PyObject_Call(exc, args, NULL); |
| 697 | Py_DECREF(args); |
| 698 | if (v != NULL) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 699 | _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v); |
Antoine Pitrou | 5d6fbe8 | 2011-10-12 19:39:57 +0200 | [diff] [blame] | 700 | Py_DECREF(v); |
| 701 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 703 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | LocalFree(s_buf); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 705 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | return NULL; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 707 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 708 | |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 709 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 710 | PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 711 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 713 | PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | Py_XDECREF(name); |
| 715 | return result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 718 | #ifdef MS_WINDOWS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 719 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 720 | PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 721 | { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 722 | PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 723 | PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 724 | Py_XDECREF(name); |
| 725 | return result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 726 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 727 | #endif /* MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 728 | |
| 729 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 730 | PyErr_SetFromErrno(PyObject *exc) |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 731 | { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 732 | return PyErr_SetFromErrnoWithFilenameObjects(exc, NULL, NULL); |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 733 | } |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 734 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 735 | #ifdef MS_WINDOWS |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 736 | /* Windows specific error code handling */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 737 | PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 738 | PyObject *exc, |
| 739 | int ierr, |
| 740 | PyObject *filenameObject) |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 741 | { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 742 | return PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, ierr, |
| 743 | filenameObject, NULL); |
| 744 | } |
| 745 | |
| 746 | PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 747 | PyObject *exc, |
| 748 | int ierr, |
| 749 | PyObject *filenameObject, |
| 750 | PyObject *filenameObject2) |
| 751 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 752 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 753 | int len; |
| 754 | WCHAR *s_buf = NULL; /* Free via LocalFree */ |
| 755 | PyObject *message; |
Victor Stinner | 9ea8e4c | 2011-10-17 20:18:58 +0200 | [diff] [blame] | 756 | PyObject *args, *v; |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | DWORD err = (DWORD)ierr; |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 759 | if (err==0) { |
| 760 | err = GetLastError(); |
| 761 | } |
| 762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | len = FormatMessageW( |
| 764 | /* Error API error */ |
| 765 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 766 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 767 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 768 | NULL, /* no message source */ |
| 769 | err, |
| 770 | MAKELANGID(LANG_NEUTRAL, |
| 771 | SUBLANG_DEFAULT), /* Default language */ |
| 772 | (LPWSTR) &s_buf, |
| 773 | 0, /* size not used */ |
| 774 | NULL); /* no args */ |
| 775 | if (len==0) { |
| 776 | /* Only seen this in out of mem situations */ |
Serhiy Storchaka | f41f8f9 | 2015-04-02 09:47:27 +0300 | [diff] [blame] | 777 | message = PyUnicode_FromFormat("Windows Error 0x%x", err); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | s_buf = NULL; |
| 779 | } else { |
| 780 | /* remove trailing cr/lf and dots */ |
| 781 | while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) |
| 782 | s_buf[--len] = L'\0'; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 783 | message = PyUnicode_FromWideChar(s_buf, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | } |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | if (message == NULL) |
| 787 | { |
| 788 | LocalFree(s_buf); |
| 789 | return NULL; |
| 790 | } |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 791 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 792 | if (filenameObject == NULL) { |
| 793 | assert(filenameObject2 == NULL); |
| 794 | filenameObject = filenameObject2 = Py_None; |
| 795 | } |
| 796 | else if (filenameObject2 == NULL) |
| 797 | filenameObject2 = Py_None; |
| 798 | /* This is the constructor signature for OSError. |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 799 | The POSIX translation will be figured out by the constructor. */ |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 800 | args = Py_BuildValue("(iOOiO)", 0, message, filenameObject, err, filenameObject2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 801 | Py_DECREF(message); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 802 | |
Victor Stinner | 9ea8e4c | 2011-10-17 20:18:58 +0200 | [diff] [blame] | 803 | if (args != NULL) { |
| 804 | v = PyObject_Call(exc, args, NULL); |
| 805 | Py_DECREF(args); |
| 806 | if (v != NULL) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 807 | _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v); |
Victor Stinner | 9ea8e4c | 2011-10-17 20:18:58 +0200 | [diff] [blame] | 808 | Py_DECREF(v); |
| 809 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | } |
| 811 | LocalFree(s_buf); |
| 812 | return NULL; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 815 | PyObject *PyErr_SetExcFromWindowsErrWithFilename( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | PyObject *exc, |
| 817 | int ierr, |
| 818 | const char *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 819 | { |
Victor Stinner | 92be939 | 2010-12-28 00:28:21 +0000 | [diff] [blame] | 820 | PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 821 | PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | ierr, |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 823 | name, |
| 824 | NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | Py_XDECREF(name); |
| 826 | return ret; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 829 | PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | PyObject *exc, |
| 831 | int ierr, |
| 832 | const Py_UNICODE *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 833 | { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 834 | PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 835 | PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | ierr, |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 837 | name, |
| 838 | NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | Py_XDECREF(name); |
| 840 | return ret; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 841 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 842 | |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 843 | PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr) |
| 844 | { |
Larry Hastings | 8f9f0f1 | 2014-02-10 03:43:57 -0800 | [diff] [blame] | 845 | return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL); |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 848 | PyObject *PyErr_SetFromWindowsErr(int ierr) |
| 849 | { |
Larry Hastings | 8f9f0f1 | 2014-02-10 03:43:57 -0800 | [diff] [blame] | 850 | return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError, |
| 851 | ierr, NULL); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 854 | PyObject *PyErr_SetFromWindowsErrWithFilename( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | int ierr, |
| 856 | const char *filename) |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 857 | { |
Victor Stinner | 92be939 | 2010-12-28 00:28:21 +0000 | [diff] [blame] | 858 | PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 859 | PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 860 | PyExc_OSError, |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 861 | ierr, name, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | Py_XDECREF(name); |
| 863 | return result; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 864 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 865 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 866 | PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 867 | int ierr, |
| 868 | const Py_UNICODE *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 869 | { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 870 | PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 871 | PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 872 | PyExc_OSError, |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 873 | ierr, name, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | Py_XDECREF(name); |
| 875 | return result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 876 | } |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 877 | #endif /* MS_WINDOWS */ |
| 878 | |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 879 | PyObject * |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 880 | PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg, |
| 881 | PyObject *name, PyObject *path) |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 882 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 883 | PyThreadState *tstate = _PyThreadState_GET(); |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 884 | int issubclass; |
Victor Stinner | f45a561 | 2016-08-23 00:04:41 +0200 | [diff] [blame] | 885 | PyObject *kwargs, *error; |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 886 | |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 887 | issubclass = PyObject_IsSubclass(exception, PyExc_ImportError); |
| 888 | if (issubclass < 0) { |
| 889 | return NULL; |
| 890 | } |
| 891 | else if (!issubclass) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 892 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 893 | "expected a subclass of ImportError"); |
Brian Curtin | 94c001b | 2012-04-18 08:30:51 -0500 | [diff] [blame] | 894 | return NULL; |
Victor Stinner | f45a561 | 2016-08-23 00:04:41 +0200 | [diff] [blame] | 895 | } |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 896 | |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 897 | if (msg == NULL) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 898 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 899 | "expected a message argument"); |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 900 | return NULL; |
Benjamin Peterson | da20cd2 | 2012-04-18 10:48:00 -0400 | [diff] [blame] | 901 | } |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 902 | |
Brian Curtin | 94c001b | 2012-04-18 08:30:51 -0500 | [diff] [blame] | 903 | if (name == NULL) { |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 904 | name = Py_None; |
Brian Curtin | 94c001b | 2012-04-18 08:30:51 -0500 | [diff] [blame] | 905 | } |
Brian Curtin | 94c001b | 2012-04-18 08:30:51 -0500 | [diff] [blame] | 906 | if (path == NULL) { |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 907 | path = Py_None; |
Brian Curtin | 94c001b | 2012-04-18 08:30:51 -0500 | [diff] [blame] | 908 | } |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 909 | |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 910 | kwargs = PyDict_New(); |
| 911 | if (kwargs == NULL) { |
| 912 | return NULL; |
| 913 | } |
Victor Stinner | f45a561 | 2016-08-23 00:04:41 +0200 | [diff] [blame] | 914 | if (PyDict_SetItemString(kwargs, "name", name) < 0) { |
Berker Peksag | ec766d3 | 2016-05-01 09:06:36 +0300 | [diff] [blame] | 915 | goto done; |
Victor Stinner | f45a561 | 2016-08-23 00:04:41 +0200 | [diff] [blame] | 916 | } |
| 917 | if (PyDict_SetItemString(kwargs, "path", path) < 0) { |
Berker Peksag | ec766d3 | 2016-05-01 09:06:36 +0300 | [diff] [blame] | 918 | goto done; |
Victor Stinner | f45a561 | 2016-08-23 00:04:41 +0200 | [diff] [blame] | 919 | } |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 920 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 921 | error = PyObject_VectorcallDict(exception, &msg, 1, kwargs); |
Benjamin Peterson | da20cd2 | 2012-04-18 10:48:00 -0400 | [diff] [blame] | 922 | if (error != NULL) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 923 | _PyErr_SetObject(tstate, (PyObject *)Py_TYPE(error), error); |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 924 | Py_DECREF(error); |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 925 | } |
| 926 | |
Berker Peksag | ec766d3 | 2016-05-01 09:06:36 +0300 | [diff] [blame] | 927 | done: |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 928 | Py_DECREF(kwargs); |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 929 | return NULL; |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 930 | } |
| 931 | |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 932 | PyObject * |
| 933 | PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) |
| 934 | { |
| 935 | return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path); |
| 936 | } |
| 937 | |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 938 | void |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 939 | _PyErr_BadInternalCall(const char *filename, int lineno) |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 940 | { |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 941 | PyThreadState *tstate = _PyThreadState_GET(); |
| 942 | _PyErr_Format(tstate, PyExc_SystemError, |
| 943 | "%s:%d: bad argument to internal function", |
| 944 | filename, lineno); |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can |
| 948 | export the entry point for existing object code: */ |
| 949 | #undef PyErr_BadInternalCall |
| 950 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 951 | PyErr_BadInternalCall(void) |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 952 | { |
Victor Stinner | fb3a630 | 2013-07-12 00:37:30 +0200 | [diff] [blame] | 953 | assert(0 && "bad argument to internal function"); |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 954 | PyThreadState *tstate = _PyThreadState_GET(); |
| 955 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 956 | "bad argument to internal function"); |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 957 | } |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 958 | #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) |
| 959 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 960 | |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 961 | static PyObject * |
| 962 | _PyErr_FormatV(PyThreadState *tstate, PyObject *exception, |
| 963 | const char *format, va_list vargs) |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 964 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | PyObject* string; |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 966 | |
Victor Stinner | de821be | 2015-03-24 12:41:23 +0100 | [diff] [blame] | 967 | /* Issue #23571: PyUnicode_FromFormatV() must not be called with an |
| 968 | exception set, it calls arbitrary Python code like PyObject_Repr() */ |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 969 | _PyErr_Clear(tstate); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | string = PyUnicode_FromFormatV(format, vargs); |
Victor Stinner | de821be | 2015-03-24 12:41:23 +0100 | [diff] [blame] | 972 | |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 973 | _PyErr_SetObject(tstate, exception, string); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | Py_XDECREF(string); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | return NULL; |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 976 | } |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 977 | |
| 978 | |
Antoine Pitrou | 0676a40 | 2014-09-30 21:16:27 +0200 | [diff] [blame] | 979 | PyObject * |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 980 | PyErr_FormatV(PyObject *exception, const char *format, va_list vargs) |
| 981 | { |
| 982 | PyThreadState *tstate = _PyThreadState_GET(); |
| 983 | return _PyErr_FormatV(tstate, exception, format, vargs); |
| 984 | } |
| 985 | |
| 986 | |
| 987 | PyObject * |
| 988 | _PyErr_Format(PyThreadState *tstate, PyObject *exception, |
| 989 | const char *format, ...) |
Antoine Pitrou | 0676a40 | 2014-09-30 21:16:27 +0200 | [diff] [blame] | 990 | { |
| 991 | va_list vargs; |
| 992 | #ifdef HAVE_STDARG_PROTOTYPES |
| 993 | va_start(vargs, format); |
| 994 | #else |
| 995 | va_start(vargs); |
| 996 | #endif |
Victor Stinner | 438a12d | 2019-05-24 17:01:38 +0200 | [diff] [blame] | 997 | _PyErr_FormatV(tstate, exception, format, vargs); |
| 998 | va_end(vargs); |
| 999 | return NULL; |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | PyObject * |
| 1004 | PyErr_Format(PyObject *exception, const char *format, ...) |
| 1005 | { |
| 1006 | PyThreadState *tstate = _PyThreadState_GET(); |
| 1007 | va_list vargs; |
| 1008 | #ifdef HAVE_STDARG_PROTOTYPES |
| 1009 | va_start(vargs, format); |
| 1010 | #else |
| 1011 | va_start(vargs); |
| 1012 | #endif |
| 1013 | _PyErr_FormatV(tstate, exception, format, vargs); |
Antoine Pitrou | 0676a40 | 2014-09-30 21:16:27 +0200 | [diff] [blame] | 1014 | va_end(vargs); |
| 1015 | return NULL; |
| 1016 | } |
| 1017 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1018 | |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 1019 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 1020 | PyErr_NewException(const char *name, PyObject *base, PyObject *dict) |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 1021 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1022 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | PyObject *modulename = NULL; |
| 1024 | PyObject *classname = NULL; |
| 1025 | PyObject *mydict = NULL; |
| 1026 | PyObject *bases = NULL; |
| 1027 | PyObject *result = NULL; |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1028 | |
| 1029 | const char *dot = strrchr(name, '.'); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | if (dot == NULL) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1031 | _PyErr_SetString(tstate, PyExc_SystemError, |
| 1032 | "PyErr_NewException: name must be module.class"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | return NULL; |
| 1034 | } |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1035 | if (base == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | base = PyExc_Exception; |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1037 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | if (dict == NULL) { |
| 1039 | dict = mydict = PyDict_New(); |
| 1040 | if (dict == NULL) |
| 1041 | goto failure; |
| 1042 | } |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1043 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 1044 | if (_PyDict_GetItemIdWithError(dict, &PyId___module__) == NULL) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1045 | if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 1046 | goto failure; |
| 1047 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | modulename = PyUnicode_FromStringAndSize(name, |
| 1049 | (Py_ssize_t)(dot-name)); |
| 1050 | if (modulename == NULL) |
| 1051 | goto failure; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 1052 | if (_PyDict_SetItemId(dict, &PyId___module__, modulename) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | goto failure; |
| 1054 | } |
| 1055 | if (PyTuple_Check(base)) { |
| 1056 | bases = base; |
| 1057 | /* INCREF as we create a new ref in the else branch */ |
| 1058 | Py_INCREF(bases); |
| 1059 | } else { |
| 1060 | bases = PyTuple_Pack(1, base); |
| 1061 | if (bases == NULL) |
| 1062 | goto failure; |
| 1063 | } |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 1064 | /* Create a real class. */ |
Victor Stinner | 7eeb5b5 | 2010-06-07 19:57:46 +0000 | [diff] [blame] | 1065 | result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 | dot+1, bases, dict); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 1067 | failure: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | Py_XDECREF(bases); |
| 1069 | Py_XDECREF(mydict); |
| 1070 | Py_XDECREF(classname); |
| 1071 | Py_XDECREF(modulename); |
| 1072 | return result; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 1073 | } |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 1074 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 1075 | |
| 1076 | /* Create an exception with docstring */ |
| 1077 | PyObject * |
| 1078 | PyErr_NewExceptionWithDoc(const char *name, const char *doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | PyObject *base, PyObject *dict) |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 1080 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1081 | int result; |
| 1082 | PyObject *ret = NULL; |
| 1083 | PyObject *mydict = NULL; /* points to the dict only if we create it */ |
| 1084 | PyObject *docobj; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 1085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | if (dict == NULL) { |
| 1087 | dict = mydict = PyDict_New(); |
| 1088 | if (dict == NULL) { |
| 1089 | return NULL; |
| 1090 | } |
| 1091 | } |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 1092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | if (doc != NULL) { |
| 1094 | docobj = PyUnicode_FromString(doc); |
| 1095 | if (docobj == NULL) |
| 1096 | goto failure; |
| 1097 | result = PyDict_SetItemString(dict, "__doc__", docobj); |
| 1098 | Py_DECREF(docobj); |
| 1099 | if (result < 0) |
| 1100 | goto failure; |
| 1101 | } |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 1102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | ret = PyErr_NewException(name, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 1104 | failure: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 | Py_XDECREF(mydict); |
| 1106 | return ret; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1110 | PyDoc_STRVAR(UnraisableHookArgs__doc__, |
| 1111 | "UnraisableHookArgs\n\ |
| 1112 | \n\ |
| 1113 | Type used to pass arguments to sys.unraisablehook."); |
| 1114 | |
| 1115 | static PyTypeObject UnraisableHookArgsType; |
| 1116 | |
| 1117 | static PyStructSequence_Field UnraisableHookArgs_fields[] = { |
| 1118 | {"exc_type", "Exception type"}, |
| 1119 | {"exc_value", "Exception value"}, |
| 1120 | {"exc_traceback", "Exception traceback"}, |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1121 | {"err_msg", "Error message"}, |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1122 | {"object", "Object causing the exception"}, |
| 1123 | {0} |
| 1124 | }; |
| 1125 | |
| 1126 | static PyStructSequence_Desc UnraisableHookArgs_desc = { |
| 1127 | .name = "UnraisableHookArgs", |
| 1128 | .doc = UnraisableHookArgs__doc__, |
| 1129 | .fields = UnraisableHookArgs_fields, |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1130 | .n_in_sequence = 5 |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1131 | }; |
| 1132 | |
| 1133 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1134 | PyStatus |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1135 | _PyErr_Init(void) |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 1136 | { |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1137 | if (UnraisableHookArgsType.tp_name == NULL) { |
| 1138 | if (PyStructSequence_InitType2(&UnraisableHookArgsType, |
| 1139 | &UnraisableHookArgs_desc) < 0) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1140 | return _PyStatus_ERR("failed to initialize UnraisableHookArgs type"); |
Martin Panter | 3263f68 | 2016-02-28 03:16:11 +0000 | [diff] [blame] | 1141 | } |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1142 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1143 | return _PyStatus_OK(); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | |
| 1147 | static PyObject * |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1148 | make_unraisable_hook_args(PyThreadState *tstate, PyObject *exc_type, |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1149 | PyObject *exc_value, PyObject *exc_tb, |
| 1150 | PyObject *err_msg, PyObject *obj) |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1151 | { |
| 1152 | PyObject *args = PyStructSequence_New(&UnraisableHookArgsType); |
| 1153 | if (args == NULL) { |
| 1154 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | } |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1156 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1157 | Py_ssize_t pos = 0; |
| 1158 | #define ADD_ITEM(exc_type) \ |
| 1159 | do { \ |
| 1160 | if (exc_type == NULL) { \ |
| 1161 | exc_type = Py_None; \ |
| 1162 | } \ |
| 1163 | Py_INCREF(exc_type); \ |
| 1164 | PyStructSequence_SET_ITEM(args, pos++, exc_type); \ |
| 1165 | } while (0) |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1166 | |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1167 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1168 | ADD_ITEM(exc_type); |
| 1169 | ADD_ITEM(exc_value); |
| 1170 | ADD_ITEM(exc_tb); |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1171 | ADD_ITEM(err_msg); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1172 | ADD_ITEM(obj); |
| 1173 | #undef ADD_ITEM |
| 1174 | |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1175 | if (_PyErr_Occurred(tstate)) { |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1176 | Py_DECREF(args); |
| 1177 | return NULL; |
| 1178 | } |
| 1179 | return args; |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | /* Default implementation of sys.unraisablehook. |
| 1185 | |
| 1186 | It can be called to log the exception of a custom sys.unraisablehook. |
| 1187 | |
| 1188 | Do nothing if sys.stderr attribute doesn't exist or is set to None. */ |
| 1189 | static int |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1190 | write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type, |
| 1191 | PyObject *exc_value, PyObject *exc_tb, |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1192 | PyObject *err_msg, PyObject *obj, PyObject *file) |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1193 | { |
| 1194 | if (obj != NULL && obj != Py_None) { |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1195 | if (err_msg != NULL && err_msg != Py_None) { |
| 1196 | if (PyFile_WriteObject(err_msg, file, Py_PRINT_RAW) < 0) { |
| 1197 | return -1; |
| 1198 | } |
| 1199 | if (PyFile_WriteString(": ", file) < 0) { |
| 1200 | return -1; |
| 1201 | } |
| 1202 | } |
| 1203 | else { |
| 1204 | if (PyFile_WriteString("Exception ignored in: ", file) < 0) { |
| 1205 | return -1; |
| 1206 | } |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | if (PyFile_WriteObject(obj, file, 0) < 0) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1210 | _PyErr_Clear(tstate); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1211 | if (PyFile_WriteString("<object repr() failed>", file) < 0) { |
| 1212 | return -1; |
| 1213 | } |
| 1214 | } |
| 1215 | if (PyFile_WriteString("\n", file) < 0) { |
| 1216 | return -1; |
| 1217 | } |
| 1218 | } |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1219 | else if (err_msg != NULL && err_msg != Py_None) { |
| 1220 | if (PyFile_WriteObject(err_msg, file, Py_PRINT_RAW) < 0) { |
| 1221 | return -1; |
| 1222 | } |
| 1223 | if (PyFile_WriteString(":\n", file) < 0) { |
| 1224 | return -1; |
| 1225 | } |
| 1226 | } |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1227 | |
| 1228 | if (exc_tb != NULL && exc_tb != Py_None) { |
| 1229 | if (PyTraceBack_Print(exc_tb, file) < 0) { |
| 1230 | /* continue even if writing the traceback failed */ |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1231 | _PyErr_Clear(tstate); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
Victor Stinner | df22c03 | 2019-05-23 01:00:58 +0200 | [diff] [blame] | 1235 | if (exc_type == NULL || exc_type == Py_None) { |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1236 | return -1; |
| 1237 | } |
| 1238 | |
| 1239 | assert(PyExceptionClass_Check(exc_type)); |
| 1240 | const char *className = PyExceptionClass_Name(exc_type); |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1241 | if (className != NULL) { |
Serhiy Storchaka | ceeef10 | 2018-06-15 11:09:43 +0300 | [diff] [blame] | 1242 | const char *dot = strrchr(className, '.'); |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1243 | if (dot != NULL) { |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1244 | className = dot+1; |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1245 | } |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1246 | } |
| 1247 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1248 | PyObject *moduleName = _PyObject_GetAttrId(exc_type, &PyId___module__); |
Oren Milman | f6e61df | 2017-09-14 01:30:05 +0300 | [diff] [blame] | 1249 | if (moduleName == NULL || !PyUnicode_Check(moduleName)) { |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1250 | Py_XDECREF(moduleName); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1251 | _PyErr_Clear(tstate); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1252 | if (PyFile_WriteString("<unknown>", file) < 0) { |
| 1253 | return -1; |
| 1254 | } |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1255 | } |
| 1256 | else { |
Serhiy Storchaka | f5894dd | 2016-11-16 15:40:39 +0200 | [diff] [blame] | 1257 | if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins)) { |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1258 | if (PyFile_WriteObject(moduleName, file, Py_PRINT_RAW) < 0) { |
| 1259 | Py_DECREF(moduleName); |
| 1260 | return -1; |
| 1261 | } |
| 1262 | Py_DECREF(moduleName); |
| 1263 | if (PyFile_WriteString(".", file) < 0) { |
| 1264 | return -1; |
| 1265 | } |
| 1266 | } |
| 1267 | else { |
| 1268 | Py_DECREF(moduleName); |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | if (className == NULL) { |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1272 | if (PyFile_WriteString("<unknown>", file) < 0) { |
| 1273 | return -1; |
| 1274 | } |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1275 | } |
| 1276 | else { |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1277 | if (PyFile_WriteString(className, file) < 0) { |
| 1278 | return -1; |
| 1279 | } |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1280 | } |
| 1281 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1282 | if (exc_value && exc_value != Py_None) { |
| 1283 | if (PyFile_WriteString(": ", file) < 0) { |
| 1284 | return -1; |
| 1285 | } |
| 1286 | if (PyFile_WriteObject(exc_value, file, Py_PRINT_RAW) < 0) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1287 | _PyErr_Clear(tstate); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1288 | if (PyFile_WriteString("<exception str() failed>", file) < 0) { |
| 1289 | return -1; |
Martin Panter | 3263f68 | 2016-02-28 03:16:11 +0000 | [diff] [blame] | 1290 | } |
| 1291 | } |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1292 | } |
Victor Stinner | df22c03 | 2019-05-23 01:00:58 +0200 | [diff] [blame] | 1293 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1294 | if (PyFile_WriteString("\n", file) < 0) { |
| 1295 | return -1; |
| 1296 | } |
Victor Stinner | a85a1d3 | 2019-05-28 16:01:17 +0200 | [diff] [blame] | 1297 | |
| 1298 | /* Explicitly call file.flush() */ |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 1299 | PyObject *res = _PyObject_CallMethodIdNoArgs(file, &PyId_flush); |
Victor Stinner | a85a1d3 | 2019-05-28 16:01:17 +0200 | [diff] [blame] | 1300 | if (!res) { |
| 1301 | return -1; |
| 1302 | } |
| 1303 | Py_DECREF(res); |
| 1304 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1305 | return 0; |
| 1306 | } |
| 1307 | |
| 1308 | |
| 1309 | static int |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1310 | write_unraisable_exc(PyThreadState *tstate, PyObject *exc_type, |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1311 | PyObject *exc_value, PyObject *exc_tb, PyObject *err_msg, |
| 1312 | PyObject *obj) |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1313 | { |
| 1314 | PyObject *file = _PySys_GetObjectId(&PyId_stderr); |
| 1315 | if (file == NULL || file == Py_None) { |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | /* Hold a strong reference to ensure that sys.stderr doesn't go away |
| 1320 | while we use it */ |
| 1321 | Py_INCREF(file); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1322 | int res = write_unraisable_exc_file(tstate, exc_type, exc_value, exc_tb, |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1323 | err_msg, obj, file); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1324 | Py_DECREF(file); |
| 1325 | |
| 1326 | return res; |
| 1327 | } |
| 1328 | |
| 1329 | |
| 1330 | PyObject* |
| 1331 | _PyErr_WriteUnraisableDefaultHook(PyObject *args) |
| 1332 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1333 | PyThreadState *tstate = _PyThreadState_GET(); |
| 1334 | |
Andy Lester | 5572870 | 2020-03-06 16:53:17 -0600 | [diff] [blame] | 1335 | if (!Py_IS_TYPE(args, &UnraisableHookArgsType)) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1336 | _PyErr_SetString(tstate, PyExc_TypeError, |
| 1337 | "sys.unraisablehook argument type " |
| 1338 | "must be UnraisableHookArgs"); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1339 | return NULL; |
| 1340 | } |
| 1341 | |
| 1342 | /* Borrowed references */ |
| 1343 | PyObject *exc_type = PyStructSequence_GET_ITEM(args, 0); |
| 1344 | PyObject *exc_value = PyStructSequence_GET_ITEM(args, 1); |
| 1345 | PyObject *exc_tb = PyStructSequence_GET_ITEM(args, 2); |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1346 | PyObject *err_msg = PyStructSequence_GET_ITEM(args, 3); |
| 1347 | PyObject *obj = PyStructSequence_GET_ITEM(args, 4); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1348 | |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1349 | if (write_unraisable_exc(tstate, exc_type, exc_value, exc_tb, err_msg, obj) < 0) { |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1350 | return NULL; |
| 1351 | } |
| 1352 | Py_RETURN_NONE; |
| 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | /* Call sys.unraisablehook(). |
| 1357 | |
| 1358 | This function can be used when an exception has occurred but there is no way |
| 1359 | for Python to handle it. For example, when a destructor raises an exception |
| 1360 | or during garbage collection (gc.collect()). |
| 1361 | |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1362 | If err_msg_str is non-NULL, the error message is formatted as: |
| 1363 | "Exception ignored %s" % err_msg_str. Otherwise, use "Exception ignored in" |
| 1364 | error message. |
| 1365 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1366 | An exception must be set when calling this function. */ |
| 1367 | void |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1368 | _PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj) |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1369 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1370 | PyThreadState *tstate = _PyThreadState_GET(); |
| 1371 | assert(tstate != NULL); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1372 | |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1373 | PyObject *err_msg = NULL; |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1374 | PyObject *exc_type, *exc_value, *exc_tb; |
| 1375 | _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1376 | |
| 1377 | assert(exc_type != NULL); |
| 1378 | |
| 1379 | if (exc_type == NULL) { |
| 1380 | /* sys.unraisablehook requires that at least exc_type is set */ |
| 1381 | goto default_hook; |
| 1382 | } |
| 1383 | |
Victor Stinner | df22c03 | 2019-05-23 01:00:58 +0200 | [diff] [blame] | 1384 | if (exc_tb == NULL) { |
Victor Stinner | 4386b90 | 2020-04-29 03:01:43 +0200 | [diff] [blame] | 1385 | PyFrameObject *frame = tstate->frame; |
Victor Stinner | df22c03 | 2019-05-23 01:00:58 +0200 | [diff] [blame] | 1386 | if (frame != NULL) { |
| 1387 | exc_tb = _PyTraceBack_FromFrame(NULL, frame); |
| 1388 | if (exc_tb == NULL) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1389 | _PyErr_Clear(tstate); |
Victor Stinner | df22c03 | 2019-05-23 01:00:58 +0200 | [diff] [blame] | 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1394 | _PyErr_NormalizeException(tstate, &exc_type, &exc_value, &exc_tb); |
Victor Stinner | df22c03 | 2019-05-23 01:00:58 +0200 | [diff] [blame] | 1395 | |
| 1396 | if (exc_tb != NULL && exc_tb != Py_None && PyTraceBack_Check(exc_tb)) { |
| 1397 | if (PyException_SetTraceback(exc_value, exc_tb) < 0) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1398 | _PyErr_Clear(tstate); |
Victor Stinner | df22c03 | 2019-05-23 01:00:58 +0200 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1402 | if (err_msg_str != NULL) { |
| 1403 | err_msg = PyUnicode_FromFormat("Exception ignored %s", err_msg_str); |
| 1404 | if (err_msg == NULL) { |
| 1405 | PyErr_Clear(); |
| 1406 | } |
| 1407 | } |
| 1408 | |
Steve Dower | bea33f5 | 2019-11-28 08:46:11 -0800 | [diff] [blame] | 1409 | PyObject *hook_args = make_unraisable_hook_args( |
| 1410 | tstate, exc_type, exc_value, exc_tb, err_msg, obj); |
| 1411 | if (hook_args == NULL) { |
| 1412 | err_msg_str = ("Exception ignored on building " |
| 1413 | "sys.unraisablehook arguments"); |
| 1414 | goto error; |
| 1415 | } |
| 1416 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1417 | _Py_IDENTIFIER(unraisablehook); |
| 1418 | PyObject *hook = _PySys_GetObjectId(&PyId_unraisablehook); |
Steve Dower | bea33f5 | 2019-11-28 08:46:11 -0800 | [diff] [blame] | 1419 | if (hook == NULL) { |
| 1420 | Py_DECREF(hook_args); |
| 1421 | goto default_hook; |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1422 | } |
| 1423 | |
Victor Stinner | 1c1e68c | 2020-03-27 15:11:45 +0100 | [diff] [blame] | 1424 | if (_PySys_Audit(tstate, "sys.unraisablehook", "OO", hook, hook_args) < 0) { |
Steve Dower | bea33f5 | 2019-11-28 08:46:11 -0800 | [diff] [blame] | 1425 | Py_DECREF(hook_args); |
| 1426 | err_msg_str = "Exception ignored in audit hook"; |
| 1427 | obj = NULL; |
| 1428 | goto error; |
| 1429 | } |
| 1430 | |
| 1431 | if (hook == Py_None) { |
| 1432 | Py_DECREF(hook_args); |
| 1433 | goto default_hook; |
| 1434 | } |
| 1435 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 1436 | PyObject *res = PyObject_CallOneArg(hook, hook_args); |
Steve Dower | bea33f5 | 2019-11-28 08:46:11 -0800 | [diff] [blame] | 1437 | Py_DECREF(hook_args); |
| 1438 | if (res != NULL) { |
| 1439 | Py_DECREF(res); |
| 1440 | goto done; |
| 1441 | } |
| 1442 | |
| 1443 | /* sys.unraisablehook failed: log its error using default hook */ |
| 1444 | obj = hook; |
| 1445 | err_msg_str = NULL; |
| 1446 | |
| 1447 | error: |
| 1448 | /* err_msg_str and obj have been updated and we have a new exception */ |
| 1449 | Py_XSETREF(err_msg, PyUnicode_FromString(err_msg_str ? |
| 1450 | err_msg_str : "Exception ignored in sys.unraisablehook")); |
| 1451 | Py_XDECREF(exc_type); |
| 1452 | Py_XDECREF(exc_value); |
| 1453 | Py_XDECREF(exc_tb); |
| 1454 | _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb); |
| 1455 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1456 | default_hook: |
| 1457 | /* Call the default unraisable hook (ignore failure) */ |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1458 | (void)write_unraisable_exc(tstate, exc_type, exc_value, exc_tb, |
| 1459 | err_msg, obj); |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1460 | |
| 1461 | done: |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 1462 | Py_XDECREF(exc_type); |
| 1463 | Py_XDECREF(exc_value); |
| 1464 | Py_XDECREF(exc_tb); |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1465 | Py_XDECREF(err_msg); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1466 | _PyErr_Clear(tstate); /* Just in case */ |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 1467 | } |
Guido van Rossum | cfd42b5 | 2000-12-15 21:58:52 +0000 | [diff] [blame] | 1468 | |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1469 | |
| 1470 | void |
| 1471 | PyErr_WriteUnraisable(PyObject *obj) |
| 1472 | { |
| 1473 | _PyErr_WriteUnraisableMsg(NULL, obj); |
| 1474 | } |
| 1475 | |
| 1476 | |
Armin Rigo | 092381a | 2003-10-25 14:29:27 +0000 | [diff] [blame] | 1477 | extern PyObject *PyModule_GetWarningsModule(void); |
Guido van Rossum | cfd42b5 | 2000-12-15 21:58:52 +0000 | [diff] [blame] | 1478 | |
Guido van Rossum | 2fd4565 | 2001-02-28 21:46:24 +0000 | [diff] [blame] | 1479 | |
Benjamin Peterson | 2c53971 | 2010-09-20 22:42:10 +0000 | [diff] [blame] | 1480 | void |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1481 | PyErr_SyntaxLocation(const char *filename, int lineno) |
| 1482 | { |
Benjamin Peterson | 2c53971 | 2010-09-20 22:42:10 +0000 | [diff] [blame] | 1483 | PyErr_SyntaxLocationEx(filename, lineno, -1); |
| 1484 | } |
| 1485 | |
| 1486 | |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1487 | /* Set file and line information for the current exception. |
| 1488 | If the exception is not a SyntaxError, also sets additional attributes |
| 1489 | to make printing of exceptions believe it is a syntax error. */ |
Guido van Rossum | 2fd4565 | 2001-02-28 21:46:24 +0000 | [diff] [blame] | 1490 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1491 | void |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1492 | PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset) |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1493 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | PyObject *exc, *v, *tb, *tmp; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1495 | _Py_IDENTIFIER(filename); |
| 1496 | _Py_IDENTIFIER(lineno); |
| 1497 | _Py_IDENTIFIER(msg); |
| 1498 | _Py_IDENTIFIER(offset); |
| 1499 | _Py_IDENTIFIER(print_file_and_line); |
| 1500 | _Py_IDENTIFIER(text); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1501 | PyThreadState *tstate = _PyThreadState_GET(); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | /* add attributes for the line number and filename for the error */ |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1504 | _PyErr_Fetch(tstate, &exc, &v, &tb); |
| 1505 | _PyErr_NormalizeException(tstate, &exc, &v, &tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | /* XXX check that it is, indeed, a syntax error. It might not |
| 1507 | * be, though. */ |
| 1508 | tmp = PyLong_FromLong(lineno); |
| 1509 | if (tmp == NULL) |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1510 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1511 | else { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1512 | if (_PyObject_SetAttrId(v, &PyId_lineno, tmp)) { |
| 1513 | _PyErr_Clear(tstate); |
| 1514 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | Py_DECREF(tmp); |
| 1516 | } |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 1517 | tmp = NULL; |
Benjamin Peterson | 2c53971 | 2010-09-20 22:42:10 +0000 | [diff] [blame] | 1518 | if (col_offset >= 0) { |
| 1519 | tmp = PyLong_FromLong(col_offset); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1520 | if (tmp == NULL) { |
| 1521 | _PyErr_Clear(tstate); |
| 1522 | } |
Benjamin Peterson | 2c53971 | 2010-09-20 22:42:10 +0000 | [diff] [blame] | 1523 | } |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1524 | if (_PyObject_SetAttrId(v, &PyId_offset, tmp ? tmp : Py_None)) { |
| 1525 | _PyErr_Clear(tstate); |
| 1526 | } |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 1527 | Py_XDECREF(tmp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1528 | if (filename != NULL) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1529 | if (_PyObject_SetAttrId(v, &PyId_filename, filename)) { |
| 1530 | _PyErr_Clear(tstate); |
| 1531 | } |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1532 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1533 | tmp = PyErr_ProgramTextObject(filename, lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | if (tmp) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1535 | if (_PyObject_SetAttrId(v, &PyId_text, tmp)) { |
| 1536 | _PyErr_Clear(tstate); |
| 1537 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | Py_DECREF(tmp); |
| 1539 | } |
| 1540 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1541 | if (exc != PyExc_SyntaxError) { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1542 | if (!_PyObject_HasAttrId(v, &PyId_msg)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | tmp = PyObject_Str(v); |
| 1544 | if (tmp) { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1545 | if (_PyObject_SetAttrId(v, &PyId_msg, tmp)) { |
| 1546 | _PyErr_Clear(tstate); |
| 1547 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | Py_DECREF(tmp); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1549 | } |
| 1550 | else { |
| 1551 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1552 | } |
| 1553 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1554 | if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) { |
| 1555 | if (_PyObject_SetAttrId(v, &PyId_print_file_and_line, |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1556 | Py_None)) { |
| 1557 | _PyErr_Clear(tstate); |
| 1558 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1559 | } |
| 1560 | } |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1561 | _PyErr_Restore(tstate, exc, v, tb); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1564 | void |
| 1565 | PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) |
| 1566 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1567 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1568 | PyObject *fileobj; |
| 1569 | if (filename != NULL) { |
| 1570 | fileobj = PyUnicode_DecodeFSDefault(filename); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1571 | if (fileobj == NULL) { |
| 1572 | _PyErr_Clear(tstate); |
| 1573 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1574 | } |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1575 | else { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1576 | fileobj = NULL; |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1577 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1578 | PyErr_SyntaxLocationObject(fileobj, lineno, col_offset); |
| 1579 | Py_XDECREF(fileobj); |
| 1580 | } |
| 1581 | |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 1582 | /* Attempt to load the line of text that the exception refers to. If it |
| 1583 | fails, it will return NULL but will not set an exception. |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1584 | |
| 1585 | XXX The functionality of this function is quite similar to the |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 1586 | functionality in tb_displayline() in traceback.c. */ |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1587 | |
Antoine Pitrou | 409b538 | 2013-10-12 22:41:17 +0200 | [diff] [blame] | 1588 | static PyObject * |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1589 | err_programtext(PyThreadState *tstate, FILE *fp, int lineno) |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1590 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | int i; |
| 1592 | char linebuf[1000]; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1594 | if (fp == NULL) |
| 1595 | return NULL; |
| 1596 | for (i = 0; i < lineno; i++) { |
| 1597 | char *pLastChar = &linebuf[sizeof(linebuf) - 2]; |
| 1598 | do { |
| 1599 | *pLastChar = '\0'; |
| 1600 | if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, |
| 1601 | fp, NULL) == NULL) |
| 1602 | break; |
| 1603 | /* fgets read *something*; if it didn't get as |
| 1604 | far as pLastChar, it must have found a newline |
| 1605 | or hit the end of the file; if pLastChar is \n, |
| 1606 | it obviously found a newline; else we haven't |
| 1607 | yet seen a newline, so must continue */ |
| 1608 | } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 1609 | } |
| 1610 | fclose(fp); |
| 1611 | if (i == lineno) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1612 | PyObject *res; |
Martin Panter | ca3263c | 2016-12-11 00:18:36 +0000 | [diff] [blame] | 1613 | res = PyUnicode_FromString(linebuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | if (res == NULL) |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1615 | _PyErr_Clear(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1616 | return res; |
| 1617 | } |
| 1618 | return NULL; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1619 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1620 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1621 | PyObject * |
| 1622 | PyErr_ProgramText(const char *filename, int lineno) |
| 1623 | { |
| 1624 | FILE *fp; |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1625 | if (filename == NULL || *filename == '\0' || lineno <= 0) { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1626 | return NULL; |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1627 | } |
| 1628 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1629 | fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE); |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1630 | return err_programtext(tstate, fp, lineno); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | PyObject * |
| 1634 | PyErr_ProgramTextObject(PyObject *filename, int lineno) |
| 1635 | { |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1636 | if (filename == NULL || lineno <= 0) { |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1637 | return NULL; |
| 1638 | } |
Victor Stinner | b4bdecd | 2019-05-24 13:44:24 +0200 | [diff] [blame] | 1639 | |
| 1640 | PyThreadState *tstate = _PyThreadState_GET(); |
| 1641 | FILE *fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE); |
| 1642 | if (fp == NULL) { |
| 1643 | _PyErr_Clear(tstate); |
| 1644 | return NULL; |
| 1645 | } |
| 1646 | return err_programtext(tstate, fp, lineno); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1647 | } |
| 1648 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1649 | #ifdef __cplusplus |
| 1650 | } |
| 1651 | #endif |