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