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)) { |
| 230 | /* if the value was not an instance, or is not an instance |
| 231 | whose class is (or is derived from) type, then use the |
| 232 | value as an argument to instantiation of the type |
| 233 | class. |
| 234 | */ |
| 235 | if (!inclass || !PyObject_IsSubclass(inclass, type)) { |
| 236 | PyObject *args, *res; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | if (value == Py_None) |
| 239 | args = PyTuple_New(0); |
| 240 | else if (PyTuple_Check(value)) { |
| 241 | Py_INCREF(value); |
| 242 | args = value; |
| 243 | } |
| 244 | else |
| 245 | args = PyTuple_Pack(1, value); |
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 (args == NULL) |
| 248 | goto finally; |
| 249 | res = PyEval_CallObject(type, args); |
| 250 | Py_DECREF(args); |
| 251 | if (res == NULL) |
| 252 | goto finally; |
| 253 | Py_DECREF(value); |
| 254 | value = res; |
| 255 | } |
| 256 | /* if the class of the instance doesn't exactly match the |
| 257 | class of the type, believe the instance |
| 258 | */ |
| 259 | else if (inclass != type) { |
| 260 | Py_DECREF(type); |
| 261 | type = inclass; |
| 262 | Py_INCREF(type); |
| 263 | } |
| 264 | } |
| 265 | *exc = type; |
| 266 | *val = value; |
| 267 | return; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 268 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | Py_DECREF(type); |
| 270 | Py_DECREF(value); |
| 271 | /* If the new exception doesn't set a traceback and the old |
| 272 | exception had a traceback, use the old traceback for the |
| 273 | new exception. It's better than nothing. |
| 274 | */ |
| 275 | initial_tb = *tb; |
| 276 | PyErr_Fetch(exc, val, tb); |
| 277 | if (initial_tb != NULL) { |
| 278 | if (*tb == NULL) |
| 279 | *tb = initial_tb; |
| 280 | else |
| 281 | Py_DECREF(initial_tb); |
| 282 | } |
| 283 | /* normalize recursively */ |
| 284 | tstate = PyThreadState_GET(); |
| 285 | if (++tstate->recursion_depth > Py_GetRecursionLimit()) { |
| 286 | --tstate->recursion_depth; |
| 287 | /* throw away the old exception... */ |
| 288 | Py_DECREF(*exc); |
| 289 | Py_DECREF(*val); |
| 290 | /* ... and use the recursion error instead */ |
| 291 | *exc = PyExc_RuntimeError; |
| 292 | *val = PyExc_RecursionErrorInst; |
| 293 | Py_INCREF(*exc); |
| 294 | Py_INCREF(*val); |
| 295 | /* just keeping the old traceback */ |
| 296 | return; |
| 297 | } |
| 298 | PyErr_NormalizeException(exc, val, tb); |
| 299 | --tstate->recursion_depth; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 303 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 304 | PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | *p_type = tstate->curexc_type; |
| 309 | *p_value = tstate->curexc_value; |
| 310 | *p_traceback = tstate->curexc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | tstate->curexc_type = NULL; |
| 313 | tstate->curexc_value = NULL; |
| 314 | tstate->curexc_traceback = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 318 | PyErr_Clear(void) |
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 | PyErr_Restore(NULL, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 321 | } |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 322 | |
| 323 | /* Convenience functions to set a type error exception and return 0 */ |
| 324 | |
| 325 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 326 | PyErr_BadArgument(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 327 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | PyErr_SetString(PyExc_TypeError, |
| 329 | "bad argument type for built-in operation"); |
| 330 | return 0; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 333 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 334 | PyErr_NoMemory(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 335 | { |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 336 | PyErr_SetNone(PyExc_MemoryError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | return NULL; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 340 | PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 341 | PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 342 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | PyObject *message; |
Antoine Pitrou | 5d6fbe8 | 2011-10-12 19:39:57 +0200 | [diff] [blame] | 344 | PyObject *v, *args; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | int i = errno; |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 346 | #ifndef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | char *s; |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 348 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | WCHAR *s_buf = NULL; |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 350 | #endif /* Unix/Windows */ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 351 | |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 352 | #ifdef EINTR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | if (i == EINTR && PyErr_CheckSignals()) |
| 354 | return NULL; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 355 | #endif |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 356 | |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 357 | #ifndef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | if (i == 0) |
| 359 | s = "Error"; /* Sometimes errno didn't get set */ |
| 360 | else |
| 361 | s = strerror(i); |
| 362 | message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore"); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 363 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | if (i == 0) |
| 365 | message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */ |
| 366 | else |
| 367 | { |
| 368 | /* Note that the Win32 errors do not lineup with the |
| 369 | errno error. So if the error is in the MSVC error |
| 370 | table, we use it, otherwise we assume it really _is_ |
| 371 | a Win32 error code |
| 372 | */ |
| 373 | if (i > 0 && i < _sys_nerr) { |
| 374 | message = PyUnicode_FromString(_sys_errlist[i]); |
| 375 | } |
| 376 | else { |
| 377 | int len = FormatMessageW( |
| 378 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 379 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 380 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 381 | NULL, /* no message source */ |
| 382 | i, |
| 383 | MAKELANGID(LANG_NEUTRAL, |
| 384 | SUBLANG_DEFAULT), |
| 385 | /* Default language */ |
| 386 | (LPWSTR) &s_buf, |
| 387 | 0, /* size not used */ |
| 388 | NULL); /* no args */ |
| 389 | if (len==0) { |
| 390 | /* Only ever seen this in out-of-mem |
| 391 | situations */ |
| 392 | s_buf = NULL; |
| 393 | message = PyUnicode_FromFormat("Windows Error 0x%X", i); |
| 394 | } else { |
| 395 | /* remove trailing cr/lf and dots */ |
| 396 | while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) |
| 397 | s_buf[--len] = L'\0'; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 398 | message = PyUnicode_FromWideChar(s_buf, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 402 | #endif /* Unix/Windows */ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | if (message == NULL) |
| 405 | { |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 406 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | LocalFree(s_buf); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 408 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 | return NULL; |
| 410 | } |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | if (filenameObject != NULL) |
Antoine Pitrou | 5d6fbe8 | 2011-10-12 19:39:57 +0200 | [diff] [blame] | 413 | args = Py_BuildValue("(iOO)", i, message, filenameObject); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | else |
Antoine Pitrou | 5d6fbe8 | 2011-10-12 19:39:57 +0200 | [diff] [blame] | 415 | args = Py_BuildValue("(iO)", i, message); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | Py_DECREF(message); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 417 | |
Antoine Pitrou | 5d6fbe8 | 2011-10-12 19:39:57 +0200 | [diff] [blame] | 418 | if (args != NULL) { |
| 419 | v = PyObject_Call(exc, args, NULL); |
| 420 | Py_DECREF(args); |
| 421 | if (v != NULL) { |
| 422 | PyErr_SetObject((PyObject *) Py_TYPE(v), v); |
| 423 | Py_DECREF(v); |
| 424 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 426 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | LocalFree(s_buf); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 428 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | return NULL; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 430 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 431 | |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 432 | |
| 433 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 434 | PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 435 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; |
| 437 | PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); |
| 438 | Py_XDECREF(name); |
| 439 | return result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 442 | #ifdef MS_WINDOWS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 443 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 444 | PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | PyObject *name = filename ? |
| 447 | PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 448 | NULL; |
| 449 | PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); |
| 450 | Py_XDECREF(name); |
| 451 | return result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 452 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 453 | #endif /* MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 454 | |
| 455 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 456 | PyErr_SetFromErrno(PyObject *exc) |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | return PyErr_SetFromErrnoWithFilenameObject(exc, NULL); |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 459 | } |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 460 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 461 | #ifdef MS_WINDOWS |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 462 | /* Windows specific error code handling */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 463 | PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | PyObject *exc, |
| 465 | int ierr, |
| 466 | PyObject *filenameObject) |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | int len; |
| 469 | WCHAR *s_buf = NULL; /* Free via LocalFree */ |
| 470 | PyObject *message; |
Victor Stinner | 9ea8e4c | 2011-10-17 20:18:58 +0200 | [diff] [blame] | 471 | PyObject *args, *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | DWORD err = (DWORD)ierr; |
| 473 | if (err==0) err = GetLastError(); |
| 474 | len = FormatMessageW( |
| 475 | /* Error API error */ |
| 476 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 477 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 478 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 479 | NULL, /* no message source */ |
| 480 | err, |
| 481 | MAKELANGID(LANG_NEUTRAL, |
| 482 | SUBLANG_DEFAULT), /* Default language */ |
| 483 | (LPWSTR) &s_buf, |
| 484 | 0, /* size not used */ |
| 485 | NULL); /* no args */ |
| 486 | if (len==0) { |
| 487 | /* Only seen this in out of mem situations */ |
| 488 | message = PyUnicode_FromFormat("Windows Error 0x%X", err); |
| 489 | s_buf = NULL; |
| 490 | } else { |
| 491 | /* remove trailing cr/lf and dots */ |
| 492 | while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) |
| 493 | s_buf[--len] = L'\0'; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 494 | message = PyUnicode_FromWideChar(s_buf, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | } |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | if (message == NULL) |
| 498 | { |
| 499 | LocalFree(s_buf); |
| 500 | return NULL; |
| 501 | } |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 502 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 503 | if (filenameObject == NULL) |
| 504 | filenameObject = Py_None; |
| 505 | /* This is the constructor signature for passing a Windows error code. |
| 506 | The POSIX translation will be figured out by the constructor. */ |
Victor Stinner | 9ea8e4c | 2011-10-17 20:18:58 +0200 | [diff] [blame] | 507 | args = Py_BuildValue("(iOOi)", 0, message, filenameObject, err); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | Py_DECREF(message); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 509 | |
Victor Stinner | 9ea8e4c | 2011-10-17 20:18:58 +0200 | [diff] [blame] | 510 | if (args != NULL) { |
| 511 | v = PyObject_Call(exc, args, NULL); |
| 512 | Py_DECREF(args); |
| 513 | if (v != NULL) { |
| 514 | PyErr_SetObject((PyObject *) Py_TYPE(v), v); |
| 515 | Py_DECREF(v); |
| 516 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | } |
| 518 | LocalFree(s_buf); |
| 519 | return NULL; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 522 | PyObject *PyErr_SetExcFromWindowsErrWithFilename( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | PyObject *exc, |
| 524 | int ierr, |
| 525 | const char *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 526 | { |
Victor Stinner | 92be939 | 2010-12-28 00:28:21 +0000 | [diff] [blame] | 527 | PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, |
| 529 | ierr, |
| 530 | name); |
| 531 | Py_XDECREF(name); |
| 532 | return ret; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 535 | PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | PyObject *exc, |
| 537 | int ierr, |
| 538 | const Py_UNICODE *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 539 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | PyObject *name = filename ? |
| 541 | PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 542 | NULL; |
| 543 | PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, |
| 544 | ierr, |
| 545 | name); |
| 546 | Py_XDECREF(name); |
| 547 | return ret; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 548 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 549 | |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 550 | PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr) |
| 551 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL); |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 555 | PyObject *PyErr_SetFromWindowsErr(int ierr) |
| 556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError, |
| 558 | ierr, NULL); |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 559 | } |
| 560 | PyObject *PyErr_SetFromWindowsErrWithFilename( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | int ierr, |
| 562 | const char *filename) |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 563 | { |
Victor Stinner | 92be939 | 2010-12-28 00:28:21 +0000 | [diff] [blame] | 564 | PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 566 | PyExc_WindowsError, |
| 567 | ierr, name); |
| 568 | Py_XDECREF(name); |
| 569 | return result; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 570 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 571 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 572 | PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | int ierr, |
| 574 | const Py_UNICODE *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 575 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | PyObject *name = filename ? |
| 577 | PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 578 | NULL; |
| 579 | PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 580 | PyExc_WindowsError, |
| 581 | ierr, name); |
| 582 | Py_XDECREF(name); |
| 583 | return result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 584 | } |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 585 | #endif /* MS_WINDOWS */ |
| 586 | |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 587 | void |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 588 | _PyErr_BadInternalCall(const char *filename, int lineno) |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 589 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | PyErr_Format(PyExc_SystemError, |
| 591 | "%s:%d: bad argument to internal function", |
| 592 | filename, lineno); |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can |
| 596 | export the entry point for existing object code: */ |
| 597 | #undef PyErr_BadInternalCall |
| 598 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 599 | PyErr_BadInternalCall(void) |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 600 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | PyErr_Format(PyExc_SystemError, |
| 602 | "bad argument to internal function"); |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 603 | } |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 604 | #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) |
| 605 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 606 | |
| 607 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 608 | PyObject * |
| 609 | PyErr_Format(PyObject *exception, const char *format, ...) |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | va_list vargs; |
| 612 | PyObject* string; |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 613 | |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 614 | #ifdef HAVE_STDARG_PROTOTYPES |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | va_start(vargs, format); |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 616 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | va_start(vargs); |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 618 | #endif |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | string = PyUnicode_FromFormatV(format, vargs); |
| 621 | PyErr_SetObject(exception, string); |
| 622 | Py_XDECREF(string); |
| 623 | va_end(vargs); |
| 624 | return NULL; |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 625 | } |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 626 | |
| 627 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 628 | |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 629 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 630 | PyErr_NewException(const char *name, PyObject *base, PyObject *dict) |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | const char *dot; |
| 633 | PyObject *modulename = NULL; |
| 634 | PyObject *classname = NULL; |
| 635 | PyObject *mydict = NULL; |
| 636 | PyObject *bases = NULL; |
| 637 | PyObject *result = NULL; |
| 638 | dot = strrchr(name, '.'); |
| 639 | if (dot == NULL) { |
| 640 | PyErr_SetString(PyExc_SystemError, |
| 641 | "PyErr_NewException: name must be module.class"); |
| 642 | return NULL; |
| 643 | } |
| 644 | if (base == NULL) |
| 645 | base = PyExc_Exception; |
| 646 | if (dict == NULL) { |
| 647 | dict = mydict = PyDict_New(); |
| 648 | if (dict == NULL) |
| 649 | goto failure; |
| 650 | } |
| 651 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 652 | modulename = PyUnicode_FromStringAndSize(name, |
| 653 | (Py_ssize_t)(dot-name)); |
| 654 | if (modulename == NULL) |
| 655 | goto failure; |
| 656 | if (PyDict_SetItemString(dict, "__module__", modulename) != 0) |
| 657 | goto failure; |
| 658 | } |
| 659 | if (PyTuple_Check(base)) { |
| 660 | bases = base; |
| 661 | /* INCREF as we create a new ref in the else branch */ |
| 662 | Py_INCREF(bases); |
| 663 | } else { |
| 664 | bases = PyTuple_Pack(1, base); |
| 665 | if (bases == NULL) |
| 666 | goto failure; |
| 667 | } |
| 668 | /* Create a real new-style class. */ |
Victor Stinner | 7eeb5b5 | 2010-06-07 19:57:46 +0000 | [diff] [blame] | 669 | result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | dot+1, bases, dict); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 671 | failure: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | Py_XDECREF(bases); |
| 673 | Py_XDECREF(mydict); |
| 674 | Py_XDECREF(classname); |
| 675 | Py_XDECREF(modulename); |
| 676 | return result; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 677 | } |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 678 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 679 | |
| 680 | /* Create an exception with docstring */ |
| 681 | PyObject * |
| 682 | PyErr_NewExceptionWithDoc(const char *name, const char *doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | PyObject *base, PyObject *dict) |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 684 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | int result; |
| 686 | PyObject *ret = NULL; |
| 687 | PyObject *mydict = NULL; /* points to the dict only if we create it */ |
| 688 | PyObject *docobj; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | if (dict == NULL) { |
| 691 | dict = mydict = PyDict_New(); |
| 692 | if (dict == NULL) { |
| 693 | return NULL; |
| 694 | } |
| 695 | } |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | if (doc != NULL) { |
| 698 | docobj = PyUnicode_FromString(doc); |
| 699 | if (docobj == NULL) |
| 700 | goto failure; |
| 701 | result = PyDict_SetItemString(dict, "__doc__", docobj); |
| 702 | Py_DECREF(docobj); |
| 703 | if (result < 0) |
| 704 | goto failure; |
| 705 | } |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | ret = PyErr_NewException(name, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 708 | failure: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | Py_XDECREF(mydict); |
| 710 | return ret; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 714 | /* Call when an exception has occurred but there is no way for Python |
| 715 | to handle it. Examples: exception in __del__ or during GC. */ |
| 716 | void |
| 717 | PyErr_WriteUnraisable(PyObject *obj) |
| 718 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 719 | _Py_IDENTIFIER(__module__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | PyObject *f, *t, *v, *tb; |
| 721 | PyErr_Fetch(&t, &v, &tb); |
| 722 | f = PySys_GetObject("stderr"); |
| 723 | if (f != NULL && f != Py_None) { |
| 724 | PyFile_WriteString("Exception ", f); |
| 725 | if (t) { |
| 726 | PyObject* moduleName; |
| 727 | char* className; |
| 728 | assert(PyExceptionClass_Check(t)); |
| 729 | className = PyExceptionClass_Name(t); |
| 730 | if (className != NULL) { |
| 731 | char *dot = strrchr(className, '.'); |
| 732 | if (dot != NULL) |
| 733 | className = dot+1; |
| 734 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 735 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 736 | moduleName = _PyObject_GetAttrId(t, &PyId___module__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 737 | if (moduleName == NULL) |
| 738 | PyFile_WriteString("<unknown>", f); |
| 739 | else { |
| 740 | char* modstr = _PyUnicode_AsString(moduleName); |
| 741 | if (modstr && |
| 742 | strcmp(modstr, "builtins") != 0) |
| 743 | { |
| 744 | PyFile_WriteString(modstr, f); |
| 745 | PyFile_WriteString(".", f); |
| 746 | } |
| 747 | } |
| 748 | if (className == NULL) |
| 749 | PyFile_WriteString("<unknown>", f); |
| 750 | else |
| 751 | PyFile_WriteString(className, f); |
| 752 | if (v && v != Py_None) { |
| 753 | PyFile_WriteString(": ", f); |
| 754 | PyFile_WriteObject(v, f, 0); |
| 755 | } |
| 756 | Py_XDECREF(moduleName); |
| 757 | } |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 758 | if (obj) { |
| 759 | PyFile_WriteString(" in ", f); |
| 760 | PyFile_WriteObject(obj, f, 0); |
| 761 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | PyFile_WriteString(" ignored\n", f); |
| 763 | PyErr_Clear(); /* Just in case */ |
| 764 | } |
| 765 | Py_XDECREF(t); |
| 766 | Py_XDECREF(v); |
| 767 | Py_XDECREF(tb); |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 768 | } |
Guido van Rossum | cfd42b5 | 2000-12-15 21:58:52 +0000 | [diff] [blame] | 769 | |
Armin Rigo | 092381a | 2003-10-25 14:29:27 +0000 | [diff] [blame] | 770 | extern PyObject *PyModule_GetWarningsModule(void); |
Guido van Rossum | cfd42b5 | 2000-12-15 21:58:52 +0000 | [diff] [blame] | 771 | |
Guido van Rossum | 2fd4565 | 2001-02-28 21:46:24 +0000 | [diff] [blame] | 772 | |
Benjamin Peterson | 2c53971 | 2010-09-20 22:42:10 +0000 | [diff] [blame] | 773 | void |
| 774 | PyErr_SyntaxLocation(const char *filename, int lineno) { |
| 775 | PyErr_SyntaxLocationEx(filename, lineno, -1); |
| 776 | } |
| 777 | |
| 778 | |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 779 | /* Set file and line information for the current exception. |
| 780 | If the exception is not a SyntaxError, also sets additional attributes |
| 781 | to make printing of exceptions believe it is a syntax error. */ |
Guido van Rossum | 2fd4565 | 2001-02-28 21:46:24 +0000 | [diff] [blame] | 782 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 783 | void |
Benjamin Peterson | 2c53971 | 2010-09-20 22:42:10 +0000 | [diff] [blame] | 784 | PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 785 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | PyObject *exc, *v, *tb, *tmp; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 787 | _Py_IDENTIFIER(filename); |
| 788 | _Py_IDENTIFIER(lineno); |
| 789 | _Py_IDENTIFIER(msg); |
| 790 | _Py_IDENTIFIER(offset); |
| 791 | _Py_IDENTIFIER(print_file_and_line); |
| 792 | _Py_IDENTIFIER(text); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | /* add attributes for the line number and filename for the error */ |
| 795 | PyErr_Fetch(&exc, &v, &tb); |
| 796 | PyErr_NormalizeException(&exc, &v, &tb); |
| 797 | /* XXX check that it is, indeed, a syntax error. It might not |
| 798 | * be, though. */ |
| 799 | tmp = PyLong_FromLong(lineno); |
| 800 | if (tmp == NULL) |
| 801 | PyErr_Clear(); |
| 802 | else { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 803 | if (_PyObject_SetAttrId(v, &PyId_lineno, tmp)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | PyErr_Clear(); |
| 805 | Py_DECREF(tmp); |
| 806 | } |
Benjamin Peterson | 2c53971 | 2010-09-20 22:42:10 +0000 | [diff] [blame] | 807 | if (col_offset >= 0) { |
| 808 | tmp = PyLong_FromLong(col_offset); |
| 809 | if (tmp == NULL) |
| 810 | PyErr_Clear(); |
| 811 | else { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 812 | if (_PyObject_SetAttrId(v, &PyId_offset, tmp)) |
Benjamin Peterson | 2c53971 | 2010-09-20 22:42:10 +0000 | [diff] [blame] | 813 | PyErr_Clear(); |
| 814 | Py_DECREF(tmp); |
| 815 | } |
| 816 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | if (filename != NULL) { |
Victor Stinner | 15a71cd | 2010-10-17 19:03:16 +0000 | [diff] [blame] | 818 | tmp = PyUnicode_DecodeFSDefault(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | if (tmp == NULL) |
| 820 | PyErr_Clear(); |
| 821 | else { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 822 | if (_PyObject_SetAttrId(v, &PyId_filename, tmp)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | PyErr_Clear(); |
| 824 | Py_DECREF(tmp); |
| 825 | } |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | tmp = PyErr_ProgramText(filename, lineno); |
| 828 | if (tmp) { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 829 | if (_PyObject_SetAttrId(v, &PyId_text, tmp)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | PyErr_Clear(); |
| 831 | Py_DECREF(tmp); |
| 832 | } |
| 833 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 834 | if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | PyErr_Clear(); |
| 836 | } |
| 837 | if (exc != PyExc_SyntaxError) { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 838 | if (!_PyObject_HasAttrId(v, &PyId_msg)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | tmp = PyObject_Str(v); |
| 840 | if (tmp) { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 841 | if (_PyObject_SetAttrId(v, &PyId_msg, tmp)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | PyErr_Clear(); |
| 843 | Py_DECREF(tmp); |
| 844 | } else { |
| 845 | PyErr_Clear(); |
| 846 | } |
| 847 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 848 | if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) { |
| 849 | if (_PyObject_SetAttrId(v, &PyId_print_file_and_line, |
| 850 | Py_None)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | PyErr_Clear(); |
| 852 | } |
| 853 | } |
| 854 | PyErr_Restore(exc, v, tb); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 857 | /* Attempt to load the line of text that the exception refers to. If it |
| 858 | fails, it will return NULL but will not set an exception. |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 859 | |
| 860 | XXX The functionality of this function is quite similar to the |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 861 | functionality in tb_displayline() in traceback.c. */ |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 862 | |
| 863 | PyObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 864 | PyErr_ProgramText(const char *filename, int lineno) |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 865 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | FILE *fp; |
| 867 | int i; |
| 868 | char linebuf[1000]; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | if (filename == NULL || *filename == '\0' || lineno <= 0) |
| 871 | return NULL; |
| 872 | fp = fopen(filename, "r" PY_STDIOTEXTMODE); |
| 873 | if (fp == NULL) |
| 874 | return NULL; |
| 875 | for (i = 0; i < lineno; i++) { |
| 876 | char *pLastChar = &linebuf[sizeof(linebuf) - 2]; |
| 877 | do { |
| 878 | *pLastChar = '\0'; |
| 879 | if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, |
| 880 | fp, NULL) == NULL) |
| 881 | break; |
| 882 | /* fgets read *something*; if it didn't get as |
| 883 | far as pLastChar, it must have found a newline |
| 884 | or hit the end of the file; if pLastChar is \n, |
| 885 | it obviously found a newline; else we haven't |
| 886 | yet seen a newline, so must continue */ |
| 887 | } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 888 | } |
| 889 | fclose(fp); |
| 890 | if (i == lineno) { |
| 891 | char *p = linebuf; |
| 892 | PyObject *res; |
| 893 | while (*p == ' ' || *p == '\t' || *p == '\014') |
| 894 | p++; |
| 895 | res = PyUnicode_FromString(p); |
| 896 | if (res == NULL) |
| 897 | PyErr_Clear(); |
| 898 | return res; |
| 899 | } |
| 900 | return NULL; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 901 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 902 | |
| 903 | #ifdef __cplusplus |
| 904 | } |
| 905 | #endif |