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