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