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 | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | void |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 53 | PyErr_Restore(type, value, traceback) |
| 54 | PyObject *type; |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 55 | PyObject *value; |
| 56 | PyObject *traceback; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 57 | { |
Guido van Rossum | 885553e | 1998-12-21 18:33:30 +0000 | [diff] [blame] | 58 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 59 | PyObject *oldtype, *oldvalue, *oldtraceback; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 61 | if (traceback != NULL && !PyTraceBack_Check(traceback)) { |
| 62 | /* XXX Should never happen -- fatal error instead? */ |
| 63 | Py_DECREF(traceback); |
| 64 | traceback = NULL; |
| 65 | } |
| 66 | |
| 67 | /* Save these in locals to safeguard against recursive |
| 68 | invocation through Py_XDECREF */ |
| 69 | oldtype = tstate->curexc_type; |
| 70 | oldvalue = tstate->curexc_value; |
| 71 | oldtraceback = tstate->curexc_traceback; |
| 72 | |
| 73 | tstate->curexc_type = type; |
| 74 | tstate->curexc_value = value; |
| 75 | tstate->curexc_traceback = traceback; |
| 76 | |
| 77 | Py_XDECREF(oldtype); |
| 78 | Py_XDECREF(oldvalue); |
| 79 | Py_XDECREF(oldtraceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 83 | PyErr_SetObject(exception, value) |
| 84 | PyObject *exception; |
| 85 | PyObject *value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 87 | Py_XINCREF(exception); |
| 88 | Py_XINCREF(value); |
| 89 | PyErr_Restore(exception, value, (PyObject *)NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 93 | PyErr_SetNone(exception) |
| 94 | PyObject *exception; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 96 | PyErr_SetObject(exception, (PyObject *)NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 100 | PyErr_SetString(exception, string) |
| 101 | PyObject *exception; |
Guido van Rossum | 067998f | 1996-12-10 15:33:34 +0000 | [diff] [blame] | 102 | const char *string; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 104 | PyObject *value = PyString_FromString(string); |
| 105 | PyErr_SetObject(exception, value); |
| 106 | Py_XDECREF(value); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 110 | PyObject * |
| 111 | PyErr_Occurred() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 113 | PyThreadState *tstate = PyThreadState_Get(); |
| 114 | |
| 115 | return tstate->curexc_type; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 118 | |
| 119 | int |
| 120 | PyErr_GivenExceptionMatches(err, exc) |
| 121 | PyObject *err, *exc; |
| 122 | { |
| 123 | if (PyTuple_Check(exc)) { |
| 124 | int i, n; |
| 125 | n = PyTuple_Size(exc); |
| 126 | for (i = 0; i < n; i++) { |
| 127 | /* Test recursively */ |
| 128 | if (PyErr_GivenExceptionMatches( |
| 129 | err, PyTuple_GET_ITEM(exc, i))) |
| 130 | { |
| 131 | return 1; |
| 132 | } |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | /* err might be an instance, so check its class. */ |
| 137 | if (PyInstance_Check(err)) |
| 138 | err = (PyObject*)((PyInstanceObject*)err)->in_class; |
| 139 | |
| 140 | if (PyClass_Check(err) && PyClass_Check(exc)) |
| 141 | return PyClass_IsSubclass(err, exc); |
| 142 | |
| 143 | return err == exc; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | int |
| 148 | PyErr_ExceptionMatches(exc) |
| 149 | PyObject *exc; |
| 150 | { |
| 151 | return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /* Used in many places to normalize a raised exception, including in |
| 156 | eval_code2(), do_raise(), and PyErr_Print() |
| 157 | */ |
| 158 | void |
| 159 | PyErr_NormalizeException(exc, val, tb) |
| 160 | PyObject **exc; |
| 161 | PyObject **val; |
| 162 | PyObject **tb; |
| 163 | { |
| 164 | PyObject *type = *exc; |
| 165 | PyObject *value = *val; |
| 166 | PyObject *inclass = NULL; |
| 167 | |
| 168 | /* If PyErr_SetNone() was used, the value will have been actually |
| 169 | set to NULL. |
| 170 | */ |
| 171 | if (!value) { |
| 172 | value = Py_None; |
| 173 | Py_INCREF(value); |
| 174 | } |
| 175 | |
| 176 | if (PyInstance_Check(value)) |
| 177 | inclass = (PyObject*)((PyInstanceObject*)value)->in_class; |
| 178 | |
| 179 | /* Normalize the exception so that if the type is a class, the |
| 180 | value will be an instance. |
| 181 | */ |
| 182 | if (PyClass_Check(type)) { |
| 183 | /* if the value was not an instance, or is not an instance |
| 184 | whose class is (or is derived from) type, then use the |
| 185 | value as an argument to instantiation of the type |
| 186 | class. |
| 187 | */ |
| 188 | if (!inclass || !PyClass_IsSubclass(inclass, type)) { |
| 189 | PyObject *args, *res; |
| 190 | |
| 191 | if (value == Py_None) |
| 192 | args = Py_BuildValue("()"); |
| 193 | else if (PyTuple_Check(value)) { |
| 194 | Py_INCREF(value); |
| 195 | args = value; |
| 196 | } |
| 197 | else |
| 198 | args = Py_BuildValue("(O)", value); |
| 199 | |
| 200 | if (args == NULL) |
| 201 | goto finally; |
| 202 | res = PyEval_CallObject(type, args); |
| 203 | Py_DECREF(args); |
| 204 | if (res == NULL) |
| 205 | goto finally; |
| 206 | Py_DECREF(value); |
| 207 | value = res; |
| 208 | } |
Barry Warsaw | 3a74993 | 1997-09-30 15:00:18 +0000 | [diff] [blame] | 209 | /* if the class of the instance doesn't exactly match the |
| 210 | class of the type, believe the instance |
| 211 | */ |
| 212 | else if (inclass != type) { |
| 213 | Py_DECREF(type); |
| 214 | type = inclass; |
| 215 | Py_INCREF(type); |
| 216 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 217 | } |
| 218 | *exc = type; |
| 219 | *val = value; |
| 220 | return; |
| 221 | finally: |
Guido van Rossum | 19b55f2 | 1997-12-09 14:11:39 +0000 | [diff] [blame] | 222 | Py_DECREF(type); |
| 223 | Py_DECREF(value); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 224 | Py_XDECREF(*tb); |
| 225 | PyErr_Fetch(exc, val, tb); |
| 226 | /* normalize recursively */ |
| 227 | PyErr_NormalizeException(exc, val, tb); |
| 228 | } |
| 229 | |
| 230 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 231 | void |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 232 | PyErr_Fetch(p_type, p_value, p_traceback) |
| 233 | PyObject **p_type; |
| 234 | PyObject **p_value; |
| 235 | PyObject **p_traceback; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 236 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 237 | PyThreadState *tstate = PyThreadState_Get(); |
| 238 | |
| 239 | *p_type = tstate->curexc_type; |
| 240 | *p_value = tstate->curexc_value; |
| 241 | *p_traceback = tstate->curexc_traceback; |
| 242 | |
| 243 | tstate->curexc_type = NULL; |
| 244 | tstate->curexc_value = NULL; |
| 245 | tstate->curexc_traceback = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 249 | PyErr_Clear() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 250 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 251 | PyErr_Restore(NULL, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 252 | } |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 253 | |
| 254 | /* Convenience functions to set a type error exception and return 0 */ |
| 255 | |
| 256 | int |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 257 | PyErr_BadArgument() |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 258 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 259 | PyErr_SetString(PyExc_TypeError, |
| 260 | "illegal argument type for built-in operation"); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 264 | PyObject * |
| 265 | PyErr_NoMemory() |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 266 | { |
Barry Warsaw | 2d8adff | 1997-08-29 21:54:35 +0000 | [diff] [blame] | 267 | /* raise the pre-allocated instance if it still exists */ |
| 268 | if (PyExc_MemoryErrorInst) |
| 269 | PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst); |
| 270 | else |
| 271 | /* this will probably fail since there's no memory and hee, |
| 272 | hee, we have to instantiate this class |
| 273 | */ |
| 274 | PyErr_SetNone(PyExc_MemoryError); |
| 275 | |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 276 | return NULL; |
| 277 | } |
| 278 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 279 | PyObject * |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 280 | PyErr_SetFromErrnoWithFilename(exc, filename) |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 281 | PyObject *exc; |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 282 | char *filename; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 283 | { |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 284 | PyObject *v; |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 285 | char *s; |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 286 | int i = errno; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 287 | #ifdef EINTR |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 288 | if (i == EINTR && PyErr_CheckSignals()) |
Guido van Rossum | 5063bab | 1991-10-20 20:14:56 +0000 | [diff] [blame] | 289 | return NULL; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 290 | #endif |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 291 | if (i == 0) |
| 292 | s = "Error"; /* Sometimes errno didn't get set */ |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 293 | else |
Guido van Rossum | e0e5982 | 1998-10-14 20:38:13 +0000 | [diff] [blame] | 294 | s = strerror(i); |
| 295 | if (filename != NULL && Py_UseClassExceptionsFlag) |
| 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 | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 303 | return NULL; |
| 304 | } |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 305 | |
| 306 | |
| 307 | PyObject * |
| 308 | PyErr_SetFromErrno(exc) |
| 309 | PyObject *exc; |
| 310 | { |
| 311 | return PyErr_SetFromErrnoWithFilename(exc, NULL); |
| 312 | } |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 313 | |
| 314 | void |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 315 | PyErr_BadInternalCall() |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 316 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 317 | PyErr_SetString(PyExc_SystemError, |
| 318 | "bad argument to internal function"); |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 319 | } |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 320 | |
| 321 | |
| 322 | #ifdef HAVE_STDARG_PROTOTYPES |
| 323 | PyObject * |
| 324 | PyErr_Format(PyObject *exception, const char *format, ...) |
| 325 | #else |
| 326 | PyObject * |
| 327 | PyErr_Format(exception, format, va_alist) |
| 328 | PyObject *exception; |
| 329 | const char *format; |
| 330 | va_dcl |
| 331 | #endif |
| 332 | { |
| 333 | va_list vargs; |
| 334 | char buffer[500]; /* Caller is responsible for limiting the format */ |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 335 | |
| 336 | #ifdef HAVE_STDARG_PROTOTYPES |
| 337 | va_start(vargs, format); |
| 338 | #else |
| 339 | va_start(vargs); |
| 340 | #endif |
| 341 | |
| 342 | vsprintf(buffer, format, vargs); |
| 343 | PyErr_SetString(exception, buffer); |
| 344 | return NULL; |
| 345 | } |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 346 | |
| 347 | |
| 348 | PyObject * |
| 349 | PyErr_NewException(name, base, dict) |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 350 | char *name; /* modulename.classname */ |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 351 | PyObject *base; |
| 352 | PyObject *dict; |
| 353 | { |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 354 | char *dot; |
| 355 | PyObject *modulename = NULL; |
| 356 | PyObject *classname = NULL; |
| 357 | PyObject *mydict = NULL; |
| 358 | PyObject *bases = NULL; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 359 | PyObject *result = NULL; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 360 | dot = strrchr(name, '.'); |
| 361 | if (dot == NULL) { |
| 362 | PyErr_SetString(PyExc_SystemError, |
| 363 | "PyErr_NewException: name must be module.class"); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 364 | return NULL; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 365 | } |
| 366 | if (base == NULL) |
| 367 | base = PyExc_Exception; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 368 | if (!PyClass_Check(base)) { |
| 369 | /* Must be using string-based standard exceptions (-X) */ |
| 370 | return PyString_FromString(name); |
| 371 | } |
| 372 | if (dict == NULL) { |
| 373 | dict = mydict = PyDict_New(); |
| 374 | if (dict == NULL) |
| 375 | goto failure; |
| 376 | } |
| 377 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 378 | modulename = PyString_FromStringAndSize(name, (int)(dot-name)); |
| 379 | if (modulename == NULL) |
| 380 | goto failure; |
| 381 | if (PyDict_SetItemString(dict, "__module__", modulename) != 0) |
| 382 | goto failure; |
| 383 | } |
| 384 | classname = PyString_FromString(dot+1); |
| 385 | if (classname == NULL) |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 386 | goto failure; |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 387 | bases = Py_BuildValue("(O)", base); |
| 388 | if (bases == NULL) |
| 389 | goto failure; |
| 390 | result = PyClass_New(bases, dict, classname); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 391 | failure: |
Guido van Rossum | 2ac650f | 1997-10-03 19:50:55 +0000 | [diff] [blame] | 392 | Py_XDECREF(bases); |
| 393 | Py_XDECREF(mydict); |
| 394 | Py_XDECREF(classname); |
| 395 | Py_XDECREF(modulename); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 396 | return result; |
| 397 | } |