blob: 4d17529720e33583f60948e20c2ed4b4e7284eff [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\
Fred Drakebb9fa212001-10-05 21:50:08 +0000100 | +-- ReferenceError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000101 | +-- SystemError\n\
102 | +-- MemoryError\n\
103 |\n\
104 +---Warning\n\
Barry Warsaw675ac282000-05-26 19:05:16 +0000105 |\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000106 +-- UserWarning\n\
107 +-- DeprecationWarning\n\
108 +-- SyntaxWarning\n\
Guido van Rossumae347b32001-08-23 02:56:07 +0000109 +-- OverflowWarning\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000110 +-- RuntimeWarning";
Barry Warsaw675ac282000-05-26 19:05:16 +0000111
112
113/* Helper function for populating a dictionary with method wrappers. */
114static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000115populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +0000116{
117 if (!methods)
118 return 0;
119
120 while (methods->ml_name) {
121 /* get a wrapper for the built-in function */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000122 PyObject *func = PyCFunction_New(methods, NULL);
123 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +0000124 int status;
125
126 if (!func)
127 return -1;
128
129 /* turn the function into an unbound method */
130 if (!(meth = PyMethod_New(func, NULL, klass))) {
131 Py_DECREF(func);
132 return -1;
133 }
Barry Warsaw9667ed22001-01-23 16:08:34 +0000134
Barry Warsaw675ac282000-05-26 19:05:16 +0000135 /* add method to dictionary */
136 status = PyDict_SetItemString(dict, methods->ml_name, meth);
137 Py_DECREF(meth);
138 Py_DECREF(func);
139
140 /* stop now if an error occurred, otherwise do the next method */
141 if (status)
142 return status;
143
144 methods++;
145 }
146 return 0;
147}
148
Barry Warsaw9667ed22001-01-23 16:08:34 +0000149
Barry Warsaw675ac282000-05-26 19:05:16 +0000150
151/* This function is used to create all subsequent exception classes. */
152static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000153make_class(PyObject **klass, PyObject *base,
154 char *name, PyMethodDef *methods,
155 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +0000156{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000157 PyObject *dict = PyDict_New();
158 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000159 int status = -1;
160
161 if (!dict)
162 return -1;
163
164 /* If an error occurs from here on, goto finally instead of explicitly
165 * returning NULL.
166 */
167
168 if (docstr) {
169 if (!(str = PyString_FromString(docstr)))
170 goto finally;
171 if (PyDict_SetItemString(dict, "__doc__", str))
172 goto finally;
173 }
174
175 if (!(*klass = PyErr_NewException(name, base, dict)))
176 goto finally;
177
178 if (populate_methods(*klass, dict, methods)) {
179 Py_DECREF(*klass);
180 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000181 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000182 }
183
184 status = 0;
185
186 finally:
187 Py_XDECREF(dict);
188 Py_XDECREF(str);
189 return status;
190}
191
192
193/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000194static PyObject *
195get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000196{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000197 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000198 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000199 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000200 if (PyExc_TypeError) {
201 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000202 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000203 }
204 return NULL;
205 }
206 return self;
207}
208
209
210
211/* Notes on bootstrapping the exception classes.
212 *
213 * First thing we create is the base class for all exceptions, called
214 * appropriately enough: Exception. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000215 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000216 * for TypeError, which can conditionally exist.
217 *
218 * Next, StandardError is created (which is quite simple) followed by
219 * TypeError, because the instantiation of other exceptions can potentially
220 * throw a TypeError. Once these exceptions are created, all the others
221 * can be created in any order. See the static exctable below for the
222 * explicit bootstrap order.
223 *
224 * All classes after Exception can be created using PyErr_NewException().
225 */
226
227static char
228Exception__doc__[] = "Common base class for all exceptions.";
229
230
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000231static PyObject *
232Exception__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000233{
234 int status;
235
236 if (!(self = get_self(args)))
237 return NULL;
238
239 /* set args attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000240 /* XXX size is only a hint */
241 args = PySequence_GetSlice(args, 1, PySequence_Size(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000242 if (!args)
243 return NULL;
244 status = PyObject_SetAttrString(self, "args", args);
245 Py_DECREF(args);
246 if (status < 0)
247 return NULL;
248
249 Py_INCREF(Py_None);
250 return Py_None;
251}
252
253
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000254static PyObject *
255Exception__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000256{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000257 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000258
Fred Drake1aba5772000-08-15 15:46:16 +0000259 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000260 return NULL;
261
262 args = PyObject_GetAttrString(self, "args");
263 if (!args)
264 return NULL;
265
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000266 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000267 case 0:
268 out = PyString_FromString("");
269 break;
270 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000271 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000272 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000273 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000274 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000275 Py_DECREF(tmp);
276 }
277 else
278 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000279 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000280 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000281 default:
282 out = PyObject_Str(args);
283 break;
284 }
285
286 Py_DECREF(args);
287 return out;
288}
289
290
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000291static PyObject *
292Exception__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000293{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000294 PyObject *out;
295 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000296
Fred Drake1aba5772000-08-15 15:46:16 +0000297 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000298 return NULL;
299
300 args = PyObject_GetAttrString(self, "args");
301 if (!args)
302 return NULL;
303
304 out = PyObject_GetItem(args, index);
305 Py_DECREF(args);
306 return out;
307}
308
309
310static PyMethodDef
311Exception_methods[] = {
312 /* methods for the Exception class */
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000313 { "__getitem__", Exception__getitem__, METH_VARARGS},
314 { "__str__", Exception__str__, METH_VARARGS},
315 { "__init__", Exception__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000316 { NULL, NULL }
317};
318
319
320static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000321make_Exception(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000322{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000323 PyObject *dict = PyDict_New();
324 PyObject *str = NULL;
325 PyObject *name = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000326 int status = -1;
327
328 if (!dict)
329 return -1;
330
331 /* If an error occurs from here on, goto finally instead of explicitly
332 * returning NULL.
333 */
334
335 if (!(str = PyString_FromString(modulename)))
336 goto finally;
337 if (PyDict_SetItemString(dict, "__module__", str))
338 goto finally;
339 Py_DECREF(str);
340 if (!(str = PyString_FromString(Exception__doc__)))
341 goto finally;
342 if (PyDict_SetItemString(dict, "__doc__", str))
343 goto finally;
344
345 if (!(name = PyString_FromString("Exception")))
346 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000347
Barry Warsaw675ac282000-05-26 19:05:16 +0000348 if (!(PyExc_Exception = PyClass_New(NULL, dict, name)))
349 goto finally;
350
351 /* Now populate the dictionary with the method suite */
352 if (populate_methods(PyExc_Exception, dict, Exception_methods))
353 /* Don't need to reclaim PyExc_Exception here because that'll
354 * happen during interpreter shutdown.
355 */
356 goto finally;
357
358 status = 0;
359
360 finally:
361 Py_XDECREF(dict);
362 Py_XDECREF(str);
363 Py_XDECREF(name);
364 return status;
365}
366
367
368
369static char
370StandardError__doc__[] = "Base class for all standard Python exceptions.";
371
372static char
373TypeError__doc__[] = "Inappropriate argument type.";
374
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000375static char
376StopIteration__doc__[] = "Signal the end from iterator.next().";
377
Barry Warsaw675ac282000-05-26 19:05:16 +0000378
379
380static char
381SystemExit__doc__[] = "Request to exit from the interpreter.";
382
383
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000384static PyObject *
385SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000386{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000387 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000388 int status;
389
390 if (!(self = get_self(args)))
391 return NULL;
392
393 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000394 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000395 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000396
Barry Warsaw675ac282000-05-26 19:05:16 +0000397 status = PyObject_SetAttrString(self, "args", args);
398 if (status < 0) {
399 Py_DECREF(args);
400 return NULL;
401 }
402
403 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000404 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000405 case 0:
406 Py_INCREF(Py_None);
407 code = Py_None;
408 break;
409 case 1:
410 code = PySequence_GetItem(args, 0);
411 break;
412 default:
413 Py_INCREF(args);
414 code = args;
415 break;
416 }
417
418 status = PyObject_SetAttrString(self, "code", code);
419 Py_DECREF(code);
420 Py_DECREF(args);
421 if (status < 0)
422 return NULL;
423
424 Py_INCREF(Py_None);
425 return Py_None;
426}
427
428
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000429static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000430 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000431 {NULL, NULL}
432};
433
434
435
436static char
437KeyboardInterrupt__doc__[] = "Program interrupted by user.";
438
439static char
440ImportError__doc__[] =
441"Import can't find module, or can't find name in module.";
442
443
444
445static char
446EnvironmentError__doc__[] = "Base class for I/O related errors.";
447
448
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000449static PyObject *
450EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000451{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000452 PyObject *item0 = NULL;
453 PyObject *item1 = NULL;
454 PyObject *item2 = NULL;
455 PyObject *subslice = NULL;
456 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000457
458 if (!(self = get_self(args)))
459 return NULL;
460
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000461 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000462 return NULL;
463
464 if (PyObject_SetAttrString(self, "args", args) ||
465 PyObject_SetAttrString(self, "errno", Py_None) ||
466 PyObject_SetAttrString(self, "strerror", Py_None) ||
467 PyObject_SetAttrString(self, "filename", Py_None))
468 {
469 goto finally;
470 }
471
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000472 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000473 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000474 /* Where a function has a single filename, such as open() or some
475 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
476 * called, giving a third argument which is the filename. But, so
477 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000478 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000479 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000480 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000481 * we hack args so that it only contains two items. This also
482 * means we need our own __str__() which prints out the filename
483 * when it was supplied.
484 */
485 item0 = PySequence_GetItem(args, 0);
486 item1 = PySequence_GetItem(args, 1);
487 item2 = PySequence_GetItem(args, 2);
488 if (!item0 || !item1 || !item2)
489 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000490
Barry Warsaw675ac282000-05-26 19:05:16 +0000491 if (PyObject_SetAttrString(self, "errno", item0) ||
492 PyObject_SetAttrString(self, "strerror", item1) ||
493 PyObject_SetAttrString(self, "filename", item2))
494 {
495 goto finally;
496 }
497
498 subslice = PySequence_GetSlice(args, 0, 2);
499 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
500 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000501 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000502
503 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000504 /* Used when PyErr_SetFromErrno() is called and no filename
505 * argument is given.
506 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000507 item0 = PySequence_GetItem(args, 0);
508 item1 = PySequence_GetItem(args, 1);
509 if (!item0 || !item1)
510 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000511
Barry Warsaw675ac282000-05-26 19:05:16 +0000512 if (PyObject_SetAttrString(self, "errno", item0) ||
513 PyObject_SetAttrString(self, "strerror", item1))
514 {
515 goto finally;
516 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000517 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000518 }
519
520 Py_INCREF(Py_None);
521 rtnval = Py_None;
522
523 finally:
524 Py_DECREF(args);
525 Py_XDECREF(item0);
526 Py_XDECREF(item1);
527 Py_XDECREF(item2);
528 Py_XDECREF(subslice);
529 return rtnval;
530}
531
532
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000533static PyObject *
534EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000535{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000536 PyObject *originalself = self;
537 PyObject *filename;
538 PyObject *serrno;
539 PyObject *strerror;
540 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000541
Fred Drake1aba5772000-08-15 15:46:16 +0000542 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000543 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000544
Barry Warsaw675ac282000-05-26 19:05:16 +0000545 filename = PyObject_GetAttrString(self, "filename");
546 serrno = PyObject_GetAttrString(self, "errno");
547 strerror = PyObject_GetAttrString(self, "strerror");
548 if (!filename || !serrno || !strerror)
549 goto finally;
550
551 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000552 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
553 PyObject *repr = PyObject_Repr(filename);
554 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000555
556 if (!fmt || !repr || !tuple) {
557 Py_XDECREF(fmt);
558 Py_XDECREF(repr);
559 Py_XDECREF(tuple);
560 goto finally;
561 }
562
563 PyTuple_SET_ITEM(tuple, 0, serrno);
564 PyTuple_SET_ITEM(tuple, 1, strerror);
565 PyTuple_SET_ITEM(tuple, 2, repr);
566
567 rtnval = PyString_Format(fmt, tuple);
568
569 Py_DECREF(fmt);
570 Py_DECREF(tuple);
571 /* already freed because tuple owned only reference */
572 serrno = NULL;
573 strerror = NULL;
574 }
575 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000576 PyObject *fmt = PyString_FromString("[Errno %s] %s");
577 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000578
579 if (!fmt || !tuple) {
580 Py_XDECREF(fmt);
581 Py_XDECREF(tuple);
582 goto finally;
583 }
584
585 PyTuple_SET_ITEM(tuple, 0, serrno);
586 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000587
Barry Warsaw675ac282000-05-26 19:05:16 +0000588 rtnval = PyString_Format(fmt, tuple);
589
590 Py_DECREF(fmt);
591 Py_DECREF(tuple);
592 /* already freed because tuple owned only reference */
593 serrno = NULL;
594 strerror = NULL;
595 }
596 else
597 /* The original Python code said:
598 *
599 * return StandardError.__str__(self)
600 *
601 * but there is no StandardError__str__() function; we happen to
602 * know that's just a pass through to Exception__str__().
603 */
604 rtnval = Exception__str__(originalself, args);
605
606 finally:
607 Py_XDECREF(filename);
608 Py_XDECREF(serrno);
609 Py_XDECREF(strerror);
610 return rtnval;
611}
612
613
614static
615PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000616 {"__init__", EnvironmentError__init__, METH_VARARGS},
617 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000618 {NULL, NULL}
619};
620
621
622
623
624static char
625IOError__doc__[] = "I/O operation failed.";
626
627static char
628OSError__doc__[] = "OS system call failed.";
629
630#ifdef MS_WINDOWS
631static char
632WindowsError__doc__[] = "MS-Windows OS system call failed.";
633#endif /* MS_WINDOWS */
634
635static char
636EOFError__doc__[] = "Read beyond end of file.";
637
638static char
639RuntimeError__doc__[] = "Unspecified run-time error.";
640
641static char
642NotImplementedError__doc__[] =
643"Method or function hasn't been implemented yet.";
644
645static char
646NameError__doc__[] = "Name not found globally.";
647
648static char
649UnboundLocalError__doc__[] =
650"Local name referenced but not bound to a value.";
651
652static char
653AttributeError__doc__[] = "Attribute not found.";
654
655
656
657static char
658SyntaxError__doc__[] = "Invalid syntax.";
659
660
661static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000662SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000663{
Barry Warsaw87bec352000-08-18 05:05:37 +0000664 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000665 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000666
667 /* Additional class-creation time initializations */
668 if (!emptystring ||
669 PyObject_SetAttrString(klass, "msg", emptystring) ||
670 PyObject_SetAttrString(klass, "filename", Py_None) ||
671 PyObject_SetAttrString(klass, "lineno", Py_None) ||
672 PyObject_SetAttrString(klass, "offset", Py_None) ||
673 PyObject_SetAttrString(klass, "text", Py_None))
674 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000675 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000676 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000677 Py_XDECREF(emptystring);
678 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000679}
680
681
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000682static PyObject *
683SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000684{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000685 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000686 int lenargs;
687
688 if (!(self = get_self(args)))
689 return NULL;
690
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000691 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000692 return NULL;
693
694 if (PyObject_SetAttrString(self, "args", args))
695 goto finally;
696
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000697 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000698 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000699 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000700 int status;
701
702 if (!item0)
703 goto finally;
704 status = PyObject_SetAttrString(self, "msg", item0);
705 Py_DECREF(item0);
706 if (status)
707 goto finally;
708 }
709 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000710 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000711 PyObject *filename = NULL, *lineno = NULL;
712 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000713 int status = 1;
714
715 if (!info)
716 goto finally;
717
718 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000719 if (filename != NULL) {
720 lineno = PySequence_GetItem(info, 1);
721 if (lineno != NULL) {
722 offset = PySequence_GetItem(info, 2);
723 if (offset != NULL) {
724 text = PySequence_GetItem(info, 3);
725 if (text != NULL) {
726 status =
727 PyObject_SetAttrString(self, "filename", filename)
728 || PyObject_SetAttrString(self, "lineno", lineno)
729 || PyObject_SetAttrString(self, "offset", offset)
730 || PyObject_SetAttrString(self, "text", text);
731 Py_DECREF(text);
732 }
733 Py_DECREF(offset);
734 }
735 Py_DECREF(lineno);
736 }
737 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000738 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000739 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000740
741 if (status)
742 goto finally;
743 }
744 Py_INCREF(Py_None);
745 rtnval = Py_None;
746
747 finally:
748 Py_DECREF(args);
749 return rtnval;
750}
751
752
Fred Drake185a29b2000-08-15 16:20:36 +0000753/* This is called "my_basename" instead of just "basename" to avoid name
754 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
755 defined, and Python does define that. */
756static char *
757my_basename(char *name)
758{
759 char *cp = name;
760 char *result = name;
761
762 if (name == NULL)
763 return "???";
764 while (*cp != '\0') {
765 if (*cp == SEP)
766 result = cp + 1;
767 ++cp;
768 }
769 return result;
770}
771
772
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000773static PyObject *
774SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000775{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000776 PyObject *msg;
777 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000778 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000779
Fred Drake1aba5772000-08-15 15:46:16 +0000780 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000781 return NULL;
782
783 if (!(msg = PyObject_GetAttrString(self, "msg")))
784 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000785
Barry Warsaw675ac282000-05-26 19:05:16 +0000786 str = PyObject_Str(msg);
787 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000788 result = str;
789
790 /* XXX -- do all the additional formatting with filename and
791 lineno here */
792
793 if (PyString_Check(str)) {
794 int have_filename = 0;
795 int have_lineno = 0;
796 char *buffer = NULL;
797
Barry Warsaw77c9f502000-08-16 19:43:17 +0000798 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000799 have_filename = PyString_Check(filename);
800 else
801 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000802
803 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000804 have_lineno = PyInt_Check(lineno);
805 else
806 PyErr_Clear();
807
808 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000809 int bufsize = PyString_GET_SIZE(str) + 64;
810 if (have_filename)
811 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000812
813 buffer = PyMem_Malloc(bufsize);
814 if (buffer != NULL) {
815 if (have_filename && have_lineno)
Fred Drake04e654a2000-08-18 19:53:25 +0000816 sprintf(buffer, "%s (%s, line %ld)",
Fred Drake1aba5772000-08-15 15:46:16 +0000817 PyString_AS_STRING(str),
Fred Drake185a29b2000-08-15 16:20:36 +0000818 my_basename(PyString_AS_STRING(filename)),
Fred Drake1aba5772000-08-15 15:46:16 +0000819 PyInt_AsLong(lineno));
820 else if (have_filename)
821 sprintf(buffer, "%s (%s)",
822 PyString_AS_STRING(str),
Fred Drake185a29b2000-08-15 16:20:36 +0000823 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000824 else if (have_lineno)
Fred Drake04e654a2000-08-18 19:53:25 +0000825 sprintf(buffer, "%s (line %ld)",
Fred Drake1aba5772000-08-15 15:46:16 +0000826 PyString_AS_STRING(str),
827 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000828
Fred Drake1aba5772000-08-15 15:46:16 +0000829 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000830 PyMem_FREE(buffer);
831
Fred Drake1aba5772000-08-15 15:46:16 +0000832 if (result == NULL)
833 result = str;
834 else
835 Py_DECREF(str);
836 }
837 }
838 Py_XDECREF(filename);
839 Py_XDECREF(lineno);
840 }
841 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000842}
843
844
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000845static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000846 {"__init__", SyntaxError__init__, METH_VARARGS},
847 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000848 {NULL, NULL}
849};
850
851
852
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000853/* Exception doc strings */
854
Barry Warsaw675ac282000-05-26 19:05:16 +0000855static char
856AssertionError__doc__[] = "Assertion failed.";
857
858static char
859LookupError__doc__[] = "Base class for lookup errors.";
860
861static char
862IndexError__doc__[] = "Sequence index out of range.";
863
864static char
865KeyError__doc__[] = "Mapping key not found.";
866
867static char
868ArithmeticError__doc__[] = "Base class for arithmetic errors.";
869
870static char
871OverflowError__doc__[] = "Result too large to be represented.";
872
873static char
874ZeroDivisionError__doc__[] =
875"Second argument to a division or modulo operation was zero.";
876
877static char
878FloatingPointError__doc__[] = "Floating point operation failed.";
879
880static char
881ValueError__doc__[] = "Inappropriate argument value (of correct type).";
882
883static char
884UnicodeError__doc__[] = "Unicode related error.";
885
886static char
887SystemError__doc__[] = "Internal error in the Python interpreter.\n\
888\n\
889Please report this to the Python maintainer, along with the traceback,\n\
890the Python version, and the hardware/OS platform and version.";
891
892static char
Fred Drakebb9fa212001-10-05 21:50:08 +0000893ReferenceError__doc__[] = "Weak ref proxy used after referent went away.";
894
895static char
Barry Warsaw675ac282000-05-26 19:05:16 +0000896MemoryError__doc__[] = "Out of memory.";
897
Fred Drake85f36392000-07-11 17:53:00 +0000898static char
899IndentationError__doc__[] = "Improper indentation.";
900
901static char
902TabError__doc__[] = "Improper mixture of spaces and tabs.";
903
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000904/* Warning category docstrings */
905
906static char
907Warning__doc__[] = "Base class for warning categories.";
908
909static char
910UserWarning__doc__[] = "Base class for warnings generated by user code.";
911
912static char
913DeprecationWarning__doc__[] =
914"Base class for warnings about deprecated features.";
915
916static char
917SyntaxWarning__doc__[] = "Base class for warnings about dubious syntax.";
918
919static char
Guido van Rossumae347b32001-08-23 02:56:07 +0000920OverflowWarning__doc__[] = "Base class for warnings about numeric overflow.";
921
922static char
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000923RuntimeWarning__doc__[] =
924"Base class for warnings about dubious runtime behavior.";
925
Barry Warsaw675ac282000-05-26 19:05:16 +0000926
927
928/* module global functions */
929static PyMethodDef functions[] = {
930 /* Sentinel */
931 {NULL, NULL}
932};
933
934
935
936/* Global C API defined exceptions */
937
938PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000939PyObject *PyExc_StopIteration;
Barry Warsaw675ac282000-05-26 19:05:16 +0000940PyObject *PyExc_StandardError;
941PyObject *PyExc_ArithmeticError;
942PyObject *PyExc_LookupError;
943
944PyObject *PyExc_AssertionError;
945PyObject *PyExc_AttributeError;
946PyObject *PyExc_EOFError;
947PyObject *PyExc_FloatingPointError;
948PyObject *PyExc_EnvironmentError;
949PyObject *PyExc_IOError;
950PyObject *PyExc_OSError;
951PyObject *PyExc_ImportError;
952PyObject *PyExc_IndexError;
953PyObject *PyExc_KeyError;
954PyObject *PyExc_KeyboardInterrupt;
955PyObject *PyExc_MemoryError;
956PyObject *PyExc_NameError;
957PyObject *PyExc_OverflowError;
958PyObject *PyExc_RuntimeError;
959PyObject *PyExc_NotImplementedError;
960PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +0000961PyObject *PyExc_IndentationError;
962PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +0000963PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +0000964PyObject *PyExc_SystemError;
965PyObject *PyExc_SystemExit;
966PyObject *PyExc_UnboundLocalError;
967PyObject *PyExc_UnicodeError;
968PyObject *PyExc_TypeError;
969PyObject *PyExc_ValueError;
970PyObject *PyExc_ZeroDivisionError;
971#ifdef MS_WINDOWS
972PyObject *PyExc_WindowsError;
973#endif
974
975/* Pre-computed MemoryError instance. Best to create this as early as
976 * possibly and not wait until a MemoryError is actually raised!
977 */
978PyObject *PyExc_MemoryErrorInst;
979
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000980/* Predefined warning categories */
981PyObject *PyExc_Warning;
982PyObject *PyExc_UserWarning;
983PyObject *PyExc_DeprecationWarning;
984PyObject *PyExc_SyntaxWarning;
Guido van Rossumae347b32001-08-23 02:56:07 +0000985PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000986PyObject *PyExc_RuntimeWarning;
987
Barry Warsaw675ac282000-05-26 19:05:16 +0000988
989
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000990/* mapping between exception names and their PyObject ** */
991static struct {
992 char *name;
993 PyObject **exc;
994 PyObject **base; /* NULL == PyExc_StandardError */
995 char *docstr;
996 PyMethodDef *methods;
997 int (*classinit)(PyObject *);
998} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +0000999 /*
1000 * The first three classes MUST appear in exactly this order
1001 */
1002 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001003 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1004 StopIteration__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001005 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1006 StandardError__doc__},
1007 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1008 /*
1009 * The rest appear in depth-first order of the hierarchy
1010 */
1011 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1012 SystemExit_methods},
1013 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1014 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1015 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1016 EnvironmentError_methods},
1017 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1018 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1019#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001020 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001021 WindowsError__doc__},
1022#endif /* MS_WINDOWS */
1023 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1024 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1025 {"NotImplementedError", &PyExc_NotImplementedError,
1026 &PyExc_RuntimeError, NotImplementedError__doc__},
1027 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1028 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1029 UnboundLocalError__doc__},
1030 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1031 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1032 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001033 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1034 IndentationError__doc__},
1035 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1036 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001037 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1038 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1039 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1040 IndexError__doc__},
1041 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
1042 KeyError__doc__},
1043 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1044 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1045 OverflowError__doc__},
1046 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1047 ZeroDivisionError__doc__},
1048 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1049 FloatingPointError__doc__},
1050 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1051 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Fred Drakebb9fa212001-10-05 21:50:08 +00001052 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001053 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1054 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001055 /* Warning categories */
1056 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1057 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1058 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1059 DeprecationWarning__doc__},
1060 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Guido van Rossumae347b32001-08-23 02:56:07 +00001061 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1062 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001063 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1064 RuntimeWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001065 /* Sentinel */
1066 {NULL}
1067};
1068
1069
1070
Tim Peters5687ffe2001-02-28 16:44:18 +00001071DL_EXPORT(void)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001072_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001073{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001074 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001075 int modnamesz = strlen(modulename);
1076 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001077 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001078
Tim Peters6d6c1a32001-08-02 04:15:00 +00001079 me = Py_InitModule(modulename, functions);
1080 if (me == NULL)
1081 goto err;
1082 mydict = PyModule_GetDict(me);
1083 if (mydict == NULL)
1084 goto err;
1085 bltinmod = PyImport_ImportModule("__builtin__");
1086 if (bltinmod == NULL)
1087 goto err;
1088 bdict = PyModule_GetDict(bltinmod);
1089 if (bdict == NULL)
1090 goto err;
1091 doc = PyString_FromString(module__doc__);
1092 if (doc == NULL)
1093 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001094
Tim Peters6d6c1a32001-08-02 04:15:00 +00001095 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001096 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001097 if (i < 0) {
1098 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001099 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001100 return;
1101 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001102
1103 /* This is the base class of all exceptions, so make it first. */
1104 if (make_Exception(modulename) ||
1105 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1106 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1107 {
1108 Py_FatalError("Base class `Exception' could not be created.");
1109 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001110
Barry Warsaw675ac282000-05-26 19:05:16 +00001111 /* Now we can programmatically create all the remaining exceptions.
1112 * Remember to start the loop at 1 to skip Exceptions.
1113 */
1114 for (i=1; exctable[i].name; i++) {
1115 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001116 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1117 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001118
1119 (void)strcpy(cname, modulename);
1120 (void)strcat(cname, ".");
1121 (void)strcat(cname, exctable[i].name);
1122
1123 if (exctable[i].base == 0)
1124 base = PyExc_StandardError;
1125 else
1126 base = *exctable[i].base;
1127
1128 status = make_class(exctable[i].exc, base, cname,
1129 exctable[i].methods,
1130 exctable[i].docstr);
1131
1132 PyMem_DEL(cname);
1133
1134 if (status)
1135 Py_FatalError("Standard exception classes could not be created.");
1136
1137 if (exctable[i].classinit) {
1138 status = (*exctable[i].classinit)(*exctable[i].exc);
1139 if (status)
1140 Py_FatalError("An exception class could not be initialized.");
1141 }
1142
1143 /* Now insert the class into both this module and the __builtin__
1144 * module.
1145 */
1146 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1147 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1148 {
1149 Py_FatalError("Module dictionary insertion problem.");
1150 }
1151 }
1152
1153 /* Now we need to pre-allocate a MemoryError instance */
1154 args = Py_BuildValue("()");
1155 if (!args ||
1156 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1157 {
1158 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1159 }
1160 Py_DECREF(args);
1161
1162 /* We're done with __builtin__ */
1163 Py_DECREF(bltinmod);
1164}
1165
1166
Tim Peters5687ffe2001-02-28 16:44:18 +00001167DL_EXPORT(void)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001168_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001169{
1170 int i;
1171
1172 Py_XDECREF(PyExc_MemoryErrorInst);
1173 PyExc_MemoryErrorInst = NULL;
1174
1175 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001176 /* clear the class's dictionary, freeing up circular references
1177 * between the class and its methods.
1178 */
1179 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1180 PyDict_Clear(cdict);
1181 Py_DECREF(cdict);
1182
1183 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001184 Py_XDECREF(*exctable[i].exc);
1185 *exctable[i].exc = NULL;
1186 }
1187}