blob: adfd699320ec6def72c4d48fd73c42c758f673ea [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) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000673 PyObject_SetAttrString(klass, "text", Py_None) ||
674 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000675 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000676 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000677 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000678 Py_XDECREF(emptystring);
679 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000680}
681
682
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000683static PyObject *
684SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000685{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000686 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000687 int lenargs;
688
689 if (!(self = get_self(args)))
690 return NULL;
691
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000692 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000693 return NULL;
694
695 if (PyObject_SetAttrString(self, "args", args))
696 goto finally;
697
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000698 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000699 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000700 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000701 int status;
702
703 if (!item0)
704 goto finally;
705 status = PyObject_SetAttrString(self, "msg", item0);
706 Py_DECREF(item0);
707 if (status)
708 goto finally;
709 }
710 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000711 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000712 PyObject *filename = NULL, *lineno = NULL;
713 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000714 int status = 1;
715
716 if (!info)
717 goto finally;
718
719 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000720 if (filename != NULL) {
721 lineno = PySequence_GetItem(info, 1);
722 if (lineno != NULL) {
723 offset = PySequence_GetItem(info, 2);
724 if (offset != NULL) {
725 text = PySequence_GetItem(info, 3);
726 if (text != NULL) {
727 status =
728 PyObject_SetAttrString(self, "filename", filename)
729 || PyObject_SetAttrString(self, "lineno", lineno)
730 || PyObject_SetAttrString(self, "offset", offset)
731 || PyObject_SetAttrString(self, "text", text);
732 Py_DECREF(text);
733 }
734 Py_DECREF(offset);
735 }
736 Py_DECREF(lineno);
737 }
738 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000739 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000740 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000741
742 if (status)
743 goto finally;
744 }
745 Py_INCREF(Py_None);
746 rtnval = Py_None;
747
748 finally:
749 Py_DECREF(args);
750 return rtnval;
751}
752
753
Fred Drake185a29b2000-08-15 16:20:36 +0000754/* This is called "my_basename" instead of just "basename" to avoid name
755 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
756 defined, and Python does define that. */
757static char *
758my_basename(char *name)
759{
760 char *cp = name;
761 char *result = name;
762
763 if (name == NULL)
764 return "???";
765 while (*cp != '\0') {
766 if (*cp == SEP)
767 result = cp + 1;
768 ++cp;
769 }
770 return result;
771}
772
773
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000774static PyObject *
775SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000776{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000777 PyObject *msg;
778 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000779 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000780
Fred Drake1aba5772000-08-15 15:46:16 +0000781 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000782 return NULL;
783
784 if (!(msg = PyObject_GetAttrString(self, "msg")))
785 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000786
Barry Warsaw675ac282000-05-26 19:05:16 +0000787 str = PyObject_Str(msg);
788 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000789 result = str;
790
791 /* XXX -- do all the additional formatting with filename and
792 lineno here */
793
794 if (PyString_Check(str)) {
795 int have_filename = 0;
796 int have_lineno = 0;
797 char *buffer = NULL;
798
Barry Warsaw77c9f502000-08-16 19:43:17 +0000799 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000800 have_filename = PyString_Check(filename);
801 else
802 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000803
804 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000805 have_lineno = PyInt_Check(lineno);
806 else
807 PyErr_Clear();
808
809 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000810 int bufsize = PyString_GET_SIZE(str) + 64;
811 if (have_filename)
812 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000813
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000814 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000815 if (buffer != NULL) {
816 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000817 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
818 PyString_AS_STRING(str),
819 my_basename(PyString_AS_STRING(filename)),
820 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000821 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000822 PyOS_snprintf(buffer, bufsize, "%s (%s)",
823 PyString_AS_STRING(str),
824 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000825 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000826 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
827 PyString_AS_STRING(str),
828 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000829
Fred Drake1aba5772000-08-15 15:46:16 +0000830 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000831 PyMem_FREE(buffer);
832
Fred Drake1aba5772000-08-15 15:46:16 +0000833 if (result == NULL)
834 result = str;
835 else
836 Py_DECREF(str);
837 }
838 }
839 Py_XDECREF(filename);
840 Py_XDECREF(lineno);
841 }
842 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000843}
844
845
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000846static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000847 {"__init__", SyntaxError__init__, METH_VARARGS},
848 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000849 {NULL, NULL}
850};
851
852
853
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000854/* Exception doc strings */
855
Barry Warsaw675ac282000-05-26 19:05:16 +0000856static char
857AssertionError__doc__[] = "Assertion failed.";
858
859static char
860LookupError__doc__[] = "Base class for lookup errors.";
861
862static char
863IndexError__doc__[] = "Sequence index out of range.";
864
865static char
866KeyError__doc__[] = "Mapping key not found.";
867
868static char
869ArithmeticError__doc__[] = "Base class for arithmetic errors.";
870
871static char
872OverflowError__doc__[] = "Result too large to be represented.";
873
874static char
875ZeroDivisionError__doc__[] =
876"Second argument to a division or modulo operation was zero.";
877
878static char
879FloatingPointError__doc__[] = "Floating point operation failed.";
880
881static char
882ValueError__doc__[] = "Inappropriate argument value (of correct type).";
883
884static char
885UnicodeError__doc__[] = "Unicode related error.";
886
887static char
888SystemError__doc__[] = "Internal error in the Python interpreter.\n\
889\n\
890Please report this to the Python maintainer, along with the traceback,\n\
891the Python version, and the hardware/OS platform and version.";
892
893static char
Fred Drakebb9fa212001-10-05 21:50:08 +0000894ReferenceError__doc__[] = "Weak ref proxy used after referent went away.";
895
896static char
Barry Warsaw675ac282000-05-26 19:05:16 +0000897MemoryError__doc__[] = "Out of memory.";
898
Fred Drake85f36392000-07-11 17:53:00 +0000899static char
900IndentationError__doc__[] = "Improper indentation.";
901
902static char
903TabError__doc__[] = "Improper mixture of spaces and tabs.";
904
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000905/* Warning category docstrings */
906
907static char
908Warning__doc__[] = "Base class for warning categories.";
909
910static char
911UserWarning__doc__[] = "Base class for warnings generated by user code.";
912
913static char
914DeprecationWarning__doc__[] =
915"Base class for warnings about deprecated features.";
916
917static char
918SyntaxWarning__doc__[] = "Base class for warnings about dubious syntax.";
919
920static char
Guido van Rossumae347b32001-08-23 02:56:07 +0000921OverflowWarning__doc__[] = "Base class for warnings about numeric overflow.";
922
923static char
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000924RuntimeWarning__doc__[] =
925"Base class for warnings about dubious runtime behavior.";
926
Barry Warsaw675ac282000-05-26 19:05:16 +0000927
928
929/* module global functions */
930static PyMethodDef functions[] = {
931 /* Sentinel */
932 {NULL, NULL}
933};
934
935
936
937/* Global C API defined exceptions */
938
939PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000940PyObject *PyExc_StopIteration;
Barry Warsaw675ac282000-05-26 19:05:16 +0000941PyObject *PyExc_StandardError;
942PyObject *PyExc_ArithmeticError;
943PyObject *PyExc_LookupError;
944
945PyObject *PyExc_AssertionError;
946PyObject *PyExc_AttributeError;
947PyObject *PyExc_EOFError;
948PyObject *PyExc_FloatingPointError;
949PyObject *PyExc_EnvironmentError;
950PyObject *PyExc_IOError;
951PyObject *PyExc_OSError;
952PyObject *PyExc_ImportError;
953PyObject *PyExc_IndexError;
954PyObject *PyExc_KeyError;
955PyObject *PyExc_KeyboardInterrupt;
956PyObject *PyExc_MemoryError;
957PyObject *PyExc_NameError;
958PyObject *PyExc_OverflowError;
959PyObject *PyExc_RuntimeError;
960PyObject *PyExc_NotImplementedError;
961PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +0000962PyObject *PyExc_IndentationError;
963PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +0000964PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +0000965PyObject *PyExc_SystemError;
966PyObject *PyExc_SystemExit;
967PyObject *PyExc_UnboundLocalError;
968PyObject *PyExc_UnicodeError;
969PyObject *PyExc_TypeError;
970PyObject *PyExc_ValueError;
971PyObject *PyExc_ZeroDivisionError;
972#ifdef MS_WINDOWS
973PyObject *PyExc_WindowsError;
974#endif
975
976/* Pre-computed MemoryError instance. Best to create this as early as
977 * possibly and not wait until a MemoryError is actually raised!
978 */
979PyObject *PyExc_MemoryErrorInst;
980
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000981/* Predefined warning categories */
982PyObject *PyExc_Warning;
983PyObject *PyExc_UserWarning;
984PyObject *PyExc_DeprecationWarning;
985PyObject *PyExc_SyntaxWarning;
Guido van Rossumae347b32001-08-23 02:56:07 +0000986PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000987PyObject *PyExc_RuntimeWarning;
988
Barry Warsaw675ac282000-05-26 19:05:16 +0000989
990
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000991/* mapping between exception names and their PyObject ** */
992static struct {
993 char *name;
994 PyObject **exc;
995 PyObject **base; /* NULL == PyExc_StandardError */
996 char *docstr;
997 PyMethodDef *methods;
998 int (*classinit)(PyObject *);
999} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001000 /*
1001 * The first three classes MUST appear in exactly this order
1002 */
1003 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001004 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1005 StopIteration__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001006 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1007 StandardError__doc__},
1008 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1009 /*
1010 * The rest appear in depth-first order of the hierarchy
1011 */
1012 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1013 SystemExit_methods},
1014 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1015 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1016 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1017 EnvironmentError_methods},
1018 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1019 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1020#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001021 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001022 WindowsError__doc__},
1023#endif /* MS_WINDOWS */
1024 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1025 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1026 {"NotImplementedError", &PyExc_NotImplementedError,
1027 &PyExc_RuntimeError, NotImplementedError__doc__},
1028 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1029 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1030 UnboundLocalError__doc__},
1031 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1032 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1033 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001034 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1035 IndentationError__doc__},
1036 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1037 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001038 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1039 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1040 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1041 IndexError__doc__},
1042 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
1043 KeyError__doc__},
1044 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1045 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1046 OverflowError__doc__},
1047 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1048 ZeroDivisionError__doc__},
1049 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1050 FloatingPointError__doc__},
1051 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1052 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Fred Drakebb9fa212001-10-05 21:50:08 +00001053 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001054 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1055 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001056 /* Warning categories */
1057 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1058 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1059 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1060 DeprecationWarning__doc__},
1061 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Guido van Rossumae347b32001-08-23 02:56:07 +00001062 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1063 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001064 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1065 RuntimeWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001066 /* Sentinel */
1067 {NULL}
1068};
1069
1070
1071
Tim Peters5687ffe2001-02-28 16:44:18 +00001072DL_EXPORT(void)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001073_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001074{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001075 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001076 int modnamesz = strlen(modulename);
1077 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001078 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001079
Tim Peters6d6c1a32001-08-02 04:15:00 +00001080 me = Py_InitModule(modulename, functions);
1081 if (me == NULL)
1082 goto err;
1083 mydict = PyModule_GetDict(me);
1084 if (mydict == NULL)
1085 goto err;
1086 bltinmod = PyImport_ImportModule("__builtin__");
1087 if (bltinmod == NULL)
1088 goto err;
1089 bdict = PyModule_GetDict(bltinmod);
1090 if (bdict == NULL)
1091 goto err;
1092 doc = PyString_FromString(module__doc__);
1093 if (doc == NULL)
1094 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001095
Tim Peters6d6c1a32001-08-02 04:15:00 +00001096 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001097 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001098 if (i < 0) {
1099 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001100 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001101 return;
1102 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001103
1104 /* This is the base class of all exceptions, so make it first. */
1105 if (make_Exception(modulename) ||
1106 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1107 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1108 {
1109 Py_FatalError("Base class `Exception' could not be created.");
1110 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001111
Barry Warsaw675ac282000-05-26 19:05:16 +00001112 /* Now we can programmatically create all the remaining exceptions.
1113 * Remember to start the loop at 1 to skip Exceptions.
1114 */
1115 for (i=1; exctable[i].name; i++) {
1116 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001117 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1118 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001119
1120 (void)strcpy(cname, modulename);
1121 (void)strcat(cname, ".");
1122 (void)strcat(cname, exctable[i].name);
1123
1124 if (exctable[i].base == 0)
1125 base = PyExc_StandardError;
1126 else
1127 base = *exctable[i].base;
1128
1129 status = make_class(exctable[i].exc, base, cname,
1130 exctable[i].methods,
1131 exctable[i].docstr);
1132
1133 PyMem_DEL(cname);
1134
1135 if (status)
1136 Py_FatalError("Standard exception classes could not be created.");
1137
1138 if (exctable[i].classinit) {
1139 status = (*exctable[i].classinit)(*exctable[i].exc);
1140 if (status)
1141 Py_FatalError("An exception class could not be initialized.");
1142 }
1143
1144 /* Now insert the class into both this module and the __builtin__
1145 * module.
1146 */
1147 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1148 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1149 {
1150 Py_FatalError("Module dictionary insertion problem.");
1151 }
1152 }
1153
1154 /* Now we need to pre-allocate a MemoryError instance */
1155 args = Py_BuildValue("()");
1156 if (!args ||
1157 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1158 {
1159 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1160 }
1161 Py_DECREF(args);
1162
1163 /* We're done with __builtin__ */
1164 Py_DECREF(bltinmod);
1165}
1166
1167
Tim Peters5687ffe2001-02-28 16:44:18 +00001168DL_EXPORT(void)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001169_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001170{
1171 int i;
1172
1173 Py_XDECREF(PyExc_MemoryErrorInst);
1174 PyExc_MemoryErrorInst = NULL;
1175
1176 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001177 /* clear the class's dictionary, freeing up circular references
1178 * between the class and its methods.
1179 */
1180 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1181 PyDict_Clear(cdict);
1182 Py_DECREF(cdict);
1183
1184 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001185 Py_XDECREF(*exctable[i].exc);
1186 *exctable[i].exc = NULL;
1187 }
1188}