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