Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 11 | /* Error handling */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 13 | #include "Python.h" |
Guido van Rossum | f22120a | 1990-12-20 23:05:40 +0000 | [diff] [blame] | 14 | |
Jack Jansen | 8fd2d94 | 1994-12-14 12:54:54 +0000 | [diff] [blame] | 15 | #ifdef macintosh |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 16 | extern char *PyMac_StrError(int); |
Jack Jansen | 5ef86d5 | 1995-01-19 12:16:44 +0000 | [diff] [blame] | 17 | #undef strerror |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 18 | #define strerror PyMac_StrError |
| 19 | #endif /* macintosh */ |
| 20 | |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 21 | #ifndef __STDC__ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 22 | #ifndef MS_WINDOWS |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 23 | extern char *strerror(int); |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 24 | #endif |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 25 | #endif |
Guido van Rossum | f5401bd | 1990-11-02 17:50:28 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 27 | #ifdef MS_WIN32 |
| 28 | #include "windows.h" |
| 29 | #include "winbase.h" |
| 30 | #endif |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 33 | PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 34 | { |
Guido van Rossum | 885553e | 1998-12-21 18:33:30 +0000 | [diff] [blame] | 35 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 36 | PyObject *oldtype, *oldvalue, *oldtraceback; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 38 | if (traceback != NULL && !PyTraceBack_Check(traceback)) { |
| 39 | /* XXX Should never happen -- fatal error instead? */ |
| 40 | Py_DECREF(traceback); |
| 41 | traceback = NULL; |
| 42 | } |
| 43 | |
| 44 | /* Save these in locals to safeguard against recursive |
| 45 | invocation through Py_XDECREF */ |
| 46 | oldtype = tstate->curexc_type; |
| 47 | oldvalue = tstate->curexc_value; |
| 48 | oldtraceback = tstate->curexc_traceback; |
| 49 | |
| 50 | tstate->curexc_type = type; |
| 51 | tstate->curexc_value = value; |
| 52 | tstate->curexc_traceback = traceback; |
| 53 | |
| 54 | Py_XDECREF(oldtype); |
| 55 | Py_XDECREF(oldvalue); |
| 56 | Py_XDECREF(oldtraceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 60 | PyErr_SetObject(PyObject *exception, PyObject *value) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 76 | PyObject *value = PyString_FromString(string); |
| 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 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 85 | PyThreadState *tstate = PyThreadState_Get(); |
| 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)) { |
| 99 | int i, n; |
| 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. */ |
| 112 | if (PyInstance_Check(err)) |
| 113 | err = (PyObject*)((PyInstanceObject*)err)->in_class; |
| 114 | |
| 115 | if (PyClass_Check(err) && PyClass_Check(exc)) |
| 116 | return PyClass_IsSubclass(err, exc); |
| 117 | |
| 118 | return err == exc; |
| 119 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 120 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 121 | |
| 122 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 123 | PyErr_ExceptionMatches(PyObject *exc) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 124 | { |
| 125 | return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /* Used in many places to normalize a raised exception, including in |
| 130 | eval_code2(), do_raise(), and PyErr_Print() |
| 131 | */ |
| 132 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 133 | PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 134 | { |
| 135 | PyObject *type = *exc; |
| 136 | PyObject *value = *val; |
| 137 | PyObject *inclass = NULL; |
| 138 | |
| 139 | /* If PyErr_SetNone() was used, the value will have been actually |
| 140 | set to NULL. |
| 141 | */ |
| 142 | if (!value) { |
| 143 | value = Py_None; |
| 144 | Py_INCREF(value); |
| 145 | } |
| 146 | |
| 147 | if (PyInstance_Check(value)) |
| 148 | inclass = (PyObject*)((PyInstanceObject*)value)->in_class; |
| 149 | |
| 150 | /* Normalize the exception so that if the type is a class, the |
| 151 | value will be an instance. |
| 152 | */ |
| 153 | if (PyClass_Check(type)) { |
| 154 | /* if the value was not an instance, or is not an instance |
| 155 | whose class is (or is derived from) type, then use the |
| 156 | value as an argument to instantiation of the type |
| 157 | class. |
| 158 | */ |
| 159 | if (!inclass || !PyClass_IsSubclass(inclass, type)) { |
| 160 | PyObject *args, *res; |
| 161 | |
| 162 | if (value == Py_None) |
| 163 | args = Py_BuildValue("()"); |
| 164 | else if (PyTuple_Check(value)) { |
| 165 | Py_INCREF(value); |
| 166 | args = value; |
| 167 | } |
| 168 | else |
| 169 | args = Py_BuildValue("(O)", value); |
| 170 | |
| 171 | if (args == NULL) |
| 172 | goto finally; |
| 173 | res = PyEval_CallObject(type, args); |
| 174 | Py_DECREF(args); |
| 175 | if (res == NULL) |
| 176 | goto finally; |
| 177 | Py_DECREF(value); |
| 178 | value = res; |
| 179 | } |
Barry Warsaw | 3a74993 | 1997-09-30 15:00:18 +0000 | [diff] [blame] | 180 | /* if the class of the instance doesn't exactly match the |
| 181 | class of the type, believe the instance |
| 182 | */ |
| 183 | else if (inclass != type) { |
| 184 | Py_DECREF(type); |
| 185 | type = inclass; |
| 186 | Py_INCREF(type); |
| 187 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 188 | } |
| 189 | *exc = type; |
| 190 | *val = value; |
| 191 | return; |
| 192 | finally: |
Guido van Rossum | 19b55f2 | 1997-12-09 14:11:39 +0000 | [diff] [blame] | 193 | Py_DECREF(type); |
| 194 | Py_DECREF(value); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 195 | Py_XDECREF(*tb); |
| 196 | PyErr_Fetch(exc, val, tb); |
| 197 | /* normalize recursively */ |
| 198 | PyErr_NormalizeException(exc, val, tb); |
| 199 | } |
| 200 | |
| 201 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 202 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 203 | PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 204 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 205 | PyThreadState *tstate = PyThreadState_Get(); |
| 206 | |
| 207 | *p_type = tstate->curexc_type; |
| 208 | *p_value = tstate->curexc_value; |
| 209 | *p_traceback = tstate->curexc_traceback; |
| 210 | |
| 211 | tstate->curexc_type = NULL; |
| 212 | tstate->curexc_value = NULL; |
| 213 | tstate->curexc_traceback = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 217 | PyErr_Clear(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 218 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 219 | PyErr_Restore(NULL, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 220 | } |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 221 | |
| 222 | /* Convenience functions to set a type error exception and return 0 */ |
| 223 | |
| 224 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 225 | PyErr_BadArgument(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 226 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 227 | PyErr_SetString(PyExc_TypeError, |
| 228 | "illegal argument type for built-in operation"); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 232 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 233 | PyErr_NoMemory(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 234 | { |
Barry Warsaw | 2d8adff | 1997-08-29 21:54:35 +0000 | [diff] [blame] | 235 | /* raise the pre-allocated instance if it still exists */ |
| 236 | if (PyExc_MemoryErrorInst) |
| 237 | PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst); |
| 238 | else |
| 239 | /* this will probably fail since there's no memory and hee, |
| 240 | hee, we have to instantiate this class |
| 241 | */ |
| 242 | PyErr_SetNone(PyExc_MemoryError); |
| 243 | |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 244 | return NULL; |
| 245 | } |
| 246 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 247 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 248 | PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 249 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 250 | PyObject *v; |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 251 | char *s; |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 252 | int i = errno; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 253 | #ifdef MS_WIN32 |
| 254 | char *s_buf = NULL; |
| 255 | #endif |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 256 | #ifdef EINTR |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 257 | if (i == EINTR && PyErr_CheckSignals()) |
Guido van Rossum | 5063bab | 1991-10-20 20:14:56 +0000 | [diff] [blame] | 258 | return NULL; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 259 | #endif |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 260 | if (i == 0) |
| 261 | s = "Error"; /* Sometimes errno didn't get set */ |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 262 | else |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 263 | #ifndef MS_WIN32 |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 264 | s = strerror(i); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 265 | #else |
| 266 | { |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 267 | /* Note that the Win32 errors do not lineup with the |
| 268 | errno error. So if the error is in the MSVC error |
| 269 | table, we use it, otherwise we assume it really _is_ |
| 270 | a Win32 error code |
| 271 | */ |
Guido van Rossum | 584b16a | 2000-02-21 16:50:31 +0000 | [diff] [blame] | 272 | if (i > 0 && i < _sys_nerr) { |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 273 | s = _sys_errlist[i]; |
| 274 | } |
| 275 | else { |
| 276 | int len = FormatMessage( |
| 277 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 278 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 279 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 280 | NULL, /* no message source */ |
| 281 | i, |
| 282 | MAKELANGID(LANG_NEUTRAL, |
| 283 | SUBLANG_DEFAULT), |
| 284 | /* Default language */ |
| 285 | (LPTSTR) &s_buf, |
| 286 | 0, /* size not used */ |
| 287 | NULL); /* no args */ |
| 288 | s = s_buf; |
| 289 | /* remove trailing cr/lf and dots */ |
| 290 | while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) |
| 291 | s[--len] = '\0'; |
| 292 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 293 | } |
| 294 | #endif |
Barry Warsaw | fa5c315 | 2000-05-02 19:27:51 +0000 | [diff] [blame] | 295 | if (filename != NULL) |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 296 | v = Py_BuildValue("(iss)", i, s, filename); |
| 297 | else |
| 298 | v = Py_BuildValue("(is)", i, s); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 299 | if (v != NULL) { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 300 | PyErr_SetObject(exc, v); |
| 301 | Py_DECREF(v); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 302 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 303 | #ifdef MS_WIN32 |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 304 | LocalFree(s_buf); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 305 | #endif |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 306 | return NULL; |
| 307 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 308 | |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 309 | |
| 310 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 311 | PyErr_SetFromErrno(PyObject *exc) |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 312 | { |
| 313 | return PyErr_SetFromErrnoWithFilename(exc, NULL); |
| 314 | } |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 316 | #ifdef MS_WINDOWS |
| 317 | /* Windows specific error code handling */ |
| 318 | PyObject *PyErr_SetFromWindowsErrWithFilename( |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 319 | int ierr, |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 320 | const char *filename) |
| 321 | { |
| 322 | int len; |
| 323 | char *s; |
| 324 | PyObject *v; |
| 325 | DWORD err = (DWORD)ierr; |
| 326 | if (err==0) err = GetLastError(); |
| 327 | len = FormatMessage( |
| 328 | /* Error API error */ |
| 329 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 330 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 331 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 332 | NULL, /* no message source */ |
| 333 | err, |
| 334 | MAKELANGID(LANG_NEUTRAL, |
| 335 | SUBLANG_DEFAULT), /* Default language */ |
| 336 | (LPTSTR) &s, |
| 337 | 0, /* size not used */ |
| 338 | NULL); /* no args */ |
| 339 | /* remove trailing cr/lf and dots */ |
| 340 | while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) |
| 341 | s[--len] = '\0'; |
Barry Warsaw | fa5c315 | 2000-05-02 19:27:51 +0000 | [diff] [blame] | 342 | if (filename != NULL) |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 343 | v = Py_BuildValue("(iss)", err, s, filename); |
| 344 | else |
| 345 | v = Py_BuildValue("(is)", err, s); |
| 346 | if (v != NULL) { |
Guido van Rossum | 0b55670 | 2000-03-02 13:55:01 +0000 | [diff] [blame] | 347 | PyErr_SetObject(PyExc_WindowsError, v); |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 348 | Py_DECREF(v); |
| 349 | } |
| 350 | LocalFree(s); |
| 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | PyObject *PyErr_SetFromWindowsErr(int ierr) |
| 355 | { |
| 356 | return PyErr_SetFromWindowsErrWithFilename(ierr, NULL); |
| 357 | |
| 358 | } |
| 359 | #endif /* MS_WINDOWS */ |
| 360 | |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 361 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 362 | PyErr_BadInternalCall(void) |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 363 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 364 | PyErr_SetString(PyExc_SystemError, |
| 365 | "bad argument to internal function"); |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 366 | } |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 367 | |
| 368 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 369 | PyObject * |
| 370 | PyErr_Format(PyObject *exception, const char *format, ...) |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 371 | { |
| 372 | va_list vargs; |
| 373 | char buffer[500]; /* Caller is responsible for limiting the format */ |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 374 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 375 | va_start(vargs, format); |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 376 | |
| 377 | vsprintf(buffer, format, vargs); |
| 378 | PyErr_SetString(exception, buffer); |
| 379 | return NULL; |
| 380 | } |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 381 | |
| 382 | |
| 383 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 384 | PyErr_NewException(char *name, PyObject *base, PyObject *dict) |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 385 | { |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 386 | char *dot; |
| 387 | PyObject *modulename = NULL; |
| 388 | PyObject *classname = NULL; |
| 389 | PyObject *mydict = NULL; |
| 390 | PyObject *bases = NULL; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 391 | PyObject *result = NULL; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 392 | dot = strrchr(name, '.'); |
| 393 | if (dot == NULL) { |
| 394 | PyErr_SetString(PyExc_SystemError, |
| 395 | "PyErr_NewException: name must be module.class"); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 396 | return NULL; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 397 | } |
| 398 | if (base == NULL) |
| 399 | base = PyExc_Exception; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 400 | if (!PyClass_Check(base)) { |
| 401 | /* Must be using string-based standard exceptions (-X) */ |
| 402 | return PyString_FromString(name); |
| 403 | } |
| 404 | if (dict == NULL) { |
| 405 | dict = mydict = PyDict_New(); |
| 406 | if (dict == NULL) |
| 407 | goto failure; |
| 408 | } |
| 409 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 410 | modulename = PyString_FromStringAndSize(name, (int)(dot-name)); |
| 411 | if (modulename == NULL) |
| 412 | goto failure; |
| 413 | if (PyDict_SetItemString(dict, "__module__", modulename) != 0) |
| 414 | goto failure; |
| 415 | } |
| 416 | classname = PyString_FromString(dot+1); |
| 417 | if (classname == NULL) |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 418 | goto failure; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 419 | bases = Py_BuildValue("(O)", base); |
| 420 | if (bases == NULL) |
| 421 | goto failure; |
| 422 | result = PyClass_New(bases, dict, classname); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 423 | failure: |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 424 | Py_XDECREF(bases); |
| 425 | Py_XDECREF(mydict); |
| 426 | Py_XDECREF(classname); |
| 427 | Py_XDECREF(modulename); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 428 | return result; |
| 429 | } |