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 | eceb3e3 | 1995-06-27 13:15:15 +0000 | [diff] [blame] | 15 | #ifdef SYMANTEC__CFM68K__ |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 16 | #pragma lib_export on |
| 17 | #endif |
| 18 | |
Jack Jansen | 8fd2d94 | 1994-12-14 12:54:54 +0000 | [diff] [blame] | 19 | #ifdef macintosh |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 20 | extern char *PyMac_StrError Py_PROTO((int)); |
Jack Jansen | 5ef86d5 | 1995-01-19 12:16:44 +0000 | [diff] [blame] | 21 | #undef strerror |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 22 | #define strerror PyMac_StrError |
| 23 | #endif /* macintosh */ |
| 24 | |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 25 | #ifndef __STDC__ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 26 | #ifndef MS_WINDOWS |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 27 | extern char *strerror Py_PROTO((int)); |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 28 | #endif |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 29 | #endif |
Guido van Rossum | f5401bd | 1990-11-02 17:50:28 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 31 | #ifdef MS_WIN32 |
| 32 | #include "windows.h" |
| 33 | #include "winbase.h" |
| 34 | #endif |
| 35 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | void |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 37 | PyErr_Restore(type, value, traceback) |
| 38 | PyObject *type; |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 39 | PyObject *value; |
| 40 | PyObject *traceback; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 41 | { |
Guido van Rossum | 885553e | 1998-12-21 18:33:30 +0000 | [diff] [blame] | 42 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 43 | PyObject *oldtype, *oldvalue, *oldtraceback; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 45 | if (traceback != NULL && !PyTraceBack_Check(traceback)) { |
| 46 | /* XXX Should never happen -- fatal error instead? */ |
| 47 | Py_DECREF(traceback); |
| 48 | traceback = NULL; |
| 49 | } |
| 50 | |
| 51 | /* Save these in locals to safeguard against recursive |
| 52 | invocation through Py_XDECREF */ |
| 53 | oldtype = tstate->curexc_type; |
| 54 | oldvalue = tstate->curexc_value; |
| 55 | oldtraceback = tstate->curexc_traceback; |
| 56 | |
| 57 | tstate->curexc_type = type; |
| 58 | tstate->curexc_value = value; |
| 59 | tstate->curexc_traceback = traceback; |
| 60 | |
| 61 | Py_XDECREF(oldtype); |
| 62 | Py_XDECREF(oldvalue); |
| 63 | Py_XDECREF(oldtraceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 67 | PyErr_SetObject(exception, value) |
| 68 | PyObject *exception; |
| 69 | PyObject *value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 71 | Py_XINCREF(exception); |
| 72 | Py_XINCREF(value); |
| 73 | PyErr_Restore(exception, value, (PyObject *)NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 77 | PyErr_SetNone(exception) |
| 78 | PyObject *exception; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 80 | PyErr_SetObject(exception, (PyObject *)NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 84 | PyErr_SetString(exception, string) |
| 85 | PyObject *exception; |
Guido van Rossum | 067998f | 1996-12-10 15:33:34 +0000 | [diff] [blame] | 86 | const char *string; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 88 | PyObject *value = PyString_FromString(string); |
| 89 | PyErr_SetObject(exception, value); |
| 90 | Py_XDECREF(value); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 93 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 94 | PyObject * |
| 95 | PyErr_Occurred() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 97 | PyThreadState *tstate = PyThreadState_Get(); |
| 98 | |
| 99 | return tstate->curexc_type; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 102 | |
| 103 | int |
| 104 | PyErr_GivenExceptionMatches(err, exc) |
| 105 | PyObject *err, *exc; |
| 106 | { |
Barry Warsaw | fa5c315 | 2000-05-02 19:27:51 +0000 | [diff] [blame] | 107 | if (err == NULL || exc == NULL) { |
| 108 | /* maybe caused by "import exceptions" that failed early on */ |
| 109 | return 0; |
| 110 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 111 | if (PyTuple_Check(exc)) { |
| 112 | int i, n; |
| 113 | n = PyTuple_Size(exc); |
| 114 | for (i = 0; i < n; i++) { |
| 115 | /* Test recursively */ |
| 116 | if (PyErr_GivenExceptionMatches( |
| 117 | err, PyTuple_GET_ITEM(exc, i))) |
| 118 | { |
| 119 | return 1; |
| 120 | } |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | /* err might be an instance, so check its class. */ |
| 125 | if (PyInstance_Check(err)) |
| 126 | err = (PyObject*)((PyInstanceObject*)err)->in_class; |
| 127 | |
| 128 | if (PyClass_Check(err) && PyClass_Check(exc)) |
| 129 | return PyClass_IsSubclass(err, exc); |
| 130 | |
| 131 | return err == exc; |
| 132 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 133 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 134 | |
| 135 | int |
| 136 | PyErr_ExceptionMatches(exc) |
| 137 | PyObject *exc; |
| 138 | { |
| 139 | return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /* Used in many places to normalize a raised exception, including in |
| 144 | eval_code2(), do_raise(), and PyErr_Print() |
| 145 | */ |
| 146 | void |
| 147 | PyErr_NormalizeException(exc, val, tb) |
| 148 | PyObject **exc; |
| 149 | PyObject **val; |
| 150 | PyObject **tb; |
| 151 | { |
| 152 | PyObject *type = *exc; |
| 153 | PyObject *value = *val; |
| 154 | PyObject *inclass = NULL; |
| 155 | |
| 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 | |
| 164 | if (PyInstance_Check(value)) |
| 165 | inclass = (PyObject*)((PyInstanceObject*)value)->in_class; |
| 166 | |
| 167 | /* Normalize the exception so that if the type is a class, the |
| 168 | value will be an instance. |
| 169 | */ |
| 170 | if (PyClass_Check(type)) { |
| 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 | */ |
| 176 | if (!inclass || !PyClass_IsSubclass(inclass, type)) { |
| 177 | PyObject *args, *res; |
| 178 | |
| 179 | if (value == Py_None) |
| 180 | args = Py_BuildValue("()"); |
| 181 | else if (PyTuple_Check(value)) { |
| 182 | Py_INCREF(value); |
| 183 | args = value; |
| 184 | } |
| 185 | else |
| 186 | args = Py_BuildValue("(O)", value); |
| 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); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 212 | Py_XDECREF(*tb); |
| 213 | PyErr_Fetch(exc, val, tb); |
| 214 | /* normalize recursively */ |
| 215 | PyErr_NormalizeException(exc, val, tb); |
| 216 | } |
| 217 | |
| 218 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 219 | void |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 220 | PyErr_Fetch(p_type, p_value, p_traceback) |
| 221 | PyObject **p_type; |
| 222 | PyObject **p_value; |
| 223 | PyObject **p_traceback; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 224 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 225 | PyThreadState *tstate = PyThreadState_Get(); |
| 226 | |
| 227 | *p_type = tstate->curexc_type; |
| 228 | *p_value = tstate->curexc_value; |
| 229 | *p_traceback = tstate->curexc_traceback; |
| 230 | |
| 231 | tstate->curexc_type = NULL; |
| 232 | tstate->curexc_value = NULL; |
| 233 | tstate->curexc_traceback = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 237 | PyErr_Clear() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 238 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 239 | PyErr_Restore(NULL, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 240 | } |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 241 | |
| 242 | /* Convenience functions to set a type error exception and return 0 */ |
| 243 | |
| 244 | int |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 245 | PyErr_BadArgument() |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 246 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 247 | PyErr_SetString(PyExc_TypeError, |
| 248 | "illegal argument type for built-in operation"); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 252 | PyObject * |
| 253 | PyErr_NoMemory() |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 254 | { |
Barry Warsaw | 2d8adff | 1997-08-29 21:54:35 +0000 | [diff] [blame] | 255 | /* raise the pre-allocated instance if it still exists */ |
| 256 | if (PyExc_MemoryErrorInst) |
| 257 | PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst); |
| 258 | else |
| 259 | /* this will probably fail since there's no memory and hee, |
| 260 | hee, we have to instantiate this class |
| 261 | */ |
| 262 | PyErr_SetNone(PyExc_MemoryError); |
| 263 | |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 264 | return NULL; |
| 265 | } |
| 266 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 267 | PyObject * |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 268 | PyErr_SetFromErrnoWithFilename(exc, filename) |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 269 | PyObject *exc; |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 270 | char *filename; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 271 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 272 | PyObject *v; |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 273 | char *s; |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 274 | int i = errno; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 275 | #ifdef MS_WIN32 |
| 276 | char *s_buf = NULL; |
| 277 | #endif |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 278 | #ifdef EINTR |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 279 | if (i == EINTR && PyErr_CheckSignals()) |
Guido van Rossum | 5063bab | 1991-10-20 20:14:56 +0000 | [diff] [blame] | 280 | return NULL; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 281 | #endif |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 282 | if (i == 0) |
| 283 | s = "Error"; /* Sometimes errno didn't get set */ |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 284 | else |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 285 | #ifndef MS_WIN32 |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 286 | s = strerror(i); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 287 | #else |
| 288 | { |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 289 | /* Note that the Win32 errors do not lineup with the |
| 290 | errno error. So if the error is in the MSVC error |
| 291 | table, we use it, otherwise we assume it really _is_ |
| 292 | a Win32 error code |
| 293 | */ |
Guido van Rossum | 584b16a | 2000-02-21 16:50:31 +0000 | [diff] [blame] | 294 | if (i > 0 && i < _sys_nerr) { |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 295 | s = _sys_errlist[i]; |
| 296 | } |
| 297 | else { |
| 298 | int len = FormatMessage( |
| 299 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 300 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 301 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 302 | NULL, /* no message source */ |
| 303 | i, |
| 304 | MAKELANGID(LANG_NEUTRAL, |
| 305 | SUBLANG_DEFAULT), |
| 306 | /* Default language */ |
| 307 | (LPTSTR) &s_buf, |
| 308 | 0, /* size not used */ |
| 309 | NULL); /* no args */ |
| 310 | s = s_buf; |
| 311 | /* remove trailing cr/lf and dots */ |
| 312 | while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) |
| 313 | s[--len] = '\0'; |
| 314 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 315 | } |
| 316 | #endif |
Barry Warsaw | fa5c315 | 2000-05-02 19:27:51 +0000 | [diff] [blame] | 317 | if (filename != NULL) |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 318 | v = Py_BuildValue("(iss)", i, s, filename); |
| 319 | else |
| 320 | v = Py_BuildValue("(is)", i, s); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 321 | if (v != NULL) { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 322 | PyErr_SetObject(exc, v); |
| 323 | Py_DECREF(v); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 324 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 325 | #ifdef MS_WIN32 |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 326 | LocalFree(s_buf); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 327 | #endif |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 328 | return NULL; |
| 329 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 330 | |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 331 | |
| 332 | PyObject * |
| 333 | PyErr_SetFromErrno(exc) |
| 334 | PyObject *exc; |
| 335 | { |
| 336 | return PyErr_SetFromErrnoWithFilename(exc, NULL); |
| 337 | } |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 339 | #ifdef MS_WINDOWS |
| 340 | /* Windows specific error code handling */ |
| 341 | PyObject *PyErr_SetFromWindowsErrWithFilename( |
| 342 | int ierr, |
| 343 | const char *filename) |
| 344 | { |
| 345 | int len; |
| 346 | char *s; |
| 347 | PyObject *v; |
| 348 | DWORD err = (DWORD)ierr; |
| 349 | if (err==0) err = GetLastError(); |
| 350 | len = FormatMessage( |
| 351 | /* Error API error */ |
| 352 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 353 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 354 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 355 | NULL, /* no message source */ |
| 356 | err, |
| 357 | MAKELANGID(LANG_NEUTRAL, |
| 358 | SUBLANG_DEFAULT), /* Default language */ |
| 359 | (LPTSTR) &s, |
| 360 | 0, /* size not used */ |
| 361 | NULL); /* no args */ |
| 362 | /* remove trailing cr/lf and dots */ |
| 363 | while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) |
| 364 | s[--len] = '\0'; |
Barry Warsaw | fa5c315 | 2000-05-02 19:27:51 +0000 | [diff] [blame] | 365 | if (filename != NULL) |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 366 | v = Py_BuildValue("(iss)", err, s, filename); |
| 367 | else |
| 368 | v = Py_BuildValue("(is)", err, s); |
| 369 | if (v != NULL) { |
Guido van Rossum | 0b55670 | 2000-03-02 13:55:01 +0000 | [diff] [blame] | 370 | PyErr_SetObject(PyExc_WindowsError, v); |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 371 | Py_DECREF(v); |
| 372 | } |
| 373 | LocalFree(s); |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | PyObject *PyErr_SetFromWindowsErr(int ierr) |
| 378 | { |
| 379 | return PyErr_SetFromWindowsErrWithFilename(ierr, NULL); |
| 380 | |
| 381 | } |
| 382 | #endif /* MS_WINDOWS */ |
| 383 | |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 384 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 385 | PyErr_BadInternalCall() |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 386 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 387 | PyErr_SetString(PyExc_SystemError, |
| 388 | "bad argument to internal function"); |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 389 | } |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 390 | |
| 391 | |
| 392 | #ifdef HAVE_STDARG_PROTOTYPES |
| 393 | PyObject * |
| 394 | PyErr_Format(PyObject *exception, const char *format, ...) |
| 395 | #else |
| 396 | PyObject * |
| 397 | PyErr_Format(exception, format, va_alist) |
| 398 | PyObject *exception; |
| 399 | const char *format; |
| 400 | va_dcl |
| 401 | #endif |
| 402 | { |
| 403 | va_list vargs; |
| 404 | char buffer[500]; /* Caller is responsible for limiting the format */ |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 405 | |
| 406 | #ifdef HAVE_STDARG_PROTOTYPES |
| 407 | va_start(vargs, format); |
| 408 | #else |
| 409 | va_start(vargs); |
| 410 | #endif |
| 411 | |
| 412 | vsprintf(buffer, format, vargs); |
| 413 | PyErr_SetString(exception, buffer); |
| 414 | return NULL; |
| 415 | } |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 416 | |
| 417 | |
| 418 | PyObject * |
| 419 | PyErr_NewException(name, base, dict) |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 420 | char *name; /* modulename.classname */ |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 421 | PyObject *base; |
| 422 | PyObject *dict; |
| 423 | { |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 424 | char *dot; |
| 425 | PyObject *modulename = NULL; |
| 426 | PyObject *classname = NULL; |
| 427 | PyObject *mydict = NULL; |
| 428 | PyObject *bases = NULL; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 429 | PyObject *result = NULL; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 430 | dot = strrchr(name, '.'); |
| 431 | if (dot == NULL) { |
| 432 | PyErr_SetString(PyExc_SystemError, |
| 433 | "PyErr_NewException: name must be module.class"); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 434 | return NULL; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 435 | } |
| 436 | if (base == NULL) |
| 437 | base = PyExc_Exception; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 438 | if (!PyClass_Check(base)) { |
| 439 | /* Must be using string-based standard exceptions (-X) */ |
| 440 | return PyString_FromString(name); |
| 441 | } |
| 442 | if (dict == NULL) { |
| 443 | dict = mydict = PyDict_New(); |
| 444 | if (dict == NULL) |
| 445 | goto failure; |
| 446 | } |
| 447 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 448 | modulename = PyString_FromStringAndSize(name, (int)(dot-name)); |
| 449 | if (modulename == NULL) |
| 450 | goto failure; |
| 451 | if (PyDict_SetItemString(dict, "__module__", modulename) != 0) |
| 452 | goto failure; |
| 453 | } |
| 454 | classname = PyString_FromString(dot+1); |
| 455 | if (classname == NULL) |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 456 | goto failure; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 457 | bases = Py_BuildValue("(O)", base); |
| 458 | if (bases == NULL) |
| 459 | goto failure; |
| 460 | result = PyClass_New(bases, dict, classname); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 461 | failure: |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 462 | Py_XDECREF(bases); |
| 463 | Py_XDECREF(mydict); |
| 464 | Py_XDECREF(classname); |
| 465 | Py_XDECREF(modulename); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 466 | return result; |
| 467 | } |