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