blob: f262ef2a97d3f6d59ba8e8fd6aae4507f831736d [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);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000705 PyObject *filename = NULL, *lineno = NULL;
706 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000707 int status = 1;
708
709 if (!info)
710 goto finally;
711
712 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000713 if (filename != NULL) {
714 lineno = PySequence_GetItem(info, 1);
715 if (lineno != NULL) {
716 offset = PySequence_GetItem(info, 2);
717 if (offset != NULL) {
718 text = PySequence_GetItem(info, 3);
719 if (text != NULL) {
720 status =
721 PyObject_SetAttrString(self, "filename", filename)
722 || PyObject_SetAttrString(self, "lineno", lineno)
723 || PyObject_SetAttrString(self, "offset", offset)
724 || PyObject_SetAttrString(self, "text", text);
725 Py_DECREF(text);
726 }
727 Py_DECREF(offset);
728 }
729 Py_DECREF(lineno);
730 }
731 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000732 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000733 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000734
735 if (status)
736 goto finally;
737 }
738 Py_INCREF(Py_None);
739 rtnval = Py_None;
740
741 finally:
742 Py_DECREF(args);
743 return rtnval;
744}
745
746
Fred Drake185a29b2000-08-15 16:20:36 +0000747/* This is called "my_basename" instead of just "basename" to avoid name
748 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
749 defined, and Python does define that. */
750static char *
751my_basename(char *name)
752{
753 char *cp = name;
754 char *result = name;
755
756 if (name == NULL)
757 return "???";
758 while (*cp != '\0') {
759 if (*cp == SEP)
760 result = cp + 1;
761 ++cp;
762 }
763 return result;
764}
765
766
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000767static PyObject *
768SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000769{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000770 PyObject *msg;
771 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000772 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000773
Fred Drake1aba5772000-08-15 15:46:16 +0000774 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000775 return NULL;
776
777 if (!(msg = PyObject_GetAttrString(self, "msg")))
778 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000779
Barry Warsaw675ac282000-05-26 19:05:16 +0000780 str = PyObject_Str(msg);
781 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000782 result = str;
783
784 /* XXX -- do all the additional formatting with filename and
785 lineno here */
786
787 if (PyString_Check(str)) {
788 int have_filename = 0;
789 int have_lineno = 0;
790 char *buffer = NULL;
791
Barry Warsaw77c9f502000-08-16 19:43:17 +0000792 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000793 have_filename = PyString_Check(filename);
794 else
795 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000796
797 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000798 have_lineno = PyInt_Check(lineno);
799 else
800 PyErr_Clear();
801
802 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000803 int bufsize = PyString_GET_SIZE(str) + 64;
804 if (have_filename)
805 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000806
807 buffer = PyMem_Malloc(bufsize);
808 if (buffer != NULL) {
809 if (have_filename && have_lineno)
Fred Drake04e654a2000-08-18 19:53:25 +0000810 sprintf(buffer, "%s (%s, line %ld)",
Fred Drake1aba5772000-08-15 15:46:16 +0000811 PyString_AS_STRING(str),
Fred Drake185a29b2000-08-15 16:20:36 +0000812 my_basename(PyString_AS_STRING(filename)),
Fred Drake1aba5772000-08-15 15:46:16 +0000813 PyInt_AsLong(lineno));
814 else if (have_filename)
815 sprintf(buffer, "%s (%s)",
816 PyString_AS_STRING(str),
Fred Drake185a29b2000-08-15 16:20:36 +0000817 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000818 else if (have_lineno)
Fred Drake04e654a2000-08-18 19:53:25 +0000819 sprintf(buffer, "%s (line %ld)",
Fred Drake1aba5772000-08-15 15:46:16 +0000820 PyString_AS_STRING(str),
821 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000822
Fred Drake1aba5772000-08-15 15:46:16 +0000823 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000824 PyMem_FREE(buffer);
825
Fred Drake1aba5772000-08-15 15:46:16 +0000826 if (result == NULL)
827 result = str;
828 else
829 Py_DECREF(str);
830 }
831 }
832 Py_XDECREF(filename);
833 Py_XDECREF(lineno);
834 }
835 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000836}
837
838
839PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000840 {"__init__", SyntaxError__init__, METH_VARARGS},
841 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000842 {NULL, NULL}
843};
844
845
846
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000847/* Exception doc strings */
848
Barry Warsaw675ac282000-05-26 19:05:16 +0000849static char
850AssertionError__doc__[] = "Assertion failed.";
851
852static char
853LookupError__doc__[] = "Base class for lookup errors.";
854
855static char
856IndexError__doc__[] = "Sequence index out of range.";
857
858static char
859KeyError__doc__[] = "Mapping key not found.";
860
861static char
862ArithmeticError__doc__[] = "Base class for arithmetic errors.";
863
864static char
865OverflowError__doc__[] = "Result too large to be represented.";
866
867static char
868ZeroDivisionError__doc__[] =
869"Second argument to a division or modulo operation was zero.";
870
871static char
872FloatingPointError__doc__[] = "Floating point operation failed.";
873
874static char
875ValueError__doc__[] = "Inappropriate argument value (of correct type).";
876
877static char
878UnicodeError__doc__[] = "Unicode related error.";
879
880static char
881SystemError__doc__[] = "Internal error in the Python interpreter.\n\
882\n\
883Please report this to the Python maintainer, along with the traceback,\n\
884the Python version, and the hardware/OS platform and version.";
885
886static char
887MemoryError__doc__[] = "Out of memory.";
888
Fred Drake85f36392000-07-11 17:53:00 +0000889static char
890IndentationError__doc__[] = "Improper indentation.";
891
892static char
893TabError__doc__[] = "Improper mixture of spaces and tabs.";
894
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000895/* Warning category docstrings */
896
897static char
898Warning__doc__[] = "Base class for warning categories.";
899
900static char
901UserWarning__doc__[] = "Base class for warnings generated by user code.";
902
903static char
904DeprecationWarning__doc__[] =
905"Base class for warnings about deprecated features.";
906
907static char
908SyntaxWarning__doc__[] = "Base class for warnings about dubious syntax.";
909
910static char
911RuntimeWarning__doc__[] =
912"Base class for warnings about dubious runtime behavior.";
913
Barry Warsaw675ac282000-05-26 19:05:16 +0000914
915
916/* module global functions */
917static PyMethodDef functions[] = {
918 /* Sentinel */
919 {NULL, NULL}
920};
921
922
923
924/* Global C API defined exceptions */
925
926PyObject *PyExc_Exception;
927PyObject *PyExc_StandardError;
928PyObject *PyExc_ArithmeticError;
929PyObject *PyExc_LookupError;
930
931PyObject *PyExc_AssertionError;
932PyObject *PyExc_AttributeError;
933PyObject *PyExc_EOFError;
934PyObject *PyExc_FloatingPointError;
935PyObject *PyExc_EnvironmentError;
936PyObject *PyExc_IOError;
937PyObject *PyExc_OSError;
938PyObject *PyExc_ImportError;
939PyObject *PyExc_IndexError;
940PyObject *PyExc_KeyError;
941PyObject *PyExc_KeyboardInterrupt;
942PyObject *PyExc_MemoryError;
943PyObject *PyExc_NameError;
944PyObject *PyExc_OverflowError;
945PyObject *PyExc_RuntimeError;
946PyObject *PyExc_NotImplementedError;
947PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +0000948PyObject *PyExc_IndentationError;
949PyObject *PyExc_TabError;
Barry Warsaw675ac282000-05-26 19:05:16 +0000950PyObject *PyExc_SystemError;
951PyObject *PyExc_SystemExit;
952PyObject *PyExc_UnboundLocalError;
953PyObject *PyExc_UnicodeError;
954PyObject *PyExc_TypeError;
955PyObject *PyExc_ValueError;
956PyObject *PyExc_ZeroDivisionError;
957#ifdef MS_WINDOWS
958PyObject *PyExc_WindowsError;
959#endif
960
961/* Pre-computed MemoryError instance. Best to create this as early as
962 * possibly and not wait until a MemoryError is actually raised!
963 */
964PyObject *PyExc_MemoryErrorInst;
965
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000966/* Predefined warning categories */
967PyObject *PyExc_Warning;
968PyObject *PyExc_UserWarning;
969PyObject *PyExc_DeprecationWarning;
970PyObject *PyExc_SyntaxWarning;
971PyObject *PyExc_RuntimeWarning;
972
Barry Warsaw675ac282000-05-26 19:05:16 +0000973
974
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000975/* mapping between exception names and their PyObject ** */
976static struct {
977 char *name;
978 PyObject **exc;
979 PyObject **base; /* NULL == PyExc_StandardError */
980 char *docstr;
981 PyMethodDef *methods;
982 int (*classinit)(PyObject *);
983} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +0000984 /*
985 * The first three classes MUST appear in exactly this order
986 */
987 {"Exception", &PyExc_Exception},
988 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
989 StandardError__doc__},
990 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
991 /*
992 * The rest appear in depth-first order of the hierarchy
993 */
994 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
995 SystemExit_methods},
996 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
997 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
998 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
999 EnvironmentError_methods},
1000 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1001 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1002#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001003 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001004 WindowsError__doc__},
1005#endif /* MS_WINDOWS */
1006 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1007 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1008 {"NotImplementedError", &PyExc_NotImplementedError,
1009 &PyExc_RuntimeError, NotImplementedError__doc__},
1010 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1011 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1012 UnboundLocalError__doc__},
1013 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1014 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1015 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001016 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1017 IndentationError__doc__},
1018 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1019 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001020 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1021 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1022 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1023 IndexError__doc__},
1024 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
1025 KeyError__doc__},
1026 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1027 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1028 OverflowError__doc__},
1029 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1030 ZeroDivisionError__doc__},
1031 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1032 FloatingPointError__doc__},
1033 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1034 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
1035 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1036 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001037 /* Warning categories */
1038 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1039 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1040 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1041 DeprecationWarning__doc__},
1042 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
1043 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1044 RuntimeWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001045 /* Sentinel */
1046 {NULL}
1047};
1048
1049
1050
Tim Peters5687ffe2001-02-28 16:44:18 +00001051DL_EXPORT(void)
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001052init_exceptions(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001053{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001054 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001055 int modnamesz = strlen(modulename);
1056 int i;
1057
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001058 PyObject *me = Py_InitModule(modulename, functions);
1059 PyObject *mydict = PyModule_GetDict(me);
1060 PyObject *bltinmod = PyImport_ImportModule("__builtin__");
1061 PyObject *bdict = PyModule_GetDict(bltinmod);
1062 PyObject *doc = PyString_FromString(module__doc__);
1063 PyObject *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001064
1065 PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001066 Py_DECREF(doc);
Barry Warsaw675ac282000-05-26 19:05:16 +00001067 if (PyErr_Occurred())
1068 Py_FatalError("exceptions bootstrapping error.");
1069
1070 /* This is the base class of all exceptions, so make it first. */
1071 if (make_Exception(modulename) ||
1072 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1073 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1074 {
1075 Py_FatalError("Base class `Exception' could not be created.");
1076 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001077
Barry Warsaw675ac282000-05-26 19:05:16 +00001078 /* Now we can programmatically create all the remaining exceptions.
1079 * Remember to start the loop at 1 to skip Exceptions.
1080 */
1081 for (i=1; exctable[i].name; i++) {
1082 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001083 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1084 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001085
1086 (void)strcpy(cname, modulename);
1087 (void)strcat(cname, ".");
1088 (void)strcat(cname, exctable[i].name);
1089
1090 if (exctable[i].base == 0)
1091 base = PyExc_StandardError;
1092 else
1093 base = *exctable[i].base;
1094
1095 status = make_class(exctable[i].exc, base, cname,
1096 exctable[i].methods,
1097 exctable[i].docstr);
1098
1099 PyMem_DEL(cname);
1100
1101 if (status)
1102 Py_FatalError("Standard exception classes could not be created.");
1103
1104 if (exctable[i].classinit) {
1105 status = (*exctable[i].classinit)(*exctable[i].exc);
1106 if (status)
1107 Py_FatalError("An exception class could not be initialized.");
1108 }
1109
1110 /* Now insert the class into both this module and the __builtin__
1111 * module.
1112 */
1113 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1114 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1115 {
1116 Py_FatalError("Module dictionary insertion problem.");
1117 }
1118 }
1119
1120 /* Now we need to pre-allocate a MemoryError instance */
1121 args = Py_BuildValue("()");
1122 if (!args ||
1123 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1124 {
1125 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1126 }
1127 Py_DECREF(args);
1128
1129 /* We're done with __builtin__ */
1130 Py_DECREF(bltinmod);
1131}
1132
1133
Tim Peters5687ffe2001-02-28 16:44:18 +00001134DL_EXPORT(void)
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}