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