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