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