blob: bd8f55db107b085f18f140212f9a6eb1e8b78795 [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 */
Skip Montanaro995895f2002-03-28 20:57:51 +000028
29/* NOTE: If the exception class hierarchy changes, don't forget to update
30 * Doc/lib/libexcs.tex!
31 */
32
Barry Warsaw675ac282000-05-26 19:05:16 +000033static char
Barry Warsaw9667ed22001-01-23 16:08:34 +000034module__doc__[] =
Barry Warsaw675ac282000-05-26 19:05:16 +000035"Python's standard exception class hierarchy.\n\
36\n\
37Before Python 1.5, the standard exceptions were all simple string objects.\n\
38In Python 1.5, the standard exceptions were converted to classes organized\n\
39into a relatively flat hierarchy. String-based standard exceptions were\n\
40optional, or used as a fallback if some problem occurred while importing\n\
41the exception module. With Python 1.6, optional string-based standard\n\
42exceptions were removed (along with the -X command line flag).\n\
43\n\
44The class exceptions were implemented in such a way as to be almost\n\
45completely backward compatible. Some tricky uses of IOError could\n\
46potentially have broken, but by Python 1.6, all of these should have\n\
47been fixed. As of Python 1.6, the class-based standard exceptions are\n\
48now implemented in C, and are guaranteed to exist in the Python\n\
49interpreter.\n\
50\n\
51Here is a rundown of the class hierarchy. The classes found here are\n\
52inserted into both the exceptions module and the `built-in' module. It is\n\
53recommended that user defined class based exceptions be derived from the\n\
Tim Petersbf26e072000-07-12 04:02:10 +000054`Exception' class, although this is currently not enforced.\n"
55 /* keep string pieces "small" */
56"\n\
Barry Warsaw675ac282000-05-26 19:05:16 +000057Exception\n\
58 |\n\
59 +-- SystemExit\n\
Guido van Rossum59d1d2b2001-04-20 19:13:02 +000060 +-- StopIteration\n\
Barry Warsaw675ac282000-05-26 19:05:16 +000061 +-- StandardError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +000062 | |\n\
63 | +-- KeyboardInterrupt\n\
64 | +-- ImportError\n\
65 | +-- EnvironmentError\n\
66 | | |\n\
67 | | +-- IOError\n\
68 | | +-- OSError\n\
69 | | |\n\
70 | | +-- WindowsError\n\
71 | |\n\
72 | +-- EOFError\n\
73 | +-- RuntimeError\n\
74 | | |\n\
75 | | +-- NotImplementedError\n\
76 | |\n\
77 | +-- NameError\n\
78 | | |\n\
79 | | +-- UnboundLocalError\n\
80 | |\n\
81 | +-- AttributeError\n\
82 | +-- SyntaxError\n\
83 | | |\n\
84 | | +-- IndentationError\n\
85 | | |\n\
86 | | +-- TabError\n\
87 | |\n\
88 | +-- TypeError\n\
89 | +-- AssertionError\n\
90 | +-- LookupError\n\
91 | | |\n\
92 | | +-- IndexError\n\
93 | | +-- KeyError\n\
94 | |\n\
95 | +-- ArithmeticError\n\
96 | | |\n\
97 | | +-- OverflowError\n\
98 | | +-- ZeroDivisionError\n\
99 | | +-- FloatingPointError\n\
100 | |\n\
101 | +-- ValueError\n\
102 | | |\n\
103 | | +-- UnicodeError\n\
104 | |\n\
Fred Drakebb9fa212001-10-05 21:50:08 +0000105 | +-- ReferenceError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000106 | +-- SystemError\n\
107 | +-- MemoryError\n\
108 |\n\
109 +---Warning\n\
Barry Warsaw675ac282000-05-26 19:05:16 +0000110 |\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000111 +-- UserWarning\n\
112 +-- DeprecationWarning\n\
Neal Norwitzd68f5172002-05-29 15:54:55 +0000113 +-- PendingDeprecationWarning\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000114 +-- SyntaxWarning\n\
Guido van Rossumae347b32001-08-23 02:56:07 +0000115 +-- OverflowWarning\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000116 +-- RuntimeWarning";
Barry Warsaw675ac282000-05-26 19:05:16 +0000117
118
119/* Helper function for populating a dictionary with method wrappers. */
120static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000121populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +0000122{
123 if (!methods)
124 return 0;
125
126 while (methods->ml_name) {
127 /* get a wrapper for the built-in function */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000128 PyObject *func = PyCFunction_New(methods, NULL);
129 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +0000130 int status;
131
132 if (!func)
133 return -1;
134
135 /* turn the function into an unbound method */
136 if (!(meth = PyMethod_New(func, NULL, klass))) {
137 Py_DECREF(func);
138 return -1;
139 }
Barry Warsaw9667ed22001-01-23 16:08:34 +0000140
Barry Warsaw675ac282000-05-26 19:05:16 +0000141 /* add method to dictionary */
142 status = PyDict_SetItemString(dict, methods->ml_name, meth);
143 Py_DECREF(meth);
144 Py_DECREF(func);
145
146 /* stop now if an error occurred, otherwise do the next method */
147 if (status)
148 return status;
149
150 methods++;
151 }
152 return 0;
153}
154
Barry Warsaw9667ed22001-01-23 16:08:34 +0000155
Barry Warsaw675ac282000-05-26 19:05:16 +0000156
157/* This function is used to create all subsequent exception classes. */
158static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000159make_class(PyObject **klass, PyObject *base,
160 char *name, PyMethodDef *methods,
161 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +0000162{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000163 PyObject *dict = PyDict_New();
164 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000165 int status = -1;
166
167 if (!dict)
168 return -1;
169
170 /* If an error occurs from here on, goto finally instead of explicitly
171 * returning NULL.
172 */
173
174 if (docstr) {
175 if (!(str = PyString_FromString(docstr)))
176 goto finally;
177 if (PyDict_SetItemString(dict, "__doc__", str))
178 goto finally;
179 }
180
181 if (!(*klass = PyErr_NewException(name, base, dict)))
182 goto finally;
183
184 if (populate_methods(*klass, dict, methods)) {
185 Py_DECREF(*klass);
186 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000187 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000188 }
189
190 status = 0;
191
192 finally:
193 Py_XDECREF(dict);
194 Py_XDECREF(str);
195 return status;
196}
197
198
199/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000200static PyObject *
201get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000202{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000203 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000204 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000205 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000206 if (PyExc_TypeError) {
207 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000208 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000209 }
210 return NULL;
211 }
212 return self;
213}
214
215
216
217/* Notes on bootstrapping the exception classes.
218 *
219 * First thing we create is the base class for all exceptions, called
220 * appropriately enough: Exception. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000221 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000222 * for TypeError, which can conditionally exist.
223 *
224 * Next, StandardError is created (which is quite simple) followed by
225 * TypeError, because the instantiation of other exceptions can potentially
226 * throw a TypeError. Once these exceptions are created, all the others
227 * can be created in any order. See the static exctable below for the
228 * explicit bootstrap order.
229 *
230 * All classes after Exception can be created using PyErr_NewException().
231 */
232
233static char
234Exception__doc__[] = "Common base class for all exceptions.";
235
236
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000237static PyObject *
238Exception__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000239{
240 int status;
241
242 if (!(self = get_self(args)))
243 return NULL;
244
245 /* set args attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000246 /* XXX size is only a hint */
247 args = PySequence_GetSlice(args, 1, PySequence_Size(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000248 if (!args)
249 return NULL;
250 status = PyObject_SetAttrString(self, "args", args);
251 Py_DECREF(args);
252 if (status < 0)
253 return NULL;
254
255 Py_INCREF(Py_None);
256 return Py_None;
257}
258
259
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000260static PyObject *
261Exception__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000262{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000263 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000264
Fred Drake1aba5772000-08-15 15:46:16 +0000265 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000266 return NULL;
267
268 args = PyObject_GetAttrString(self, "args");
269 if (!args)
270 return NULL;
271
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000272 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000273 case 0:
274 out = PyString_FromString("");
275 break;
276 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000277 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000278 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000279 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000280 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000281 Py_DECREF(tmp);
282 }
283 else
284 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000285 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000286 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000287 default:
288 out = PyObject_Str(args);
289 break;
290 }
291
292 Py_DECREF(args);
293 return out;
294}
295
296
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000297static PyObject *
298Exception__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000299{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000300 PyObject *out;
301 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000302
Fred Drake1aba5772000-08-15 15:46:16 +0000303 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000304 return NULL;
305
306 args = PyObject_GetAttrString(self, "args");
307 if (!args)
308 return NULL;
309
310 out = PyObject_GetItem(args, index);
311 Py_DECREF(args);
312 return out;
313}
314
315
316static PyMethodDef
317Exception_methods[] = {
318 /* methods for the Exception class */
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000319 { "__getitem__", Exception__getitem__, METH_VARARGS},
320 { "__str__", Exception__str__, METH_VARARGS},
321 { "__init__", Exception__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000322 { NULL, NULL }
323};
324
325
326static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000327make_Exception(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000328{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000329 PyObject *dict = PyDict_New();
330 PyObject *str = NULL;
331 PyObject *name = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000332 int status = -1;
333
334 if (!dict)
335 return -1;
336
337 /* If an error occurs from here on, goto finally instead of explicitly
338 * returning NULL.
339 */
340
341 if (!(str = PyString_FromString(modulename)))
342 goto finally;
343 if (PyDict_SetItemString(dict, "__module__", str))
344 goto finally;
345 Py_DECREF(str);
346 if (!(str = PyString_FromString(Exception__doc__)))
347 goto finally;
348 if (PyDict_SetItemString(dict, "__doc__", str))
349 goto finally;
350
351 if (!(name = PyString_FromString("Exception")))
352 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000353
Barry Warsaw675ac282000-05-26 19:05:16 +0000354 if (!(PyExc_Exception = PyClass_New(NULL, dict, name)))
355 goto finally;
356
357 /* Now populate the dictionary with the method suite */
358 if (populate_methods(PyExc_Exception, dict, Exception_methods))
359 /* Don't need to reclaim PyExc_Exception here because that'll
360 * happen during interpreter shutdown.
361 */
362 goto finally;
363
364 status = 0;
365
366 finally:
367 Py_XDECREF(dict);
368 Py_XDECREF(str);
369 Py_XDECREF(name);
370 return status;
371}
372
373
374
375static char
376StandardError__doc__[] = "Base class for all standard Python exceptions.";
377
378static char
379TypeError__doc__[] = "Inappropriate argument type.";
380
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000381static char
382StopIteration__doc__[] = "Signal the end from iterator.next().";
383
Barry Warsaw675ac282000-05-26 19:05:16 +0000384
385
386static char
387SystemExit__doc__[] = "Request to exit from the interpreter.";
388
389
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000390static PyObject *
391SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000392{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000393 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000394 int status;
395
396 if (!(self = get_self(args)))
397 return NULL;
398
399 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000400 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000401 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000402
Barry Warsaw675ac282000-05-26 19:05:16 +0000403 status = PyObject_SetAttrString(self, "args", args);
404 if (status < 0) {
405 Py_DECREF(args);
406 return NULL;
407 }
408
409 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000410 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000411 case 0:
412 Py_INCREF(Py_None);
413 code = Py_None;
414 break;
415 case 1:
416 code = PySequence_GetItem(args, 0);
417 break;
418 default:
419 Py_INCREF(args);
420 code = args;
421 break;
422 }
423
424 status = PyObject_SetAttrString(self, "code", code);
425 Py_DECREF(code);
426 Py_DECREF(args);
427 if (status < 0)
428 return NULL;
429
430 Py_INCREF(Py_None);
431 return Py_None;
432}
433
434
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000435static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000436 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000437 {NULL, NULL}
438};
439
440
441
442static char
443KeyboardInterrupt__doc__[] = "Program interrupted by user.";
444
445static char
446ImportError__doc__[] =
447"Import can't find module, or can't find name in module.";
448
449
450
451static char
452EnvironmentError__doc__[] = "Base class for I/O related errors.";
453
454
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000455static PyObject *
456EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000457{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000458 PyObject *item0 = NULL;
459 PyObject *item1 = NULL;
460 PyObject *item2 = NULL;
461 PyObject *subslice = NULL;
462 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000463
464 if (!(self = get_self(args)))
465 return NULL;
466
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000467 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000468 return NULL;
469
470 if (PyObject_SetAttrString(self, "args", args) ||
471 PyObject_SetAttrString(self, "errno", Py_None) ||
472 PyObject_SetAttrString(self, "strerror", Py_None) ||
473 PyObject_SetAttrString(self, "filename", Py_None))
474 {
475 goto finally;
476 }
477
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000478 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000479 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000480 /* Where a function has a single filename, such as open() or some
481 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
482 * called, giving a third argument which is the filename. But, so
483 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000484 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000485 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000486 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000487 * we hack args so that it only contains two items. This also
488 * means we need our own __str__() which prints out the filename
489 * when it was supplied.
490 */
491 item0 = PySequence_GetItem(args, 0);
492 item1 = PySequence_GetItem(args, 1);
493 item2 = PySequence_GetItem(args, 2);
494 if (!item0 || !item1 || !item2)
495 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000496
Barry Warsaw675ac282000-05-26 19:05:16 +0000497 if (PyObject_SetAttrString(self, "errno", item0) ||
498 PyObject_SetAttrString(self, "strerror", item1) ||
499 PyObject_SetAttrString(self, "filename", item2))
500 {
501 goto finally;
502 }
503
504 subslice = PySequence_GetSlice(args, 0, 2);
505 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
506 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000507 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000508
509 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000510 /* Used when PyErr_SetFromErrno() is called and no filename
511 * argument is given.
512 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000513 item0 = PySequence_GetItem(args, 0);
514 item1 = PySequence_GetItem(args, 1);
515 if (!item0 || !item1)
516 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000517
Barry Warsaw675ac282000-05-26 19:05:16 +0000518 if (PyObject_SetAttrString(self, "errno", item0) ||
519 PyObject_SetAttrString(self, "strerror", item1))
520 {
521 goto finally;
522 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000523 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000524 }
525
526 Py_INCREF(Py_None);
527 rtnval = Py_None;
528
529 finally:
530 Py_DECREF(args);
531 Py_XDECREF(item0);
532 Py_XDECREF(item1);
533 Py_XDECREF(item2);
534 Py_XDECREF(subslice);
535 return rtnval;
536}
537
538
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000539static PyObject *
540EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000541{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000542 PyObject *originalself = self;
543 PyObject *filename;
544 PyObject *serrno;
545 PyObject *strerror;
546 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000547
Fred Drake1aba5772000-08-15 15:46:16 +0000548 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000549 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000550
Barry Warsaw675ac282000-05-26 19:05:16 +0000551 filename = PyObject_GetAttrString(self, "filename");
552 serrno = PyObject_GetAttrString(self, "errno");
553 strerror = PyObject_GetAttrString(self, "strerror");
554 if (!filename || !serrno || !strerror)
555 goto finally;
556
557 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000558 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
559 PyObject *repr = PyObject_Repr(filename);
560 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000561
562 if (!fmt || !repr || !tuple) {
563 Py_XDECREF(fmt);
564 Py_XDECREF(repr);
565 Py_XDECREF(tuple);
566 goto finally;
567 }
568
569 PyTuple_SET_ITEM(tuple, 0, serrno);
570 PyTuple_SET_ITEM(tuple, 1, strerror);
571 PyTuple_SET_ITEM(tuple, 2, repr);
572
573 rtnval = PyString_Format(fmt, tuple);
574
575 Py_DECREF(fmt);
576 Py_DECREF(tuple);
577 /* already freed because tuple owned only reference */
578 serrno = NULL;
579 strerror = NULL;
580 }
581 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000582 PyObject *fmt = PyString_FromString("[Errno %s] %s");
583 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000584
585 if (!fmt || !tuple) {
586 Py_XDECREF(fmt);
587 Py_XDECREF(tuple);
588 goto finally;
589 }
590
591 PyTuple_SET_ITEM(tuple, 0, serrno);
592 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000593
Barry Warsaw675ac282000-05-26 19:05:16 +0000594 rtnval = PyString_Format(fmt, tuple);
595
596 Py_DECREF(fmt);
597 Py_DECREF(tuple);
598 /* already freed because tuple owned only reference */
599 serrno = NULL;
600 strerror = NULL;
601 }
602 else
603 /* The original Python code said:
604 *
605 * return StandardError.__str__(self)
606 *
607 * but there is no StandardError__str__() function; we happen to
608 * know that's just a pass through to Exception__str__().
609 */
610 rtnval = Exception__str__(originalself, args);
611
612 finally:
613 Py_XDECREF(filename);
614 Py_XDECREF(serrno);
615 Py_XDECREF(strerror);
616 return rtnval;
617}
618
619
620static
621PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000622 {"__init__", EnvironmentError__init__, METH_VARARGS},
623 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000624 {NULL, NULL}
625};
626
627
628
629
630static char
631IOError__doc__[] = "I/O operation failed.";
632
633static char
634OSError__doc__[] = "OS system call failed.";
635
636#ifdef MS_WINDOWS
637static char
638WindowsError__doc__[] = "MS-Windows OS system call failed.";
639#endif /* MS_WINDOWS */
640
641static char
642EOFError__doc__[] = "Read beyond end of file.";
643
644static char
645RuntimeError__doc__[] = "Unspecified run-time error.";
646
647static char
648NotImplementedError__doc__[] =
649"Method or function hasn't been implemented yet.";
650
651static char
652NameError__doc__[] = "Name not found globally.";
653
654static char
655UnboundLocalError__doc__[] =
656"Local name referenced but not bound to a value.";
657
658static char
659AttributeError__doc__[] = "Attribute not found.";
660
661
662
663static char
664SyntaxError__doc__[] = "Invalid syntax.";
665
666
667static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000668SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000669{
Barry Warsaw87bec352000-08-18 05:05:37 +0000670 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000671 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000672
673 /* Additional class-creation time initializations */
674 if (!emptystring ||
675 PyObject_SetAttrString(klass, "msg", emptystring) ||
676 PyObject_SetAttrString(klass, "filename", Py_None) ||
677 PyObject_SetAttrString(klass, "lineno", Py_None) ||
678 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000679 PyObject_SetAttrString(klass, "text", Py_None) ||
680 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000681 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000682 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000683 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000684 Py_XDECREF(emptystring);
685 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000686}
687
688
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000689static PyObject *
690SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000691{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000692 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000693 int lenargs;
694
695 if (!(self = get_self(args)))
696 return NULL;
697
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000698 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000699 return NULL;
700
701 if (PyObject_SetAttrString(self, "args", args))
702 goto finally;
703
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000704 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000705 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000706 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000707 int status;
708
709 if (!item0)
710 goto finally;
711 status = PyObject_SetAttrString(self, "msg", item0);
712 Py_DECREF(item0);
713 if (status)
714 goto finally;
715 }
716 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000717 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000718 PyObject *filename = NULL, *lineno = NULL;
719 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000720 int status = 1;
721
722 if (!info)
723 goto finally;
724
725 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000726 if (filename != NULL) {
727 lineno = PySequence_GetItem(info, 1);
728 if (lineno != NULL) {
729 offset = PySequence_GetItem(info, 2);
730 if (offset != NULL) {
731 text = PySequence_GetItem(info, 3);
732 if (text != NULL) {
733 status =
734 PyObject_SetAttrString(self, "filename", filename)
735 || PyObject_SetAttrString(self, "lineno", lineno)
736 || PyObject_SetAttrString(self, "offset", offset)
737 || PyObject_SetAttrString(self, "text", text);
738 Py_DECREF(text);
739 }
740 Py_DECREF(offset);
741 }
742 Py_DECREF(lineno);
743 }
744 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000745 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000746 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000747
748 if (status)
749 goto finally;
750 }
751 Py_INCREF(Py_None);
752 rtnval = Py_None;
753
754 finally:
755 Py_DECREF(args);
756 return rtnval;
757}
758
759
Fred Drake185a29b2000-08-15 16:20:36 +0000760/* This is called "my_basename" instead of just "basename" to avoid name
761 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
762 defined, and Python does define that. */
763static char *
764my_basename(char *name)
765{
766 char *cp = name;
767 char *result = name;
768
769 if (name == NULL)
770 return "???";
771 while (*cp != '\0') {
772 if (*cp == SEP)
773 result = cp + 1;
774 ++cp;
775 }
776 return result;
777}
778
779
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000780static PyObject *
781SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000782{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000783 PyObject *msg;
784 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000785 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000786
Fred Drake1aba5772000-08-15 15:46:16 +0000787 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000788 return NULL;
789
790 if (!(msg = PyObject_GetAttrString(self, "msg")))
791 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000792
Barry Warsaw675ac282000-05-26 19:05:16 +0000793 str = PyObject_Str(msg);
794 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000795 result = str;
796
797 /* XXX -- do all the additional formatting with filename and
798 lineno here */
799
800 if (PyString_Check(str)) {
801 int have_filename = 0;
802 int have_lineno = 0;
803 char *buffer = NULL;
804
Barry Warsaw77c9f502000-08-16 19:43:17 +0000805 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000806 have_filename = PyString_Check(filename);
807 else
808 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000809
810 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000811 have_lineno = PyInt_Check(lineno);
812 else
813 PyErr_Clear();
814
815 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000816 int bufsize = PyString_GET_SIZE(str) + 64;
817 if (have_filename)
818 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000819
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000820 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000821 if (buffer != NULL) {
822 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000823 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
824 PyString_AS_STRING(str),
825 my_basename(PyString_AS_STRING(filename)),
826 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000827 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000828 PyOS_snprintf(buffer, bufsize, "%s (%s)",
829 PyString_AS_STRING(str),
830 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000831 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000832 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
833 PyString_AS_STRING(str),
834 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000835
Fred Drake1aba5772000-08-15 15:46:16 +0000836 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000837 PyMem_FREE(buffer);
838
Fred Drake1aba5772000-08-15 15:46:16 +0000839 if (result == NULL)
840 result = str;
841 else
842 Py_DECREF(str);
843 }
844 }
845 Py_XDECREF(filename);
846 Py_XDECREF(lineno);
847 }
848 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000849}
850
851
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000852static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000853 {"__init__", SyntaxError__init__, METH_VARARGS},
854 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000855 {NULL, NULL}
856};
857
858
859
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000860/* Exception doc strings */
861
Barry Warsaw675ac282000-05-26 19:05:16 +0000862static char
863AssertionError__doc__[] = "Assertion failed.";
864
865static char
866LookupError__doc__[] = "Base class for lookup errors.";
867
868static char
869IndexError__doc__[] = "Sequence index out of range.";
870
871static char
872KeyError__doc__[] = "Mapping key not found.";
873
874static char
875ArithmeticError__doc__[] = "Base class for arithmetic errors.";
876
877static char
878OverflowError__doc__[] = "Result too large to be represented.";
879
880static char
881ZeroDivisionError__doc__[] =
882"Second argument to a division or modulo operation was zero.";
883
884static char
885FloatingPointError__doc__[] = "Floating point operation failed.";
886
887static char
888ValueError__doc__[] = "Inappropriate argument value (of correct type).";
889
890static char
891UnicodeError__doc__[] = "Unicode related error.";
892
893static char
894SystemError__doc__[] = "Internal error in the Python interpreter.\n\
895\n\
896Please report this to the Python maintainer, along with the traceback,\n\
897the Python version, and the hardware/OS platform and version.";
898
899static char
Fred Drakebb9fa212001-10-05 21:50:08 +0000900ReferenceError__doc__[] = "Weak ref proxy used after referent went away.";
901
902static char
Barry Warsaw675ac282000-05-26 19:05:16 +0000903MemoryError__doc__[] = "Out of memory.";
904
Fred Drake85f36392000-07-11 17:53:00 +0000905static char
906IndentationError__doc__[] = "Improper indentation.";
907
908static char
909TabError__doc__[] = "Improper mixture of spaces and tabs.";
910
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000911/* Warning category docstrings */
912
913static char
914Warning__doc__[] = "Base class for warning categories.";
915
916static char
917UserWarning__doc__[] = "Base class for warnings generated by user code.";
918
919static char
920DeprecationWarning__doc__[] =
921"Base class for warnings about deprecated features.";
922
923static char
Neal Norwitzd68f5172002-05-29 15:54:55 +0000924PendingDeprecationWarning__doc__[] =
925"Base class for warnings about features which will be deprecated "
926"in the future.";
927
928static char
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000929SyntaxWarning__doc__[] = "Base class for warnings about dubious syntax.";
930
931static char
Guido van Rossumae347b32001-08-23 02:56:07 +0000932OverflowWarning__doc__[] = "Base class for warnings about numeric overflow.";
933
934static char
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000935RuntimeWarning__doc__[] =
936"Base class for warnings about dubious runtime behavior.";
937
Barry Warsaw675ac282000-05-26 19:05:16 +0000938
939
940/* module global functions */
941static PyMethodDef functions[] = {
942 /* Sentinel */
943 {NULL, NULL}
944};
945
946
947
948/* Global C API defined exceptions */
949
950PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000951PyObject *PyExc_StopIteration;
Barry Warsaw675ac282000-05-26 19:05:16 +0000952PyObject *PyExc_StandardError;
953PyObject *PyExc_ArithmeticError;
954PyObject *PyExc_LookupError;
955
956PyObject *PyExc_AssertionError;
957PyObject *PyExc_AttributeError;
958PyObject *PyExc_EOFError;
959PyObject *PyExc_FloatingPointError;
960PyObject *PyExc_EnvironmentError;
961PyObject *PyExc_IOError;
962PyObject *PyExc_OSError;
963PyObject *PyExc_ImportError;
964PyObject *PyExc_IndexError;
965PyObject *PyExc_KeyError;
966PyObject *PyExc_KeyboardInterrupt;
967PyObject *PyExc_MemoryError;
968PyObject *PyExc_NameError;
969PyObject *PyExc_OverflowError;
970PyObject *PyExc_RuntimeError;
971PyObject *PyExc_NotImplementedError;
972PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +0000973PyObject *PyExc_IndentationError;
974PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +0000975PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +0000976PyObject *PyExc_SystemError;
977PyObject *PyExc_SystemExit;
978PyObject *PyExc_UnboundLocalError;
979PyObject *PyExc_UnicodeError;
980PyObject *PyExc_TypeError;
981PyObject *PyExc_ValueError;
982PyObject *PyExc_ZeroDivisionError;
983#ifdef MS_WINDOWS
984PyObject *PyExc_WindowsError;
985#endif
986
987/* Pre-computed MemoryError instance. Best to create this as early as
988 * possibly and not wait until a MemoryError is actually raised!
989 */
990PyObject *PyExc_MemoryErrorInst;
991
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000992/* Predefined warning categories */
993PyObject *PyExc_Warning;
994PyObject *PyExc_UserWarning;
995PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +0000996PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000997PyObject *PyExc_SyntaxWarning;
Guido van Rossumae347b32001-08-23 02:56:07 +0000998PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000999PyObject *PyExc_RuntimeWarning;
1000
Barry Warsaw675ac282000-05-26 19:05:16 +00001001
1002
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001003/* mapping between exception names and their PyObject ** */
1004static struct {
1005 char *name;
1006 PyObject **exc;
1007 PyObject **base; /* NULL == PyExc_StandardError */
1008 char *docstr;
1009 PyMethodDef *methods;
1010 int (*classinit)(PyObject *);
1011} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001012 /*
1013 * The first three classes MUST appear in exactly this order
1014 */
1015 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001016 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1017 StopIteration__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001018 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1019 StandardError__doc__},
1020 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1021 /*
1022 * The rest appear in depth-first order of the hierarchy
1023 */
1024 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1025 SystemExit_methods},
1026 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1027 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1028 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1029 EnvironmentError_methods},
1030 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1031 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1032#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001033 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001034 WindowsError__doc__},
1035#endif /* MS_WINDOWS */
1036 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1037 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1038 {"NotImplementedError", &PyExc_NotImplementedError,
1039 &PyExc_RuntimeError, NotImplementedError__doc__},
1040 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1041 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1042 UnboundLocalError__doc__},
1043 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1044 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1045 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001046 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1047 IndentationError__doc__},
1048 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1049 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001050 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1051 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1052 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1053 IndexError__doc__},
1054 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
1055 KeyError__doc__},
1056 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1057 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1058 OverflowError__doc__},
1059 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1060 ZeroDivisionError__doc__},
1061 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1062 FloatingPointError__doc__},
1063 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1064 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Fred Drakebb9fa212001-10-05 21:50:08 +00001065 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001066 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1067 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001068 /* Warning categories */
1069 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1070 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1071 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1072 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001073 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1074 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001075 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Guido van Rossumae347b32001-08-23 02:56:07 +00001076 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1077 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001078 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1079 RuntimeWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001080 /* Sentinel */
1081 {NULL}
1082};
1083
1084
1085
Tim Peters5687ffe2001-02-28 16:44:18 +00001086DL_EXPORT(void)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001087_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001088{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001089 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001090 int modnamesz = strlen(modulename);
1091 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001092 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001093
Tim Peters6d6c1a32001-08-02 04:15:00 +00001094 me = Py_InitModule(modulename, functions);
1095 if (me == NULL)
1096 goto err;
1097 mydict = PyModule_GetDict(me);
1098 if (mydict == NULL)
1099 goto err;
1100 bltinmod = PyImport_ImportModule("__builtin__");
1101 if (bltinmod == NULL)
1102 goto err;
1103 bdict = PyModule_GetDict(bltinmod);
1104 if (bdict == NULL)
1105 goto err;
1106 doc = PyString_FromString(module__doc__);
1107 if (doc == NULL)
1108 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001109
Tim Peters6d6c1a32001-08-02 04:15:00 +00001110 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001111 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001112 if (i < 0) {
1113 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001114 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001115 return;
1116 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001117
1118 /* This is the base class of all exceptions, so make it first. */
1119 if (make_Exception(modulename) ||
1120 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1121 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1122 {
1123 Py_FatalError("Base class `Exception' could not be created.");
1124 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001125
Barry Warsaw675ac282000-05-26 19:05:16 +00001126 /* Now we can programmatically create all the remaining exceptions.
1127 * Remember to start the loop at 1 to skip Exceptions.
1128 */
1129 for (i=1; exctable[i].name; i++) {
1130 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001131 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1132 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001133
1134 (void)strcpy(cname, modulename);
1135 (void)strcat(cname, ".");
1136 (void)strcat(cname, exctable[i].name);
1137
1138 if (exctable[i].base == 0)
1139 base = PyExc_StandardError;
1140 else
1141 base = *exctable[i].base;
1142
1143 status = make_class(exctable[i].exc, base, cname,
1144 exctable[i].methods,
1145 exctable[i].docstr);
1146
1147 PyMem_DEL(cname);
1148
1149 if (status)
1150 Py_FatalError("Standard exception classes could not be created.");
1151
1152 if (exctable[i].classinit) {
1153 status = (*exctable[i].classinit)(*exctable[i].exc);
1154 if (status)
1155 Py_FatalError("An exception class could not be initialized.");
1156 }
1157
1158 /* Now insert the class into both this module and the __builtin__
1159 * module.
1160 */
1161 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1162 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1163 {
1164 Py_FatalError("Module dictionary insertion problem.");
1165 }
1166 }
1167
1168 /* Now we need to pre-allocate a MemoryError instance */
1169 args = Py_BuildValue("()");
1170 if (!args ||
1171 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1172 {
1173 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1174 }
1175 Py_DECREF(args);
1176
1177 /* We're done with __builtin__ */
1178 Py_DECREF(bltinmod);
1179}
1180
1181
Tim Peters5687ffe2001-02-28 16:44:18 +00001182DL_EXPORT(void)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001183_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001184{
1185 int i;
1186
1187 Py_XDECREF(PyExc_MemoryErrorInst);
1188 PyExc_MemoryErrorInst = NULL;
1189
1190 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001191 /* clear the class's dictionary, freeing up circular references
1192 * between the class and its methods.
1193 */
1194 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1195 PyDict_Clear(cdict);
1196 Py_DECREF(cdict);
1197
1198 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001199 Py_XDECREF(*exctable[i].exc);
1200 *exctable[i].exc = NULL;
1201 }
1202}