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