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