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