Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 32 | /* Error handling */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | f22120a | 1990-12-20 23:05:40 +0000 | [diff] [blame] | 35 | |
Jack Jansen | eceb3e3 | 1995-06-27 13:15:15 +0000 | [diff] [blame] | 36 | #ifdef SYMANTEC__CFM68K__ |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 37 | #pragma lib_export on |
| 38 | #endif |
| 39 | |
Jack Jansen | 8fd2d94 | 1994-12-14 12:54:54 +0000 | [diff] [blame] | 40 | #ifdef macintosh |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 41 | extern char *PyMac_StrError Py_PROTO((int)); |
Jack Jansen | 5ef86d5 | 1995-01-19 12:16:44 +0000 | [diff] [blame] | 42 | #undef strerror |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 43 | #define strerror PyMac_StrError |
| 44 | #endif /* macintosh */ |
| 45 | |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 46 | #ifndef __STDC__ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 47 | #ifndef MS_WINDOWS |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 48 | extern char *strerror Py_PROTO((int)); |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 49 | #endif |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 50 | #endif |
Guido van Rossum | f5401bd | 1990-11-02 17:50:28 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 52 | #ifdef MS_WIN32 |
| 53 | #include "windows.h" |
| 54 | #include "winbase.h" |
| 55 | #endif |
| 56 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | void |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 58 | PyErr_Restore(type, value, traceback) |
| 59 | PyObject *type; |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 60 | PyObject *value; |
| 61 | PyObject *traceback; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 62 | { |
Guido van Rossum | 885553e | 1998-12-21 18:33:30 +0000 | [diff] [blame] | 63 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 64 | PyObject *oldtype, *oldvalue, *oldtraceback; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 66 | if (traceback != NULL && !PyTraceBack_Check(traceback)) { |
| 67 | /* XXX Should never happen -- fatal error instead? */ |
| 68 | Py_DECREF(traceback); |
| 69 | traceback = NULL; |
| 70 | } |
| 71 | |
| 72 | /* Save these in locals to safeguard against recursive |
| 73 | invocation through Py_XDECREF */ |
| 74 | oldtype = tstate->curexc_type; |
| 75 | oldvalue = tstate->curexc_value; |
| 76 | oldtraceback = tstate->curexc_traceback; |
| 77 | |
| 78 | tstate->curexc_type = type; |
| 79 | tstate->curexc_value = value; |
| 80 | tstate->curexc_traceback = traceback; |
| 81 | |
| 82 | Py_XDECREF(oldtype); |
| 83 | Py_XDECREF(oldvalue); |
| 84 | Py_XDECREF(oldtraceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 88 | PyErr_SetObject(exception, value) |
| 89 | PyObject *exception; |
| 90 | PyObject *value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 91 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 92 | Py_XINCREF(exception); |
| 93 | Py_XINCREF(value); |
| 94 | PyErr_Restore(exception, value, (PyObject *)NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 98 | PyErr_SetNone(exception) |
| 99 | PyObject *exception; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 101 | PyErr_SetObject(exception, (PyObject *)NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 105 | PyErr_SetString(exception, string) |
| 106 | PyObject *exception; |
Guido van Rossum | 067998f | 1996-12-10 15:33:34 +0000 | [diff] [blame] | 107 | const char *string; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 108 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 109 | PyObject *value = PyString_FromString(string); |
| 110 | PyErr_SetObject(exception, value); |
| 111 | Py_XDECREF(value); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 115 | PyObject * |
| 116 | PyErr_Occurred() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 117 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 118 | PyThreadState *tstate = PyThreadState_Get(); |
| 119 | |
| 120 | return tstate->curexc_type; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 123 | |
| 124 | int |
| 125 | PyErr_GivenExceptionMatches(err, exc) |
| 126 | PyObject *err, *exc; |
| 127 | { |
| 128 | if (PyTuple_Check(exc)) { |
| 129 | int i, n; |
| 130 | n = PyTuple_Size(exc); |
| 131 | for (i = 0; i < n; i++) { |
| 132 | /* Test recursively */ |
| 133 | if (PyErr_GivenExceptionMatches( |
| 134 | err, PyTuple_GET_ITEM(exc, i))) |
| 135 | { |
| 136 | return 1; |
| 137 | } |
| 138 | } |
| 139 | return 0; |
| 140 | } |
| 141 | /* err might be an instance, so check its class. */ |
| 142 | if (PyInstance_Check(err)) |
| 143 | err = (PyObject*)((PyInstanceObject*)err)->in_class; |
| 144 | |
| 145 | if (PyClass_Check(err) && PyClass_Check(exc)) |
| 146 | return PyClass_IsSubclass(err, exc); |
| 147 | |
| 148 | return err == exc; |
| 149 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 150 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 151 | |
| 152 | int |
| 153 | PyErr_ExceptionMatches(exc) |
| 154 | PyObject *exc; |
| 155 | { |
| 156 | return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /* Used in many places to normalize a raised exception, including in |
| 161 | eval_code2(), do_raise(), and PyErr_Print() |
| 162 | */ |
| 163 | void |
| 164 | PyErr_NormalizeException(exc, val, tb) |
| 165 | PyObject **exc; |
| 166 | PyObject **val; |
| 167 | PyObject **tb; |
| 168 | { |
| 169 | PyObject *type = *exc; |
| 170 | PyObject *value = *val; |
| 171 | PyObject *inclass = NULL; |
| 172 | |
| 173 | /* If PyErr_SetNone() was used, the value will have been actually |
| 174 | set to NULL. |
| 175 | */ |
| 176 | if (!value) { |
| 177 | value = Py_None; |
| 178 | Py_INCREF(value); |
| 179 | } |
| 180 | |
| 181 | if (PyInstance_Check(value)) |
| 182 | inclass = (PyObject*)((PyInstanceObject*)value)->in_class; |
| 183 | |
| 184 | /* Normalize the exception so that if the type is a class, the |
| 185 | value will be an instance. |
| 186 | */ |
| 187 | if (PyClass_Check(type)) { |
| 188 | /* if the value was not an instance, or is not an instance |
| 189 | whose class is (or is derived from) type, then use the |
| 190 | value as an argument to instantiation of the type |
| 191 | class. |
| 192 | */ |
| 193 | if (!inclass || !PyClass_IsSubclass(inclass, type)) { |
| 194 | PyObject *args, *res; |
| 195 | |
| 196 | if (value == Py_None) |
| 197 | args = Py_BuildValue("()"); |
| 198 | else if (PyTuple_Check(value)) { |
| 199 | Py_INCREF(value); |
| 200 | args = value; |
| 201 | } |
| 202 | else |
| 203 | args = Py_BuildValue("(O)", value); |
| 204 | |
| 205 | if (args == NULL) |
| 206 | goto finally; |
| 207 | res = PyEval_CallObject(type, args); |
| 208 | Py_DECREF(args); |
| 209 | if (res == NULL) |
| 210 | goto finally; |
| 211 | Py_DECREF(value); |
| 212 | value = res; |
| 213 | } |
Barry Warsaw | 3a74993 | 1997-09-30 15:00:18 +0000 | [diff] [blame] | 214 | /* if the class of the instance doesn't exactly match the |
| 215 | class of the type, believe the instance |
| 216 | */ |
| 217 | else if (inclass != type) { |
| 218 | Py_DECREF(type); |
| 219 | type = inclass; |
| 220 | Py_INCREF(type); |
| 221 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 222 | } |
| 223 | *exc = type; |
| 224 | *val = value; |
| 225 | return; |
| 226 | finally: |
Guido van Rossum | 19b55f2 | 1997-12-09 14:11:39 +0000 | [diff] [blame] | 227 | Py_DECREF(type); |
| 228 | Py_DECREF(value); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 229 | Py_XDECREF(*tb); |
| 230 | PyErr_Fetch(exc, val, tb); |
| 231 | /* normalize recursively */ |
| 232 | PyErr_NormalizeException(exc, val, tb); |
| 233 | } |
| 234 | |
| 235 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 236 | void |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 237 | PyErr_Fetch(p_type, p_value, p_traceback) |
| 238 | PyObject **p_type; |
| 239 | PyObject **p_value; |
| 240 | PyObject **p_traceback; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 241 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 242 | PyThreadState *tstate = PyThreadState_Get(); |
| 243 | |
| 244 | *p_type = tstate->curexc_type; |
| 245 | *p_value = tstate->curexc_value; |
| 246 | *p_traceback = tstate->curexc_traceback; |
| 247 | |
| 248 | tstate->curexc_type = NULL; |
| 249 | tstate->curexc_value = NULL; |
| 250 | tstate->curexc_traceback = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 254 | PyErr_Clear() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 255 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 256 | PyErr_Restore(NULL, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | } |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 258 | |
| 259 | /* Convenience functions to set a type error exception and return 0 */ |
| 260 | |
| 261 | int |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 262 | PyErr_BadArgument() |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 263 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 264 | PyErr_SetString(PyExc_TypeError, |
| 265 | "illegal argument type for built-in operation"); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 269 | PyObject * |
| 270 | PyErr_NoMemory() |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 271 | { |
Barry Warsaw | 2d8adff | 1997-08-29 21:54:35 +0000 | [diff] [blame] | 272 | /* raise the pre-allocated instance if it still exists */ |
| 273 | if (PyExc_MemoryErrorInst) |
| 274 | PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst); |
| 275 | else |
| 276 | /* this will probably fail since there's no memory and hee, |
| 277 | hee, we have to instantiate this class |
| 278 | */ |
| 279 | PyErr_SetNone(PyExc_MemoryError); |
| 280 | |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 281 | return NULL; |
| 282 | } |
| 283 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 284 | PyObject * |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 285 | PyErr_SetFromErrnoWithFilename(exc, filename) |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 286 | PyObject *exc; |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 287 | char *filename; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 288 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 289 | PyObject *v; |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 290 | char *s; |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 291 | int i = errno; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 292 | #ifdef EINTR |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 293 | if (i == EINTR && PyErr_CheckSignals()) |
Guido van Rossum | 5063bab | 1991-10-20 20:14:56 +0000 | [diff] [blame] | 294 | return NULL; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 295 | #endif |
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 | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 299 | #ifndef MS_WIN32 |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 300 | s = strerror(i); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 301 | #else |
| 302 | { |
| 303 | int len = FormatMessage( |
| 304 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 305 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 306 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 307 | NULL, /* no message source */ |
| 308 | i, |
| 309 | MAKELANGID(LANG_NEUTRAL, |
| 310 | SUBLANG_DEFAULT), /* Default language */ |
| 311 | (LPTSTR) &s, |
| 312 | 0, /* size not used */ |
| 313 | NULL); /* no args */ |
| 314 | /* remove trailing cr/lf and dots */ |
| 315 | while (len > 0 && s[len-1] <= '.') |
| 316 | s[--len] = '\0'; |
| 317 | } |
| 318 | #endif |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 319 | if (filename != NULL && Py_UseClassExceptionsFlag) |
| 320 | v = Py_BuildValue("(iss)", i, s, filename); |
| 321 | else |
| 322 | v = Py_BuildValue("(is)", i, s); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 323 | if (v != NULL) { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 324 | PyErr_SetObject(exc, v); |
| 325 | Py_DECREF(v); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 326 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 327 | #ifdef MS_WIN32 |
| 328 | LocalFree(s); |
| 329 | #endif |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 330 | return NULL; |
| 331 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 332 | |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 333 | |
| 334 | PyObject * |
| 335 | PyErr_SetFromErrno(exc) |
| 336 | PyObject *exc; |
| 337 | { |
| 338 | return PyErr_SetFromErrnoWithFilename(exc, NULL); |
| 339 | } |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 340 | |
| 341 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 342 | PyErr_BadInternalCall() |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 343 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 344 | PyErr_SetString(PyExc_SystemError, |
| 345 | "bad argument to internal function"); |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 346 | } |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 347 | |
| 348 | |
| 349 | #ifdef HAVE_STDARG_PROTOTYPES |
| 350 | PyObject * |
| 351 | PyErr_Format(PyObject *exception, const char *format, ...) |
| 352 | #else |
| 353 | PyObject * |
| 354 | PyErr_Format(exception, format, va_alist) |
| 355 | PyObject *exception; |
| 356 | const char *format; |
| 357 | va_dcl |
| 358 | #endif |
| 359 | { |
| 360 | va_list vargs; |
| 361 | char buffer[500]; /* Caller is responsible for limiting the format */ |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 362 | |
| 363 | #ifdef HAVE_STDARG_PROTOTYPES |
| 364 | va_start(vargs, format); |
| 365 | #else |
| 366 | va_start(vargs); |
| 367 | #endif |
| 368 | |
| 369 | vsprintf(buffer, format, vargs); |
| 370 | PyErr_SetString(exception, buffer); |
| 371 | return NULL; |
| 372 | } |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 373 | |
| 374 | |
| 375 | PyObject * |
| 376 | PyErr_NewException(name, base, dict) |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 377 | char *name; /* modulename.classname */ |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 378 | PyObject *base; |
| 379 | PyObject *dict; |
| 380 | { |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 381 | char *dot; |
| 382 | PyObject *modulename = NULL; |
| 383 | PyObject *classname = NULL; |
| 384 | PyObject *mydict = NULL; |
| 385 | PyObject *bases = NULL; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 386 | PyObject *result = NULL; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 387 | dot = strrchr(name, '.'); |
| 388 | if (dot == NULL) { |
| 389 | PyErr_SetString(PyExc_SystemError, |
| 390 | "PyErr_NewException: name must be module.class"); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 391 | return NULL; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 392 | } |
| 393 | if (base == NULL) |
| 394 | base = PyExc_Exception; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 395 | if (!PyClass_Check(base)) { |
| 396 | /* Must be using string-based standard exceptions (-X) */ |
| 397 | return PyString_FromString(name); |
| 398 | } |
| 399 | if (dict == NULL) { |
| 400 | dict = mydict = PyDict_New(); |
| 401 | if (dict == NULL) |
| 402 | goto failure; |
| 403 | } |
| 404 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 405 | modulename = PyString_FromStringAndSize(name, (int)(dot-name)); |
| 406 | if (modulename == NULL) |
| 407 | goto failure; |
| 408 | if (PyDict_SetItemString(dict, "__module__", modulename) != 0) |
| 409 | goto failure; |
| 410 | } |
| 411 | classname = PyString_FromString(dot+1); |
| 412 | if (classname == NULL) |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 413 | goto failure; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 414 | bases = Py_BuildValue("(O)", base); |
| 415 | if (bases == NULL) |
| 416 | goto failure; |
| 417 | result = PyClass_New(bases, dict, classname); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 418 | failure: |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 419 | Py_XDECREF(bases); |
| 420 | Py_XDECREF(mydict); |
| 421 | Py_XDECREF(classname); |
| 422 | Py_XDECREF(modulename); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 423 | return result; |
| 424 | } |