Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1 | /* This module provides the suite of standard class-based exceptions for |
| 2 | * Python's builtin module. This is a complete C implementation of what, |
| 3 | * in Python 1.5.2, was contained in the exceptions.py module. The problem |
| 4 | * there was that if exceptions.py could not be imported for some reason, |
| 5 | * the entire interpreter would abort. |
| 6 | * |
| 7 | * By moving the exceptions into C and statically linking, we can guarantee |
| 8 | * that the standard exceptions will always be available. |
| 9 | * |
| 10 | * history: |
| 11 | * 98-08-19 fl created (for pyexe) |
| 12 | * 00-02-08 fl updated for 1.5.2 |
| 13 | * 26-May-2000 baw vetted for Python 1.6 |
| 14 | * |
| 15 | * written by Fredrik Lundh |
| 16 | * modifications, additions, cleanups, and proofreading by Barry Warsaw |
| 17 | * |
| 18 | * Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved. |
| 19 | */ |
| 20 | |
| 21 | #include "Python.h" |
Fred Drake | 185a29b | 2000-08-15 16:20:36 +0000 | [diff] [blame] | 22 | #include "osdefs.h" |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 23 | |
Tim Peters | bf26e07 | 2000-07-12 04:02:10 +0000 | [diff] [blame] | 24 | /* Caution: MS Visual C++ 6 errors if a single string literal exceeds |
| 25 | * 2Kb. So the module docstring has been broken roughly in half, using |
| 26 | * compile-time literal concatenation. |
| 27 | */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 28 | static char |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 29 | module__doc__[] = |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 30 | "Python's standard exception class hierarchy.\n\ |
| 31 | \n\ |
| 32 | Before Python 1.5, the standard exceptions were all simple string objects.\n\ |
| 33 | In Python 1.5, the standard exceptions were converted to classes organized\n\ |
| 34 | into a relatively flat hierarchy. String-based standard exceptions were\n\ |
| 35 | optional, or used as a fallback if some problem occurred while importing\n\ |
| 36 | the exception module. With Python 1.6, optional string-based standard\n\ |
| 37 | exceptions were removed (along with the -X command line flag).\n\ |
| 38 | \n\ |
| 39 | The class exceptions were implemented in such a way as to be almost\n\ |
| 40 | completely backward compatible. Some tricky uses of IOError could\n\ |
| 41 | potentially have broken, but by Python 1.6, all of these should have\n\ |
| 42 | been fixed. As of Python 1.6, the class-based standard exceptions are\n\ |
| 43 | now implemented in C, and are guaranteed to exist in the Python\n\ |
| 44 | interpreter.\n\ |
| 45 | \n\ |
| 46 | Here is a rundown of the class hierarchy. The classes found here are\n\ |
| 47 | inserted into both the exceptions module and the `built-in' module. It is\n\ |
| 48 | recommended that user defined class based exceptions be derived from the\n\ |
Tim Peters | bf26e07 | 2000-07-12 04:02:10 +0000 | [diff] [blame] | 49 | `Exception' class, although this is currently not enforced.\n" |
| 50 | /* keep string pieces "small" */ |
| 51 | "\n\ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 52 | Exception\n\ |
| 53 | |\n\ |
| 54 | +-- SystemExit\n\ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 55 | +-- StopIteration\n\ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 56 | +-- StandardError\n\ |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 57 | | |\n\ |
| 58 | | +-- KeyboardInterrupt\n\ |
| 59 | | +-- ImportError\n\ |
| 60 | | +-- EnvironmentError\n\ |
| 61 | | | |\n\ |
| 62 | | | +-- IOError\n\ |
| 63 | | | +-- OSError\n\ |
| 64 | | | |\n\ |
| 65 | | | +-- WindowsError\n\ |
| 66 | | |\n\ |
| 67 | | +-- EOFError\n\ |
| 68 | | +-- RuntimeError\n\ |
| 69 | | | |\n\ |
| 70 | | | +-- NotImplementedError\n\ |
| 71 | | |\n\ |
| 72 | | +-- NameError\n\ |
| 73 | | | |\n\ |
| 74 | | | +-- UnboundLocalError\n\ |
| 75 | | |\n\ |
| 76 | | +-- AttributeError\n\ |
| 77 | | +-- SyntaxError\n\ |
| 78 | | | |\n\ |
| 79 | | | +-- IndentationError\n\ |
| 80 | | | |\n\ |
| 81 | | | +-- TabError\n\ |
| 82 | | |\n\ |
| 83 | | +-- TypeError\n\ |
| 84 | | +-- AssertionError\n\ |
| 85 | | +-- LookupError\n\ |
| 86 | | | |\n\ |
| 87 | | | +-- IndexError\n\ |
| 88 | | | +-- KeyError\n\ |
| 89 | | |\n\ |
| 90 | | +-- ArithmeticError\n\ |
| 91 | | | |\n\ |
| 92 | | | +-- OverflowError\n\ |
| 93 | | | +-- ZeroDivisionError\n\ |
| 94 | | | +-- FloatingPointError\n\ |
| 95 | | |\n\ |
| 96 | | +-- ValueError\n\ |
| 97 | | | |\n\ |
| 98 | | | +-- UnicodeError\n\ |
| 99 | | |\n\ |
| 100 | | +-- SystemError\n\ |
| 101 | | +-- MemoryError\n\ |
| 102 | |\n\ |
| 103 | +---Warning\n\ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 104 | |\n\ |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 105 | +-- UserWarning\n\ |
| 106 | +-- DeprecationWarning\n\ |
| 107 | +-- SyntaxWarning\n\ |
Guido van Rossum | ae347b3 | 2001-08-23 02:56:07 +0000 | [diff] [blame^] | 108 | +-- OverflowWarning\n\ |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 109 | +-- RuntimeWarning"; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | /* Helper function for populating a dictionary with method wrappers. */ |
| 113 | static int |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 114 | populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 115 | { |
| 116 | if (!methods) |
| 117 | return 0; |
| 118 | |
| 119 | while (methods->ml_name) { |
| 120 | /* get a wrapper for the built-in function */ |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 121 | PyObject *func = PyCFunction_New(methods, NULL); |
| 122 | PyObject *meth; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 123 | int status; |
| 124 | |
| 125 | if (!func) |
| 126 | return -1; |
| 127 | |
| 128 | /* turn the function into an unbound method */ |
| 129 | if (!(meth = PyMethod_New(func, NULL, klass))) { |
| 130 | Py_DECREF(func); |
| 131 | return -1; |
| 132 | } |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 133 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 134 | /* add method to dictionary */ |
| 135 | status = PyDict_SetItemString(dict, methods->ml_name, meth); |
| 136 | Py_DECREF(meth); |
| 137 | Py_DECREF(func); |
| 138 | |
| 139 | /* stop now if an error occurred, otherwise do the next method */ |
| 140 | if (status) |
| 141 | return status; |
| 142 | |
| 143 | methods++; |
| 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 148 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 149 | |
| 150 | /* This function is used to create all subsequent exception classes. */ |
| 151 | static int |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 152 | make_class(PyObject **klass, PyObject *base, |
| 153 | char *name, PyMethodDef *methods, |
| 154 | char *docstr) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 155 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 156 | PyObject *dict = PyDict_New(); |
| 157 | PyObject *str = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 158 | int status = -1; |
| 159 | |
| 160 | if (!dict) |
| 161 | return -1; |
| 162 | |
| 163 | /* If an error occurs from here on, goto finally instead of explicitly |
| 164 | * returning NULL. |
| 165 | */ |
| 166 | |
| 167 | if (docstr) { |
| 168 | if (!(str = PyString_FromString(docstr))) |
| 169 | goto finally; |
| 170 | if (PyDict_SetItemString(dict, "__doc__", str)) |
| 171 | goto finally; |
| 172 | } |
| 173 | |
| 174 | if (!(*klass = PyErr_NewException(name, base, dict))) |
| 175 | goto finally; |
| 176 | |
| 177 | if (populate_methods(*klass, dict, methods)) { |
| 178 | Py_DECREF(*klass); |
| 179 | *klass = NULL; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 180 | goto finally; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | status = 0; |
| 184 | |
| 185 | finally: |
| 186 | Py_XDECREF(dict); |
| 187 | Py_XDECREF(str); |
| 188 | return status; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */ |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 193 | static PyObject * |
| 194 | get_self(PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 195 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 196 | PyObject *self = PyTuple_GetItem(args, 0); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 197 | if (!self) { |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 198 | /* Watch out for being called to early in the bootstrapping process */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 199 | if (PyExc_TypeError) { |
| 200 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 201 | "unbound method must be called with instance as first argument"); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 202 | } |
| 203 | return NULL; |
| 204 | } |
| 205 | return self; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | /* Notes on bootstrapping the exception classes. |
| 211 | * |
| 212 | * First thing we create is the base class for all exceptions, called |
| 213 | * appropriately enough: Exception. Creation of this class makes no |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 214 | * assumptions about the existence of any other exception class -- except |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 215 | * for TypeError, which can conditionally exist. |
| 216 | * |
| 217 | * Next, StandardError is created (which is quite simple) followed by |
| 218 | * TypeError, because the instantiation of other exceptions can potentially |
| 219 | * throw a TypeError. Once these exceptions are created, all the others |
| 220 | * can be created in any order. See the static exctable below for the |
| 221 | * explicit bootstrap order. |
| 222 | * |
| 223 | * All classes after Exception can be created using PyErr_NewException(). |
| 224 | */ |
| 225 | |
| 226 | static char |
| 227 | Exception__doc__[] = "Common base class for all exceptions."; |
| 228 | |
| 229 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 230 | static PyObject * |
| 231 | Exception__init__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 232 | { |
| 233 | int status; |
| 234 | |
| 235 | if (!(self = get_self(args))) |
| 236 | return NULL; |
| 237 | |
| 238 | /* set args attribute */ |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 239 | /* XXX size is only a hint */ |
| 240 | args = PySequence_GetSlice(args, 1, PySequence_Size(args)); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 241 | if (!args) |
| 242 | return NULL; |
| 243 | status = PyObject_SetAttrString(self, "args", args); |
| 244 | Py_DECREF(args); |
| 245 | if (status < 0) |
| 246 | return NULL; |
| 247 | |
| 248 | Py_INCREF(Py_None); |
| 249 | return Py_None; |
| 250 | } |
| 251 | |
| 252 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 253 | static PyObject * |
| 254 | Exception__str__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 255 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 256 | PyObject *out; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 257 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 258 | if (!PyArg_ParseTuple(args, "O:__str__", &self)) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 259 | return NULL; |
| 260 | |
| 261 | args = PyObject_GetAttrString(self, "args"); |
| 262 | if (!args) |
| 263 | return NULL; |
| 264 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 265 | switch (PySequence_Size(args)) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 266 | case 0: |
| 267 | out = PyString_FromString(""); |
| 268 | break; |
| 269 | case 1: |
Barry Warsaw | b781655 | 2000-07-09 22:27:10 +0000 | [diff] [blame] | 270 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 271 | PyObject *tmp = PySequence_GetItem(args, 0); |
Barry Warsaw | b781655 | 2000-07-09 22:27:10 +0000 | [diff] [blame] | 272 | if (tmp) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 273 | out = PyObject_Str(tmp); |
Barry Warsaw | b781655 | 2000-07-09 22:27:10 +0000 | [diff] [blame] | 274 | Py_DECREF(tmp); |
| 275 | } |
| 276 | else |
| 277 | out = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 278 | break; |
Barry Warsaw | b781655 | 2000-07-09 22:27:10 +0000 | [diff] [blame] | 279 | } |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 280 | default: |
| 281 | out = PyObject_Str(args); |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | Py_DECREF(args); |
| 286 | return out; |
| 287 | } |
| 288 | |
| 289 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 290 | static PyObject * |
| 291 | Exception__getitem__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 292 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 293 | PyObject *out; |
| 294 | PyObject *index; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 295 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 296 | if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index)) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 297 | return NULL; |
| 298 | |
| 299 | args = PyObject_GetAttrString(self, "args"); |
| 300 | if (!args) |
| 301 | return NULL; |
| 302 | |
| 303 | out = PyObject_GetItem(args, index); |
| 304 | Py_DECREF(args); |
| 305 | return out; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | static PyMethodDef |
| 310 | Exception_methods[] = { |
| 311 | /* methods for the Exception class */ |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 312 | { "__getitem__", Exception__getitem__, METH_VARARGS}, |
| 313 | { "__str__", Exception__str__, METH_VARARGS}, |
| 314 | { "__init__", Exception__init__, METH_VARARGS}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 315 | { NULL, NULL } |
| 316 | }; |
| 317 | |
| 318 | |
| 319 | static int |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 320 | make_Exception(char *modulename) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 321 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 322 | PyObject *dict = PyDict_New(); |
| 323 | PyObject *str = NULL; |
| 324 | PyObject *name = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 325 | int status = -1; |
| 326 | |
| 327 | if (!dict) |
| 328 | return -1; |
| 329 | |
| 330 | /* If an error occurs from here on, goto finally instead of explicitly |
| 331 | * returning NULL. |
| 332 | */ |
| 333 | |
| 334 | if (!(str = PyString_FromString(modulename))) |
| 335 | goto finally; |
| 336 | if (PyDict_SetItemString(dict, "__module__", str)) |
| 337 | goto finally; |
| 338 | Py_DECREF(str); |
| 339 | if (!(str = PyString_FromString(Exception__doc__))) |
| 340 | goto finally; |
| 341 | if (PyDict_SetItemString(dict, "__doc__", str)) |
| 342 | goto finally; |
| 343 | |
| 344 | if (!(name = PyString_FromString("Exception"))) |
| 345 | goto finally; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 346 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 347 | if (!(PyExc_Exception = PyClass_New(NULL, dict, name))) |
| 348 | goto finally; |
| 349 | |
| 350 | /* Now populate the dictionary with the method suite */ |
| 351 | if (populate_methods(PyExc_Exception, dict, Exception_methods)) |
| 352 | /* Don't need to reclaim PyExc_Exception here because that'll |
| 353 | * happen during interpreter shutdown. |
| 354 | */ |
| 355 | goto finally; |
| 356 | |
| 357 | status = 0; |
| 358 | |
| 359 | finally: |
| 360 | Py_XDECREF(dict); |
| 361 | Py_XDECREF(str); |
| 362 | Py_XDECREF(name); |
| 363 | return status; |
| 364 | } |
| 365 | |
| 366 | |
| 367 | |
| 368 | static char |
| 369 | StandardError__doc__[] = "Base class for all standard Python exceptions."; |
| 370 | |
| 371 | static char |
| 372 | TypeError__doc__[] = "Inappropriate argument type."; |
| 373 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 374 | static char |
| 375 | StopIteration__doc__[] = "Signal the end from iterator.next()."; |
| 376 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 377 | |
| 378 | |
| 379 | static char |
| 380 | SystemExit__doc__[] = "Request to exit from the interpreter."; |
| 381 | |
| 382 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 383 | static PyObject * |
| 384 | SystemExit__init__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 385 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 386 | PyObject *code; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 387 | int status; |
| 388 | |
| 389 | if (!(self = get_self(args))) |
| 390 | return NULL; |
| 391 | |
| 392 | /* Set args attribute. */ |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 393 | if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args)))) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 394 | return NULL; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 395 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 396 | status = PyObject_SetAttrString(self, "args", args); |
| 397 | if (status < 0) { |
| 398 | Py_DECREF(args); |
| 399 | return NULL; |
| 400 | } |
| 401 | |
| 402 | /* set code attribute */ |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 403 | switch (PySequence_Size(args)) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 404 | case 0: |
| 405 | Py_INCREF(Py_None); |
| 406 | code = Py_None; |
| 407 | break; |
| 408 | case 1: |
| 409 | code = PySequence_GetItem(args, 0); |
| 410 | break; |
| 411 | default: |
| 412 | Py_INCREF(args); |
| 413 | code = args; |
| 414 | break; |
| 415 | } |
| 416 | |
| 417 | status = PyObject_SetAttrString(self, "code", code); |
| 418 | Py_DECREF(code); |
| 419 | Py_DECREF(args); |
| 420 | if (status < 0) |
| 421 | return NULL; |
| 422 | |
| 423 | Py_INCREF(Py_None); |
| 424 | return Py_None; |
| 425 | } |
| 426 | |
| 427 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 428 | static PyMethodDef SystemExit_methods[] = { |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 429 | { "__init__", SystemExit__init__, METH_VARARGS}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 430 | {NULL, NULL} |
| 431 | }; |
| 432 | |
| 433 | |
| 434 | |
| 435 | static char |
| 436 | KeyboardInterrupt__doc__[] = "Program interrupted by user."; |
| 437 | |
| 438 | static char |
| 439 | ImportError__doc__[] = |
| 440 | "Import can't find module, or can't find name in module."; |
| 441 | |
| 442 | |
| 443 | |
| 444 | static char |
| 445 | EnvironmentError__doc__[] = "Base class for I/O related errors."; |
| 446 | |
| 447 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 448 | static PyObject * |
| 449 | EnvironmentError__init__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 450 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 451 | PyObject *item0 = NULL; |
| 452 | PyObject *item1 = NULL; |
| 453 | PyObject *item2 = NULL; |
| 454 | PyObject *subslice = NULL; |
| 455 | PyObject *rtnval = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 456 | |
| 457 | if (!(self = get_self(args))) |
| 458 | return NULL; |
| 459 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 460 | if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args)))) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 461 | return NULL; |
| 462 | |
| 463 | if (PyObject_SetAttrString(self, "args", args) || |
| 464 | PyObject_SetAttrString(self, "errno", Py_None) || |
| 465 | PyObject_SetAttrString(self, "strerror", Py_None) || |
| 466 | PyObject_SetAttrString(self, "filename", Py_None)) |
| 467 | { |
| 468 | goto finally; |
| 469 | } |
| 470 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 471 | switch (PySequence_Size(args)) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 472 | case 3: |
Barry Warsaw | 7dfeb42 | 2000-07-09 04:56:25 +0000 | [diff] [blame] | 473 | /* Where a function has a single filename, such as open() or some |
| 474 | * of the os module functions, PyErr_SetFromErrnoWithFilename() is |
| 475 | * called, giving a third argument which is the filename. But, so |
| 476 | * that old code using in-place unpacking doesn't break, e.g.: |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 477 | * |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 478 | * except IOError, (errno, strerror): |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 479 | * |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 480 | * we hack args so that it only contains two items. This also |
| 481 | * means we need our own __str__() which prints out the filename |
| 482 | * when it was supplied. |
| 483 | */ |
| 484 | item0 = PySequence_GetItem(args, 0); |
| 485 | item1 = PySequence_GetItem(args, 1); |
| 486 | item2 = PySequence_GetItem(args, 2); |
| 487 | if (!item0 || !item1 || !item2) |
| 488 | goto finally; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 489 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 490 | if (PyObject_SetAttrString(self, "errno", item0) || |
| 491 | PyObject_SetAttrString(self, "strerror", item1) || |
| 492 | PyObject_SetAttrString(self, "filename", item2)) |
| 493 | { |
| 494 | goto finally; |
| 495 | } |
| 496 | |
| 497 | subslice = PySequence_GetSlice(args, 0, 2); |
| 498 | if (!subslice || PyObject_SetAttrString(self, "args", subslice)) |
| 499 | goto finally; |
Barry Warsaw | 7dfeb42 | 2000-07-09 04:56:25 +0000 | [diff] [blame] | 500 | break; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 501 | |
| 502 | case 2: |
Barry Warsaw | 7dfeb42 | 2000-07-09 04:56:25 +0000 | [diff] [blame] | 503 | /* Used when PyErr_SetFromErrno() is called and no filename |
| 504 | * argument is given. |
| 505 | */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 506 | item0 = PySequence_GetItem(args, 0); |
| 507 | item1 = PySequence_GetItem(args, 1); |
| 508 | if (!item0 || !item1) |
| 509 | goto finally; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 510 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 511 | if (PyObject_SetAttrString(self, "errno", item0) || |
| 512 | PyObject_SetAttrString(self, "strerror", item1)) |
| 513 | { |
| 514 | goto finally; |
| 515 | } |
Barry Warsaw | 7dfeb42 | 2000-07-09 04:56:25 +0000 | [diff] [blame] | 516 | break; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | Py_INCREF(Py_None); |
| 520 | rtnval = Py_None; |
| 521 | |
| 522 | finally: |
| 523 | Py_DECREF(args); |
| 524 | Py_XDECREF(item0); |
| 525 | Py_XDECREF(item1); |
| 526 | Py_XDECREF(item2); |
| 527 | Py_XDECREF(subslice); |
| 528 | return rtnval; |
| 529 | } |
| 530 | |
| 531 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 532 | static PyObject * |
| 533 | EnvironmentError__str__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 534 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 535 | PyObject *originalself = self; |
| 536 | PyObject *filename; |
| 537 | PyObject *serrno; |
| 538 | PyObject *strerror; |
| 539 | PyObject *rtnval = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 540 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 541 | if (!PyArg_ParseTuple(args, "O:__str__", &self)) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 542 | return NULL; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 543 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 544 | filename = PyObject_GetAttrString(self, "filename"); |
| 545 | serrno = PyObject_GetAttrString(self, "errno"); |
| 546 | strerror = PyObject_GetAttrString(self, "strerror"); |
| 547 | if (!filename || !serrno || !strerror) |
| 548 | goto finally; |
| 549 | |
| 550 | if (filename != Py_None) { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 551 | PyObject *fmt = PyString_FromString("[Errno %s] %s: %s"); |
| 552 | PyObject *repr = PyObject_Repr(filename); |
| 553 | PyObject *tuple = PyTuple_New(3); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 554 | |
| 555 | if (!fmt || !repr || !tuple) { |
| 556 | Py_XDECREF(fmt); |
| 557 | Py_XDECREF(repr); |
| 558 | Py_XDECREF(tuple); |
| 559 | goto finally; |
| 560 | } |
| 561 | |
| 562 | PyTuple_SET_ITEM(tuple, 0, serrno); |
| 563 | PyTuple_SET_ITEM(tuple, 1, strerror); |
| 564 | PyTuple_SET_ITEM(tuple, 2, repr); |
| 565 | |
| 566 | rtnval = PyString_Format(fmt, tuple); |
| 567 | |
| 568 | Py_DECREF(fmt); |
| 569 | Py_DECREF(tuple); |
| 570 | /* already freed because tuple owned only reference */ |
| 571 | serrno = NULL; |
| 572 | strerror = NULL; |
| 573 | } |
| 574 | else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 575 | PyObject *fmt = PyString_FromString("[Errno %s] %s"); |
| 576 | PyObject *tuple = PyTuple_New(2); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 577 | |
| 578 | if (!fmt || !tuple) { |
| 579 | Py_XDECREF(fmt); |
| 580 | Py_XDECREF(tuple); |
| 581 | goto finally; |
| 582 | } |
| 583 | |
| 584 | PyTuple_SET_ITEM(tuple, 0, serrno); |
| 585 | PyTuple_SET_ITEM(tuple, 1, strerror); |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 586 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 587 | rtnval = PyString_Format(fmt, tuple); |
| 588 | |
| 589 | Py_DECREF(fmt); |
| 590 | Py_DECREF(tuple); |
| 591 | /* already freed because tuple owned only reference */ |
| 592 | serrno = NULL; |
| 593 | strerror = NULL; |
| 594 | } |
| 595 | else |
| 596 | /* The original Python code said: |
| 597 | * |
| 598 | * return StandardError.__str__(self) |
| 599 | * |
| 600 | * but there is no StandardError__str__() function; we happen to |
| 601 | * know that's just a pass through to Exception__str__(). |
| 602 | */ |
| 603 | rtnval = Exception__str__(originalself, args); |
| 604 | |
| 605 | finally: |
| 606 | Py_XDECREF(filename); |
| 607 | Py_XDECREF(serrno); |
| 608 | Py_XDECREF(strerror); |
| 609 | return rtnval; |
| 610 | } |
| 611 | |
| 612 | |
| 613 | static |
| 614 | PyMethodDef EnvironmentError_methods[] = { |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 615 | {"__init__", EnvironmentError__init__, METH_VARARGS}, |
| 616 | {"__str__", EnvironmentError__str__, METH_VARARGS}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 617 | {NULL, NULL} |
| 618 | }; |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | static char |
| 624 | IOError__doc__[] = "I/O operation failed."; |
| 625 | |
| 626 | static char |
| 627 | OSError__doc__[] = "OS system call failed."; |
| 628 | |
| 629 | #ifdef MS_WINDOWS |
| 630 | static char |
| 631 | WindowsError__doc__[] = "MS-Windows OS system call failed."; |
| 632 | #endif /* MS_WINDOWS */ |
| 633 | |
| 634 | static char |
| 635 | EOFError__doc__[] = "Read beyond end of file."; |
| 636 | |
| 637 | static char |
| 638 | RuntimeError__doc__[] = "Unspecified run-time error."; |
| 639 | |
| 640 | static char |
| 641 | NotImplementedError__doc__[] = |
| 642 | "Method or function hasn't been implemented yet."; |
| 643 | |
| 644 | static char |
| 645 | NameError__doc__[] = "Name not found globally."; |
| 646 | |
| 647 | static char |
| 648 | UnboundLocalError__doc__[] = |
| 649 | "Local name referenced but not bound to a value."; |
| 650 | |
| 651 | static char |
| 652 | AttributeError__doc__[] = "Attribute not found."; |
| 653 | |
| 654 | |
| 655 | |
| 656 | static char |
| 657 | SyntaxError__doc__[] = "Invalid syntax."; |
| 658 | |
| 659 | |
| 660 | static int |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 661 | SyntaxError__classinit__(PyObject *klass) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 662 | { |
Barry Warsaw | 87bec35 | 2000-08-18 05:05:37 +0000 | [diff] [blame] | 663 | int retval = 0; |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 664 | PyObject *emptystring = PyString_FromString(""); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 665 | |
| 666 | /* Additional class-creation time initializations */ |
| 667 | if (!emptystring || |
| 668 | PyObject_SetAttrString(klass, "msg", emptystring) || |
| 669 | PyObject_SetAttrString(klass, "filename", Py_None) || |
| 670 | PyObject_SetAttrString(klass, "lineno", Py_None) || |
| 671 | PyObject_SetAttrString(klass, "offset", Py_None) || |
| 672 | PyObject_SetAttrString(klass, "text", Py_None)) |
| 673 | { |
Barry Warsaw | 87bec35 | 2000-08-18 05:05:37 +0000 | [diff] [blame] | 674 | retval = -1; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 675 | } |
Barry Warsaw | 87bec35 | 2000-08-18 05:05:37 +0000 | [diff] [blame] | 676 | Py_XDECREF(emptystring); |
| 677 | return retval; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 681 | static PyObject * |
| 682 | SyntaxError__init__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 683 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 684 | PyObject *rtnval = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 685 | int lenargs; |
| 686 | |
| 687 | if (!(self = get_self(args))) |
| 688 | return NULL; |
| 689 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 690 | if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args)))) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 691 | return NULL; |
| 692 | |
| 693 | if (PyObject_SetAttrString(self, "args", args)) |
| 694 | goto finally; |
| 695 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 696 | lenargs = PySequence_Size(args); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 697 | if (lenargs >= 1) { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 698 | PyObject *item0 = PySequence_GetItem(args, 0); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 699 | int status; |
| 700 | |
| 701 | if (!item0) |
| 702 | goto finally; |
| 703 | status = PyObject_SetAttrString(self, "msg", item0); |
| 704 | Py_DECREF(item0); |
| 705 | if (status) |
| 706 | goto finally; |
| 707 | } |
| 708 | if (lenargs == 2) { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 709 | PyObject *info = PySequence_GetItem(args, 1); |
Fred Drake | 9da7f3b | 2001-02-28 21:52:10 +0000 | [diff] [blame] | 710 | PyObject *filename = NULL, *lineno = NULL; |
| 711 | PyObject *offset = NULL, *text = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 712 | int status = 1; |
| 713 | |
| 714 | if (!info) |
| 715 | goto finally; |
| 716 | |
| 717 | filename = PySequence_GetItem(info, 0); |
Fred Drake | 9da7f3b | 2001-02-28 21:52:10 +0000 | [diff] [blame] | 718 | if (filename != NULL) { |
| 719 | lineno = PySequence_GetItem(info, 1); |
| 720 | if (lineno != NULL) { |
| 721 | offset = PySequence_GetItem(info, 2); |
| 722 | if (offset != NULL) { |
| 723 | text = PySequence_GetItem(info, 3); |
| 724 | if (text != NULL) { |
| 725 | status = |
| 726 | PyObject_SetAttrString(self, "filename", filename) |
| 727 | || PyObject_SetAttrString(self, "lineno", lineno) |
| 728 | || PyObject_SetAttrString(self, "offset", offset) |
| 729 | || PyObject_SetAttrString(self, "text", text); |
| 730 | Py_DECREF(text); |
| 731 | } |
| 732 | Py_DECREF(offset); |
| 733 | } |
| 734 | Py_DECREF(lineno); |
| 735 | } |
| 736 | Py_DECREF(filename); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 737 | } |
Fred Drake | 9da7f3b | 2001-02-28 21:52:10 +0000 | [diff] [blame] | 738 | Py_DECREF(info); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 739 | |
| 740 | if (status) |
| 741 | goto finally; |
| 742 | } |
| 743 | Py_INCREF(Py_None); |
| 744 | rtnval = Py_None; |
| 745 | |
| 746 | finally: |
| 747 | Py_DECREF(args); |
| 748 | return rtnval; |
| 749 | } |
| 750 | |
| 751 | |
Fred Drake | 185a29b | 2000-08-15 16:20:36 +0000 | [diff] [blame] | 752 | /* This is called "my_basename" instead of just "basename" to avoid name |
| 753 | conflicts with glibc; basename is already prototyped if _GNU_SOURCE is |
| 754 | defined, and Python does define that. */ |
| 755 | static char * |
| 756 | my_basename(char *name) |
| 757 | { |
| 758 | char *cp = name; |
| 759 | char *result = name; |
| 760 | |
| 761 | if (name == NULL) |
| 762 | return "???"; |
| 763 | while (*cp != '\0') { |
| 764 | if (*cp == SEP) |
| 765 | result = cp + 1; |
| 766 | ++cp; |
| 767 | } |
| 768 | return result; |
| 769 | } |
| 770 | |
| 771 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 772 | static PyObject * |
| 773 | SyntaxError__str__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 774 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 775 | PyObject *msg; |
| 776 | PyObject *str; |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 777 | PyObject *filename, *lineno, *result; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 778 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 779 | if (!PyArg_ParseTuple(args, "O:__str__", &self)) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 780 | return NULL; |
| 781 | |
| 782 | if (!(msg = PyObject_GetAttrString(self, "msg"))) |
| 783 | return NULL; |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 784 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 785 | str = PyObject_Str(msg); |
| 786 | Py_DECREF(msg); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 787 | result = str; |
| 788 | |
| 789 | /* XXX -- do all the additional formatting with filename and |
| 790 | lineno here */ |
| 791 | |
| 792 | if (PyString_Check(str)) { |
| 793 | int have_filename = 0; |
| 794 | int have_lineno = 0; |
| 795 | char *buffer = NULL; |
| 796 | |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 797 | if ((filename = PyObject_GetAttrString(self, "filename")) != NULL) |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 798 | have_filename = PyString_Check(filename); |
| 799 | else |
| 800 | PyErr_Clear(); |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 801 | |
| 802 | if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL) |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 803 | have_lineno = PyInt_Check(lineno); |
| 804 | else |
| 805 | PyErr_Clear(); |
| 806 | |
| 807 | if (have_filename || have_lineno) { |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 808 | int bufsize = PyString_GET_SIZE(str) + 64; |
| 809 | if (have_filename) |
| 810 | bufsize += PyString_GET_SIZE(filename); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 811 | |
| 812 | buffer = PyMem_Malloc(bufsize); |
| 813 | if (buffer != NULL) { |
| 814 | if (have_filename && have_lineno) |
Fred Drake | 04e654a | 2000-08-18 19:53:25 +0000 | [diff] [blame] | 815 | sprintf(buffer, "%s (%s, line %ld)", |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 816 | PyString_AS_STRING(str), |
Fred Drake | 185a29b | 2000-08-15 16:20:36 +0000 | [diff] [blame] | 817 | my_basename(PyString_AS_STRING(filename)), |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 818 | PyInt_AsLong(lineno)); |
| 819 | else if (have_filename) |
| 820 | sprintf(buffer, "%s (%s)", |
| 821 | PyString_AS_STRING(str), |
Fred Drake | 185a29b | 2000-08-15 16:20:36 +0000 | [diff] [blame] | 822 | my_basename(PyString_AS_STRING(filename))); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 823 | else if (have_lineno) |
Fred Drake | 04e654a | 2000-08-18 19:53:25 +0000 | [diff] [blame] | 824 | sprintf(buffer, "%s (line %ld)", |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 825 | PyString_AS_STRING(str), |
| 826 | PyInt_AsLong(lineno)); |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 827 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 828 | result = PyString_FromString(buffer); |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 829 | PyMem_FREE(buffer); |
| 830 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 831 | if (result == NULL) |
| 832 | result = str; |
| 833 | else |
| 834 | Py_DECREF(str); |
| 835 | } |
| 836 | } |
| 837 | Py_XDECREF(filename); |
| 838 | Py_XDECREF(lineno); |
| 839 | } |
| 840 | return result; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 844 | static PyMethodDef SyntaxError_methods[] = { |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 845 | {"__init__", SyntaxError__init__, METH_VARARGS}, |
| 846 | {"__str__", SyntaxError__str__, METH_VARARGS}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 847 | {NULL, NULL} |
| 848 | }; |
| 849 | |
| 850 | |
| 851 | |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 852 | /* Exception doc strings */ |
| 853 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 854 | static char |
| 855 | AssertionError__doc__[] = "Assertion failed."; |
| 856 | |
| 857 | static char |
| 858 | LookupError__doc__[] = "Base class for lookup errors."; |
| 859 | |
| 860 | static char |
| 861 | IndexError__doc__[] = "Sequence index out of range."; |
| 862 | |
| 863 | static char |
| 864 | KeyError__doc__[] = "Mapping key not found."; |
| 865 | |
| 866 | static char |
| 867 | ArithmeticError__doc__[] = "Base class for arithmetic errors."; |
| 868 | |
| 869 | static char |
| 870 | OverflowError__doc__[] = "Result too large to be represented."; |
| 871 | |
| 872 | static char |
| 873 | ZeroDivisionError__doc__[] = |
| 874 | "Second argument to a division or modulo operation was zero."; |
| 875 | |
| 876 | static char |
| 877 | FloatingPointError__doc__[] = "Floating point operation failed."; |
| 878 | |
| 879 | static char |
| 880 | ValueError__doc__[] = "Inappropriate argument value (of correct type)."; |
| 881 | |
| 882 | static char |
| 883 | UnicodeError__doc__[] = "Unicode related error."; |
| 884 | |
| 885 | static char |
| 886 | SystemError__doc__[] = "Internal error in the Python interpreter.\n\ |
| 887 | \n\ |
| 888 | Please report this to the Python maintainer, along with the traceback,\n\ |
| 889 | the Python version, and the hardware/OS platform and version."; |
| 890 | |
| 891 | static char |
| 892 | MemoryError__doc__[] = "Out of memory."; |
| 893 | |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 894 | static char |
| 895 | IndentationError__doc__[] = "Improper indentation."; |
| 896 | |
| 897 | static char |
| 898 | TabError__doc__[] = "Improper mixture of spaces and tabs."; |
| 899 | |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 900 | /* Warning category docstrings */ |
| 901 | |
| 902 | static char |
| 903 | Warning__doc__[] = "Base class for warning categories."; |
| 904 | |
| 905 | static char |
| 906 | UserWarning__doc__[] = "Base class for warnings generated by user code."; |
| 907 | |
| 908 | static char |
| 909 | DeprecationWarning__doc__[] = |
| 910 | "Base class for warnings about deprecated features."; |
| 911 | |
| 912 | static char |
| 913 | SyntaxWarning__doc__[] = "Base class for warnings about dubious syntax."; |
| 914 | |
| 915 | static char |
Guido van Rossum | ae347b3 | 2001-08-23 02:56:07 +0000 | [diff] [blame^] | 916 | OverflowWarning__doc__[] = "Base class for warnings about numeric overflow."; |
| 917 | |
| 918 | static char |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 919 | RuntimeWarning__doc__[] = |
| 920 | "Base class for warnings about dubious runtime behavior."; |
| 921 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 922 | |
| 923 | |
| 924 | /* module global functions */ |
| 925 | static PyMethodDef functions[] = { |
| 926 | /* Sentinel */ |
| 927 | {NULL, NULL} |
| 928 | }; |
| 929 | |
| 930 | |
| 931 | |
| 932 | /* Global C API defined exceptions */ |
| 933 | |
| 934 | PyObject *PyExc_Exception; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 935 | PyObject *PyExc_StopIteration; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 936 | PyObject *PyExc_StandardError; |
| 937 | PyObject *PyExc_ArithmeticError; |
| 938 | PyObject *PyExc_LookupError; |
| 939 | |
| 940 | PyObject *PyExc_AssertionError; |
| 941 | PyObject *PyExc_AttributeError; |
| 942 | PyObject *PyExc_EOFError; |
| 943 | PyObject *PyExc_FloatingPointError; |
| 944 | PyObject *PyExc_EnvironmentError; |
| 945 | PyObject *PyExc_IOError; |
| 946 | PyObject *PyExc_OSError; |
| 947 | PyObject *PyExc_ImportError; |
| 948 | PyObject *PyExc_IndexError; |
| 949 | PyObject *PyExc_KeyError; |
| 950 | PyObject *PyExc_KeyboardInterrupt; |
| 951 | PyObject *PyExc_MemoryError; |
| 952 | PyObject *PyExc_NameError; |
| 953 | PyObject *PyExc_OverflowError; |
| 954 | PyObject *PyExc_RuntimeError; |
| 955 | PyObject *PyExc_NotImplementedError; |
| 956 | PyObject *PyExc_SyntaxError; |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 957 | PyObject *PyExc_IndentationError; |
| 958 | PyObject *PyExc_TabError; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 959 | PyObject *PyExc_SystemError; |
| 960 | PyObject *PyExc_SystemExit; |
| 961 | PyObject *PyExc_UnboundLocalError; |
| 962 | PyObject *PyExc_UnicodeError; |
| 963 | PyObject *PyExc_TypeError; |
| 964 | PyObject *PyExc_ValueError; |
| 965 | PyObject *PyExc_ZeroDivisionError; |
| 966 | #ifdef MS_WINDOWS |
| 967 | PyObject *PyExc_WindowsError; |
| 968 | #endif |
| 969 | |
| 970 | /* Pre-computed MemoryError instance. Best to create this as early as |
| 971 | * possibly and not wait until a MemoryError is actually raised! |
| 972 | */ |
| 973 | PyObject *PyExc_MemoryErrorInst; |
| 974 | |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 975 | /* Predefined warning categories */ |
| 976 | PyObject *PyExc_Warning; |
| 977 | PyObject *PyExc_UserWarning; |
| 978 | PyObject *PyExc_DeprecationWarning; |
| 979 | PyObject *PyExc_SyntaxWarning; |
Guido van Rossum | ae347b3 | 2001-08-23 02:56:07 +0000 | [diff] [blame^] | 980 | PyObject *PyExc_OverflowWarning; |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 981 | PyObject *PyExc_RuntimeWarning; |
| 982 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 983 | |
| 984 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 985 | /* mapping between exception names and their PyObject ** */ |
| 986 | static struct { |
| 987 | char *name; |
| 988 | PyObject **exc; |
| 989 | PyObject **base; /* NULL == PyExc_StandardError */ |
| 990 | char *docstr; |
| 991 | PyMethodDef *methods; |
| 992 | int (*classinit)(PyObject *); |
| 993 | } exctable[] = { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 994 | /* |
| 995 | * The first three classes MUST appear in exactly this order |
| 996 | */ |
| 997 | {"Exception", &PyExc_Exception}, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 998 | {"StopIteration", &PyExc_StopIteration, &PyExc_Exception, |
| 999 | StopIteration__doc__}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1000 | {"StandardError", &PyExc_StandardError, &PyExc_Exception, |
| 1001 | StandardError__doc__}, |
| 1002 | {"TypeError", &PyExc_TypeError, 0, TypeError__doc__}, |
| 1003 | /* |
| 1004 | * The rest appear in depth-first order of the hierarchy |
| 1005 | */ |
| 1006 | {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__, |
| 1007 | SystemExit_methods}, |
| 1008 | {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__}, |
| 1009 | {"ImportError", &PyExc_ImportError, 0, ImportError__doc__}, |
| 1010 | {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__, |
| 1011 | EnvironmentError_methods}, |
| 1012 | {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__}, |
| 1013 | {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__}, |
| 1014 | #ifdef MS_WINDOWS |
Mark Hammond | 557a044 | 2000-08-15 00:37:32 +0000 | [diff] [blame] | 1015 | {"WindowsError", &PyExc_WindowsError, &PyExc_OSError, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1016 | WindowsError__doc__}, |
| 1017 | #endif /* MS_WINDOWS */ |
| 1018 | {"EOFError", &PyExc_EOFError, 0, EOFError__doc__}, |
| 1019 | {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__}, |
| 1020 | {"NotImplementedError", &PyExc_NotImplementedError, |
| 1021 | &PyExc_RuntimeError, NotImplementedError__doc__}, |
| 1022 | {"NameError", &PyExc_NameError, 0, NameError__doc__}, |
| 1023 | {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError, |
| 1024 | UnboundLocalError__doc__}, |
| 1025 | {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__}, |
| 1026 | {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__, |
| 1027 | SyntaxError_methods, SyntaxError__classinit__}, |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 1028 | {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError, |
| 1029 | IndentationError__doc__}, |
| 1030 | {"TabError", &PyExc_TabError, &PyExc_IndentationError, |
| 1031 | TabError__doc__}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1032 | {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__}, |
| 1033 | {"LookupError", &PyExc_LookupError, 0, LookupError__doc__}, |
| 1034 | {"IndexError", &PyExc_IndexError, &PyExc_LookupError, |
| 1035 | IndexError__doc__}, |
| 1036 | {"KeyError", &PyExc_KeyError, &PyExc_LookupError, |
| 1037 | KeyError__doc__}, |
| 1038 | {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__}, |
| 1039 | {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError, |
| 1040 | OverflowError__doc__}, |
| 1041 | {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError, |
| 1042 | ZeroDivisionError__doc__}, |
| 1043 | {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError, |
| 1044 | FloatingPointError__doc__}, |
| 1045 | {"ValueError", &PyExc_ValueError, 0, ValueError__doc__}, |
| 1046 | {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__}, |
| 1047 | {"SystemError", &PyExc_SystemError, 0, SystemError__doc__}, |
| 1048 | {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__}, |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1049 | /* Warning categories */ |
| 1050 | {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__}, |
| 1051 | {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__}, |
| 1052 | {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning, |
| 1053 | DeprecationWarning__doc__}, |
| 1054 | {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__}, |
Guido van Rossum | ae347b3 | 2001-08-23 02:56:07 +0000 | [diff] [blame^] | 1055 | {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning, |
| 1056 | OverflowWarning__doc__}, |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1057 | {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning, |
| 1058 | RuntimeWarning__doc__}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1059 | /* Sentinel */ |
| 1060 | {NULL} |
| 1061 | }; |
| 1062 | |
| 1063 | |
| 1064 | |
Tim Peters | 5687ffe | 2001-02-28 16:44:18 +0000 | [diff] [blame] | 1065 | DL_EXPORT(void) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1066 | _PyExc_Init(void) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1067 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 1068 | char *modulename = "exceptions"; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1069 | int modnamesz = strlen(modulename); |
| 1070 | int i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1071 | PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1072 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1073 | me = Py_InitModule(modulename, functions); |
| 1074 | if (me == NULL) |
| 1075 | goto err; |
| 1076 | mydict = PyModule_GetDict(me); |
| 1077 | if (mydict == NULL) |
| 1078 | goto err; |
| 1079 | bltinmod = PyImport_ImportModule("__builtin__"); |
| 1080 | if (bltinmod == NULL) |
| 1081 | goto err; |
| 1082 | bdict = PyModule_GetDict(bltinmod); |
| 1083 | if (bdict == NULL) |
| 1084 | goto err; |
| 1085 | doc = PyString_FromString(module__doc__); |
| 1086 | if (doc == NULL) |
| 1087 | goto err; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1088 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1089 | i = PyDict_SetItemString(mydict, "__doc__", doc); |
Barry Warsaw | 8fcaa92 | 2000-07-01 04:45:52 +0000 | [diff] [blame] | 1090 | Py_DECREF(doc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1091 | if (i < 0) { |
| 1092 | err: |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1093 | Py_FatalError("exceptions bootstrapping error."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1094 | return; |
| 1095 | } |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1096 | |
| 1097 | /* This is the base class of all exceptions, so make it first. */ |
| 1098 | if (make_Exception(modulename) || |
| 1099 | PyDict_SetItemString(mydict, "Exception", PyExc_Exception) || |
| 1100 | PyDict_SetItemString(bdict, "Exception", PyExc_Exception)) |
| 1101 | { |
| 1102 | Py_FatalError("Base class `Exception' could not be created."); |
| 1103 | } |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 1104 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1105 | /* Now we can programmatically create all the remaining exceptions. |
| 1106 | * Remember to start the loop at 1 to skip Exceptions. |
| 1107 | */ |
| 1108 | for (i=1; exctable[i].name; i++) { |
| 1109 | int status; |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 1110 | char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2); |
| 1111 | PyObject *base; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1112 | |
| 1113 | (void)strcpy(cname, modulename); |
| 1114 | (void)strcat(cname, "."); |
| 1115 | (void)strcat(cname, exctable[i].name); |
| 1116 | |
| 1117 | if (exctable[i].base == 0) |
| 1118 | base = PyExc_StandardError; |
| 1119 | else |
| 1120 | base = *exctable[i].base; |
| 1121 | |
| 1122 | status = make_class(exctable[i].exc, base, cname, |
| 1123 | exctable[i].methods, |
| 1124 | exctable[i].docstr); |
| 1125 | |
| 1126 | PyMem_DEL(cname); |
| 1127 | |
| 1128 | if (status) |
| 1129 | Py_FatalError("Standard exception classes could not be created."); |
| 1130 | |
| 1131 | if (exctable[i].classinit) { |
| 1132 | status = (*exctable[i].classinit)(*exctable[i].exc); |
| 1133 | if (status) |
| 1134 | Py_FatalError("An exception class could not be initialized."); |
| 1135 | } |
| 1136 | |
| 1137 | /* Now insert the class into both this module and the __builtin__ |
| 1138 | * module. |
| 1139 | */ |
| 1140 | if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) || |
| 1141 | PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc)) |
| 1142 | { |
| 1143 | Py_FatalError("Module dictionary insertion problem."); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | /* Now we need to pre-allocate a MemoryError instance */ |
| 1148 | args = Py_BuildValue("()"); |
| 1149 | if (!args || |
| 1150 | !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args))) |
| 1151 | { |
| 1152 | Py_FatalError("Cannot pre-allocate MemoryError instance\n"); |
| 1153 | } |
| 1154 | Py_DECREF(args); |
| 1155 | |
| 1156 | /* We're done with __builtin__ */ |
| 1157 | Py_DECREF(bltinmod); |
| 1158 | } |
| 1159 | |
| 1160 | |
Tim Peters | 5687ffe | 2001-02-28 16:44:18 +0000 | [diff] [blame] | 1161 | DL_EXPORT(void) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1162 | _PyExc_Fini(void) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1163 | { |
| 1164 | int i; |
| 1165 | |
| 1166 | Py_XDECREF(PyExc_MemoryErrorInst); |
| 1167 | PyExc_MemoryErrorInst = NULL; |
| 1168 | |
| 1169 | for (i=0; exctable[i].name; i++) { |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 1170 | /* clear the class's dictionary, freeing up circular references |
| 1171 | * between the class and its methods. |
| 1172 | */ |
| 1173 | PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__"); |
| 1174 | PyDict_Clear(cdict); |
| 1175 | Py_DECREF(cdict); |
| 1176 | |
| 1177 | /* Now decref the exception class */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1178 | Py_XDECREF(*exctable[i].exc); |
| 1179 | *exctable[i].exc = NULL; |
| 1180 | } |
| 1181 | } |