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