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 | |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 18 | #ifdef MS_WIN32 |
| 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; |
| 131 | |
Guido van Rossum | ed473a4 | 2000-08-07 19:18:27 +0000 | [diff] [blame] | 132 | if (type == NULL) { |
| 133 | /* This is a bug. Should never happen. Don't dump core. */ |
| 134 | PyErr_SetString(PyExc_SystemError, |
| 135 | "PyErr_NormalizeException() called without exception"); |
| 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); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 194 | Py_XDECREF(*tb); |
| 195 | PyErr_Fetch(exc, val, tb); |
| 196 | /* normalize recursively */ |
| 197 | PyErr_NormalizeException(exc, val, tb); |
| 198 | } |
| 199 | |
| 200 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 201 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 202 | PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 203 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 204 | PyThreadState *tstate = PyThreadState_Get(); |
| 205 | |
| 206 | *p_type = tstate->curexc_type; |
| 207 | *p_value = tstate->curexc_value; |
| 208 | *p_traceback = tstate->curexc_traceback; |
| 209 | |
| 210 | tstate->curexc_type = NULL; |
| 211 | tstate->curexc_value = NULL; |
| 212 | tstate->curexc_traceback = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 216 | PyErr_Clear(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 217 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 218 | PyErr_Restore(NULL, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 219 | } |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 220 | |
| 221 | /* Convenience functions to set a type error exception and return 0 */ |
| 222 | |
| 223 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 224 | PyErr_BadArgument(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 225 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 226 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 227 | "bad argument type for built-in operation"); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 231 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 232 | PyErr_NoMemory(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 233 | { |
Vladimir Marangozov | 0888ff1 | 2000-08-18 18:01:06 +0000 | [diff] [blame] | 234 | if (PyErr_ExceptionMatches(PyExc_MemoryError)) |
| 235 | /* already current */ |
| 236 | return NULL; |
| 237 | |
Barry Warsaw | 2d8adff | 1997-08-29 21:54:35 +0000 | [diff] [blame] | 238 | /* raise the pre-allocated instance if it still exists */ |
| 239 | if (PyExc_MemoryErrorInst) |
| 240 | PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst); |
| 241 | else |
| 242 | /* this will probably fail since there's no memory and hee, |
| 243 | hee, we have to instantiate this class |
| 244 | */ |
| 245 | PyErr_SetNone(PyExc_MemoryError); |
| 246 | |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 247 | return NULL; |
| 248 | } |
| 249 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 250 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 251 | PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 252 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 253 | PyObject *v; |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 254 | char *s; |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 255 | int i = errno; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 256 | #ifdef MS_WIN32 |
| 257 | char *s_buf = NULL; |
| 258 | #endif |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 259 | #ifdef EINTR |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 260 | if (i == EINTR && PyErr_CheckSignals()) |
Guido van Rossum | 5063bab | 1991-10-20 20:14:56 +0000 | [diff] [blame] | 261 | return NULL; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 262 | #endif |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 263 | if (i == 0) |
| 264 | s = "Error"; /* Sometimes errno didn't get set */ |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 265 | else |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 266 | #ifndef MS_WIN32 |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 267 | s = strerror(i); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 268 | #else |
| 269 | { |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 270 | /* Note that the Win32 errors do not lineup with the |
| 271 | errno error. So if the error is in the MSVC error |
| 272 | table, we use it, otherwise we assume it really _is_ |
| 273 | a Win32 error code |
| 274 | */ |
Guido van Rossum | 584b16a | 2000-02-21 16:50:31 +0000 | [diff] [blame] | 275 | if (i > 0 && i < _sys_nerr) { |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 276 | s = _sys_errlist[i]; |
| 277 | } |
| 278 | else { |
| 279 | int len = FormatMessage( |
| 280 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 281 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 282 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 283 | NULL, /* no message source */ |
| 284 | i, |
| 285 | MAKELANGID(LANG_NEUTRAL, |
| 286 | SUBLANG_DEFAULT), |
| 287 | /* Default language */ |
| 288 | (LPTSTR) &s_buf, |
| 289 | 0, /* size not used */ |
| 290 | NULL); /* no args */ |
| 291 | s = s_buf; |
| 292 | /* remove trailing cr/lf and dots */ |
| 293 | while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) |
| 294 | s[--len] = '\0'; |
| 295 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 296 | } |
| 297 | #endif |
Barry Warsaw | fa5c315 | 2000-05-02 19:27:51 +0000 | [diff] [blame] | 298 | if (filename != NULL) |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 299 | v = Py_BuildValue("(iss)", i, s, filename); |
| 300 | else |
| 301 | v = Py_BuildValue("(is)", i, s); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 302 | if (v != NULL) { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 303 | PyErr_SetObject(exc, v); |
| 304 | Py_DECREF(v); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 305 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 306 | #ifdef MS_WIN32 |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 307 | LocalFree(s_buf); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 308 | #endif |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 309 | return NULL; |
| 310 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 311 | |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 312 | |
| 313 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 314 | PyErr_SetFromErrno(PyObject *exc) |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 315 | { |
| 316 | return PyErr_SetFromErrnoWithFilename(exc, NULL); |
| 317 | } |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 318 | |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 319 | #ifdef MS_WINDOWS |
| 320 | /* Windows specific error code handling */ |
| 321 | PyObject *PyErr_SetFromWindowsErrWithFilename( |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 322 | int ierr, |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 323 | const char *filename) |
| 324 | { |
| 325 | int len; |
| 326 | char *s; |
| 327 | PyObject *v; |
| 328 | DWORD err = (DWORD)ierr; |
| 329 | if (err==0) err = GetLastError(); |
| 330 | len = FormatMessage( |
| 331 | /* Error API error */ |
| 332 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 333 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 334 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 335 | NULL, /* no message source */ |
| 336 | err, |
| 337 | MAKELANGID(LANG_NEUTRAL, |
| 338 | SUBLANG_DEFAULT), /* Default language */ |
| 339 | (LPTSTR) &s, |
| 340 | 0, /* size not used */ |
| 341 | NULL); /* no args */ |
| 342 | /* remove trailing cr/lf and dots */ |
| 343 | while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) |
| 344 | s[--len] = '\0'; |
Barry Warsaw | fa5c315 | 2000-05-02 19:27:51 +0000 | [diff] [blame] | 345 | if (filename != NULL) |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 346 | v = Py_BuildValue("(iss)", err, s, filename); |
| 347 | else |
| 348 | v = Py_BuildValue("(is)", err, s); |
| 349 | if (v != NULL) { |
Guido van Rossum | 0b55670 | 2000-03-02 13:55:01 +0000 | [diff] [blame] | 350 | PyErr_SetObject(PyExc_WindowsError, v); |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 351 | Py_DECREF(v); |
| 352 | } |
| 353 | LocalFree(s); |
| 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | PyObject *PyErr_SetFromWindowsErr(int ierr) |
| 358 | { |
| 359 | return PyErr_SetFromWindowsErrWithFilename(ierr, NULL); |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 360 | } |
| 361 | #endif /* MS_WINDOWS */ |
| 362 | |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 363 | void |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 364 | _PyErr_BadInternalCall(char *filename, int lineno) |
| 365 | { |
| 366 | PyErr_Format(PyExc_SystemError, |
| 367 | "%s:%d: bad argument to internal function", |
| 368 | filename, lineno); |
| 369 | } |
| 370 | |
| 371 | /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can |
| 372 | export the entry point for existing object code: */ |
| 373 | #undef PyErr_BadInternalCall |
| 374 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 375 | PyErr_BadInternalCall(void) |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 376 | { |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 377 | PyErr_Format(PyExc_SystemError, |
| 378 | "bad argument to internal function"); |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 379 | } |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 380 | #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) |
| 381 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 382 | |
| 383 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 384 | PyObject * |
| 385 | PyErr_Format(PyObject *exception, const char *format, ...) |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 386 | { |
| 387 | va_list vargs; |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 388 | PyObject* string; |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 389 | |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 390 | #ifdef HAVE_STDARG_PROTOTYPES |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 391 | va_start(vargs, format); |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 392 | #else |
| 393 | va_start(vargs); |
| 394 | #endif |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 395 | |
Barry Warsaw | 876c8cb | 2001-08-24 18:35:23 +0000 | [diff] [blame^] | 396 | string = PyString_FromFormatV(format, vargs); |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 397 | PyErr_SetObject(exception, string); |
| 398 | Py_XDECREF(string); |
| 399 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 400 | return NULL; |
| 401 | } |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 402 | |
| 403 | |
| 404 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 405 | PyErr_NewException(char *name, PyObject *base, PyObject *dict) |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 406 | { |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 407 | char *dot; |
| 408 | PyObject *modulename = NULL; |
| 409 | PyObject *classname = NULL; |
| 410 | PyObject *mydict = NULL; |
| 411 | PyObject *bases = NULL; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 412 | PyObject *result = NULL; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 413 | dot = strrchr(name, '.'); |
| 414 | if (dot == NULL) { |
| 415 | PyErr_SetString(PyExc_SystemError, |
| 416 | "PyErr_NewException: name must be module.class"); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 417 | return NULL; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 418 | } |
| 419 | if (base == NULL) |
| 420 | base = PyExc_Exception; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 421 | if (!PyClass_Check(base)) { |
| 422 | /* Must be using string-based standard exceptions (-X) */ |
| 423 | return PyString_FromString(name); |
| 424 | } |
| 425 | if (dict == NULL) { |
| 426 | dict = mydict = PyDict_New(); |
| 427 | if (dict == NULL) |
| 428 | goto failure; |
| 429 | } |
| 430 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 431 | modulename = PyString_FromStringAndSize(name, (int)(dot-name)); |
| 432 | if (modulename == NULL) |
| 433 | goto failure; |
| 434 | if (PyDict_SetItemString(dict, "__module__", modulename) != 0) |
| 435 | goto failure; |
| 436 | } |
| 437 | classname = PyString_FromString(dot+1); |
| 438 | if (classname == NULL) |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 439 | goto failure; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 440 | bases = Py_BuildValue("(O)", base); |
| 441 | if (bases == NULL) |
| 442 | goto failure; |
| 443 | result = PyClass_New(bases, dict, classname); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 444 | failure: |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 445 | Py_XDECREF(bases); |
| 446 | Py_XDECREF(mydict); |
| 447 | Py_XDECREF(classname); |
| 448 | Py_XDECREF(modulename); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 449 | return result; |
| 450 | } |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 451 | |
| 452 | /* Call when an exception has occurred but there is no way for Python |
| 453 | to handle it. Examples: exception in __del__ or during GC. */ |
| 454 | void |
| 455 | PyErr_WriteUnraisable(PyObject *obj) |
| 456 | { |
| 457 | PyObject *f, *t, *v, *tb; |
| 458 | PyErr_Fetch(&t, &v, &tb); |
| 459 | f = PySys_GetObject("stderr"); |
| 460 | if (f != NULL) { |
| 461 | PyFile_WriteString("Exception ", f); |
| 462 | if (t) { |
| 463 | PyFile_WriteObject(t, f, Py_PRINT_RAW); |
| 464 | if (v && v != Py_None) { |
| 465 | PyFile_WriteString(": ", f); |
| 466 | PyFile_WriteObject(v, f, 0); |
| 467 | } |
| 468 | } |
| 469 | PyFile_WriteString(" in ", f); |
| 470 | PyFile_WriteObject(obj, f, 0); |
| 471 | PyFile_WriteString(" ignored\n", f); |
| 472 | PyErr_Clear(); /* Just in case */ |
| 473 | } |
| 474 | Py_XDECREF(t); |
| 475 | Py_XDECREF(v); |
| 476 | Py_XDECREF(tb); |
| 477 | } |
Guido van Rossum | cfd42b5 | 2000-12-15 21:58:52 +0000 | [diff] [blame] | 478 | |
| 479 | |
| 480 | /* Function to issue a warning message; may raise an exception. */ |
| 481 | int |
| 482 | PyErr_Warn(PyObject *category, char *message) |
| 483 | { |
| 484 | PyObject *mod, *dict, *func = NULL; |
| 485 | |
| 486 | mod = PyImport_ImportModule("warnings"); |
| 487 | if (mod != NULL) { |
| 488 | dict = PyModule_GetDict(mod); |
| 489 | func = PyDict_GetItemString(dict, "warn"); |
| 490 | Py_DECREF(mod); |
| 491 | } |
| 492 | if (func == NULL) { |
| 493 | PySys_WriteStderr("warning: %s\n", message); |
| 494 | return 0; |
| 495 | } |
| 496 | else { |
| 497 | PyObject *args, *res; |
| 498 | |
| 499 | if (category == NULL) |
| 500 | category = PyExc_RuntimeWarning; |
| 501 | args = Py_BuildValue("(sO)", message, category); |
| 502 | if (args == NULL) |
| 503 | return -1; |
| 504 | res = PyEval_CallObject(func, args); |
| 505 | Py_DECREF(args); |
| 506 | if (res == NULL) |
| 507 | return -1; |
| 508 | Py_DECREF(res); |
| 509 | return 0; |
| 510 | } |
| 511 | } |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 512 | |
Guido van Rossum | 2fd4565 | 2001-02-28 21:46:24 +0000 | [diff] [blame] | 513 | |
| 514 | /* Warning with explicit origin */ |
| 515 | int |
| 516 | PyErr_WarnExplicit(PyObject *category, char *message, |
| 517 | char *filename, int lineno, |
| 518 | char *module, PyObject *registry) |
| 519 | { |
| 520 | PyObject *mod, *dict, *func = NULL; |
| 521 | |
| 522 | mod = PyImport_ImportModule("warnings"); |
| 523 | if (mod != NULL) { |
| 524 | dict = PyModule_GetDict(mod); |
| 525 | func = PyDict_GetItemString(dict, "warn_explicit"); |
| 526 | Py_DECREF(mod); |
| 527 | } |
| 528 | if (func == NULL) { |
| 529 | PySys_WriteStderr("warning: %s\n", message); |
| 530 | return 0; |
| 531 | } |
| 532 | else { |
| 533 | PyObject *args, *res; |
| 534 | |
| 535 | if (category == NULL) |
| 536 | category = PyExc_RuntimeWarning; |
| 537 | if (registry == NULL) |
| 538 | registry = Py_None; |
| 539 | args = Py_BuildValue("(sOsizO)", message, category, |
| 540 | filename, lineno, module, registry); |
| 541 | if (args == NULL) |
| 542 | return -1; |
| 543 | res = PyEval_CallObject(func, args); |
| 544 | Py_DECREF(args); |
| 545 | if (res == NULL) |
| 546 | return -1; |
| 547 | Py_DECREF(res); |
| 548 | return 0; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | |
| 553 | /* XXX There's a comment missing here */ |
| 554 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 555 | void |
| 556 | PyErr_SyntaxLocation(char *filename, int lineno) |
| 557 | { |
| 558 | PyObject *exc, *v, *tb, *tmp; |
| 559 | |
| 560 | /* add attributes for the line number and filename for the error */ |
| 561 | PyErr_Fetch(&exc, &v, &tb); |
| 562 | PyErr_NormalizeException(&exc, &v, &tb); |
| 563 | /* XXX check that it is, indeed, a syntax error */ |
| 564 | tmp = PyInt_FromLong(lineno); |
| 565 | if (tmp == NULL) |
| 566 | PyErr_Clear(); |
| 567 | else { |
| 568 | if (PyObject_SetAttrString(v, "lineno", tmp)) |
| 569 | PyErr_Clear(); |
| 570 | Py_DECREF(tmp); |
| 571 | } |
| 572 | if (filename != NULL) { |
| 573 | tmp = PyString_FromString(filename); |
| 574 | if (tmp == NULL) |
| 575 | PyErr_Clear(); |
| 576 | else { |
| 577 | if (PyObject_SetAttrString(v, "filename", tmp)) |
| 578 | PyErr_Clear(); |
| 579 | Py_DECREF(tmp); |
| 580 | } |
| 581 | |
| 582 | tmp = PyErr_ProgramText(filename, lineno); |
| 583 | if (tmp) { |
| 584 | PyObject_SetAttrString(v, "text", tmp); |
| 585 | Py_DECREF(tmp); |
| 586 | } |
| 587 | } |
| 588 | PyErr_Restore(exc, v, tb); |
| 589 | } |
| 590 | |
| 591 | /* com_fetch_program_text will attempt to load the line of text that |
| 592 | the exception refers to. If it fails, it will return NULL but will |
| 593 | not set an exception. |
| 594 | |
| 595 | XXX The functionality of this function is quite similar to the |
| 596 | functionality in tb_displayline() in traceback.c. |
| 597 | */ |
| 598 | |
| 599 | PyObject * |
| 600 | PyErr_ProgramText(char *filename, int lineno) |
| 601 | { |
| 602 | FILE *fp; |
| 603 | int i; |
| 604 | char linebuf[1000]; |
| 605 | |
| 606 | if (filename == NULL || lineno <= 0) |
| 607 | return NULL; |
| 608 | fp = fopen(filename, "r"); |
| 609 | if (fp == NULL) |
| 610 | return NULL; |
| 611 | for (i = 0; i < lineno; i++) { |
| 612 | char *pLastChar = &linebuf[sizeof(linebuf) - 2]; |
| 613 | do { |
| 614 | *pLastChar = '\0'; |
| 615 | if (fgets(linebuf, sizeof linebuf, fp) == NULL) |
| 616 | break; |
| 617 | /* fgets read *something*; if it didn't get as |
| 618 | far as pLastChar, it must have found a newline |
| 619 | or hit the end of the file; if pLastChar is \n, |
| 620 | it obviously found a newline; else we haven't |
| 621 | yet seen a newline, so must continue */ |
| 622 | } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 623 | } |
| 624 | fclose(fp); |
| 625 | if (i == lineno) { |
| 626 | char *p = linebuf; |
| 627 | while (*p == ' ' || *p == '\t' || *p == '\014') |
| 628 | p++; |
| 629 | return PyString_FromString(p); |
| 630 | } |
| 631 | return NULL; |
| 632 | } |