blob: c020defcb5283ba4bacea5b97cc5eb93618a8810 [file] [log] [blame]
Barry Warsaw675ac282000-05-26 19:05:16 +00001/* 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 Drake185a29b2000-08-15 16:20:36 +000022#include "osdefs.h"
Barry Warsaw675ac282000-05-26 19:05:16 +000023
Tim Petersbf26e072000-07-12 04:02:10 +000024/* 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 Warsaw675ac282000-05-26 19:05:16 +000028static char
Barry Warsaw9667ed22001-01-23 16:08:34 +000029module__doc__[] =
Barry Warsaw675ac282000-05-26 19:05:16 +000030"Python's standard exception class hierarchy.\n\
31\n\
32Before Python 1.5, the standard exceptions were all simple string objects.\n\
33In Python 1.5, the standard exceptions were converted to classes organized\n\
34into a relatively flat hierarchy. String-based standard exceptions were\n\
35optional, or used as a fallback if some problem occurred while importing\n\
36the exception module. With Python 1.6, optional string-based standard\n\
37exceptions were removed (along with the -X command line flag).\n\
38\n\
39The class exceptions were implemented in such a way as to be almost\n\
40completely backward compatible. Some tricky uses of IOError could\n\
41potentially have broken, but by Python 1.6, all of these should have\n\
42been fixed. As of Python 1.6, the class-based standard exceptions are\n\
43now implemented in C, and are guaranteed to exist in the Python\n\
44interpreter.\n\
45\n\
46Here is a rundown of the class hierarchy. The classes found here are\n\
47inserted into both the exceptions module and the `built-in' module. It is\n\
48recommended that user defined class based exceptions be derived from the\n\
Tim Petersbf26e072000-07-12 04:02:10 +000049`Exception' class, although this is currently not enforced.\n"
50 /* keep string pieces "small" */
51"\n\
Barry Warsaw675ac282000-05-26 19:05:16 +000052Exception\n\
53 |\n\
54 +-- SystemExit\n\
55 +-- StandardError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +000056 | |\n\
57 | +-- KeyboardInterrupt\n\
58 | +-- ImportError\n\
59 | +-- EnvironmentError\n\
60 | | |\n\
61 | | +-- IOError\n\
62 | | +-- OSError\n\
63 | | |\n\
64 | | +-- WindowsError\n\
65 | |\n\
66 | +-- EOFError\n\
67 | +-- RuntimeError\n\
68 | | |\n\
69 | | +-- NotImplementedError\n\
70 | |\n\
71 | +-- NameError\n\
72 | | |\n\
73 | | +-- UnboundLocalError\n\
74 | |\n\
75 | +-- AttributeError\n\
76 | +-- SyntaxError\n\
77 | | |\n\
78 | | +-- IndentationError\n\
79 | | |\n\
80 | | +-- TabError\n\
81 | |\n\
82 | +-- TypeError\n\
83 | +-- AssertionError\n\
84 | +-- LookupError\n\
85 | | |\n\
86 | | +-- IndexError\n\
87 | | +-- KeyError\n\
88 | |\n\
89 | +-- ArithmeticError\n\
90 | | |\n\
91 | | +-- OverflowError\n\
92 | | +-- ZeroDivisionError\n\
93 | | +-- FloatingPointError\n\
94 | |\n\
95 | +-- ValueError\n\
96 | | |\n\
97 | | +-- UnicodeError\n\
98 | |\n\
99 | +-- SystemError\n\
100 | +-- MemoryError\n\
101 |\n\
102 +---Warning\n\
Barry Warsaw675ac282000-05-26 19:05:16 +0000103 |\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000104 +-- UserWarning\n\
105 +-- DeprecationWarning\n\
106 +-- SyntaxWarning\n\
107 +-- RuntimeWarning";
Barry Warsaw675ac282000-05-26 19:05:16 +0000108
109
110/* Helper function for populating a dictionary with method wrappers. */
111static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000112populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +0000113{
114 if (!methods)
115 return 0;
116
117 while (methods->ml_name) {
118 /* get a wrapper for the built-in function */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000119 PyObject *func = PyCFunction_New(methods, NULL);
120 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +0000121 int status;
122
123 if (!func)
124 return -1;
125
126 /* turn the function into an unbound method */
127 if (!(meth = PyMethod_New(func, NULL, klass))) {
128 Py_DECREF(func);
129 return -1;
130 }
Barry Warsaw9667ed22001-01-23 16:08:34 +0000131
Barry Warsaw675ac282000-05-26 19:05:16 +0000132 /* add method to dictionary */
133 status = PyDict_SetItemString(dict, methods->ml_name, meth);
134 Py_DECREF(meth);
135 Py_DECREF(func);
136
137 /* stop now if an error occurred, otherwise do the next method */
138 if (status)
139 return status;
140
141 methods++;
142 }
143 return 0;
144}
145
Barry Warsaw9667ed22001-01-23 16:08:34 +0000146
Barry Warsaw675ac282000-05-26 19:05:16 +0000147
148/* This function is used to create all subsequent exception classes. */
149static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000150make_class(PyObject **klass, PyObject *base,
151 char *name, PyMethodDef *methods,
152 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +0000153{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000154 PyObject *dict = PyDict_New();
155 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000156 int status = -1;
157
158 if (!dict)
159 return -1;
160
161 /* If an error occurs from here on, goto finally instead of explicitly
162 * returning NULL.
163 */
164
165 if (docstr) {
166 if (!(str = PyString_FromString(docstr)))
167 goto finally;
168 if (PyDict_SetItemString(dict, "__doc__", str))
169 goto finally;
170 }
171
172 if (!(*klass = PyErr_NewException(name, base, dict)))
173 goto finally;
174
175 if (populate_methods(*klass, dict, methods)) {
176 Py_DECREF(*klass);
177 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000178 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000179 }
180
181 status = 0;
182
183 finally:
184 Py_XDECREF(dict);
185 Py_XDECREF(str);
186 return status;
187}
188
189
190/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000191static PyObject *
192get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000193{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000194 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000195 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000196 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000197 if (PyExc_TypeError) {
198 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000199 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000200 }
201 return NULL;
202 }
203 return self;
204}
205
206
207
208/* Notes on bootstrapping the exception classes.
209 *
210 * First thing we create is the base class for all exceptions, called
211 * appropriately enough: Exception. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000212 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000213 * for TypeError, which can conditionally exist.
214 *
215 * Next, StandardError is created (which is quite simple) followed by
216 * TypeError, because the instantiation of other exceptions can potentially
217 * throw a TypeError. Once these exceptions are created, all the others
218 * can be created in any order. See the static exctable below for the
219 * explicit bootstrap order.
220 *
221 * All classes after Exception can be created using PyErr_NewException().
222 */
223
224static char
225Exception__doc__[] = "Common base class for all exceptions.";
226
227
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000228static PyObject *
229Exception__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000230{
231 int status;
232
233 if (!(self = get_self(args)))
234 return NULL;
235
236 /* set args attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000237 /* XXX size is only a hint */
238 args = PySequence_GetSlice(args, 1, PySequence_Size(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000239 if (!args)
240 return NULL;
241 status = PyObject_SetAttrString(self, "args", args);
242 Py_DECREF(args);
243 if (status < 0)
244 return NULL;
245
246 Py_INCREF(Py_None);
247 return Py_None;
248}
249
250
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000251static PyObject *
252Exception__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000253{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000254 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000255
Fred Drake1aba5772000-08-15 15:46:16 +0000256 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000257 return NULL;
258
259 args = PyObject_GetAttrString(self, "args");
260 if (!args)
261 return NULL;
262
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000263 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000264 case 0:
265 out = PyString_FromString("");
266 break;
267 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000268 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000269 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000270 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000271 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000272 Py_DECREF(tmp);
273 }
274 else
275 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000276 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000277 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000278 default:
279 out = PyObject_Str(args);
280 break;
281 }
282
283 Py_DECREF(args);
284 return out;
285}
286
287
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000288static PyObject *
289Exception__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000290{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000291 PyObject *out;
292 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000293
Fred Drake1aba5772000-08-15 15:46:16 +0000294 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000295 return NULL;
296
297 args = PyObject_GetAttrString(self, "args");
298 if (!args)
299 return NULL;
300
301 out = PyObject_GetItem(args, index);
302 Py_DECREF(args);
303 return out;
304}
305
306
307static PyMethodDef
308Exception_methods[] = {
309 /* methods for the Exception class */
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000310 { "__getitem__", Exception__getitem__, METH_VARARGS},
311 { "__str__", Exception__str__, METH_VARARGS},
312 { "__init__", Exception__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000313 { NULL, NULL }
314};
315
316
317static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000318make_Exception(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000319{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000320 PyObject *dict = PyDict_New();
321 PyObject *str = NULL;
322 PyObject *name = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000323 int status = -1;
324
325 if (!dict)
326 return -1;
327
328 /* If an error occurs from here on, goto finally instead of explicitly
329 * returning NULL.
330 */
331
332 if (!(str = PyString_FromString(modulename)))
333 goto finally;
334 if (PyDict_SetItemString(dict, "__module__", str))
335 goto finally;
336 Py_DECREF(str);
337 if (!(str = PyString_FromString(Exception__doc__)))
338 goto finally;
339 if (PyDict_SetItemString(dict, "__doc__", str))
340 goto finally;
341
342 if (!(name = PyString_FromString("Exception")))
343 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000344
Barry Warsaw675ac282000-05-26 19:05:16 +0000345 if (!(PyExc_Exception = PyClass_New(NULL, dict, name)))
346 goto finally;
347
348 /* Now populate the dictionary with the method suite */
349 if (populate_methods(PyExc_Exception, dict, Exception_methods))
350 /* Don't need to reclaim PyExc_Exception here because that'll
351 * happen during interpreter shutdown.
352 */
353 goto finally;
354
355 status = 0;
356
357 finally:
358 Py_XDECREF(dict);
359 Py_XDECREF(str);
360 Py_XDECREF(name);
361 return status;
362}
363
364
365
366static char
367StandardError__doc__[] = "Base class for all standard Python exceptions.";
368
369static char
370TypeError__doc__[] = "Inappropriate argument type.";
371
372
373
374static char
375SystemExit__doc__[] = "Request to exit from the interpreter.";
376
377
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000378static PyObject *
379SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000380{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000381 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000382 int status;
383
384 if (!(self = get_self(args)))
385 return NULL;
386
387 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000388 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000389 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000390
Barry Warsaw675ac282000-05-26 19:05:16 +0000391 status = PyObject_SetAttrString(self, "args", args);
392 if (status < 0) {
393 Py_DECREF(args);
394 return NULL;
395 }
396
397 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000398 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000399 case 0:
400 Py_INCREF(Py_None);
401 code = Py_None;
402 break;
403 case 1:
404 code = PySequence_GetItem(args, 0);
405 break;
406 default:
407 Py_INCREF(args);
408 code = args;
409 break;
410 }
411
412 status = PyObject_SetAttrString(self, "code", code);
413 Py_DECREF(code);
414 Py_DECREF(args);
415 if (status < 0)
416 return NULL;
417
418 Py_INCREF(Py_None);
419 return Py_None;
420}
421
422
423PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000424 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000425 {NULL, NULL}
426};
427
428
429
430static char
431KeyboardInterrupt__doc__[] = "Program interrupted by user.";
432
433static char
434ImportError__doc__[] =
435"Import can't find module, or can't find name in module.";
436
437
438
439static char
440EnvironmentError__doc__[] = "Base class for I/O related errors.";
441
442
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000443static PyObject *
444EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000445{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000446 PyObject *item0 = NULL;
447 PyObject *item1 = NULL;
448 PyObject *item2 = NULL;
449 PyObject *subslice = NULL;
450 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000451
452 if (!(self = get_self(args)))
453 return NULL;
454
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000455 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000456 return NULL;
457
458 if (PyObject_SetAttrString(self, "args", args) ||
459 PyObject_SetAttrString(self, "errno", Py_None) ||
460 PyObject_SetAttrString(self, "strerror", Py_None) ||
461 PyObject_SetAttrString(self, "filename", Py_None))
462 {
463 goto finally;
464 }
465
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000466 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000467 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000468 /* Where a function has a single filename, such as open() or some
469 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
470 * called, giving a third argument which is the filename. But, so
471 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000472 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000473 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000474 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000475 * we hack args so that it only contains two items. This also
476 * means we need our own __str__() which prints out the filename
477 * when it was supplied.
478 */
479 item0 = PySequence_GetItem(args, 0);
480 item1 = PySequence_GetItem(args, 1);
481 item2 = PySequence_GetItem(args, 2);
482 if (!item0 || !item1 || !item2)
483 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000484
Barry Warsaw675ac282000-05-26 19:05:16 +0000485 if (PyObject_SetAttrString(self, "errno", item0) ||
486 PyObject_SetAttrString(self, "strerror", item1) ||
487 PyObject_SetAttrString(self, "filename", item2))
488 {
489 goto finally;
490 }
491
492 subslice = PySequence_GetSlice(args, 0, 2);
493 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
494 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000495 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000496
497 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000498 /* Used when PyErr_SetFromErrno() is called and no filename
499 * argument is given.
500 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000501 item0 = PySequence_GetItem(args, 0);
502 item1 = PySequence_GetItem(args, 1);
503 if (!item0 || !item1)
504 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000505
Barry Warsaw675ac282000-05-26 19:05:16 +0000506 if (PyObject_SetAttrString(self, "errno", item0) ||
507 PyObject_SetAttrString(self, "strerror", item1))
508 {
509 goto finally;
510 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000511 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000512 }
513
514 Py_INCREF(Py_None);
515 rtnval = Py_None;
516
517 finally:
518 Py_DECREF(args);
519 Py_XDECREF(item0);
520 Py_XDECREF(item1);
521 Py_XDECREF(item2);
522 Py_XDECREF(subslice);
523 return rtnval;
524}
525
526
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000527static PyObject *
528EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000529{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000530 PyObject *originalself = self;
531 PyObject *filename;
532 PyObject *serrno;
533 PyObject *strerror;
534 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000535
Fred Drake1aba5772000-08-15 15:46:16 +0000536 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000537 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000538
Barry Warsaw675ac282000-05-26 19:05:16 +0000539 filename = PyObject_GetAttrString(self, "filename");
540 serrno = PyObject_GetAttrString(self, "errno");
541 strerror = PyObject_GetAttrString(self, "strerror");
542 if (!filename || !serrno || !strerror)
543 goto finally;
544
545 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000546 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
547 PyObject *repr = PyObject_Repr(filename);
548 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000549
550 if (!fmt || !repr || !tuple) {
551 Py_XDECREF(fmt);
552 Py_XDECREF(repr);
553 Py_XDECREF(tuple);
554 goto finally;
555 }
556
557 PyTuple_SET_ITEM(tuple, 0, serrno);
558 PyTuple_SET_ITEM(tuple, 1, strerror);
559 PyTuple_SET_ITEM(tuple, 2, repr);
560
561 rtnval = PyString_Format(fmt, tuple);
562
563 Py_DECREF(fmt);
564 Py_DECREF(tuple);
565 /* already freed because tuple owned only reference */
566 serrno = NULL;
567 strerror = NULL;
568 }
569 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000570 PyObject *fmt = PyString_FromString("[Errno %s] %s");
571 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000572
573 if (!fmt || !tuple) {
574 Py_XDECREF(fmt);
575 Py_XDECREF(tuple);
576 goto finally;
577 }
578
579 PyTuple_SET_ITEM(tuple, 0, serrno);
580 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000581
Barry Warsaw675ac282000-05-26 19:05:16 +0000582 rtnval = PyString_Format(fmt, tuple);
583
584 Py_DECREF(fmt);
585 Py_DECREF(tuple);
586 /* already freed because tuple owned only reference */
587 serrno = NULL;
588 strerror = NULL;
589 }
590 else
591 /* The original Python code said:
592 *
593 * return StandardError.__str__(self)
594 *
595 * but there is no StandardError__str__() function; we happen to
596 * know that's just a pass through to Exception__str__().
597 */
598 rtnval = Exception__str__(originalself, args);
599
600 finally:
601 Py_XDECREF(filename);
602 Py_XDECREF(serrno);
603 Py_XDECREF(strerror);
604 return rtnval;
605}
606
607
608static
609PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000610 {"__init__", EnvironmentError__init__, METH_VARARGS},
611 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000612 {NULL, NULL}
613};
614
615
616
617
618static char
619IOError__doc__[] = "I/O operation failed.";
620
621static char
622OSError__doc__[] = "OS system call failed.";
623
624#ifdef MS_WINDOWS
625static char
626WindowsError__doc__[] = "MS-Windows OS system call failed.";
627#endif /* MS_WINDOWS */
628
629static char
630EOFError__doc__[] = "Read beyond end of file.";
631
632static char
633RuntimeError__doc__[] = "Unspecified run-time error.";
634
635static char
636NotImplementedError__doc__[] =
637"Method or function hasn't been implemented yet.";
638
639static char
640NameError__doc__[] = "Name not found globally.";
641
642static char
643UnboundLocalError__doc__[] =
644"Local name referenced but not bound to a value.";
645
646static char
647AttributeError__doc__[] = "Attribute not found.";
648
649
650
651static char
652SyntaxError__doc__[] = "Invalid syntax.";
653
654
655static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000656SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000657{
Barry Warsaw87bec352000-08-18 05:05:37 +0000658 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000659 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000660
661 /* Additional class-creation time initializations */
662 if (!emptystring ||
663 PyObject_SetAttrString(klass, "msg", emptystring) ||
664 PyObject_SetAttrString(klass, "filename", Py_None) ||
665 PyObject_SetAttrString(klass, "lineno", Py_None) ||
666 PyObject_SetAttrString(klass, "offset", Py_None) ||
667 PyObject_SetAttrString(klass, "text", Py_None))
668 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000669 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000670 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000671 Py_XDECREF(emptystring);
672 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000673}
674
675
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000676static PyObject *
677SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000678{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000679 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000680 int lenargs;
681
682 if (!(self = get_self(args)))
683 return NULL;
684
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000685 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000686 return NULL;
687
688 if (PyObject_SetAttrString(self, "args", args))
689 goto finally;
690
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000691 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000692 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000693 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000694 int status;
695
696 if (!item0)
697 goto finally;
698 status = PyObject_SetAttrString(self, "msg", item0);
699 Py_DECREF(item0);
700 if (status)
701 goto finally;
702 }
703 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000704 PyObject *info = PySequence_GetItem(args, 1);
Barry Warsaw675ac282000-05-26 19:05:16 +0000705 PyObject *filename, *lineno, *offset, *text;
706 int status = 1;
707
708 if (!info)
709 goto finally;
710
711 filename = PySequence_GetItem(info, 0);
712 lineno = PySequence_GetItem(info, 1);
713 offset = PySequence_GetItem(info, 2);
714 text = PySequence_GetItem(info, 3);
715
716 Py_DECREF(info);
717
718 if (filename && lineno && offset && text) {
719 status = PyObject_SetAttrString(self, "filename", filename) ||
720 PyObject_SetAttrString(self, "lineno", lineno) ||
721 PyObject_SetAttrString(self, "offset", offset) ||
722 PyObject_SetAttrString(self, "text", text);
723 }
724 Py_XDECREF(filename);
725 Py_XDECREF(lineno);
726 Py_XDECREF(offset);
727 Py_XDECREF(text);
728
729 if (status)
730 goto finally;
731 }
732 Py_INCREF(Py_None);
733 rtnval = Py_None;
734
735 finally:
736 Py_DECREF(args);
737 return rtnval;
738}
739
740
Fred Drake185a29b2000-08-15 16:20:36 +0000741/* This is called "my_basename" instead of just "basename" to avoid name
742 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
743 defined, and Python does define that. */
744static char *
745my_basename(char *name)
746{
747 char *cp = name;
748 char *result = name;
749
750 if (name == NULL)
751 return "???";
752 while (*cp != '\0') {
753 if (*cp == SEP)
754 result = cp + 1;
755 ++cp;
756 }
757 return result;
758}
759
760
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000761static PyObject *
762SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000763{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000764 PyObject *msg;
765 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000766 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000767
Fred Drake1aba5772000-08-15 15:46:16 +0000768 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000769 return NULL;
770
771 if (!(msg = PyObject_GetAttrString(self, "msg")))
772 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000773
Barry Warsaw675ac282000-05-26 19:05:16 +0000774 str = PyObject_Str(msg);
775 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000776 result = str;
777
778 /* XXX -- do all the additional formatting with filename and
779 lineno here */
780
781 if (PyString_Check(str)) {
782 int have_filename = 0;
783 int have_lineno = 0;
784 char *buffer = NULL;
785
Barry Warsaw77c9f502000-08-16 19:43:17 +0000786 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000787 have_filename = PyString_Check(filename);
788 else
789 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000790
791 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000792 have_lineno = PyInt_Check(lineno);
793 else
794 PyErr_Clear();
795
796 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000797 int bufsize = PyString_GET_SIZE(str) + 64;
798 if (have_filename)
799 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000800
801 buffer = PyMem_Malloc(bufsize);
802 if (buffer != NULL) {
803 if (have_filename && have_lineno)
Fred Drake04e654a2000-08-18 19:53:25 +0000804 sprintf(buffer, "%s (%s, line %ld)",
Fred Drake1aba5772000-08-15 15:46:16 +0000805 PyString_AS_STRING(str),
Fred Drake185a29b2000-08-15 16:20:36 +0000806 my_basename(PyString_AS_STRING(filename)),
Fred Drake1aba5772000-08-15 15:46:16 +0000807 PyInt_AsLong(lineno));
808 else if (have_filename)
809 sprintf(buffer, "%s (%s)",
810 PyString_AS_STRING(str),
Fred Drake185a29b2000-08-15 16:20:36 +0000811 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000812 else if (have_lineno)
Fred Drake04e654a2000-08-18 19:53:25 +0000813 sprintf(buffer, "%s (line %ld)",
Fred Drake1aba5772000-08-15 15:46:16 +0000814 PyString_AS_STRING(str),
815 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000816
Fred Drake1aba5772000-08-15 15:46:16 +0000817 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000818 PyMem_FREE(buffer);
819
Fred Drake1aba5772000-08-15 15:46:16 +0000820 if (result == NULL)
821 result = str;
822 else
823 Py_DECREF(str);
824 }
825 }
826 Py_XDECREF(filename);
827 Py_XDECREF(lineno);
828 }
829 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000830}
831
832
833PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000834 {"__init__", SyntaxError__init__, METH_VARARGS},
835 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000836 {NULL, NULL}
837};
838
839
840
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000841/* Exception doc strings */
842
Barry Warsaw675ac282000-05-26 19:05:16 +0000843static char
844AssertionError__doc__[] = "Assertion failed.";
845
846static char
847LookupError__doc__[] = "Base class for lookup errors.";
848
849static char
850IndexError__doc__[] = "Sequence index out of range.";
851
852static char
853KeyError__doc__[] = "Mapping key not found.";
854
855static char
856ArithmeticError__doc__[] = "Base class for arithmetic errors.";
857
858static char
859OverflowError__doc__[] = "Result too large to be represented.";
860
861static char
862ZeroDivisionError__doc__[] =
863"Second argument to a division or modulo operation was zero.";
864
865static char
866FloatingPointError__doc__[] = "Floating point operation failed.";
867
868static char
869ValueError__doc__[] = "Inappropriate argument value (of correct type).";
870
871static char
872UnicodeError__doc__[] = "Unicode related error.";
873
874static char
875SystemError__doc__[] = "Internal error in the Python interpreter.\n\
876\n\
877Please report this to the Python maintainer, along with the traceback,\n\
878the Python version, and the hardware/OS platform and version.";
879
880static char
881MemoryError__doc__[] = "Out of memory.";
882
Fred Drake85f36392000-07-11 17:53:00 +0000883static char
884IndentationError__doc__[] = "Improper indentation.";
885
886static char
887TabError__doc__[] = "Improper mixture of spaces and tabs.";
888
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000889/* Warning category docstrings */
890
891static char
892Warning__doc__[] = "Base class for warning categories.";
893
894static char
895UserWarning__doc__[] = "Base class for warnings generated by user code.";
896
897static char
898DeprecationWarning__doc__[] =
899"Base class for warnings about deprecated features.";
900
901static char
902SyntaxWarning__doc__[] = "Base class for warnings about dubious syntax.";
903
904static char
905RuntimeWarning__doc__[] =
906"Base class for warnings about dubious runtime behavior.";
907
Barry Warsaw675ac282000-05-26 19:05:16 +0000908
909
910/* module global functions */
911static PyMethodDef functions[] = {
912 /* Sentinel */
913 {NULL, NULL}
914};
915
916
917
918/* Global C API defined exceptions */
919
920PyObject *PyExc_Exception;
921PyObject *PyExc_StandardError;
922PyObject *PyExc_ArithmeticError;
923PyObject *PyExc_LookupError;
924
925PyObject *PyExc_AssertionError;
926PyObject *PyExc_AttributeError;
927PyObject *PyExc_EOFError;
928PyObject *PyExc_FloatingPointError;
929PyObject *PyExc_EnvironmentError;
930PyObject *PyExc_IOError;
931PyObject *PyExc_OSError;
932PyObject *PyExc_ImportError;
933PyObject *PyExc_IndexError;
934PyObject *PyExc_KeyError;
935PyObject *PyExc_KeyboardInterrupt;
936PyObject *PyExc_MemoryError;
937PyObject *PyExc_NameError;
938PyObject *PyExc_OverflowError;
939PyObject *PyExc_RuntimeError;
940PyObject *PyExc_NotImplementedError;
941PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +0000942PyObject *PyExc_IndentationError;
943PyObject *PyExc_TabError;
Barry Warsaw675ac282000-05-26 19:05:16 +0000944PyObject *PyExc_SystemError;
945PyObject *PyExc_SystemExit;
946PyObject *PyExc_UnboundLocalError;
947PyObject *PyExc_UnicodeError;
948PyObject *PyExc_TypeError;
949PyObject *PyExc_ValueError;
950PyObject *PyExc_ZeroDivisionError;
951#ifdef MS_WINDOWS
952PyObject *PyExc_WindowsError;
953#endif
954
955/* Pre-computed MemoryError instance. Best to create this as early as
956 * possibly and not wait until a MemoryError is actually raised!
957 */
958PyObject *PyExc_MemoryErrorInst;
959
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000960/* Predefined warning categories */
961PyObject *PyExc_Warning;
962PyObject *PyExc_UserWarning;
963PyObject *PyExc_DeprecationWarning;
964PyObject *PyExc_SyntaxWarning;
965PyObject *PyExc_RuntimeWarning;
966
Barry Warsaw675ac282000-05-26 19:05:16 +0000967
968
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000969/* mapping between exception names and their PyObject ** */
970static struct {
971 char *name;
972 PyObject **exc;
973 PyObject **base; /* NULL == PyExc_StandardError */
974 char *docstr;
975 PyMethodDef *methods;
976 int (*classinit)(PyObject *);
977} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +0000978 /*
979 * The first three classes MUST appear in exactly this order
980 */
981 {"Exception", &PyExc_Exception},
982 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
983 StandardError__doc__},
984 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
985 /*
986 * The rest appear in depth-first order of the hierarchy
987 */
988 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
989 SystemExit_methods},
990 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
991 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
992 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
993 EnvironmentError_methods},
994 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
995 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
996#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +0000997 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +0000998 WindowsError__doc__},
999#endif /* MS_WINDOWS */
1000 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1001 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1002 {"NotImplementedError", &PyExc_NotImplementedError,
1003 &PyExc_RuntimeError, NotImplementedError__doc__},
1004 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1005 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1006 UnboundLocalError__doc__},
1007 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1008 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1009 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001010 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1011 IndentationError__doc__},
1012 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1013 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001014 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1015 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1016 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1017 IndexError__doc__},
1018 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
1019 KeyError__doc__},
1020 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1021 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1022 OverflowError__doc__},
1023 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1024 ZeroDivisionError__doc__},
1025 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1026 FloatingPointError__doc__},
1027 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1028 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
1029 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1030 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001031 /* Warning categories */
1032 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1033 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1034 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1035 DeprecationWarning__doc__},
1036 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
1037 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1038 RuntimeWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001039 /* Sentinel */
1040 {NULL}
1041};
1042
1043
1044
1045void
1046#ifdef WIN32
1047__declspec(dllexport)
1048#endif /* WIN32 */
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001049init_exceptions(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001050{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001051 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001052 int modnamesz = strlen(modulename);
1053 int i;
1054
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001055 PyObject *me = Py_InitModule(modulename, functions);
1056 PyObject *mydict = PyModule_GetDict(me);
1057 PyObject *bltinmod = PyImport_ImportModule("__builtin__");
1058 PyObject *bdict = PyModule_GetDict(bltinmod);
1059 PyObject *doc = PyString_FromString(module__doc__);
1060 PyObject *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001061
1062 PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001063 Py_DECREF(doc);
Barry Warsaw675ac282000-05-26 19:05:16 +00001064 if (PyErr_Occurred())
1065 Py_FatalError("exceptions bootstrapping error.");
1066
1067 /* This is the base class of all exceptions, so make it first. */
1068 if (make_Exception(modulename) ||
1069 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1070 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1071 {
1072 Py_FatalError("Base class `Exception' could not be created.");
1073 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001074
Barry Warsaw675ac282000-05-26 19:05:16 +00001075 /* Now we can programmatically create all the remaining exceptions.
1076 * Remember to start the loop at 1 to skip Exceptions.
1077 */
1078 for (i=1; exctable[i].name; i++) {
1079 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001080 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1081 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001082
1083 (void)strcpy(cname, modulename);
1084 (void)strcat(cname, ".");
1085 (void)strcat(cname, exctable[i].name);
1086
1087 if (exctable[i].base == 0)
1088 base = PyExc_StandardError;
1089 else
1090 base = *exctable[i].base;
1091
1092 status = make_class(exctable[i].exc, base, cname,
1093 exctable[i].methods,
1094 exctable[i].docstr);
1095
1096 PyMem_DEL(cname);
1097
1098 if (status)
1099 Py_FatalError("Standard exception classes could not be created.");
1100
1101 if (exctable[i].classinit) {
1102 status = (*exctable[i].classinit)(*exctable[i].exc);
1103 if (status)
1104 Py_FatalError("An exception class could not be initialized.");
1105 }
1106
1107 /* Now insert the class into both this module and the __builtin__
1108 * module.
1109 */
1110 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1111 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1112 {
1113 Py_FatalError("Module dictionary insertion problem.");
1114 }
1115 }
1116
1117 /* Now we need to pre-allocate a MemoryError instance */
1118 args = Py_BuildValue("()");
1119 if (!args ||
1120 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1121 {
1122 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1123 }
1124 Py_DECREF(args);
1125
1126 /* We're done with __builtin__ */
1127 Py_DECREF(bltinmod);
1128}
1129
1130
1131void
1132#ifdef WIN32
1133__declspec(dllexport)
1134#endif /* WIN32 */
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001135fini_exceptions(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001136{
1137 int i;
1138
1139 Py_XDECREF(PyExc_MemoryErrorInst);
1140 PyExc_MemoryErrorInst = NULL;
1141
1142 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001143 /* clear the class's dictionary, freeing up circular references
1144 * between the class and its methods.
1145 */
1146 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1147 PyDict_Clear(cdict);
1148 Py_DECREF(cdict);
1149
1150 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001151 Py_XDECREF(*exctable[i].exc);
1152 *exctable[i].exc = NULL;
1153 }
1154}