blob: ef9d01c0a58c06ea7b4dcd6cb749a1020d81d560 [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\
Guido van Rossumae347b32001-08-23 02:56:07 +0000108 +-- OverflowWarning\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000109 +-- RuntimeWarning";
Barry Warsaw675ac282000-05-26 19:05:16 +0000110
111
112/* Helper function for populating a dictionary with method wrappers. */
113static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000114populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +0000115{
116 if (!methods)
117 return 0;
118
119 while (methods->ml_name) {
120 /* get a wrapper for the built-in function */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000121 PyObject *func = PyCFunction_New(methods, NULL);
122 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +0000123 int status;
124
125 if (!func)
126 return -1;
127
128 /* turn the function into an unbound method */
129 if (!(meth = PyMethod_New(func, NULL, klass))) {
130 Py_DECREF(func);
131 return -1;
132 }
Barry Warsaw9667ed22001-01-23 16:08:34 +0000133
Barry Warsaw675ac282000-05-26 19:05:16 +0000134 /* add method to dictionary */
135 status = PyDict_SetItemString(dict, methods->ml_name, meth);
136 Py_DECREF(meth);
137 Py_DECREF(func);
138
139 /* stop now if an error occurred, otherwise do the next method */
140 if (status)
141 return status;
142
143 methods++;
144 }
145 return 0;
146}
147
Barry Warsaw9667ed22001-01-23 16:08:34 +0000148
Barry Warsaw675ac282000-05-26 19:05:16 +0000149
150/* This function is used to create all subsequent exception classes. */
151static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000152make_class(PyObject **klass, PyObject *base,
153 char *name, PyMethodDef *methods,
154 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +0000155{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000156 PyObject *dict = PyDict_New();
157 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000158 int status = -1;
159
160 if (!dict)
161 return -1;
162
163 /* If an error occurs from here on, goto finally instead of explicitly
164 * returning NULL.
165 */
166
167 if (docstr) {
168 if (!(str = PyString_FromString(docstr)))
169 goto finally;
170 if (PyDict_SetItemString(dict, "__doc__", str))
171 goto finally;
172 }
173
174 if (!(*klass = PyErr_NewException(name, base, dict)))
175 goto finally;
176
177 if (populate_methods(*klass, dict, methods)) {
178 Py_DECREF(*klass);
179 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000180 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000181 }
182
183 status = 0;
184
185 finally:
186 Py_XDECREF(dict);
187 Py_XDECREF(str);
188 return status;
189}
190
191
192/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000193static PyObject *
194get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000195{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000196 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000197 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000198 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000199 if (PyExc_TypeError) {
200 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000201 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000202 }
203 return NULL;
204 }
205 return self;
206}
207
208
209
210/* Notes on bootstrapping the exception classes.
211 *
212 * First thing we create is the base class for all exceptions, called
213 * appropriately enough: Exception. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000214 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000215 * for TypeError, which can conditionally exist.
216 *
217 * Next, StandardError is created (which is quite simple) followed by
218 * TypeError, because the instantiation of other exceptions can potentially
219 * throw a TypeError. Once these exceptions are created, all the others
220 * can be created in any order. See the static exctable below for the
221 * explicit bootstrap order.
222 *
223 * All classes after Exception can be created using PyErr_NewException().
224 */
225
226static char
227Exception__doc__[] = "Common base class for all exceptions.";
228
229
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000230static PyObject *
231Exception__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000232{
233 int status;
234
235 if (!(self = get_self(args)))
236 return NULL;
237
238 /* set args attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000239 /* XXX size is only a hint */
240 args = PySequence_GetSlice(args, 1, PySequence_Size(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000241 if (!args)
242 return NULL;
243 status = PyObject_SetAttrString(self, "args", args);
244 Py_DECREF(args);
245 if (status < 0)
246 return NULL;
247
248 Py_INCREF(Py_None);
249 return Py_None;
250}
251
252
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000253static PyObject *
254Exception__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000255{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000256 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000257
Fred Drake1aba5772000-08-15 15:46:16 +0000258 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000259 return NULL;
260
261 args = PyObject_GetAttrString(self, "args");
262 if (!args)
263 return NULL;
264
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000265 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000266 case 0:
267 out = PyString_FromString("");
268 break;
269 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000270 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000271 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000272 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000273 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000274 Py_DECREF(tmp);
275 }
276 else
277 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000278 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000279 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000280 default:
281 out = PyObject_Str(args);
282 break;
283 }
284
285 Py_DECREF(args);
286 return out;
287}
288
289
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000290static PyObject *
291Exception__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000292{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000293 PyObject *out;
294 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000295
Fred Drake1aba5772000-08-15 15:46:16 +0000296 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000297 return NULL;
298
299 args = PyObject_GetAttrString(self, "args");
300 if (!args)
301 return NULL;
302
303 out = PyObject_GetItem(args, index);
304 Py_DECREF(args);
305 return out;
306}
307
308
309static PyMethodDef
310Exception_methods[] = {
311 /* methods for the Exception class */
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000312 { "__getitem__", Exception__getitem__, METH_VARARGS},
313 { "__str__", Exception__str__, METH_VARARGS},
314 { "__init__", Exception__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000315 { NULL, NULL }
316};
317
318
319static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000320make_Exception(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000321{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000322 PyObject *dict = PyDict_New();
323 PyObject *str = NULL;
324 PyObject *name = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000325 int status = -1;
326
327 if (!dict)
328 return -1;
329
330 /* If an error occurs from here on, goto finally instead of explicitly
331 * returning NULL.
332 */
333
334 if (!(str = PyString_FromString(modulename)))
335 goto finally;
336 if (PyDict_SetItemString(dict, "__module__", str))
337 goto finally;
338 Py_DECREF(str);
339 if (!(str = PyString_FromString(Exception__doc__)))
340 goto finally;
341 if (PyDict_SetItemString(dict, "__doc__", str))
342 goto finally;
343
344 if (!(name = PyString_FromString("Exception")))
345 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000346
Barry Warsaw675ac282000-05-26 19:05:16 +0000347 if (!(PyExc_Exception = PyClass_New(NULL, dict, name)))
348 goto finally;
349
350 /* Now populate the dictionary with the method suite */
351 if (populate_methods(PyExc_Exception, dict, Exception_methods))
352 /* Don't need to reclaim PyExc_Exception here because that'll
353 * happen during interpreter shutdown.
354 */
355 goto finally;
356
357 status = 0;
358
359 finally:
360 Py_XDECREF(dict);
361 Py_XDECREF(str);
362 Py_XDECREF(name);
363 return status;
364}
365
366
367
368static char
369StandardError__doc__[] = "Base class for all standard Python exceptions.";
370
371static char
372TypeError__doc__[] = "Inappropriate argument type.";
373
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000374static char
375StopIteration__doc__[] = "Signal the end from iterator.next().";
376
Barry Warsaw675ac282000-05-26 19:05:16 +0000377
378
379static char
380SystemExit__doc__[] = "Request to exit from the interpreter.";
381
382
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000383static PyObject *
384SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000385{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000386 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000387 int status;
388
389 if (!(self = get_self(args)))
390 return NULL;
391
392 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000393 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000394 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000395
Barry Warsaw675ac282000-05-26 19:05:16 +0000396 status = PyObject_SetAttrString(self, "args", args);
397 if (status < 0) {
398 Py_DECREF(args);
399 return NULL;
400 }
401
402 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000403 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000404 case 0:
405 Py_INCREF(Py_None);
406 code = Py_None;
407 break;
408 case 1:
409 code = PySequence_GetItem(args, 0);
410 break;
411 default:
412 Py_INCREF(args);
413 code = args;
414 break;
415 }
416
417 status = PyObject_SetAttrString(self, "code", code);
418 Py_DECREF(code);
419 Py_DECREF(args);
420 if (status < 0)
421 return NULL;
422
423 Py_INCREF(Py_None);
424 return Py_None;
425}
426
427
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000428static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000429 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000430 {NULL, NULL}
431};
432
433
434
435static char
436KeyboardInterrupt__doc__[] = "Program interrupted by user.";
437
438static char
439ImportError__doc__[] =
440"Import can't find module, or can't find name in module.";
441
442
443
444static char
445EnvironmentError__doc__[] = "Base class for I/O related errors.";
446
447
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000448static PyObject *
449EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000450{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000451 PyObject *item0 = NULL;
452 PyObject *item1 = NULL;
453 PyObject *item2 = NULL;
454 PyObject *subslice = NULL;
455 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000456
457 if (!(self = get_self(args)))
458 return NULL;
459
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000460 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000461 return NULL;
462
463 if (PyObject_SetAttrString(self, "args", args) ||
464 PyObject_SetAttrString(self, "errno", Py_None) ||
465 PyObject_SetAttrString(self, "strerror", Py_None) ||
466 PyObject_SetAttrString(self, "filename", Py_None))
467 {
468 goto finally;
469 }
470
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000471 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000472 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000473 /* Where a function has a single filename, such as open() or some
474 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
475 * called, giving a third argument which is the filename. But, so
476 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000477 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000478 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000479 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000480 * we hack args so that it only contains two items. This also
481 * means we need our own __str__() which prints out the filename
482 * when it was supplied.
483 */
484 item0 = PySequence_GetItem(args, 0);
485 item1 = PySequence_GetItem(args, 1);
486 item2 = PySequence_GetItem(args, 2);
487 if (!item0 || !item1 || !item2)
488 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000489
Barry Warsaw675ac282000-05-26 19:05:16 +0000490 if (PyObject_SetAttrString(self, "errno", item0) ||
491 PyObject_SetAttrString(self, "strerror", item1) ||
492 PyObject_SetAttrString(self, "filename", item2))
493 {
494 goto finally;
495 }
496
497 subslice = PySequence_GetSlice(args, 0, 2);
498 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
499 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000500 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000501
502 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000503 /* Used when PyErr_SetFromErrno() is called and no filename
504 * argument is given.
505 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000506 item0 = PySequence_GetItem(args, 0);
507 item1 = PySequence_GetItem(args, 1);
508 if (!item0 || !item1)
509 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000510
Barry Warsaw675ac282000-05-26 19:05:16 +0000511 if (PyObject_SetAttrString(self, "errno", item0) ||
512 PyObject_SetAttrString(self, "strerror", item1))
513 {
514 goto finally;
515 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000516 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000517 }
518
519 Py_INCREF(Py_None);
520 rtnval = Py_None;
521
522 finally:
523 Py_DECREF(args);
524 Py_XDECREF(item0);
525 Py_XDECREF(item1);
526 Py_XDECREF(item2);
527 Py_XDECREF(subslice);
528 return rtnval;
529}
530
531
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000532static PyObject *
533EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000534{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000535 PyObject *originalself = self;
536 PyObject *filename;
537 PyObject *serrno;
538 PyObject *strerror;
539 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000540
Fred Drake1aba5772000-08-15 15:46:16 +0000541 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000542 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000543
Barry Warsaw675ac282000-05-26 19:05:16 +0000544 filename = PyObject_GetAttrString(self, "filename");
545 serrno = PyObject_GetAttrString(self, "errno");
546 strerror = PyObject_GetAttrString(self, "strerror");
547 if (!filename || !serrno || !strerror)
548 goto finally;
549
550 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000551 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
552 PyObject *repr = PyObject_Repr(filename);
553 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000554
555 if (!fmt || !repr || !tuple) {
556 Py_XDECREF(fmt);
557 Py_XDECREF(repr);
558 Py_XDECREF(tuple);
559 goto finally;
560 }
561
562 PyTuple_SET_ITEM(tuple, 0, serrno);
563 PyTuple_SET_ITEM(tuple, 1, strerror);
564 PyTuple_SET_ITEM(tuple, 2, repr);
565
566 rtnval = PyString_Format(fmt, tuple);
567
568 Py_DECREF(fmt);
569 Py_DECREF(tuple);
570 /* already freed because tuple owned only reference */
571 serrno = NULL;
572 strerror = NULL;
573 }
574 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000575 PyObject *fmt = PyString_FromString("[Errno %s] %s");
576 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000577
578 if (!fmt || !tuple) {
579 Py_XDECREF(fmt);
580 Py_XDECREF(tuple);
581 goto finally;
582 }
583
584 PyTuple_SET_ITEM(tuple, 0, serrno);
585 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000586
Barry Warsaw675ac282000-05-26 19:05:16 +0000587 rtnval = PyString_Format(fmt, tuple);
588
589 Py_DECREF(fmt);
590 Py_DECREF(tuple);
591 /* already freed because tuple owned only reference */
592 serrno = NULL;
593 strerror = NULL;
594 }
595 else
596 /* The original Python code said:
597 *
598 * return StandardError.__str__(self)
599 *
600 * but there is no StandardError__str__() function; we happen to
601 * know that's just a pass through to Exception__str__().
602 */
603 rtnval = Exception__str__(originalself, args);
604
605 finally:
606 Py_XDECREF(filename);
607 Py_XDECREF(serrno);
608 Py_XDECREF(strerror);
609 return rtnval;
610}
611
612
613static
614PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000615 {"__init__", EnvironmentError__init__, METH_VARARGS},
616 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000617 {NULL, NULL}
618};
619
620
621
622
623static char
624IOError__doc__[] = "I/O operation failed.";
625
626static char
627OSError__doc__[] = "OS system call failed.";
628
629#ifdef MS_WINDOWS
630static char
631WindowsError__doc__[] = "MS-Windows OS system call failed.";
632#endif /* MS_WINDOWS */
633
634static char
635EOFError__doc__[] = "Read beyond end of file.";
636
637static char
638RuntimeError__doc__[] = "Unspecified run-time error.";
639
640static char
641NotImplementedError__doc__[] =
642"Method or function hasn't been implemented yet.";
643
644static char
645NameError__doc__[] = "Name not found globally.";
646
647static char
648UnboundLocalError__doc__[] =
649"Local name referenced but not bound to a value.";
650
651static char
652AttributeError__doc__[] = "Attribute not found.";
653
654
655
656static char
657SyntaxError__doc__[] = "Invalid syntax.";
658
659
660static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000661SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000662{
Barry Warsaw87bec352000-08-18 05:05:37 +0000663 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000664 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000665
666 /* Additional class-creation time initializations */
667 if (!emptystring ||
668 PyObject_SetAttrString(klass, "msg", emptystring) ||
669 PyObject_SetAttrString(klass, "filename", Py_None) ||
670 PyObject_SetAttrString(klass, "lineno", Py_None) ||
671 PyObject_SetAttrString(klass, "offset", Py_None) ||
672 PyObject_SetAttrString(klass, "text", Py_None))
673 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000674 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000675 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000676 Py_XDECREF(emptystring);
677 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000678}
679
680
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000681static PyObject *
682SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000683{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000684 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000685 int lenargs;
686
687 if (!(self = get_self(args)))
688 return NULL;
689
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000690 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000691 return NULL;
692
693 if (PyObject_SetAttrString(self, "args", args))
694 goto finally;
695
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000696 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000697 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000698 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000699 int status;
700
701 if (!item0)
702 goto finally;
703 status = PyObject_SetAttrString(self, "msg", item0);
704 Py_DECREF(item0);
705 if (status)
706 goto finally;
707 }
708 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000709 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000710 PyObject *filename = NULL, *lineno = NULL;
711 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000712 int status = 1;
713
714 if (!info)
715 goto finally;
716
717 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000718 if (filename != NULL) {
719 lineno = PySequence_GetItem(info, 1);
720 if (lineno != NULL) {
721 offset = PySequence_GetItem(info, 2);
722 if (offset != NULL) {
723 text = PySequence_GetItem(info, 3);
724 if (text != NULL) {
725 status =
726 PyObject_SetAttrString(self, "filename", filename)
727 || PyObject_SetAttrString(self, "lineno", lineno)
728 || PyObject_SetAttrString(self, "offset", offset)
729 || PyObject_SetAttrString(self, "text", text);
730 Py_DECREF(text);
731 }
732 Py_DECREF(offset);
733 }
734 Py_DECREF(lineno);
735 }
736 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000737 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000738 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000739
740 if (status)
741 goto finally;
742 }
743 Py_INCREF(Py_None);
744 rtnval = Py_None;
745
746 finally:
747 Py_DECREF(args);
748 return rtnval;
749}
750
751
Fred Drake185a29b2000-08-15 16:20:36 +0000752/* This is called "my_basename" instead of just "basename" to avoid name
753 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
754 defined, and Python does define that. */
755static char *
756my_basename(char *name)
757{
758 char *cp = name;
759 char *result = name;
760
761 if (name == NULL)
762 return "???";
763 while (*cp != '\0') {
764 if (*cp == SEP)
765 result = cp + 1;
766 ++cp;
767 }
768 return result;
769}
770
771
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000772static PyObject *
773SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000774{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000775 PyObject *msg;
776 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000777 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000778
Fred Drake1aba5772000-08-15 15:46:16 +0000779 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000780 return NULL;
781
782 if (!(msg = PyObject_GetAttrString(self, "msg")))
783 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000784
Barry Warsaw675ac282000-05-26 19:05:16 +0000785 str = PyObject_Str(msg);
786 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000787 result = str;
788
789 /* XXX -- do all the additional formatting with filename and
790 lineno here */
791
792 if (PyString_Check(str)) {
793 int have_filename = 0;
794 int have_lineno = 0;
795 char *buffer = NULL;
796
Barry Warsaw77c9f502000-08-16 19:43:17 +0000797 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000798 have_filename = PyString_Check(filename);
799 else
800 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000801
802 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000803 have_lineno = PyInt_Check(lineno);
804 else
805 PyErr_Clear();
806
807 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000808 int bufsize = PyString_GET_SIZE(str) + 64;
809 if (have_filename)
810 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000811
812 buffer = PyMem_Malloc(bufsize);
813 if (buffer != NULL) {
814 if (have_filename && have_lineno)
Fred Drake04e654a2000-08-18 19:53:25 +0000815 sprintf(buffer, "%s (%s, line %ld)",
Fred Drake1aba5772000-08-15 15:46:16 +0000816 PyString_AS_STRING(str),
Fred Drake185a29b2000-08-15 16:20:36 +0000817 my_basename(PyString_AS_STRING(filename)),
Fred Drake1aba5772000-08-15 15:46:16 +0000818 PyInt_AsLong(lineno));
819 else if (have_filename)
820 sprintf(buffer, "%s (%s)",
821 PyString_AS_STRING(str),
Fred Drake185a29b2000-08-15 16:20:36 +0000822 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000823 else if (have_lineno)
Fred Drake04e654a2000-08-18 19:53:25 +0000824 sprintf(buffer, "%s (line %ld)",
Fred Drake1aba5772000-08-15 15:46:16 +0000825 PyString_AS_STRING(str),
826 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000827
Fred Drake1aba5772000-08-15 15:46:16 +0000828 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000829 PyMem_FREE(buffer);
830
Fred Drake1aba5772000-08-15 15:46:16 +0000831 if (result == NULL)
832 result = str;
833 else
834 Py_DECREF(str);
835 }
836 }
837 Py_XDECREF(filename);
838 Py_XDECREF(lineno);
839 }
840 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000841}
842
843
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000844static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000845 {"__init__", SyntaxError__init__, METH_VARARGS},
846 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000847 {NULL, NULL}
848};
849
850
851
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000852/* Exception doc strings */
853
Barry Warsaw675ac282000-05-26 19:05:16 +0000854static char
855AssertionError__doc__[] = "Assertion failed.";
856
857static char
858LookupError__doc__[] = "Base class for lookup errors.";
859
860static char
861IndexError__doc__[] = "Sequence index out of range.";
862
863static char
864KeyError__doc__[] = "Mapping key not found.";
865
866static char
867ArithmeticError__doc__[] = "Base class for arithmetic errors.";
868
869static char
870OverflowError__doc__[] = "Result too large to be represented.";
871
872static char
873ZeroDivisionError__doc__[] =
874"Second argument to a division or modulo operation was zero.";
875
876static char
877FloatingPointError__doc__[] = "Floating point operation failed.";
878
879static char
880ValueError__doc__[] = "Inappropriate argument value (of correct type).";
881
882static char
883UnicodeError__doc__[] = "Unicode related error.";
884
885static char
886SystemError__doc__[] = "Internal error in the Python interpreter.\n\
887\n\
888Please report this to the Python maintainer, along with the traceback,\n\
889the Python version, and the hardware/OS platform and version.";
890
891static char
892MemoryError__doc__[] = "Out of memory.";
893
Fred Drake85f36392000-07-11 17:53:00 +0000894static char
895IndentationError__doc__[] = "Improper indentation.";
896
897static char
898TabError__doc__[] = "Improper mixture of spaces and tabs.";
899
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000900/* Warning category docstrings */
901
902static char
903Warning__doc__[] = "Base class for warning categories.";
904
905static char
906UserWarning__doc__[] = "Base class for warnings generated by user code.";
907
908static char
909DeprecationWarning__doc__[] =
910"Base class for warnings about deprecated features.";
911
912static char
913SyntaxWarning__doc__[] = "Base class for warnings about dubious syntax.";
914
915static char
Guido van Rossumae347b32001-08-23 02:56:07 +0000916OverflowWarning__doc__[] = "Base class for warnings about numeric overflow.";
917
918static char
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000919RuntimeWarning__doc__[] =
920"Base class for warnings about dubious runtime behavior.";
921
Barry Warsaw675ac282000-05-26 19:05:16 +0000922
923
924/* module global functions */
925static PyMethodDef functions[] = {
926 /* Sentinel */
927 {NULL, NULL}
928};
929
930
931
932/* Global C API defined exceptions */
933
934PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000935PyObject *PyExc_StopIteration;
Barry Warsaw675ac282000-05-26 19:05:16 +0000936PyObject *PyExc_StandardError;
937PyObject *PyExc_ArithmeticError;
938PyObject *PyExc_LookupError;
939
940PyObject *PyExc_AssertionError;
941PyObject *PyExc_AttributeError;
942PyObject *PyExc_EOFError;
943PyObject *PyExc_FloatingPointError;
944PyObject *PyExc_EnvironmentError;
945PyObject *PyExc_IOError;
946PyObject *PyExc_OSError;
947PyObject *PyExc_ImportError;
948PyObject *PyExc_IndexError;
949PyObject *PyExc_KeyError;
950PyObject *PyExc_KeyboardInterrupt;
951PyObject *PyExc_MemoryError;
952PyObject *PyExc_NameError;
953PyObject *PyExc_OverflowError;
954PyObject *PyExc_RuntimeError;
955PyObject *PyExc_NotImplementedError;
956PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +0000957PyObject *PyExc_IndentationError;
958PyObject *PyExc_TabError;
Barry Warsaw675ac282000-05-26 19:05:16 +0000959PyObject *PyExc_SystemError;
960PyObject *PyExc_SystemExit;
961PyObject *PyExc_UnboundLocalError;
962PyObject *PyExc_UnicodeError;
963PyObject *PyExc_TypeError;
964PyObject *PyExc_ValueError;
965PyObject *PyExc_ZeroDivisionError;
966#ifdef MS_WINDOWS
967PyObject *PyExc_WindowsError;
968#endif
969
970/* Pre-computed MemoryError instance. Best to create this as early as
971 * possibly and not wait until a MemoryError is actually raised!
972 */
973PyObject *PyExc_MemoryErrorInst;
974
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000975/* Predefined warning categories */
976PyObject *PyExc_Warning;
977PyObject *PyExc_UserWarning;
978PyObject *PyExc_DeprecationWarning;
979PyObject *PyExc_SyntaxWarning;
Guido van Rossumae347b32001-08-23 02:56:07 +0000980PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000981PyObject *PyExc_RuntimeWarning;
982
Barry Warsaw675ac282000-05-26 19:05:16 +0000983
984
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000985/* mapping between exception names and their PyObject ** */
986static struct {
987 char *name;
988 PyObject **exc;
989 PyObject **base; /* NULL == PyExc_StandardError */
990 char *docstr;
991 PyMethodDef *methods;
992 int (*classinit)(PyObject *);
993} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +0000994 /*
995 * The first three classes MUST appear in exactly this order
996 */
997 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000998 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
999 StopIteration__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001000 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1001 StandardError__doc__},
1002 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1003 /*
1004 * The rest appear in depth-first order of the hierarchy
1005 */
1006 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1007 SystemExit_methods},
1008 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1009 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1010 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1011 EnvironmentError_methods},
1012 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1013 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1014#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001015 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001016 WindowsError__doc__},
1017#endif /* MS_WINDOWS */
1018 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1019 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1020 {"NotImplementedError", &PyExc_NotImplementedError,
1021 &PyExc_RuntimeError, NotImplementedError__doc__},
1022 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1023 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1024 UnboundLocalError__doc__},
1025 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1026 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1027 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001028 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1029 IndentationError__doc__},
1030 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1031 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001032 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1033 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1034 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1035 IndexError__doc__},
1036 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
1037 KeyError__doc__},
1038 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1039 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1040 OverflowError__doc__},
1041 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1042 ZeroDivisionError__doc__},
1043 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1044 FloatingPointError__doc__},
1045 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1046 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
1047 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1048 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001049 /* Warning categories */
1050 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1051 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1052 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1053 DeprecationWarning__doc__},
1054 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Guido van Rossumae347b32001-08-23 02:56:07 +00001055 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1056 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001057 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1058 RuntimeWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001059 /* Sentinel */
1060 {NULL}
1061};
1062
1063
1064
Tim Peters5687ffe2001-02-28 16:44:18 +00001065DL_EXPORT(void)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001066_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001067{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001068 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001069 int modnamesz = strlen(modulename);
1070 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001071 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001072
Tim Peters6d6c1a32001-08-02 04:15:00 +00001073 me = Py_InitModule(modulename, functions);
1074 if (me == NULL)
1075 goto err;
1076 mydict = PyModule_GetDict(me);
1077 if (mydict == NULL)
1078 goto err;
1079 bltinmod = PyImport_ImportModule("__builtin__");
1080 if (bltinmod == NULL)
1081 goto err;
1082 bdict = PyModule_GetDict(bltinmod);
1083 if (bdict == NULL)
1084 goto err;
1085 doc = PyString_FromString(module__doc__);
1086 if (doc == NULL)
1087 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001088
Tim Peters6d6c1a32001-08-02 04:15:00 +00001089 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001090 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001091 if (i < 0) {
1092 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001093 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001094 return;
1095 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001096
1097 /* This is the base class of all exceptions, so make it first. */
1098 if (make_Exception(modulename) ||
1099 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1100 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1101 {
1102 Py_FatalError("Base class `Exception' could not be created.");
1103 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001104
Barry Warsaw675ac282000-05-26 19:05:16 +00001105 /* Now we can programmatically create all the remaining exceptions.
1106 * Remember to start the loop at 1 to skip Exceptions.
1107 */
1108 for (i=1; exctable[i].name; i++) {
1109 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001110 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1111 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001112
1113 (void)strcpy(cname, modulename);
1114 (void)strcat(cname, ".");
1115 (void)strcat(cname, exctable[i].name);
1116
1117 if (exctable[i].base == 0)
1118 base = PyExc_StandardError;
1119 else
1120 base = *exctable[i].base;
1121
1122 status = make_class(exctable[i].exc, base, cname,
1123 exctable[i].methods,
1124 exctable[i].docstr);
1125
1126 PyMem_DEL(cname);
1127
1128 if (status)
1129 Py_FatalError("Standard exception classes could not be created.");
1130
1131 if (exctable[i].classinit) {
1132 status = (*exctable[i].classinit)(*exctable[i].exc);
1133 if (status)
1134 Py_FatalError("An exception class could not be initialized.");
1135 }
1136
1137 /* Now insert the class into both this module and the __builtin__
1138 * module.
1139 */
1140 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1141 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1142 {
1143 Py_FatalError("Module dictionary insertion problem.");
1144 }
1145 }
1146
1147 /* Now we need to pre-allocate a MemoryError instance */
1148 args = Py_BuildValue("()");
1149 if (!args ||
1150 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1151 {
1152 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1153 }
1154 Py_DECREF(args);
1155
1156 /* We're done with __builtin__ */
1157 Py_DECREF(bltinmod);
1158}
1159
1160
Tim Peters5687ffe2001-02-28 16:44:18 +00001161DL_EXPORT(void)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001162_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001163{
1164 int i;
1165
1166 Py_XDECREF(PyExc_MemoryErrorInst);
1167 PyExc_MemoryErrorInst = NULL;
1168
1169 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001170 /* clear the class's dictionary, freeing up circular references
1171 * between the class and its methods.
1172 */
1173 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1174 PyDict_Clear(cdict);
1175 Py_DECREF(cdict);
1176
1177 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001178 Py_XDECREF(*exctable[i].exc);
1179 *exctable[i].exc = NULL;
1180 }
1181}