blob: 214d8e5e623bd5aa3fd709c10d289b6fdf44edfc [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\
Guido van Rossum59d1d2b2001-04-20 19:13:02 +000055 +-- StopIteration\n\
Barry Warsaw675ac282000-05-26 19:05:16 +000056 +-- StandardError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +000057 | |\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 Warsaw675ac282000-05-26 19:05:16 +0000104 |\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000105 +-- UserWarning\n\
106 +-- DeprecationWarning\n\
107 +-- SyntaxWarning\n\
108 +-- RuntimeWarning";
Barry Warsaw675ac282000-05-26 19:05:16 +0000109
110
111/* Helper function for populating a dictionary with method wrappers. */
112static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000113populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +0000114{
115 if (!methods)
116 return 0;
117
118 while (methods->ml_name) {
119 /* get a wrapper for the built-in function */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000120 PyObject *func = PyCFunction_New(methods, NULL);
121 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +0000122 int status;
123
124 if (!func)
125 return -1;
126
127 /* turn the function into an unbound method */
128 if (!(meth = PyMethod_New(func, NULL, klass))) {
129 Py_DECREF(func);
130 return -1;
131 }
Barry Warsaw9667ed22001-01-23 16:08:34 +0000132
Barry Warsaw675ac282000-05-26 19:05:16 +0000133 /* add method to dictionary */
134 status = PyDict_SetItemString(dict, methods->ml_name, meth);
135 Py_DECREF(meth);
136 Py_DECREF(func);
137
138 /* stop now if an error occurred, otherwise do the next method */
139 if (status)
140 return status;
141
142 methods++;
143 }
144 return 0;
145}
146
Barry Warsaw9667ed22001-01-23 16:08:34 +0000147
Barry Warsaw675ac282000-05-26 19:05:16 +0000148
149/* This function is used to create all subsequent exception classes. */
150static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000151make_class(PyObject **klass, PyObject *base,
152 char *name, PyMethodDef *methods,
153 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +0000154{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000155 PyObject *dict = PyDict_New();
156 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000157 int status = -1;
158
159 if (!dict)
160 return -1;
161
162 /* If an error occurs from here on, goto finally instead of explicitly
163 * returning NULL.
164 */
165
166 if (docstr) {
167 if (!(str = PyString_FromString(docstr)))
168 goto finally;
169 if (PyDict_SetItemString(dict, "__doc__", str))
170 goto finally;
171 }
172
173 if (!(*klass = PyErr_NewException(name, base, dict)))
174 goto finally;
175
176 if (populate_methods(*klass, dict, methods)) {
177 Py_DECREF(*klass);
178 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000179 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000180 }
181
182 status = 0;
183
184 finally:
185 Py_XDECREF(dict);
186 Py_XDECREF(str);
187 return status;
188}
189
190
191/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000192static PyObject *
193get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000194{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000195 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000196 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000197 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000198 if (PyExc_TypeError) {
199 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000200 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000201 }
202 return NULL;
203 }
204 return self;
205}
206
207
208
209/* Notes on bootstrapping the exception classes.
210 *
211 * First thing we create is the base class for all exceptions, called
212 * appropriately enough: Exception. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000213 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000214 * for TypeError, which can conditionally exist.
215 *
216 * Next, StandardError is created (which is quite simple) followed by
217 * TypeError, because the instantiation of other exceptions can potentially
218 * throw a TypeError. Once these exceptions are created, all the others
219 * can be created in any order. See the static exctable below for the
220 * explicit bootstrap order.
221 *
222 * All classes after Exception can be created using PyErr_NewException().
223 */
224
225static char
226Exception__doc__[] = "Common base class for all exceptions.";
227
228
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000229static PyObject *
230Exception__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000231{
232 int status;
233
234 if (!(self = get_self(args)))
235 return NULL;
236
237 /* set args attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000238 /* XXX size is only a hint */
239 args = PySequence_GetSlice(args, 1, PySequence_Size(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000240 if (!args)
241 return NULL;
242 status = PyObject_SetAttrString(self, "args", args);
243 Py_DECREF(args);
244 if (status < 0)
245 return NULL;
246
247 Py_INCREF(Py_None);
248 return Py_None;
249}
250
251
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000252static PyObject *
253Exception__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000254{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000255 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000256
Fred Drake1aba5772000-08-15 15:46:16 +0000257 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000258 return NULL;
259
260 args = PyObject_GetAttrString(self, "args");
261 if (!args)
262 return NULL;
263
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000264 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000265 case 0:
266 out = PyString_FromString("");
267 break;
268 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000269 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000270 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000271 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000272 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000273 Py_DECREF(tmp);
274 }
275 else
276 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000277 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000278 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000279 default:
280 out = PyObject_Str(args);
281 break;
282 }
283
284 Py_DECREF(args);
285 return out;
286}
287
288
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000289static PyObject *
290Exception__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000291{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000292 PyObject *out;
293 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000294
Fred Drake1aba5772000-08-15 15:46:16 +0000295 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000296 return NULL;
297
298 args = PyObject_GetAttrString(self, "args");
299 if (!args)
300 return NULL;
301
302 out = PyObject_GetItem(args, index);
303 Py_DECREF(args);
304 return out;
305}
306
307
308static PyMethodDef
309Exception_methods[] = {
310 /* methods for the Exception class */
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000311 { "__getitem__", Exception__getitem__, METH_VARARGS},
312 { "__str__", Exception__str__, METH_VARARGS},
313 { "__init__", Exception__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000314 { NULL, NULL }
315};
316
317
318static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000319make_Exception(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000320{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000321 PyObject *dict = PyDict_New();
322 PyObject *str = NULL;
323 PyObject *name = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000324 int status = -1;
325
326 if (!dict)
327 return -1;
328
329 /* If an error occurs from here on, goto finally instead of explicitly
330 * returning NULL.
331 */
332
333 if (!(str = PyString_FromString(modulename)))
334 goto finally;
335 if (PyDict_SetItemString(dict, "__module__", str))
336 goto finally;
337 Py_DECREF(str);
338 if (!(str = PyString_FromString(Exception__doc__)))
339 goto finally;
340 if (PyDict_SetItemString(dict, "__doc__", str))
341 goto finally;
342
343 if (!(name = PyString_FromString("Exception")))
344 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000345
Barry Warsaw675ac282000-05-26 19:05:16 +0000346 if (!(PyExc_Exception = PyClass_New(NULL, dict, name)))
347 goto finally;
348
349 /* Now populate the dictionary with the method suite */
350 if (populate_methods(PyExc_Exception, dict, Exception_methods))
351 /* Don't need to reclaim PyExc_Exception here because that'll
352 * happen during interpreter shutdown.
353 */
354 goto finally;
355
356 status = 0;
357
358 finally:
359 Py_XDECREF(dict);
360 Py_XDECREF(str);
361 Py_XDECREF(name);
362 return status;
363}
364
365
366
367static char
368StandardError__doc__[] = "Base class for all standard Python exceptions.";
369
370static char
371TypeError__doc__[] = "Inappropriate argument type.";
372
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000373static char
374StopIteration__doc__[] = "Signal the end from iterator.next().";
375
Barry Warsaw675ac282000-05-26 19:05:16 +0000376
377
378static char
379SystemExit__doc__[] = "Request to exit from the interpreter.";
380
381
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000382static PyObject *
383SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000384{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000385 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000386 int status;
387
388 if (!(self = get_self(args)))
389 return NULL;
390
391 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000392 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000393 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000394
Barry Warsaw675ac282000-05-26 19:05:16 +0000395 status = PyObject_SetAttrString(self, "args", args);
396 if (status < 0) {
397 Py_DECREF(args);
398 return NULL;
399 }
400
401 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000402 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000403 case 0:
404 Py_INCREF(Py_None);
405 code = Py_None;
406 break;
407 case 1:
408 code = PySequence_GetItem(args, 0);
409 break;
410 default:
411 Py_INCREF(args);
412 code = args;
413 break;
414 }
415
416 status = PyObject_SetAttrString(self, "code", code);
417 Py_DECREF(code);
418 Py_DECREF(args);
419 if (status < 0)
420 return NULL;
421
422 Py_INCREF(Py_None);
423 return Py_None;
424}
425
426
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000427static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000428 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000429 {NULL, NULL}
430};
431
432
433
434static char
435KeyboardInterrupt__doc__[] = "Program interrupted by user.";
436
437static char
438ImportError__doc__[] =
439"Import can't find module, or can't find name in module.";
440
441
442
443static char
444EnvironmentError__doc__[] = "Base class for I/O related errors.";
445
446
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000447static PyObject *
448EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000449{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000450 PyObject *item0 = NULL;
451 PyObject *item1 = NULL;
452 PyObject *item2 = NULL;
453 PyObject *subslice = NULL;
454 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000455
456 if (!(self = get_self(args)))
457 return NULL;
458
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000459 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000460 return NULL;
461
462 if (PyObject_SetAttrString(self, "args", args) ||
463 PyObject_SetAttrString(self, "errno", Py_None) ||
464 PyObject_SetAttrString(self, "strerror", Py_None) ||
465 PyObject_SetAttrString(self, "filename", Py_None))
466 {
467 goto finally;
468 }
469
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000470 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000471 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000472 /* Where a function has a single filename, such as open() or some
473 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
474 * called, giving a third argument which is the filename. But, so
475 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000476 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000477 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000478 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000479 * we hack args so that it only contains two items. This also
480 * means we need our own __str__() which prints out the filename
481 * when it was supplied.
482 */
483 item0 = PySequence_GetItem(args, 0);
484 item1 = PySequence_GetItem(args, 1);
485 item2 = PySequence_GetItem(args, 2);
486 if (!item0 || !item1 || !item2)
487 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000488
Barry Warsaw675ac282000-05-26 19:05:16 +0000489 if (PyObject_SetAttrString(self, "errno", item0) ||
490 PyObject_SetAttrString(self, "strerror", item1) ||
491 PyObject_SetAttrString(self, "filename", item2))
492 {
493 goto finally;
494 }
495
496 subslice = PySequence_GetSlice(args, 0, 2);
497 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
498 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000499 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000500
501 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000502 /* Used when PyErr_SetFromErrno() is called and no filename
503 * argument is given.
504 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000505 item0 = PySequence_GetItem(args, 0);
506 item1 = PySequence_GetItem(args, 1);
507 if (!item0 || !item1)
508 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000509
Barry Warsaw675ac282000-05-26 19:05:16 +0000510 if (PyObject_SetAttrString(self, "errno", item0) ||
511 PyObject_SetAttrString(self, "strerror", item1))
512 {
513 goto finally;
514 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000515 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000516 }
517
518 Py_INCREF(Py_None);
519 rtnval = Py_None;
520
521 finally:
522 Py_DECREF(args);
523 Py_XDECREF(item0);
524 Py_XDECREF(item1);
525 Py_XDECREF(item2);
526 Py_XDECREF(subslice);
527 return rtnval;
528}
529
530
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000531static PyObject *
532EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000533{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000534 PyObject *originalself = self;
535 PyObject *filename;
536 PyObject *serrno;
537 PyObject *strerror;
538 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000539
Fred Drake1aba5772000-08-15 15:46:16 +0000540 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000541 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000542
Barry Warsaw675ac282000-05-26 19:05:16 +0000543 filename = PyObject_GetAttrString(self, "filename");
544 serrno = PyObject_GetAttrString(self, "errno");
545 strerror = PyObject_GetAttrString(self, "strerror");
546 if (!filename || !serrno || !strerror)
547 goto finally;
548
549 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000550 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
551 PyObject *repr = PyObject_Repr(filename);
552 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000553
554 if (!fmt || !repr || !tuple) {
555 Py_XDECREF(fmt);
556 Py_XDECREF(repr);
557 Py_XDECREF(tuple);
558 goto finally;
559 }
560
561 PyTuple_SET_ITEM(tuple, 0, serrno);
562 PyTuple_SET_ITEM(tuple, 1, strerror);
563 PyTuple_SET_ITEM(tuple, 2, repr);
564
565 rtnval = PyString_Format(fmt, tuple);
566
567 Py_DECREF(fmt);
568 Py_DECREF(tuple);
569 /* already freed because tuple owned only reference */
570 serrno = NULL;
571 strerror = NULL;
572 }
573 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000574 PyObject *fmt = PyString_FromString("[Errno %s] %s");
575 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000576
577 if (!fmt || !tuple) {
578 Py_XDECREF(fmt);
579 Py_XDECREF(tuple);
580 goto finally;
581 }
582
583 PyTuple_SET_ITEM(tuple, 0, serrno);
584 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000585
Barry Warsaw675ac282000-05-26 19:05:16 +0000586 rtnval = PyString_Format(fmt, tuple);
587
588 Py_DECREF(fmt);
589 Py_DECREF(tuple);
590 /* already freed because tuple owned only reference */
591 serrno = NULL;
592 strerror = NULL;
593 }
594 else
595 /* The original Python code said:
596 *
597 * return StandardError.__str__(self)
598 *
599 * but there is no StandardError__str__() function; we happen to
600 * know that's just a pass through to Exception__str__().
601 */
602 rtnval = Exception__str__(originalself, args);
603
604 finally:
605 Py_XDECREF(filename);
606 Py_XDECREF(serrno);
607 Py_XDECREF(strerror);
608 return rtnval;
609}
610
611
612static
613PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000614 {"__init__", EnvironmentError__init__, METH_VARARGS},
615 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000616 {NULL, NULL}
617};
618
619
620
621
622static char
623IOError__doc__[] = "I/O operation failed.";
624
625static char
626OSError__doc__[] = "OS system call failed.";
627
628#ifdef MS_WINDOWS
629static char
630WindowsError__doc__[] = "MS-Windows OS system call failed.";
631#endif /* MS_WINDOWS */
632
633static char
634EOFError__doc__[] = "Read beyond end of file.";
635
636static char
637RuntimeError__doc__[] = "Unspecified run-time error.";
638
639static char
640NotImplementedError__doc__[] =
641"Method or function hasn't been implemented yet.";
642
643static char
644NameError__doc__[] = "Name not found globally.";
645
646static char
647UnboundLocalError__doc__[] =
648"Local name referenced but not bound to a value.";
649
650static char
651AttributeError__doc__[] = "Attribute not found.";
652
653
654
655static char
656SyntaxError__doc__[] = "Invalid syntax.";
657
658
659static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000660SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000661{
Barry Warsaw87bec352000-08-18 05:05:37 +0000662 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000663 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000664
665 /* Additional class-creation time initializations */
666 if (!emptystring ||
667 PyObject_SetAttrString(klass, "msg", emptystring) ||
668 PyObject_SetAttrString(klass, "filename", Py_None) ||
669 PyObject_SetAttrString(klass, "lineno", Py_None) ||
670 PyObject_SetAttrString(klass, "offset", Py_None) ||
671 PyObject_SetAttrString(klass, "text", Py_None))
672 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000673 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000674 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000675 Py_XDECREF(emptystring);
676 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000677}
678
679
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000680static PyObject *
681SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000682{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000683 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000684 int lenargs;
685
686 if (!(self = get_self(args)))
687 return NULL;
688
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000689 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000690 return NULL;
691
692 if (PyObject_SetAttrString(self, "args", args))
693 goto finally;
694
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000695 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000696 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000697 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000698 int status;
699
700 if (!item0)
701 goto finally;
702 status = PyObject_SetAttrString(self, "msg", item0);
703 Py_DECREF(item0);
704 if (status)
705 goto finally;
706 }
707 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000708 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000709 PyObject *filename = NULL, *lineno = NULL;
710 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000711 int status = 1;
712
713 if (!info)
714 goto finally;
715
716 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000717 if (filename != NULL) {
718 lineno = PySequence_GetItem(info, 1);
719 if (lineno != NULL) {
720 offset = PySequence_GetItem(info, 2);
721 if (offset != NULL) {
722 text = PySequence_GetItem(info, 3);
723 if (text != NULL) {
724 status =
725 PyObject_SetAttrString(self, "filename", filename)
726 || PyObject_SetAttrString(self, "lineno", lineno)
727 || PyObject_SetAttrString(self, "offset", offset)
728 || PyObject_SetAttrString(self, "text", text);
729 Py_DECREF(text);
730 }
731 Py_DECREF(offset);
732 }
733 Py_DECREF(lineno);
734 }
735 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000736 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000737 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000738
739 if (status)
740 goto finally;
741 }
742 Py_INCREF(Py_None);
743 rtnval = Py_None;
744
745 finally:
746 Py_DECREF(args);
747 return rtnval;
748}
749
750
Fred Drake185a29b2000-08-15 16:20:36 +0000751/* This is called "my_basename" instead of just "basename" to avoid name
752 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
753 defined, and Python does define that. */
754static char *
755my_basename(char *name)
756{
757 char *cp = name;
758 char *result = name;
759
760 if (name == NULL)
761 return "???";
762 while (*cp != '\0') {
763 if (*cp == SEP)
764 result = cp + 1;
765 ++cp;
766 }
767 return result;
768}
769
770
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000771static PyObject *
772SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000773{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000774 PyObject *msg;
775 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000776 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000777
Fred Drake1aba5772000-08-15 15:46:16 +0000778 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000779 return NULL;
780
781 if (!(msg = PyObject_GetAttrString(self, "msg")))
782 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000783
Barry Warsaw675ac282000-05-26 19:05:16 +0000784 str = PyObject_Str(msg);
785 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000786 result = str;
787
788 /* XXX -- do all the additional formatting with filename and
789 lineno here */
790
791 if (PyString_Check(str)) {
792 int have_filename = 0;
793 int have_lineno = 0;
794 char *buffer = NULL;
795
Barry Warsaw77c9f502000-08-16 19:43:17 +0000796 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000797 have_filename = PyString_Check(filename);
798 else
799 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000800
801 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000802 have_lineno = PyInt_Check(lineno);
803 else
804 PyErr_Clear();
805
806 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000807 int bufsize = PyString_GET_SIZE(str) + 64;
808 if (have_filename)
809 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000810
811 buffer = PyMem_Malloc(bufsize);
812 if (buffer != NULL) {
813 if (have_filename && have_lineno)
Fred Drake04e654a2000-08-18 19:53:25 +0000814 sprintf(buffer, "%s (%s, line %ld)",
Fred Drake1aba5772000-08-15 15:46:16 +0000815 PyString_AS_STRING(str),
Fred Drake185a29b2000-08-15 16:20:36 +0000816 my_basename(PyString_AS_STRING(filename)),
Fred Drake1aba5772000-08-15 15:46:16 +0000817 PyInt_AsLong(lineno));
818 else if (have_filename)
819 sprintf(buffer, "%s (%s)",
820 PyString_AS_STRING(str),
Fred Drake185a29b2000-08-15 16:20:36 +0000821 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000822 else if (have_lineno)
Fred Drake04e654a2000-08-18 19:53:25 +0000823 sprintf(buffer, "%s (line %ld)",
Fred Drake1aba5772000-08-15 15:46:16 +0000824 PyString_AS_STRING(str),
825 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000826
Fred Drake1aba5772000-08-15 15:46:16 +0000827 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000828 PyMem_FREE(buffer);
829
Fred Drake1aba5772000-08-15 15:46:16 +0000830 if (result == NULL)
831 result = str;
832 else
833 Py_DECREF(str);
834 }
835 }
836 Py_XDECREF(filename);
837 Py_XDECREF(lineno);
838 }
839 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000840}
841
842
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000843static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000844 {"__init__", SyntaxError__init__, METH_VARARGS},
845 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000846 {NULL, NULL}
847};
848
849
850
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000851/* Exception doc strings */
852
Barry Warsaw675ac282000-05-26 19:05:16 +0000853static char
854AssertionError__doc__[] = "Assertion failed.";
855
856static char
857LookupError__doc__[] = "Base class for lookup errors.";
858
859static char
860IndexError__doc__[] = "Sequence index out of range.";
861
862static char
863KeyError__doc__[] = "Mapping key not found.";
864
865static char
866ArithmeticError__doc__[] = "Base class for arithmetic errors.";
867
868static char
869OverflowError__doc__[] = "Result too large to be represented.";
870
871static char
872ZeroDivisionError__doc__[] =
873"Second argument to a division or modulo operation was zero.";
874
875static char
876FloatingPointError__doc__[] = "Floating point operation failed.";
877
878static char
879ValueError__doc__[] = "Inappropriate argument value (of correct type).";
880
881static char
882UnicodeError__doc__[] = "Unicode related error.";
883
884static char
885SystemError__doc__[] = "Internal error in the Python interpreter.\n\
886\n\
887Please report this to the Python maintainer, along with the traceback,\n\
888the Python version, and the hardware/OS platform and version.";
889
890static char
891MemoryError__doc__[] = "Out of memory.";
892
Fred Drake85f36392000-07-11 17:53:00 +0000893static char
894IndentationError__doc__[] = "Improper indentation.";
895
896static char
897TabError__doc__[] = "Improper mixture of spaces and tabs.";
898
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000899/* Warning category docstrings */
900
901static char
902Warning__doc__[] = "Base class for warning categories.";
903
904static char
905UserWarning__doc__[] = "Base class for warnings generated by user code.";
906
907static char
908DeprecationWarning__doc__[] =
909"Base class for warnings about deprecated features.";
910
911static char
912SyntaxWarning__doc__[] = "Base class for warnings about dubious syntax.";
913
914static char
915RuntimeWarning__doc__[] =
916"Base class for warnings about dubious runtime behavior.";
917
Barry Warsaw675ac282000-05-26 19:05:16 +0000918
919
920/* module global functions */
921static PyMethodDef functions[] = {
922 /* Sentinel */
923 {NULL, NULL}
924};
925
926
927
928/* Global C API defined exceptions */
929
930PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000931PyObject *PyExc_StopIteration;
Barry Warsaw675ac282000-05-26 19:05:16 +0000932PyObject *PyExc_StandardError;
933PyObject *PyExc_ArithmeticError;
934PyObject *PyExc_LookupError;
935
936PyObject *PyExc_AssertionError;
937PyObject *PyExc_AttributeError;
938PyObject *PyExc_EOFError;
939PyObject *PyExc_FloatingPointError;
940PyObject *PyExc_EnvironmentError;
941PyObject *PyExc_IOError;
942PyObject *PyExc_OSError;
943PyObject *PyExc_ImportError;
944PyObject *PyExc_IndexError;
945PyObject *PyExc_KeyError;
946PyObject *PyExc_KeyboardInterrupt;
947PyObject *PyExc_MemoryError;
948PyObject *PyExc_NameError;
949PyObject *PyExc_OverflowError;
950PyObject *PyExc_RuntimeError;
951PyObject *PyExc_NotImplementedError;
952PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +0000953PyObject *PyExc_IndentationError;
954PyObject *PyExc_TabError;
Barry Warsaw675ac282000-05-26 19:05:16 +0000955PyObject *PyExc_SystemError;
956PyObject *PyExc_SystemExit;
957PyObject *PyExc_UnboundLocalError;
958PyObject *PyExc_UnicodeError;
959PyObject *PyExc_TypeError;
960PyObject *PyExc_ValueError;
961PyObject *PyExc_ZeroDivisionError;
962#ifdef MS_WINDOWS
963PyObject *PyExc_WindowsError;
964#endif
965
966/* Pre-computed MemoryError instance. Best to create this as early as
967 * possibly and not wait until a MemoryError is actually raised!
968 */
969PyObject *PyExc_MemoryErrorInst;
970
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000971/* Predefined warning categories */
972PyObject *PyExc_Warning;
973PyObject *PyExc_UserWarning;
974PyObject *PyExc_DeprecationWarning;
975PyObject *PyExc_SyntaxWarning;
976PyObject *PyExc_RuntimeWarning;
977
Barry Warsaw675ac282000-05-26 19:05:16 +0000978
979
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000980/* mapping between exception names and their PyObject ** */
981static struct {
982 char *name;
983 PyObject **exc;
984 PyObject **base; /* NULL == PyExc_StandardError */
985 char *docstr;
986 PyMethodDef *methods;
987 int (*classinit)(PyObject *);
988} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +0000989 /*
990 * The first three classes MUST appear in exactly this order
991 */
992 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000993 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
994 StopIteration__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +0000995 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
996 StandardError__doc__},
997 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
998 /*
999 * The rest appear in depth-first order of the hierarchy
1000 */
1001 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1002 SystemExit_methods},
1003 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1004 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1005 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1006 EnvironmentError_methods},
1007 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1008 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1009#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001010 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001011 WindowsError__doc__},
1012#endif /* MS_WINDOWS */
1013 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1014 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1015 {"NotImplementedError", &PyExc_NotImplementedError,
1016 &PyExc_RuntimeError, NotImplementedError__doc__},
1017 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1018 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1019 UnboundLocalError__doc__},
1020 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1021 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1022 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001023 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1024 IndentationError__doc__},
1025 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1026 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001027 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1028 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1029 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1030 IndexError__doc__},
1031 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
1032 KeyError__doc__},
1033 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1034 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1035 OverflowError__doc__},
1036 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1037 ZeroDivisionError__doc__},
1038 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1039 FloatingPointError__doc__},
1040 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1041 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
1042 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1043 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001044 /* Warning categories */
1045 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1046 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1047 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1048 DeprecationWarning__doc__},
1049 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
1050 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1051 RuntimeWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001052 /* Sentinel */
1053 {NULL}
1054};
1055
1056
1057
Tim Peters5687ffe2001-02-28 16:44:18 +00001058DL_EXPORT(void)
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001059init_exceptions(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001060{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001061 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001062 int modnamesz = strlen(modulename);
1063 int i;
1064
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001065 PyObject *me = Py_InitModule(modulename, functions);
1066 PyObject *mydict = PyModule_GetDict(me);
1067 PyObject *bltinmod = PyImport_ImportModule("__builtin__");
1068 PyObject *bdict = PyModule_GetDict(bltinmod);
1069 PyObject *doc = PyString_FromString(module__doc__);
1070 PyObject *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001071
1072 PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001073 Py_DECREF(doc);
Barry Warsaw675ac282000-05-26 19:05:16 +00001074 if (PyErr_Occurred())
1075 Py_FatalError("exceptions bootstrapping error.");
1076
1077 /* This is the base class of all exceptions, so make it first. */
1078 if (make_Exception(modulename) ||
1079 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1080 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1081 {
1082 Py_FatalError("Base class `Exception' could not be created.");
1083 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001084
Barry Warsaw675ac282000-05-26 19:05:16 +00001085 /* Now we can programmatically create all the remaining exceptions.
1086 * Remember to start the loop at 1 to skip Exceptions.
1087 */
1088 for (i=1; exctable[i].name; i++) {
1089 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001090 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1091 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001092
1093 (void)strcpy(cname, modulename);
1094 (void)strcat(cname, ".");
1095 (void)strcat(cname, exctable[i].name);
1096
1097 if (exctable[i].base == 0)
1098 base = PyExc_StandardError;
1099 else
1100 base = *exctable[i].base;
1101
1102 status = make_class(exctable[i].exc, base, cname,
1103 exctable[i].methods,
1104 exctable[i].docstr);
1105
1106 PyMem_DEL(cname);
1107
1108 if (status)
1109 Py_FatalError("Standard exception classes could not be created.");
1110
1111 if (exctable[i].classinit) {
1112 status = (*exctable[i].classinit)(*exctable[i].exc);
1113 if (status)
1114 Py_FatalError("An exception class could not be initialized.");
1115 }
1116
1117 /* Now insert the class into both this module and the __builtin__
1118 * module.
1119 */
1120 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1121 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1122 {
1123 Py_FatalError("Module dictionary insertion problem.");
1124 }
1125 }
1126
1127 /* Now we need to pre-allocate a MemoryError instance */
1128 args = Py_BuildValue("()");
1129 if (!args ||
1130 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1131 {
1132 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1133 }
1134 Py_DECREF(args);
1135
1136 /* We're done with __builtin__ */
1137 Py_DECREF(bltinmod);
1138}
1139
1140
Tim Peters5687ffe2001-02-28 16:44:18 +00001141DL_EXPORT(void)
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001142fini_exceptions(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001143{
1144 int i;
1145
1146 Py_XDECREF(PyExc_MemoryErrorInst);
1147 PyExc_MemoryErrorInst = NULL;
1148
1149 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001150 /* clear the class's dictionary, freeing up circular references
1151 * between the class and its methods.
1152 */
1153 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1154 PyDict_Clear(cdict);
1155 Py_DECREF(cdict);
1156
1157 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001158 Py_XDECREF(*exctable[i].exc);
1159 *exctable[i].exc = NULL;
1160 }
1161}