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 | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +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 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 133 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 134 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 135 | return tstate->curexc_type; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 138 | |
| 139 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 140 | PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 141 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 142 | if (err == NULL || exc == NULL) { |
| 143 | /* maybe caused by "import exceptions" that failed early on */ |
| 144 | return 0; |
| 145 | } |
| 146 | if (PyTuple_Check(exc)) { |
| 147 | Py_ssize_t i, n; |
| 148 | n = PyTuple_Size(exc); |
| 149 | for (i = 0; i < n; i++) { |
| 150 | /* Test recursively */ |
| 151 | if (PyErr_GivenExceptionMatches( |
| 152 | err, PyTuple_GET_ITEM(exc, i))) |
| 153 | { |
| 154 | return 1; |
| 155 | } |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | /* err might be an instance, so check its class. */ |
| 160 | if (PyExceptionInstance_Check(err)) |
| 161 | err = PyExceptionInstance_Class(err); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 162 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 163 | if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) { |
| 164 | int res = 0; |
| 165 | PyObject *exception, *value, *tb; |
| 166 | PyErr_Fetch(&exception, &value, &tb); |
| 167 | /* PyObject_IsSubclass() can recurse and therefore is |
| 168 | not safe (see test_bad_getattr in test.pickletester). */ |
| 169 | res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc); |
| 170 | /* This function must not fail, so print the error here */ |
| 171 | if (res == -1) { |
| 172 | PyErr_WriteUnraisable(err); |
| 173 | res = 0; |
| 174 | } |
| 175 | PyErr_Restore(exception, value, tb); |
| 176 | return res; |
| 177 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 178 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 179 | return err == exc; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 180 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 181 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 182 | |
| 183 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 184 | PyErr_ExceptionMatches(PyObject *exc) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 185 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 186 | return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
| 190 | /* Used in many places to normalize a raised exception, including in |
| 191 | eval_code2(), do_raise(), and PyErr_Print() |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 192 | |
| 193 | XXX: should PyErr_NormalizeException() also call |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 194 | PyException_SetTraceback() with the resulting value and tb? |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 195 | */ |
| 196 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 197 | PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 198 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 199 | PyObject *type = *exc; |
| 200 | PyObject *value = *val; |
| 201 | PyObject *inclass = NULL; |
| 202 | PyObject *initial_tb = NULL; |
| 203 | PyThreadState *tstate = NULL; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 204 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 205 | if (type == NULL) { |
| 206 | /* There was no exception, so nothing to do. */ |
| 207 | return; |
| 208 | } |
Guido van Rossum | ed473a4 | 2000-08-07 19:18:27 +0000 | [diff] [blame] | 209 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 210 | /* If PyErr_SetNone() was used, the value will have been actually |
| 211 | set to NULL. |
| 212 | */ |
| 213 | if (!value) { |
| 214 | value = Py_None; |
| 215 | Py_INCREF(value); |
| 216 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 217 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 218 | if (PyExceptionInstance_Check(value)) |
| 219 | inclass = PyExceptionInstance_Class(value); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 220 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 221 | /* Normalize the exception so that if the type is a class, the |
| 222 | value will be an instance. |
| 223 | */ |
| 224 | if (PyExceptionClass_Check(type)) { |
| 225 | /* if the value was not an instance, or is not an instance |
| 226 | whose class is (or is derived from) type, then use the |
| 227 | value as an argument to instantiation of the type |
| 228 | class. |
| 229 | */ |
| 230 | if (!inclass || !PyObject_IsSubclass(inclass, type)) { |
| 231 | PyObject *args, *res; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 233 | if (value == Py_None) |
| 234 | args = PyTuple_New(0); |
| 235 | else if (PyTuple_Check(value)) { |
| 236 | Py_INCREF(value); |
| 237 | args = value; |
| 238 | } |
| 239 | else |
| 240 | args = PyTuple_Pack(1, value); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 241 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 242 | if (args == NULL) |
| 243 | goto finally; |
| 244 | res = PyEval_CallObject(type, args); |
| 245 | Py_DECREF(args); |
| 246 | if (res == NULL) |
| 247 | goto finally; |
| 248 | Py_DECREF(value); |
| 249 | value = res; |
| 250 | } |
| 251 | /* if the class of the instance doesn't exactly match the |
| 252 | class of the type, believe the instance |
| 253 | */ |
| 254 | else if (inclass != type) { |
| 255 | Py_DECREF(type); |
| 256 | type = inclass; |
| 257 | Py_INCREF(type); |
| 258 | } |
| 259 | } |
| 260 | *exc = type; |
| 261 | *val = value; |
| 262 | return; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 263 | finally: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 264 | Py_DECREF(type); |
| 265 | Py_DECREF(value); |
| 266 | /* If the new exception doesn't set a traceback and the old |
| 267 | exception had a traceback, use the old traceback for the |
| 268 | new exception. It's better than nothing. |
| 269 | */ |
| 270 | initial_tb = *tb; |
| 271 | PyErr_Fetch(exc, val, tb); |
| 272 | if (initial_tb != NULL) { |
| 273 | if (*tb == NULL) |
| 274 | *tb = initial_tb; |
| 275 | else |
| 276 | Py_DECREF(initial_tb); |
| 277 | } |
| 278 | /* normalize recursively */ |
| 279 | tstate = PyThreadState_GET(); |
| 280 | if (++tstate->recursion_depth > Py_GetRecursionLimit()) { |
| 281 | --tstate->recursion_depth; |
| 282 | /* throw away the old exception... */ |
| 283 | Py_DECREF(*exc); |
| 284 | Py_DECREF(*val); |
| 285 | /* ... and use the recursion error instead */ |
| 286 | *exc = PyExc_RuntimeError; |
| 287 | *val = PyExc_RecursionErrorInst; |
| 288 | Py_INCREF(*exc); |
| 289 | Py_INCREF(*val); |
| 290 | /* just keeping the old traceback */ |
| 291 | return; |
| 292 | } |
| 293 | PyErr_NormalizeException(exc, val, tb); |
| 294 | --tstate->recursion_depth; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 298 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 299 | PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 300 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 301 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 303 | *p_type = tstate->curexc_type; |
| 304 | *p_value = tstate->curexc_value; |
| 305 | *p_traceback = tstate->curexc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 306 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 307 | tstate->curexc_type = NULL; |
| 308 | tstate->curexc_value = NULL; |
| 309 | tstate->curexc_traceback = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 313 | PyErr_Clear(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 314 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 315 | PyErr_Restore(NULL, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 316 | } |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 317 | |
| 318 | /* Convenience functions to set a type error exception and return 0 */ |
| 319 | |
| 320 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 321 | PyErr_BadArgument(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 322 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 323 | PyErr_SetString(PyExc_TypeError, |
| 324 | "bad argument type for built-in operation"); |
| 325 | return 0; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 328 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 329 | PyErr_NoMemory(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 330 | { |
Antoine Pitrou | 98e2b45 | 2010-10-28 23:06:57 +0000 | [diff] [blame^] | 331 | PyErr_SetNone(PyExc_MemoryError); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 332 | return NULL; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 335 | PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 336 | PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 337 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 338 | PyObject *message; |
| 339 | PyObject *v; |
| 340 | int i = errno; |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 341 | #ifdef PLAN9 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 342 | char errbuf[ERRMAX]; |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 343 | #else |
| 344 | #ifndef MS_WINDOWS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 345 | char *s; |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 346 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 347 | WCHAR *s_buf = NULL; |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 348 | #endif /* Unix/Windows */ |
| 349 | #endif /* PLAN 9*/ |
| 350 | |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 351 | #ifdef EINTR |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 352 | if (i == EINTR && PyErr_CheckSignals()) |
| 353 | return NULL; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 354 | #endif |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 355 | |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 356 | #ifdef PLAN9 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 357 | rerrstr(errbuf, sizeof errbuf); |
| 358 | message = PyUnicode_DecodeUTF8(errbuf, strlen(errbuf), "ignore"); |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 359 | #else |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 360 | #ifndef MS_WINDOWS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 361 | if (i == 0) |
| 362 | s = "Error"; /* Sometimes errno didn't get set */ |
| 363 | else |
| 364 | s = strerror(i); |
| 365 | message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore"); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 366 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 367 | if (i == 0) |
| 368 | message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */ |
| 369 | else |
| 370 | { |
| 371 | /* Note that the Win32 errors do not lineup with the |
| 372 | errno error. So if the error is in the MSVC error |
| 373 | table, we use it, otherwise we assume it really _is_ |
| 374 | a Win32 error code |
| 375 | */ |
| 376 | if (i > 0 && i < _sys_nerr) { |
| 377 | message = PyUnicode_FromString(_sys_errlist[i]); |
| 378 | } |
| 379 | else { |
| 380 | int len = FormatMessageW( |
| 381 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 382 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 383 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 384 | NULL, /* no message source */ |
| 385 | i, |
| 386 | MAKELANGID(LANG_NEUTRAL, |
| 387 | SUBLANG_DEFAULT), |
| 388 | /* Default language */ |
| 389 | (LPWSTR) &s_buf, |
| 390 | 0, /* size not used */ |
| 391 | NULL); /* no args */ |
| 392 | if (len==0) { |
| 393 | /* Only ever seen this in out-of-mem |
| 394 | situations */ |
| 395 | s_buf = NULL; |
| 396 | message = PyUnicode_FromFormat("Windows Error 0x%X", i); |
| 397 | } else { |
| 398 | /* remove trailing cr/lf and dots */ |
| 399 | while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) |
| 400 | s_buf[--len] = L'\0'; |
| 401 | message = PyUnicode_FromUnicode(s_buf, len); |
| 402 | } |
| 403 | } |
| 404 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 405 | #endif /* Unix/Windows */ |
| 406 | #endif /* PLAN 9*/ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 407 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 408 | if (message == NULL) |
| 409 | { |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 410 | #ifdef MS_WINDOWS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 411 | LocalFree(s_buf); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 412 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 413 | return NULL; |
| 414 | } |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 415 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 416 | if (filenameObject != NULL) |
| 417 | v = Py_BuildValue("(iOO)", i, message, filenameObject); |
| 418 | else |
| 419 | v = Py_BuildValue("(iO)", i, message); |
| 420 | Py_DECREF(message); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 421 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 422 | if (v != NULL) { |
| 423 | PyErr_SetObject(exc, v); |
| 424 | Py_DECREF(v); |
| 425 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 426 | #ifdef MS_WINDOWS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 427 | LocalFree(s_buf); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 428 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 436 | PyObject *name = filename ? PyUnicode_FromString(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 | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 468 | int len; |
| 469 | WCHAR *s_buf = NULL; /* Free via LocalFree */ |
| 470 | PyObject *message; |
| 471 | PyObject *v; |
| 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'; |
| 494 | message = PyUnicode_FromUnicode(s_buf, len); |
| 495 | } |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 496 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 503 | if (filenameObject != NULL) |
| 504 | v = Py_BuildValue("(iOO)", err, message, filenameObject); |
| 505 | else |
| 506 | v = Py_BuildValue("(iO)", err, message); |
| 507 | Py_DECREF(message); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 508 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 509 | if (v != NULL) { |
| 510 | PyErr_SetObject(exc, v); |
| 511 | Py_DECREF(v); |
| 512 | } |
| 513 | LocalFree(s_buf); |
| 514 | return NULL; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 517 | PyObject *PyErr_SetExcFromWindowsErrWithFilename( |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 518 | PyObject *exc, |
| 519 | int ierr, |
| 520 | const char *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 521 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 522 | PyObject *name = filename ? PyUnicode_FromString(filename) : NULL; |
| 523 | PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, |
| 524 | ierr, |
| 525 | name); |
| 526 | Py_XDECREF(name); |
| 527 | return ret; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 530 | PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename( |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 531 | PyObject *exc, |
| 532 | int ierr, |
| 533 | const Py_UNICODE *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 534 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 535 | PyObject *name = filename ? |
| 536 | PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 537 | NULL; |
| 538 | PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, |
| 539 | ierr, |
| 540 | name); |
| 541 | Py_XDECREF(name); |
| 542 | return ret; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 543 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 544 | |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 545 | PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr) |
| 546 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 547 | return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL); |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 550 | PyObject *PyErr_SetFromWindowsErr(int ierr) |
| 551 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 552 | return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError, |
| 553 | ierr, NULL); |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 554 | } |
| 555 | PyObject *PyErr_SetFromWindowsErrWithFilename( |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 556 | int ierr, |
| 557 | const char *filename) |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 558 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 559 | PyObject *name = filename ? PyUnicode_FromString(filename) : NULL; |
| 560 | PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 561 | PyExc_WindowsError, |
| 562 | ierr, name); |
| 563 | Py_XDECREF(name); |
| 564 | return result; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 565 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 566 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 567 | PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 568 | int ierr, |
| 569 | const Py_UNICODE *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 570 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 571 | PyObject *name = filename ? |
| 572 | PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 573 | NULL; |
| 574 | PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 575 | PyExc_WindowsError, |
| 576 | ierr, name); |
| 577 | Py_XDECREF(name); |
| 578 | return result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 579 | } |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 580 | #endif /* MS_WINDOWS */ |
| 581 | |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 582 | void |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 583 | _PyErr_BadInternalCall(const char *filename, int lineno) |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 584 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 585 | PyErr_Format(PyExc_SystemError, |
| 586 | "%s:%d: bad argument to internal function", |
| 587 | filename, lineno); |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can |
| 591 | export the entry point for existing object code: */ |
| 592 | #undef PyErr_BadInternalCall |
| 593 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 594 | PyErr_BadInternalCall(void) |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 595 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 596 | PyErr_Format(PyExc_SystemError, |
| 597 | "bad argument to internal function"); |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 598 | } |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 599 | #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) |
| 600 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 601 | |
| 602 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 603 | PyObject * |
| 604 | PyErr_Format(PyObject *exception, const char *format, ...) |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 605 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 606 | va_list vargs; |
| 607 | PyObject* string; |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 608 | |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 609 | #ifdef HAVE_STDARG_PROTOTYPES |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 610 | va_start(vargs, format); |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 611 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 612 | va_start(vargs); |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 613 | #endif |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 614 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 615 | string = PyUnicode_FromFormatV(format, vargs); |
| 616 | PyErr_SetObject(exception, string); |
| 617 | Py_XDECREF(string); |
| 618 | va_end(vargs); |
| 619 | return NULL; |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 620 | } |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 621 | |
| 622 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 623 | |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 624 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 625 | PyErr_NewException(const char *name, PyObject *base, PyObject *dict) |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 626 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 627 | const char *dot; |
| 628 | PyObject *modulename = NULL; |
| 629 | PyObject *classname = NULL; |
| 630 | PyObject *mydict = NULL; |
| 631 | PyObject *bases = NULL; |
| 632 | PyObject *result = NULL; |
| 633 | dot = strrchr(name, '.'); |
| 634 | if (dot == NULL) { |
| 635 | PyErr_SetString(PyExc_SystemError, |
| 636 | "PyErr_NewException: name must be module.class"); |
| 637 | return NULL; |
| 638 | } |
| 639 | if (base == NULL) |
| 640 | base = PyExc_Exception; |
| 641 | if (dict == NULL) { |
| 642 | dict = mydict = PyDict_New(); |
| 643 | if (dict == NULL) |
| 644 | goto failure; |
| 645 | } |
| 646 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 647 | modulename = PyUnicode_FromStringAndSize(name, |
| 648 | (Py_ssize_t)(dot-name)); |
| 649 | if (modulename == NULL) |
| 650 | goto failure; |
| 651 | if (PyDict_SetItemString(dict, "__module__", modulename) != 0) |
| 652 | goto failure; |
| 653 | } |
| 654 | if (PyTuple_Check(base)) { |
| 655 | bases = base; |
| 656 | /* INCREF as we create a new ref in the else branch */ |
| 657 | Py_INCREF(bases); |
| 658 | } else { |
| 659 | bases = PyTuple_Pack(1, base); |
| 660 | if (bases == NULL) |
| 661 | goto failure; |
| 662 | } |
| 663 | /* Create a real new-style class. */ |
| 664 | result = PyObject_CallFunction((PyObject *)&PyType_Type, "UOO", |
| 665 | dot+1, bases, dict); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 666 | failure: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 667 | Py_XDECREF(bases); |
| 668 | Py_XDECREF(mydict); |
| 669 | Py_XDECREF(classname); |
| 670 | Py_XDECREF(modulename); |
| 671 | return result; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 672 | } |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 673 | |
| 674 | /* Call when an exception has occurred but there is no way for Python |
| 675 | to handle it. Examples: exception in __del__ or during GC. */ |
| 676 | void |
| 677 | PyErr_WriteUnraisable(PyObject *obj) |
| 678 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 679 | PyObject *f, *t, *v, *tb; |
| 680 | PyErr_Fetch(&t, &v, &tb); |
| 681 | f = PySys_GetObject("stderr"); |
| 682 | if (f != NULL && f != Py_None) { |
| 683 | PyFile_WriteString("Exception ", f); |
| 684 | if (t) { |
| 685 | PyObject* moduleName; |
| 686 | char* className; |
| 687 | assert(PyExceptionClass_Check(t)); |
| 688 | className = PyExceptionClass_Name(t); |
| 689 | if (className != NULL) { |
| 690 | char *dot = strrchr(className, '.'); |
| 691 | if (dot != NULL) |
| 692 | className = dot+1; |
| 693 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 694 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 695 | moduleName = PyObject_GetAttrString(t, "__module__"); |
| 696 | if (moduleName == NULL) |
| 697 | PyFile_WriteString("<unknown>", f); |
| 698 | else { |
| 699 | char* modstr = _PyUnicode_AsString(moduleName); |
| 700 | if (modstr && |
| 701 | strcmp(modstr, "builtins") != 0) |
| 702 | { |
| 703 | PyFile_WriteString(modstr, f); |
| 704 | PyFile_WriteString(".", f); |
| 705 | } |
| 706 | } |
| 707 | if (className == NULL) |
| 708 | PyFile_WriteString("<unknown>", f); |
| 709 | else |
| 710 | PyFile_WriteString(className, f); |
| 711 | if (v && v != Py_None) { |
| 712 | PyFile_WriteString(": ", f); |
| 713 | PyFile_WriteObject(v, f, 0); |
| 714 | } |
| 715 | Py_XDECREF(moduleName); |
| 716 | } |
| 717 | PyFile_WriteString(" in ", f); |
| 718 | PyFile_WriteObject(obj, f, 0); |
| 719 | PyFile_WriteString(" ignored\n", f); |
| 720 | PyErr_Clear(); /* Just in case */ |
| 721 | } |
| 722 | Py_XDECREF(t); |
| 723 | Py_XDECREF(v); |
| 724 | Py_XDECREF(tb); |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 725 | } |
Guido van Rossum | cfd42b5 | 2000-12-15 21:58:52 +0000 | [diff] [blame] | 726 | |
Armin Rigo | 092381a | 2003-10-25 14:29:27 +0000 | [diff] [blame] | 727 | extern PyObject *PyModule_GetWarningsModule(void); |
Guido van Rossum | cfd42b5 | 2000-12-15 21:58:52 +0000 | [diff] [blame] | 728 | |
Guido van Rossum | 2fd4565 | 2001-02-28 21:46:24 +0000 | [diff] [blame] | 729 | |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 730 | /* Set file and line information for the current exception. |
| 731 | If the exception is not a SyntaxError, also sets additional attributes |
| 732 | to make printing of exceptions believe it is a syntax error. */ |
Guido van Rossum | 2fd4565 | 2001-02-28 21:46:24 +0000 | [diff] [blame] | 733 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 734 | void |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 735 | PyErr_SyntaxLocation(const char *filename, int lineno) |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 736 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 737 | PyObject *exc, *v, *tb, *tmp; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 738 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 739 | /* add attributes for the line number and filename for the error */ |
| 740 | PyErr_Fetch(&exc, &v, &tb); |
| 741 | PyErr_NormalizeException(&exc, &v, &tb); |
| 742 | /* XXX check that it is, indeed, a syntax error. It might not |
| 743 | * be, though. */ |
| 744 | tmp = PyLong_FromLong(lineno); |
| 745 | if (tmp == NULL) |
| 746 | PyErr_Clear(); |
| 747 | else { |
| 748 | if (PyObject_SetAttrString(v, "lineno", tmp)) |
| 749 | PyErr_Clear(); |
| 750 | Py_DECREF(tmp); |
| 751 | } |
| 752 | if (filename != NULL) { |
| 753 | tmp = PyUnicode_FromString(filename); |
| 754 | if (tmp == NULL) |
| 755 | PyErr_Clear(); |
| 756 | else { |
| 757 | if (PyObject_SetAttrString(v, "filename", tmp)) |
| 758 | PyErr_Clear(); |
| 759 | Py_DECREF(tmp); |
| 760 | } |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 761 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 762 | tmp = PyErr_ProgramText(filename, lineno); |
| 763 | if (tmp) { |
| 764 | if (PyObject_SetAttrString(v, "text", tmp)) |
| 765 | PyErr_Clear(); |
| 766 | Py_DECREF(tmp); |
| 767 | } |
| 768 | } |
| 769 | if (PyObject_SetAttrString(v, "offset", Py_None)) { |
| 770 | PyErr_Clear(); |
| 771 | } |
| 772 | if (exc != PyExc_SyntaxError) { |
| 773 | if (!PyObject_HasAttrString(v, "msg")) { |
| 774 | tmp = PyObject_Str(v); |
| 775 | if (tmp) { |
| 776 | if (PyObject_SetAttrString(v, "msg", tmp)) |
| 777 | PyErr_Clear(); |
| 778 | Py_DECREF(tmp); |
| 779 | } else { |
| 780 | PyErr_Clear(); |
| 781 | } |
| 782 | } |
| 783 | if (!PyObject_HasAttrString(v, "print_file_and_line")) { |
| 784 | if (PyObject_SetAttrString(v, "print_file_and_line", |
| 785 | Py_None)) |
| 786 | PyErr_Clear(); |
| 787 | } |
| 788 | } |
| 789 | PyErr_Restore(exc, v, tb); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 792 | /* Attempt to load the line of text that the exception refers to. If it |
| 793 | fails, it will return NULL but will not set an exception. |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 794 | |
| 795 | XXX The functionality of this function is quite similar to the |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 796 | functionality in tb_displayline() in traceback.c. */ |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 797 | |
| 798 | PyObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 799 | PyErr_ProgramText(const char *filename, int lineno) |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 800 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 801 | FILE *fp; |
| 802 | int i; |
| 803 | char linebuf[1000]; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 804 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 805 | if (filename == NULL || *filename == '\0' || lineno <= 0) |
| 806 | return NULL; |
| 807 | fp = fopen(filename, "r" PY_STDIOTEXTMODE); |
| 808 | if (fp == NULL) |
| 809 | return NULL; |
| 810 | for (i = 0; i < lineno; i++) { |
| 811 | char *pLastChar = &linebuf[sizeof(linebuf) - 2]; |
| 812 | do { |
| 813 | *pLastChar = '\0'; |
| 814 | if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, |
| 815 | fp, NULL) == NULL) |
| 816 | break; |
| 817 | /* fgets read *something*; if it didn't get as |
| 818 | far as pLastChar, it must have found a newline |
| 819 | or hit the end of the file; if pLastChar is \n, |
| 820 | it obviously found a newline; else we haven't |
| 821 | yet seen a newline, so must continue */ |
| 822 | } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 823 | } |
| 824 | fclose(fp); |
| 825 | if (i == lineno) { |
| 826 | char *p = linebuf; |
| 827 | PyObject *res; |
| 828 | while (*p == ' ' || *p == '\t' || *p == '\014') |
| 829 | p++; |
| 830 | res = PyUnicode_FromString(p); |
| 831 | if (res == NULL) |
| 832 | PyErr_Clear(); |
| 833 | return res; |
| 834 | } |
| 835 | return NULL; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 836 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 837 | |
| 838 | #ifdef __cplusplus |
| 839 | } |
| 840 | #endif |