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