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