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