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