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