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 | { |
Guido van Rossum | 885553e | 1998-12-21 18:33:30 +0000 | [diff] [blame] | 27 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 28 | PyObject *oldtype, *oldvalue, *oldtraceback; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 30 | if (traceback != NULL && !PyTraceBack_Check(traceback)) { |
| 31 | /* XXX Should never happen -- fatal error instead? */ |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 32 | /* Well, it could be None. */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 33 | Py_DECREF(traceback); |
| 34 | traceback = NULL; |
| 35 | } |
| 36 | |
| 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; |
| 42 | |
| 43 | tstate->curexc_type = type; |
| 44 | tstate->curexc_value = value; |
| 45 | tstate->curexc_traceback = traceback; |
| 46 | |
| 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 | { |
Guido van Rossum | b4fb6e4 | 2008-06-14 20:20:24 +0000 | [diff] [blame] | 55 | PyThreadState *tstate = PyThreadState_GET(); |
| 56 | PyObject *tb = NULL; |
| 57 | |
Thomas Wouters | 303de6a | 2006-04-20 22:42:37 +0000 | [diff] [blame] | 58 | if (exception != NULL && |
| 59 | !PyExceptionClass_Check(exception)) { |
Thomas Wouters | 303de6a | 2006-04-20 22:42:37 +0000 | [diff] [blame] | 60 | PyErr_Format(PyExc_SystemError, |
Walter Dörwald | 573c08c | 2007-05-25 15:46:59 +0000 | [diff] [blame] | 61 | "exception %R not a BaseException subclass", |
| 62 | exception); |
Thomas Wouters | 303de6a | 2006-04-20 22:42:37 +0000 | [diff] [blame] | 63 | return; |
| 64 | } |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 65 | Py_XINCREF(value); |
Guido van Rossum | b4fb6e4 | 2008-06-14 20:20:24 +0000 | [diff] [blame] | 66 | if (tstate->exc_value != NULL && tstate->exc_value != Py_None) { |
| 67 | /* Implicit exception chaining */ |
| 68 | if (value == NULL || !PyExceptionInstance_Check(value)) { |
| 69 | /* We must normalize the value right now */ |
| 70 | PyObject *args, *fixed_value; |
| 71 | if (value == NULL || value == Py_None) |
| 72 | args = PyTuple_New(0); |
| 73 | else if (PyTuple_Check(value)) { |
| 74 | Py_INCREF(value); |
| 75 | args = value; |
| 76 | } |
| 77 | else |
| 78 | args = PyTuple_Pack(1, value); |
| 79 | fixed_value = args ? |
| 80 | PyEval_CallObject(exception, args) : NULL; |
| 81 | Py_XDECREF(args); |
| 82 | Py_XDECREF(value); |
| 83 | if (fixed_value == NULL) |
| 84 | return; |
| 85 | value = fixed_value; |
| 86 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 87 | /* Avoid reference cycles through the context chain. |
| 88 | This is O(chain length) but context chains are |
| 89 | usually very short. Sensitive readers may try |
| 90 | to inline the call to PyException_GetContext. */ |
| 91 | if (tstate->exc_value != value) { |
| 92 | PyObject *o = tstate->exc_value, *context; |
| 93 | while ((context = PyException_GetContext(o))) { |
| 94 | Py_DECREF(context); |
| 95 | if (context == value) { |
| 96 | PyException_SetContext(o, NULL); |
| 97 | break; |
| 98 | } |
| 99 | o = context; |
| 100 | } |
| 101 | Py_INCREF(tstate->exc_value); |
| 102 | PyException_SetContext(value, tstate->exc_value); |
| 103 | } |
Guido van Rossum | b4fb6e4 | 2008-06-14 20:20:24 +0000 | [diff] [blame] | 104 | } |
| 105 | if (value != NULL && PyExceptionInstance_Check(value)) |
| 106 | tb = PyException_GetTraceback(value); |
| 107 | Py_XINCREF(exception); |
| 108 | PyErr_Restore(exception, value, tb); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 112 | PyErr_SetNone(PyObject *exception) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 114 | PyErr_SetObject(exception, (PyObject *)NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 118 | PyErr_SetString(PyObject *exception, const char *string) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 119 | { |
Walter Dörwald | 573c08c | 2007-05-25 15:46:59 +0000 | [diff] [blame] | 120 | PyObject *value = PyUnicode_FromString(string); |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 121 | PyErr_SetObject(exception, value); |
| 122 | Py_XDECREF(value); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 126 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 127 | PyErr_Occurred(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 128 | { |
Tim Peters | 024da35 | 2001-05-30 06:09:50 +0000 | [diff] [blame] | 129 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 130 | |
| 131 | return tstate->curexc_type; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 134 | |
| 135 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 136 | PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 137 | { |
Barry Warsaw | fa5c315 | 2000-05-02 19:27:51 +0000 | [diff] [blame] | 138 | if (err == NULL || exc == NULL) { |
| 139 | /* maybe caused by "import exceptions" that failed early on */ |
| 140 | return 0; |
| 141 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 142 | if (PyTuple_Check(exc)) { |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 143 | Py_ssize_t i, n; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 144 | n = PyTuple_Size(exc); |
| 145 | for (i = 0; i < n; i++) { |
| 146 | /* Test recursively */ |
| 147 | if (PyErr_GivenExceptionMatches( |
| 148 | err, PyTuple_GET_ITEM(exc, i))) |
| 149 | { |
| 150 | return 1; |
| 151 | } |
| 152 | } |
| 153 | return 0; |
| 154 | } |
| 155 | /* err might be an instance, so check its class. */ |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 156 | if (PyExceptionInstance_Check(err)) |
| 157 | err = PyExceptionInstance_Class(err); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 158 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 159 | if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) { |
Benjamin Peterson | 69c88f7 | 2008-07-31 01:47:08 +0000 | [diff] [blame] | 160 | int res = 0; |
| 161 | PyObject *exception, *value, *tb; |
| 162 | PyErr_Fetch(&exception, &value, &tb); |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame^] | 163 | /* PyObject_IsSubclass() can recurse and therefore is |
| 164 | not safe (see test_bad_getattr in test.pickletester). */ |
| 165 | res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc); |
Benjamin Peterson | 69c88f7 | 2008-07-31 01:47:08 +0000 | [diff] [blame] | 166 | /* This function must not fail, so print the error here */ |
| 167 | if (res == -1) { |
| 168 | PyErr_WriteUnraisable(err); |
Benjamin Peterson | 69c88f7 | 2008-07-31 01:47:08 +0000 | [diff] [blame] | 169 | res = 0; |
| 170 | } |
| 171 | PyErr_Restore(exception, value, tb); |
| 172 | return res; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 173 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 174 | |
| 175 | return err == exc; |
| 176 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 177 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 178 | |
| 179 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 180 | PyErr_ExceptionMatches(PyObject *exc) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 181 | { |
| 182 | return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /* Used in many places to normalize a raised exception, including in |
| 187 | eval_code2(), do_raise(), and PyErr_Print() |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 188 | |
| 189 | XXX: should PyErr_NormalizeException() also call |
| 190 | PyException_SetTraceback() with the resulting value and tb? |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 191 | */ |
| 192 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 193 | PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 194 | { |
| 195 | PyObject *type = *exc; |
| 196 | PyObject *value = *val; |
| 197 | PyObject *inclass = NULL; |
Jeremy Hylton | e2e2c9f | 2001-09-26 19:58:38 +0000 | [diff] [blame] | 198 | PyObject *initial_tb = NULL; |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 199 | PyThreadState *tstate = NULL; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 200 | |
Guido van Rossum | ed473a4 | 2000-08-07 19:18:27 +0000 | [diff] [blame] | 201 | if (type == NULL) { |
Guido van Rossum | 6b3fffa | 2003-04-10 20:29:48 +0000 | [diff] [blame] | 202 | /* There was no exception, so nothing to do. */ |
| 203 | return; |
Guido van Rossum | ed473a4 | 2000-08-07 19:18:27 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 206 | /* If PyErr_SetNone() was used, the value will have been actually |
| 207 | set to NULL. |
| 208 | */ |
| 209 | if (!value) { |
| 210 | value = Py_None; |
| 211 | Py_INCREF(value); |
| 212 | } |
| 213 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 214 | if (PyExceptionInstance_Check(value)) |
| 215 | inclass = PyExceptionInstance_Class(value); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 216 | |
| 217 | /* Normalize the exception so that if the type is a class, the |
| 218 | value will be an instance. |
| 219 | */ |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 220 | if (PyExceptionClass_Check(type)) { |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 221 | /* if the value was not an instance, or is not an instance |
| 222 | whose class is (or is derived from) type, then use the |
| 223 | value as an argument to instantiation of the type |
| 224 | class. |
| 225 | */ |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 226 | if (!inclass || !PyObject_IsSubclass(inclass, type)) { |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 227 | PyObject *args, *res; |
| 228 | |
| 229 | if (value == Py_None) |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 230 | args = PyTuple_New(0); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 231 | else if (PyTuple_Check(value)) { |
| 232 | Py_INCREF(value); |
| 233 | args = value; |
| 234 | } |
| 235 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 236 | args = PyTuple_Pack(1, value); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 237 | |
| 238 | if (args == NULL) |
| 239 | goto finally; |
| 240 | res = PyEval_CallObject(type, args); |
| 241 | Py_DECREF(args); |
| 242 | if (res == NULL) |
| 243 | goto finally; |
| 244 | Py_DECREF(value); |
| 245 | value = res; |
| 246 | } |
Barry Warsaw | 3a74993 | 1997-09-30 15:00:18 +0000 | [diff] [blame] | 247 | /* if the class of the instance doesn't exactly match the |
| 248 | class of the type, believe the instance |
| 249 | */ |
| 250 | else if (inclass != type) { |
| 251 | Py_DECREF(type); |
| 252 | type = inclass; |
| 253 | Py_INCREF(type); |
| 254 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 255 | } |
| 256 | *exc = type; |
| 257 | *val = value; |
| 258 | return; |
| 259 | finally: |
Guido van Rossum | 19b55f2 | 1997-12-09 14:11:39 +0000 | [diff] [blame] | 260 | Py_DECREF(type); |
| 261 | Py_DECREF(value); |
Jeremy Hylton | e2e2c9f | 2001-09-26 19:58:38 +0000 | [diff] [blame] | 262 | /* If the new exception doesn't set a traceback and the old |
| 263 | exception had a traceback, use the old traceback for the |
| 264 | new exception. It's better than nothing. |
| 265 | */ |
| 266 | initial_tb = *tb; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 267 | PyErr_Fetch(exc, val, tb); |
Jeremy Hylton | e2e2c9f | 2001-09-26 19:58:38 +0000 | [diff] [blame] | 268 | if (initial_tb != NULL) { |
| 269 | if (*tb == NULL) |
| 270 | *tb = initial_tb; |
| 271 | else |
| 272 | Py_DECREF(initial_tb); |
| 273 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 274 | /* normalize recursively */ |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 275 | tstate = PyThreadState_GET(); |
| 276 | if (++tstate->recursion_depth > Py_GetRecursionLimit()) { |
| 277 | --tstate->recursion_depth; |
| 278 | PyErr_SetObject(PyExc_RuntimeError, PyExc_RecursionErrorInst); |
| 279 | return; |
| 280 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 281 | PyErr_NormalizeException(exc, val, tb); |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 282 | --tstate->recursion_depth; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 286 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 287 | PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 288 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 289 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 290 | |
| 291 | *p_type = tstate->curexc_type; |
| 292 | *p_value = tstate->curexc_value; |
| 293 | *p_traceback = tstate->curexc_traceback; |
| 294 | |
| 295 | tstate->curexc_type = NULL; |
| 296 | tstate->curexc_value = NULL; |
| 297 | tstate->curexc_traceback = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 301 | PyErr_Clear(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 302 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 303 | PyErr_Restore(NULL, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 304 | } |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 305 | |
| 306 | /* Convenience functions to set a type error exception and return 0 */ |
| 307 | |
| 308 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 309 | PyErr_BadArgument(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 310 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 311 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 312 | "bad argument type for built-in operation"); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 316 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 317 | PyErr_NoMemory(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 318 | { |
Vladimir Marangozov | 0888ff1 | 2000-08-18 18:01:06 +0000 | [diff] [blame] | 319 | if (PyErr_ExceptionMatches(PyExc_MemoryError)) |
| 320 | /* already current */ |
| 321 | return NULL; |
| 322 | |
Barry Warsaw | 2d8adff | 1997-08-29 21:54:35 +0000 | [diff] [blame] | 323 | /* raise the pre-allocated instance if it still exists */ |
| 324 | if (PyExc_MemoryErrorInst) |
Amaury Forgeot d'Arc | e19cadb | 2008-07-31 22:56:02 +0000 | [diff] [blame] | 325 | { |
| 326 | /* Clear the previous traceback, otherwise it will be appended |
| 327 | * to the current one. |
| 328 | * |
| 329 | * The following statement is not likely to raise any error; |
| 330 | * if it does, we simply discard it. |
| 331 | */ |
| 332 | PyException_SetTraceback(PyExc_MemoryErrorInst, Py_None); |
| 333 | |
Barry Warsaw | 2d8adff | 1997-08-29 21:54:35 +0000 | [diff] [blame] | 334 | PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst); |
Amaury Forgeot d'Arc | e19cadb | 2008-07-31 22:56:02 +0000 | [diff] [blame] | 335 | } |
Barry Warsaw | 2d8adff | 1997-08-29 21:54:35 +0000 | [diff] [blame] | 336 | else |
| 337 | /* this will probably fail since there's no memory and hee, |
| 338 | hee, we have to instantiate this class |
| 339 | */ |
| 340 | PyErr_SetNone(PyExc_MemoryError); |
| 341 | |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 342 | return NULL; |
| 343 | } |
| 344 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 345 | PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 346 | PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 347 | { |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 348 | PyObject *message; |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 349 | PyObject *v; |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 350 | int i = errno; |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 351 | #ifdef PLAN9 |
| 352 | char errbuf[ERRMAX]; |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 353 | #else |
| 354 | #ifndef MS_WINDOWS |
| 355 | char *s; |
| 356 | #else |
| 357 | WCHAR *s_buf = NULL; |
| 358 | #endif /* Unix/Windows */ |
| 359 | #endif /* PLAN 9*/ |
| 360 | |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 361 | #ifdef EINTR |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 362 | if (i == EINTR && PyErr_CheckSignals()) |
Guido van Rossum | 5063bab | 1991-10-20 20:14:56 +0000 | [diff] [blame] | 363 | return NULL; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 364 | #endif |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 365 | |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 366 | #ifdef PLAN9 |
| 367 | rerrstr(errbuf, sizeof errbuf); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 368 | message = PyUnicode_DecodeUTF8(errbuf, strlen(errbuf), "ignore"); |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 369 | #else |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 370 | #ifndef MS_WINDOWS |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 371 | if (i == 0) |
| 372 | s = "Error"; /* Sometimes errno didn't get set */ |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 373 | else |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 374 | s = strerror(i); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 375 | message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore"); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 376 | #else |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 377 | if (i == 0) |
| 378 | message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */ |
| 379 | else |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 380 | { |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 381 | /* Note that the Win32 errors do not lineup with the |
| 382 | errno error. So if the error is in the MSVC error |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 383 | table, we use it, otherwise we assume it really _is_ |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 384 | a Win32 error code |
| 385 | */ |
Guido van Rossum | 584b16a | 2000-02-21 16:50:31 +0000 | [diff] [blame] | 386 | if (i > 0 && i < _sys_nerr) { |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 387 | message = PyUnicode_FromString(_sys_errlist[i]); |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 388 | } |
| 389 | else { |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 390 | int len = FormatMessageW( |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 391 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 392 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 393 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 394 | NULL, /* no message source */ |
| 395 | i, |
| 396 | MAKELANGID(LANG_NEUTRAL, |
| 397 | SUBLANG_DEFAULT), |
| 398 | /* Default language */ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 399 | (LPWSTR) &s_buf, |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 400 | 0, /* size not used */ |
| 401 | NULL); /* no args */ |
Mark Hammond | 3d61a06 | 2002-10-04 00:13:02 +0000 | [diff] [blame] | 402 | if (len==0) { |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 403 | /* Only ever seen this in out-of-mem |
Mark Hammond | 3d61a06 | 2002-10-04 00:13:02 +0000 | [diff] [blame] | 404 | situations */ |
Mark Hammond | 3d61a06 | 2002-10-04 00:13:02 +0000 | [diff] [blame] | 405 | s_buf = NULL; |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 406 | message = PyUnicode_FromFormat("Windows Error 0x%X", i); |
Mark Hammond | 3d61a06 | 2002-10-04 00:13:02 +0000 | [diff] [blame] | 407 | } else { |
Mark Hammond | 3d61a06 | 2002-10-04 00:13:02 +0000 | [diff] [blame] | 408 | /* remove trailing cr/lf and dots */ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 409 | while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) |
| 410 | s_buf[--len] = L'\0'; |
| 411 | message = PyUnicode_FromUnicode(s_buf, len); |
Mark Hammond | 3d61a06 | 2002-10-04 00:13:02 +0000 | [diff] [blame] | 412 | } |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 413 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 414 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 415 | #endif /* Unix/Windows */ |
| 416 | #endif /* PLAN 9*/ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 417 | |
| 418 | if (message == NULL) |
| 419 | { |
| 420 | #ifdef MS_WINDOWS |
| 421 | LocalFree(s_buf); |
| 422 | #endif |
| 423 | return NULL; |
| 424 | } |
| 425 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 426 | if (filenameObject != NULL) |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 427 | v = Py_BuildValue("(iOO)", i, message, filenameObject); |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 428 | else |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 429 | v = Py_BuildValue("(iO)", i, message); |
| 430 | Py_DECREF(message); |
| 431 | |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 432 | if (v != NULL) { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 433 | PyErr_SetObject(exc, v); |
| 434 | Py_DECREF(v); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 435 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 436 | #ifdef MS_WINDOWS |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 437 | LocalFree(s_buf); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 438 | #endif |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 439 | return NULL; |
| 440 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 441 | |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 442 | |
| 443 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 444 | PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 445 | { |
Neal Norwitz | cd79596 | 2007-08-24 19:54:13 +0000 | [diff] [blame] | 446 | PyObject *name = filename ? PyUnicode_FromString(filename) : NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 447 | PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); |
Mark Hammond | da7efaa | 2002-10-04 00:09:38 +0000 | [diff] [blame] | 448 | Py_XDECREF(name); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 449 | return result; |
| 450 | } |
| 451 | |
| 452 | #ifdef Py_WIN_WIDE_FILENAMES |
| 453 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 454 | PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 455 | { |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 456 | PyObject *name = filename ? |
| 457 | PyUnicode_FromUnicode(filename, wcslen(filename)) : |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 458 | NULL; |
| 459 | PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); |
| 460 | Py_XDECREF(name); |
| 461 | return result; |
| 462 | } |
| 463 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 464 | |
| 465 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 466 | PyErr_SetFromErrno(PyObject *exc) |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 467 | { |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 468 | return PyErr_SetFromErrnoWithFilenameObject(exc, NULL); |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 469 | } |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 470 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 471 | #ifdef MS_WINDOWS |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 472 | /* Windows specific error code handling */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 473 | PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 474 | PyObject *exc, |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 475 | int ierr, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 476 | PyObject *filenameObject) |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 477 | { |
| 478 | int len; |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 479 | WCHAR *s_buf = NULL; /* Free via LocalFree */ |
| 480 | PyObject *message; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 481 | PyObject *v; |
| 482 | DWORD err = (DWORD)ierr; |
| 483 | if (err==0) err = GetLastError(); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 484 | len = FormatMessageW( |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 485 | /* Error API error */ |
| 486 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 487 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 488 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 489 | NULL, /* no message source */ |
| 490 | err, |
| 491 | MAKELANGID(LANG_NEUTRAL, |
| 492 | SUBLANG_DEFAULT), /* Default language */ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 493 | (LPWSTR) &s_buf, |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 494 | 0, /* size not used */ |
| 495 | NULL); /* no args */ |
Mark Hammond | 3d61a06 | 2002-10-04 00:13:02 +0000 | [diff] [blame] | 496 | if (len==0) { |
| 497 | /* Only seen this in out of mem situations */ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 498 | message = PyUnicode_FromFormat("Windows Error 0x%X", err); |
Mark Hammond | 3d61a06 | 2002-10-04 00:13:02 +0000 | [diff] [blame] | 499 | s_buf = NULL; |
| 500 | } else { |
Mark Hammond | 3d61a06 | 2002-10-04 00:13:02 +0000 | [diff] [blame] | 501 | /* remove trailing cr/lf and dots */ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 502 | while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) |
| 503 | s_buf[--len] = L'\0'; |
| 504 | message = PyUnicode_FromUnicode(s_buf, len); |
Mark Hammond | 3d61a06 | 2002-10-04 00:13:02 +0000 | [diff] [blame] | 505 | } |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 506 | |
| 507 | if (message == NULL) |
| 508 | { |
| 509 | LocalFree(s_buf); |
| 510 | return NULL; |
| 511 | } |
| 512 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 513 | if (filenameObject != NULL) |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 514 | v = Py_BuildValue("(iOO)", err, message, filenameObject); |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 515 | else |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 516 | v = Py_BuildValue("(iO)", err, message); |
| 517 | Py_DECREF(message); |
| 518 | |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 519 | if (v != NULL) { |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 520 | PyErr_SetObject(exc, v); |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 521 | Py_DECREF(v); |
| 522 | } |
Mark Hammond | 3d61a06 | 2002-10-04 00:13:02 +0000 | [diff] [blame] | 523 | LocalFree(s_buf); |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 524 | return NULL; |
| 525 | } |
| 526 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 527 | PyObject *PyErr_SetExcFromWindowsErrWithFilename( |
| 528 | PyObject *exc, |
| 529 | int ierr, |
| 530 | const char *filename) |
| 531 | { |
Neal Norwitz | cd79596 | 2007-08-24 19:54:13 +0000 | [diff] [blame] | 532 | PyObject *name = filename ? PyUnicode_FromString(filename) : NULL; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 533 | PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, |
| 534 | ierr, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 535 | name); |
| 536 | Py_XDECREF(name); |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | #ifdef Py_WIN_WIDE_FILENAMES |
| 541 | PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename( |
| 542 | PyObject *exc, |
| 543 | int ierr, |
| 544 | const Py_UNICODE *filename) |
| 545 | { |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 546 | PyObject *name = filename ? |
| 547 | PyUnicode_FromUnicode(filename, wcslen(filename)) : |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 548 | NULL; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 549 | PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, |
| 550 | ierr, |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 551 | name); |
| 552 | Py_XDECREF(name); |
| 553 | return ret; |
| 554 | } |
| 555 | #endif /* Py_WIN_WIDE_FILENAMES */ |
| 556 | |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 557 | PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr) |
| 558 | { |
| 559 | return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL); |
| 560 | } |
| 561 | |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 562 | PyObject *PyErr_SetFromWindowsErr(int ierr) |
| 563 | { |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 564 | return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError, |
| 565 | ierr, NULL); |
| 566 | } |
| 567 | PyObject *PyErr_SetFromWindowsErrWithFilename( |
| 568 | int ierr, |
| 569 | const char *filename) |
| 570 | { |
Neal Norwitz | cd79596 | 2007-08-24 19:54:13 +0000 | [diff] [blame] | 571 | PyObject *name = filename ? PyUnicode_FromString(filename) : NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 572 | PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 573 | PyExc_WindowsError, |
| 574 | ierr, name); |
Mark Hammond | da7efaa | 2002-10-04 00:09:38 +0000 | [diff] [blame] | 575 | Py_XDECREF(name); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 576 | return result; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 577 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 578 | |
| 579 | #ifdef Py_WIN_WIDE_FILENAMES |
| 580 | PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( |
| 581 | int ierr, |
| 582 | const Py_UNICODE *filename) |
| 583 | { |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 584 | PyObject *name = filename ? |
| 585 | PyUnicode_FromUnicode(filename, wcslen(filename)) : |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 586 | NULL; |
| 587 | PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 588 | PyExc_WindowsError, |
| 589 | ierr, name); |
Mark Hammond | da7efaa | 2002-10-04 00:09:38 +0000 | [diff] [blame] | 590 | Py_XDECREF(name); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 591 | return result; |
| 592 | } |
| 593 | #endif /* Py_WIN_WIDE_FILENAMES */ |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 594 | #endif /* MS_WINDOWS */ |
| 595 | |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 596 | void |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 597 | _PyErr_BadInternalCall(const char *filename, int lineno) |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 598 | { |
| 599 | PyErr_Format(PyExc_SystemError, |
| 600 | "%s:%d: bad argument to internal function", |
| 601 | filename, lineno); |
| 602 | } |
| 603 | |
| 604 | /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can |
| 605 | export the entry point for existing object code: */ |
| 606 | #undef PyErr_BadInternalCall |
| 607 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 608 | PyErr_BadInternalCall(void) |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 609 | { |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 610 | PyErr_Format(PyExc_SystemError, |
| 611 | "bad argument to internal function"); |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 612 | } |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 613 | #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) |
| 614 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 615 | |
| 616 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 617 | PyObject * |
| 618 | PyErr_Format(PyObject *exception, const char *format, ...) |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 619 | { |
| 620 | va_list vargs; |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 621 | PyObject* string; |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 622 | |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 623 | #ifdef HAVE_STDARG_PROTOTYPES |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 624 | va_start(vargs, format); |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 625 | #else |
| 626 | va_start(vargs); |
| 627 | #endif |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 628 | |
Walter Dörwald | 573c08c | 2007-05-25 15:46:59 +0000 | [diff] [blame] | 629 | string = PyUnicode_FromFormatV(format, vargs); |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 630 | PyErr_SetObject(exception, string); |
| 631 | Py_XDECREF(string); |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 632 | va_end(vargs); |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 633 | return NULL; |
| 634 | } |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 635 | |
| 636 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 637 | |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 638 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 639 | PyErr_NewException(const char *name, PyObject *base, PyObject *dict) |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 640 | { |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 641 | const char *dot; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 642 | PyObject *modulename = NULL; |
| 643 | PyObject *classname = NULL; |
| 644 | PyObject *mydict = NULL; |
| 645 | PyObject *bases = NULL; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 646 | PyObject *result = NULL; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 647 | dot = strrchr(name, '.'); |
| 648 | if (dot == NULL) { |
| 649 | PyErr_SetString(PyExc_SystemError, |
| 650 | "PyErr_NewException: name must be module.class"); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 651 | return NULL; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 652 | } |
| 653 | if (base == NULL) |
| 654 | base = PyExc_Exception; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 655 | if (dict == NULL) { |
| 656 | dict = mydict = PyDict_New(); |
| 657 | if (dict == NULL) |
| 658 | goto failure; |
| 659 | } |
| 660 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
Neal Norwitz | cd79596 | 2007-08-24 19:54:13 +0000 | [diff] [blame] | 661 | modulename = PyUnicode_FromStringAndSize(name, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 662 | (Py_ssize_t)(dot-name)); |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 663 | if (modulename == NULL) |
| 664 | goto failure; |
| 665 | if (PyDict_SetItemString(dict, "__module__", modulename) != 0) |
| 666 | goto failure; |
| 667 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 668 | if (PyTuple_Check(base)) { |
| 669 | bases = base; |
| 670 | /* INCREF as we create a new ref in the else branch */ |
| 671 | Py_INCREF(bases); |
| 672 | } else { |
| 673 | bases = PyTuple_Pack(1, base); |
| 674 | if (bases == NULL) |
| 675 | goto failure; |
| 676 | } |
| 677 | /* Create a real new-style class. */ |
Guido van Rossum | e845c0f | 2007-11-02 23:07:07 +0000 | [diff] [blame] | 678 | result = PyObject_CallFunction((PyObject *)&PyType_Type, "UOO", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 679 | dot+1, bases, dict); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 680 | failure: |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 681 | Py_XDECREF(bases); |
| 682 | Py_XDECREF(mydict); |
| 683 | Py_XDECREF(classname); |
| 684 | Py_XDECREF(modulename); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 685 | return result; |
| 686 | } |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 687 | |
| 688 | /* Call when an exception has occurred but there is no way for Python |
| 689 | to handle it. Examples: exception in __del__ or during GC. */ |
| 690 | void |
| 691 | PyErr_WriteUnraisable(PyObject *obj) |
| 692 | { |
| 693 | PyObject *f, *t, *v, *tb; |
| 694 | PyErr_Fetch(&t, &v, &tb); |
| 695 | f = PySys_GetObject("stderr"); |
Christian Heimes | 2be0373 | 2007-11-15 02:26:46 +0000 | [diff] [blame] | 696 | if (f != NULL && f != Py_None) { |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 697 | PyFile_WriteString("Exception ", f); |
| 698 | if (t) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 699 | PyObject* moduleName; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 700 | char* className; |
| 701 | assert(PyExceptionClass_Check(t)); |
| 702 | className = PyExceptionClass_Name(t); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 703 | if (className != NULL) { |
| 704 | char *dot = strrchr(className, '.'); |
| 705 | if (dot != NULL) |
| 706 | className = dot+1; |
| 707 | } |
| 708 | |
| 709 | moduleName = PyObject_GetAttrString(t, "__module__"); |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 710 | if (moduleName == NULL) |
| 711 | PyFile_WriteString("<unknown>", f); |
| 712 | else { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 713 | char* modstr = _PyUnicode_AsString(moduleName); |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 714 | if (modstr && |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 715 | strcmp(modstr, "builtins") != 0) |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 716 | { |
| 717 | PyFile_WriteString(modstr, f); |
| 718 | PyFile_WriteString(".", f); |
| 719 | } |
| 720 | } |
| 721 | if (className == NULL) |
| 722 | PyFile_WriteString("<unknown>", f); |
| 723 | else |
| 724 | PyFile_WriteString(className, f); |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 725 | if (v && v != Py_None) { |
| 726 | PyFile_WriteString(": ", f); |
| 727 | PyFile_WriteObject(v, f, 0); |
| 728 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 729 | Py_XDECREF(moduleName); |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 730 | } |
| 731 | PyFile_WriteString(" in ", f); |
| 732 | PyFile_WriteObject(obj, f, 0); |
| 733 | PyFile_WriteString(" ignored\n", f); |
| 734 | PyErr_Clear(); /* Just in case */ |
| 735 | } |
| 736 | Py_XDECREF(t); |
| 737 | Py_XDECREF(v); |
| 738 | Py_XDECREF(tb); |
| 739 | } |
Guido van Rossum | cfd42b5 | 2000-12-15 21:58:52 +0000 | [diff] [blame] | 740 | |
Armin Rigo | 092381a | 2003-10-25 14:29:27 +0000 | [diff] [blame] | 741 | extern PyObject *PyModule_GetWarningsModule(void); |
Guido van Rossum | cfd42b5 | 2000-12-15 21:58:52 +0000 | [diff] [blame] | 742 | |
Guido van Rossum | 2fd4565 | 2001-02-28 21:46:24 +0000 | [diff] [blame] | 743 | |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 744 | /* Set file and line information for the current exception. |
| 745 | If the exception is not a SyntaxError, also sets additional attributes |
| 746 | to make printing of exceptions believe it is a syntax error. */ |
Guido van Rossum | 2fd4565 | 2001-02-28 21:46:24 +0000 | [diff] [blame] | 747 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 748 | void |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 749 | PyErr_SyntaxLocation(const char *filename, int lineno) |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 750 | { |
| 751 | PyObject *exc, *v, *tb, *tmp; |
| 752 | |
| 753 | /* add attributes for the line number and filename for the error */ |
| 754 | PyErr_Fetch(&exc, &v, &tb); |
| 755 | PyErr_NormalizeException(&exc, &v, &tb); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 756 | /* XXX check that it is, indeed, a syntax error. It might not |
| 757 | * be, though. */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 758 | tmp = PyLong_FromLong(lineno); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 759 | if (tmp == NULL) |
| 760 | PyErr_Clear(); |
| 761 | else { |
| 762 | if (PyObject_SetAttrString(v, "lineno", tmp)) |
| 763 | PyErr_Clear(); |
| 764 | Py_DECREF(tmp); |
| 765 | } |
| 766 | if (filename != NULL) { |
Neal Norwitz | cd79596 | 2007-08-24 19:54:13 +0000 | [diff] [blame] | 767 | tmp = PyUnicode_FromString(filename); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 768 | if (tmp == NULL) |
| 769 | PyErr_Clear(); |
| 770 | else { |
| 771 | if (PyObject_SetAttrString(v, "filename", tmp)) |
| 772 | PyErr_Clear(); |
| 773 | Py_DECREF(tmp); |
| 774 | } |
| 775 | |
| 776 | tmp = PyErr_ProgramText(filename, lineno); |
| 777 | if (tmp) { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 778 | if (PyObject_SetAttrString(v, "text", tmp)) |
| 779 | PyErr_Clear(); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 780 | Py_DECREF(tmp); |
| 781 | } |
| 782 | } |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 783 | if (PyObject_SetAttrString(v, "offset", Py_None)) { |
| 784 | PyErr_Clear(); |
| 785 | } |
| 786 | if (exc != PyExc_SyntaxError) { |
| 787 | if (!PyObject_HasAttrString(v, "msg")) { |
| 788 | tmp = PyObject_Str(v); |
| 789 | if (tmp) { |
| 790 | if (PyObject_SetAttrString(v, "msg", tmp)) |
| 791 | PyErr_Clear(); |
| 792 | Py_DECREF(tmp); |
| 793 | } else { |
| 794 | PyErr_Clear(); |
| 795 | } |
| 796 | } |
| 797 | if (!PyObject_HasAttrString(v, "print_file_and_line")) { |
| 798 | if (PyObject_SetAttrString(v, "print_file_and_line", |
| 799 | Py_None)) |
| 800 | PyErr_Clear(); |
| 801 | } |
| 802 | } |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 803 | PyErr_Restore(exc, v, tb); |
| 804 | } |
| 805 | |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 806 | /* Attempt to load the line of text that the exception refers to. If it |
| 807 | fails, it will return NULL but will not set an exception. |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 808 | |
| 809 | XXX The functionality of this function is quite similar to the |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 810 | functionality in tb_displayline() in traceback.c. */ |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 811 | |
| 812 | PyObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 813 | PyErr_ProgramText(const char *filename, int lineno) |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 814 | { |
| 815 | FILE *fp; |
| 816 | int i; |
| 817 | char linebuf[1000]; |
| 818 | |
Tim Peters | a7444f4 | 2006-02-27 23:29:46 +0000 | [diff] [blame] | 819 | if (filename == NULL || *filename == '\0' || lineno <= 0) |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 820 | return NULL; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 821 | fp = fopen(filename, "r" PY_STDIOTEXTMODE); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 822 | if (fp == NULL) |
| 823 | return NULL; |
| 824 | for (i = 0; i < lineno; i++) { |
| 825 | char *pLastChar = &linebuf[sizeof(linebuf) - 2]; |
| 826 | do { |
| 827 | *pLastChar = '\0'; |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 828 | if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, |
| 829 | fp, NULL) == NULL) |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 830 | break; |
| 831 | /* fgets read *something*; if it didn't get as |
| 832 | far as pLastChar, it must have found a newline |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 833 | or hit the end of the file; if pLastChar is \n, |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 834 | it obviously found a newline; else we haven't |
| 835 | yet seen a newline, so must continue */ |
| 836 | } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 837 | } |
| 838 | fclose(fp); |
| 839 | if (i == lineno) { |
| 840 | char *p = linebuf; |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 841 | PyObject *res; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 842 | while (*p == ' ' || *p == '\t' || *p == '\014') |
| 843 | p++; |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 844 | res = PyUnicode_FromString(p); |
| 845 | if (res == NULL) |
| 846 | PyErr_Clear(); |
| 847 | return res; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 848 | } |
| 849 | return NULL; |
| 850 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 851 | |
| 852 | #ifdef __cplusplus |
| 853 | } |
| 854 | #endif |