blob: 934850aa066146dc46eff3cc2b1bdfcb4195435e [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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000033PyDoc_STRVAR(module__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +000034"Python's standard exception class hierarchy.\n\
35\n\
36Before Python 1.5, the standard exceptions were all simple string objects.\n\
37In Python 1.5, the standard exceptions were converted to classes organized\n\
38into a relatively flat hierarchy. String-based standard exceptions were\n\
39optional, or used as a fallback if some problem occurred while importing\n\
40the exception module. With Python 1.6, optional string-based standard\n\
41exceptions were removed (along with the -X command line flag).\n\
42\n\
43The class exceptions were implemented in such a way as to be almost\n\
44completely backward compatible. Some tricky uses of IOError could\n\
45potentially have broken, but by Python 1.6, all of these should have\n\
46been fixed. As of Python 1.6, the class-based standard exceptions are\n\
47now implemented in C, and are guaranteed to exist in the Python\n\
48interpreter.\n\
49\n\
50Here is a rundown of the class hierarchy. The classes found here are\n\
51inserted into both the exceptions module and the `built-in' module. It is\n\
52recommended that user defined class based exceptions be derived from the\n\
Tim Petersbf26e072000-07-12 04:02:10 +000053`Exception' class, although this is currently not enforced.\n"
54 /* keep string pieces "small" */
55"\n\
Barry Warsaw675ac282000-05-26 19:05:16 +000056Exception\n\
57 |\n\
58 +-- SystemExit\n\
Guido van Rossum59d1d2b2001-04-20 19:13:02 +000059 +-- StopIteration\n\
Barry Warsaw675ac282000-05-26 19:05:16 +000060 +-- StandardError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +000061 | |\n\
62 | +-- KeyboardInterrupt\n\
63 | +-- ImportError\n\
64 | +-- EnvironmentError\n\
65 | | |\n\
66 | | +-- IOError\n\
67 | | +-- OSError\n\
68 | | |\n\
69 | | +-- WindowsError\n\
70 | |\n\
71 | +-- EOFError\n\
72 | +-- RuntimeError\n\
73 | | |\n\
74 | | +-- NotImplementedError\n\
75 | |\n\
76 | +-- NameError\n\
77 | | |\n\
78 | | +-- UnboundLocalError\n\
79 | |\n\
80 | +-- AttributeError\n\
81 | +-- SyntaxError\n\
82 | | |\n\
83 | | +-- IndentationError\n\
84 | | |\n\
85 | | +-- TabError\n\
86 | |\n\
87 | +-- TypeError\n\
88 | +-- AssertionError\n\
89 | +-- LookupError\n\
90 | | |\n\
91 | | +-- IndexError\n\
92 | | +-- KeyError\n\
93 | |\n\
94 | +-- ArithmeticError\n\
95 | | |\n\
96 | | +-- OverflowError\n\
97 | | +-- ZeroDivisionError\n\
98 | | +-- FloatingPointError\n\
99 | |\n\
100 | +-- ValueError\n\
101 | | |\n\
102 | | +-- UnicodeError\n\
103 | |\n\
Fred Drakebb9fa212001-10-05 21:50:08 +0000104 | +-- ReferenceError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000105 | +-- SystemError\n\
106 | +-- MemoryError\n\
107 |\n\
108 +---Warning\n\
Barry Warsaw675ac282000-05-26 19:05:16 +0000109 |\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000110 +-- UserWarning\n\
111 +-- DeprecationWarning\n\
Neal Norwitzd68f5172002-05-29 15:54:55 +0000112 +-- PendingDeprecationWarning\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000113 +-- SyntaxWarning\n\
Guido van Rossumae347b32001-08-23 02:56:07 +0000114 +-- OverflowWarning\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000115 +-- RuntimeWarning"
116);
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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000233PyDoc_STRVAR(Exception__doc__, "Common base class for all exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000234
235
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000236static PyObject *
237Exception__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000238{
239 int status;
240
241 if (!(self = get_self(args)))
242 return NULL;
243
244 /* set args attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000245 /* XXX size is only a hint */
246 args = PySequence_GetSlice(args, 1, PySequence_Size(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000247 if (!args)
248 return NULL;
249 status = PyObject_SetAttrString(self, "args", args);
250 Py_DECREF(args);
251 if (status < 0)
252 return NULL;
253
254 Py_INCREF(Py_None);
255 return Py_None;
256}
257
258
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000259static PyObject *
260Exception__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000261{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000262 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000263
Fred Drake1aba5772000-08-15 15:46:16 +0000264 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000265 return NULL;
266
267 args = PyObject_GetAttrString(self, "args");
268 if (!args)
269 return NULL;
270
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000271 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000272 case 0:
273 out = PyString_FromString("");
274 break;
275 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000276 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000277 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000278 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000279 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000280 Py_DECREF(tmp);
281 }
282 else
283 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000284 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000285 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000286 default:
287 out = PyObject_Str(args);
288 break;
289 }
290
291 Py_DECREF(args);
292 return out;
293}
294
295
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000296static PyObject *
297Exception__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000298{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000299 PyObject *out;
300 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000301
Fred Drake1aba5772000-08-15 15:46:16 +0000302 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000303 return NULL;
304
305 args = PyObject_GetAttrString(self, "args");
306 if (!args)
307 return NULL;
308
309 out = PyObject_GetItem(args, index);
310 Py_DECREF(args);
311 return out;
312}
313
314
315static PyMethodDef
316Exception_methods[] = {
317 /* methods for the Exception class */
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000318 { "__getitem__", Exception__getitem__, METH_VARARGS},
319 { "__str__", Exception__str__, METH_VARARGS},
320 { "__init__", Exception__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000321 { NULL, NULL }
322};
323
324
325static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000326make_Exception(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000327{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000328 PyObject *dict = PyDict_New();
329 PyObject *str = NULL;
330 PyObject *name = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000331 int status = -1;
332
333 if (!dict)
334 return -1;
335
336 /* If an error occurs from here on, goto finally instead of explicitly
337 * returning NULL.
338 */
339
340 if (!(str = PyString_FromString(modulename)))
341 goto finally;
342 if (PyDict_SetItemString(dict, "__module__", str))
343 goto finally;
344 Py_DECREF(str);
345 if (!(str = PyString_FromString(Exception__doc__)))
346 goto finally;
347 if (PyDict_SetItemString(dict, "__doc__", str))
348 goto finally;
349
350 if (!(name = PyString_FromString("Exception")))
351 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000352
Barry Warsaw675ac282000-05-26 19:05:16 +0000353 if (!(PyExc_Exception = PyClass_New(NULL, dict, name)))
354 goto finally;
355
356 /* Now populate the dictionary with the method suite */
357 if (populate_methods(PyExc_Exception, dict, Exception_methods))
358 /* Don't need to reclaim PyExc_Exception here because that'll
359 * happen during interpreter shutdown.
360 */
361 goto finally;
362
363 status = 0;
364
365 finally:
366 Py_XDECREF(dict);
367 Py_XDECREF(str);
368 Py_XDECREF(name);
369 return status;
370}
371
372
373
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000374PyDoc_STRVAR(StandardError__doc__,
375"Base class for all standard Python exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000376
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000377PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000378
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000379PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000380
Barry Warsaw675ac282000-05-26 19:05:16 +0000381
382
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000383PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000384
385
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000386static PyObject *
387SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000388{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000389 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000390 int status;
391
392 if (!(self = get_self(args)))
393 return NULL;
394
395 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000396 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000397 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000398
Barry Warsaw675ac282000-05-26 19:05:16 +0000399 status = PyObject_SetAttrString(self, "args", args);
400 if (status < 0) {
401 Py_DECREF(args);
402 return NULL;
403 }
404
405 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000406 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000407 case 0:
408 Py_INCREF(Py_None);
409 code = Py_None;
410 break;
411 case 1:
412 code = PySequence_GetItem(args, 0);
413 break;
414 default:
415 Py_INCREF(args);
416 code = args;
417 break;
418 }
419
420 status = PyObject_SetAttrString(self, "code", code);
421 Py_DECREF(code);
422 Py_DECREF(args);
423 if (status < 0)
424 return NULL;
425
426 Py_INCREF(Py_None);
427 return Py_None;
428}
429
430
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000431static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000432 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000433 {NULL, NULL}
434};
435
436
437
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000438PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000439
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000440PyDoc_STRVAR(ImportError__doc__,
441"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000442
443
444
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000445PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000446
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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000623PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000625PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000626
627#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000628PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000629#endif /* MS_WINDOWS */
630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000631PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000632
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000633PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000634
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000635PyDoc_STRVAR(NotImplementedError__doc__,
636"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000637
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000638PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000639
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000640PyDoc_STRVAR(UnboundLocalError__doc__,
641"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000643PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000644
645
646
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000647PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000648
649
650static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000651SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000652{
Barry Warsaw87bec352000-08-18 05:05:37 +0000653 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000654 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000655
656 /* Additional class-creation time initializations */
657 if (!emptystring ||
658 PyObject_SetAttrString(klass, "msg", emptystring) ||
659 PyObject_SetAttrString(klass, "filename", Py_None) ||
660 PyObject_SetAttrString(klass, "lineno", Py_None) ||
661 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000662 PyObject_SetAttrString(klass, "text", Py_None) ||
663 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000664 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000665 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000666 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000667 Py_XDECREF(emptystring);
668 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000669}
670
671
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000672static PyObject *
673SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000674{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000675 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000676 int lenargs;
677
678 if (!(self = get_self(args)))
679 return NULL;
680
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000681 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000682 return NULL;
683
684 if (PyObject_SetAttrString(self, "args", args))
685 goto finally;
686
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000687 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000688 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000689 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000690 int status;
691
692 if (!item0)
693 goto finally;
694 status = PyObject_SetAttrString(self, "msg", item0);
695 Py_DECREF(item0);
696 if (status)
697 goto finally;
698 }
699 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000700 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000701 PyObject *filename = NULL, *lineno = NULL;
702 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000703 int status = 1;
704
705 if (!info)
706 goto finally;
707
708 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000709 if (filename != NULL) {
710 lineno = PySequence_GetItem(info, 1);
711 if (lineno != NULL) {
712 offset = PySequence_GetItem(info, 2);
713 if (offset != NULL) {
714 text = PySequence_GetItem(info, 3);
715 if (text != NULL) {
716 status =
717 PyObject_SetAttrString(self, "filename", filename)
718 || PyObject_SetAttrString(self, "lineno", lineno)
719 || PyObject_SetAttrString(self, "offset", offset)
720 || PyObject_SetAttrString(self, "text", text);
721 Py_DECREF(text);
722 }
723 Py_DECREF(offset);
724 }
725 Py_DECREF(lineno);
726 }
727 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000728 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000729 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000730
731 if (status)
732 goto finally;
733 }
734 Py_INCREF(Py_None);
735 rtnval = Py_None;
736
737 finally:
738 Py_DECREF(args);
739 return rtnval;
740}
741
742
Fred Drake185a29b2000-08-15 16:20:36 +0000743/* This is called "my_basename" instead of just "basename" to avoid name
744 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
745 defined, and Python does define that. */
746static char *
747my_basename(char *name)
748{
749 char *cp = name;
750 char *result = name;
751
752 if (name == NULL)
753 return "???";
754 while (*cp != '\0') {
755 if (*cp == SEP)
756 result = cp + 1;
757 ++cp;
758 }
759 return result;
760}
761
762
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000763static PyObject *
764SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000765{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000766 PyObject *msg;
767 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000768 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000769
Fred Drake1aba5772000-08-15 15:46:16 +0000770 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000771 return NULL;
772
773 if (!(msg = PyObject_GetAttrString(self, "msg")))
774 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000775
Barry Warsaw675ac282000-05-26 19:05:16 +0000776 str = PyObject_Str(msg);
777 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000778 result = str;
779
780 /* XXX -- do all the additional formatting with filename and
781 lineno here */
782
783 if (PyString_Check(str)) {
784 int have_filename = 0;
785 int have_lineno = 0;
786 char *buffer = NULL;
787
Barry Warsaw77c9f502000-08-16 19:43:17 +0000788 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000789 have_filename = PyString_Check(filename);
790 else
791 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000792
793 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000794 have_lineno = PyInt_Check(lineno);
795 else
796 PyErr_Clear();
797
798 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000799 int bufsize = PyString_GET_SIZE(str) + 64;
800 if (have_filename)
801 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000802
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000803 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000804 if (buffer != NULL) {
805 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000806 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
807 PyString_AS_STRING(str),
808 my_basename(PyString_AS_STRING(filename)),
809 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000810 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000811 PyOS_snprintf(buffer, bufsize, "%s (%s)",
812 PyString_AS_STRING(str),
813 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000814 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000815 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
816 PyString_AS_STRING(str),
817 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000818
Fred Drake1aba5772000-08-15 15:46:16 +0000819 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000820 PyMem_FREE(buffer);
821
Fred Drake1aba5772000-08-15 15:46:16 +0000822 if (result == NULL)
823 result = str;
824 else
825 Py_DECREF(str);
826 }
827 }
828 Py_XDECREF(filename);
829 Py_XDECREF(lineno);
830 }
831 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000832}
833
834
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000835static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000836 {"__init__", SyntaxError__init__, METH_VARARGS},
837 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000838 {NULL, NULL}
839};
840
841
842
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000843/* Exception doc strings */
844
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000845PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000846
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000847PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000848
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000849PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000850
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000851PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000852
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000853PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000854
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000855PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000856
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000857PyDoc_STRVAR(ZeroDivisionError__doc__,
858"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000859
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000860PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000861
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000862PyDoc_STRVAR(ValueError__doc__,
863"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +0000864
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000865PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000866
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000867PyDoc_STRVAR(SystemError__doc__,
868"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +0000869\n\
870Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000871the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000872
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000873PyDoc_STRVAR(ReferenceError__doc__,
874"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +0000875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000876PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000878PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +0000879
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000880PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +0000881
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000882/* Warning category docstrings */
883
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000884PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000885
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000886PyDoc_STRVAR(UserWarning__doc__,
887"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000888
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000889PyDoc_STRVAR(DeprecationWarning__doc__,
890"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000891
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000892PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +0000893"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000894"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +0000895
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000896PyDoc_STRVAR(SyntaxWarning__doc__,
897"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000898
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000899PyDoc_STRVAR(OverflowWarning__doc__,
900"Base class for warnings about numeric overflow.");
Guido van Rossumae347b32001-08-23 02:56:07 +0000901
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000902PyDoc_STRVAR(RuntimeWarning__doc__,
903"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000904
Barry Warsaw675ac282000-05-26 19:05:16 +0000905
906
907/* module global functions */
908static PyMethodDef functions[] = {
909 /* Sentinel */
910 {NULL, NULL}
911};
912
913
914
915/* Global C API defined exceptions */
916
917PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000918PyObject *PyExc_StopIteration;
Barry Warsaw675ac282000-05-26 19:05:16 +0000919PyObject *PyExc_StandardError;
920PyObject *PyExc_ArithmeticError;
921PyObject *PyExc_LookupError;
922
923PyObject *PyExc_AssertionError;
924PyObject *PyExc_AttributeError;
925PyObject *PyExc_EOFError;
926PyObject *PyExc_FloatingPointError;
927PyObject *PyExc_EnvironmentError;
928PyObject *PyExc_IOError;
929PyObject *PyExc_OSError;
930PyObject *PyExc_ImportError;
931PyObject *PyExc_IndexError;
932PyObject *PyExc_KeyError;
933PyObject *PyExc_KeyboardInterrupt;
934PyObject *PyExc_MemoryError;
935PyObject *PyExc_NameError;
936PyObject *PyExc_OverflowError;
937PyObject *PyExc_RuntimeError;
938PyObject *PyExc_NotImplementedError;
939PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +0000940PyObject *PyExc_IndentationError;
941PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +0000942PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +0000943PyObject *PyExc_SystemError;
944PyObject *PyExc_SystemExit;
945PyObject *PyExc_UnboundLocalError;
946PyObject *PyExc_UnicodeError;
947PyObject *PyExc_TypeError;
948PyObject *PyExc_ValueError;
949PyObject *PyExc_ZeroDivisionError;
950#ifdef MS_WINDOWS
951PyObject *PyExc_WindowsError;
952#endif
953
954/* Pre-computed MemoryError instance. Best to create this as early as
955 * possibly and not wait until a MemoryError is actually raised!
956 */
957PyObject *PyExc_MemoryErrorInst;
958
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000959/* Predefined warning categories */
960PyObject *PyExc_Warning;
961PyObject *PyExc_UserWarning;
962PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +0000963PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000964PyObject *PyExc_SyntaxWarning;
Guido van Rossumae347b32001-08-23 02:56:07 +0000965PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000966PyObject *PyExc_RuntimeWarning;
967
Barry Warsaw675ac282000-05-26 19:05:16 +0000968
969
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000970/* mapping between exception names and their PyObject ** */
971static struct {
972 char *name;
973 PyObject **exc;
974 PyObject **base; /* NULL == PyExc_StandardError */
975 char *docstr;
976 PyMethodDef *methods;
977 int (*classinit)(PyObject *);
978} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +0000979 /*
980 * The first three classes MUST appear in exactly this order
981 */
982 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000983 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
984 StopIteration__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +0000985 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
986 StandardError__doc__},
987 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
988 /*
989 * The rest appear in depth-first order of the hierarchy
990 */
991 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
992 SystemExit_methods},
993 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
994 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
995 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
996 EnvironmentError_methods},
997 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
998 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
999#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001000 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001001 WindowsError__doc__},
1002#endif /* MS_WINDOWS */
1003 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1004 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1005 {"NotImplementedError", &PyExc_NotImplementedError,
1006 &PyExc_RuntimeError, NotImplementedError__doc__},
1007 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1008 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1009 UnboundLocalError__doc__},
1010 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1011 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1012 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001013 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1014 IndentationError__doc__},
1015 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1016 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001017 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1018 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1019 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1020 IndexError__doc__},
1021 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
1022 KeyError__doc__},
1023 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1024 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1025 OverflowError__doc__},
1026 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1027 ZeroDivisionError__doc__},
1028 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1029 FloatingPointError__doc__},
1030 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1031 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Fred Drakebb9fa212001-10-05 21:50:08 +00001032 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001033 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1034 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001035 /* Warning categories */
1036 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1037 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1038 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1039 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001040 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1041 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001042 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Guido van Rossumae347b32001-08-23 02:56:07 +00001043 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1044 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001045 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1046 RuntimeWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001047 /* Sentinel */
1048 {NULL}
1049};
1050
1051
1052
Mark Hammonda2905272002-07-29 13:42:14 +00001053void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001054_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001055{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001056 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001057 int modnamesz = strlen(modulename);
1058 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001059 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001060
Tim Peters6d6c1a32001-08-02 04:15:00 +00001061 me = Py_InitModule(modulename, functions);
1062 if (me == NULL)
1063 goto err;
1064 mydict = PyModule_GetDict(me);
1065 if (mydict == NULL)
1066 goto err;
1067 bltinmod = PyImport_ImportModule("__builtin__");
1068 if (bltinmod == NULL)
1069 goto err;
1070 bdict = PyModule_GetDict(bltinmod);
1071 if (bdict == NULL)
1072 goto err;
1073 doc = PyString_FromString(module__doc__);
1074 if (doc == NULL)
1075 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001076
Tim Peters6d6c1a32001-08-02 04:15:00 +00001077 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001078 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001079 if (i < 0) {
1080 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001081 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001082 return;
1083 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001084
1085 /* This is the base class of all exceptions, so make it first. */
1086 if (make_Exception(modulename) ||
1087 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1088 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1089 {
1090 Py_FatalError("Base class `Exception' could not be created.");
1091 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001092
Barry Warsaw675ac282000-05-26 19:05:16 +00001093 /* Now we can programmatically create all the remaining exceptions.
1094 * Remember to start the loop at 1 to skip Exceptions.
1095 */
1096 for (i=1; exctable[i].name; i++) {
1097 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001098 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1099 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001100
1101 (void)strcpy(cname, modulename);
1102 (void)strcat(cname, ".");
1103 (void)strcat(cname, exctable[i].name);
1104
1105 if (exctable[i].base == 0)
1106 base = PyExc_StandardError;
1107 else
1108 base = *exctable[i].base;
1109
1110 status = make_class(exctable[i].exc, base, cname,
1111 exctable[i].methods,
1112 exctable[i].docstr);
1113
1114 PyMem_DEL(cname);
1115
1116 if (status)
1117 Py_FatalError("Standard exception classes could not be created.");
1118
1119 if (exctable[i].classinit) {
1120 status = (*exctable[i].classinit)(*exctable[i].exc);
1121 if (status)
1122 Py_FatalError("An exception class could not be initialized.");
1123 }
1124
1125 /* Now insert the class into both this module and the __builtin__
1126 * module.
1127 */
1128 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1129 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1130 {
1131 Py_FatalError("Module dictionary insertion problem.");
1132 }
1133 }
1134
1135 /* Now we need to pre-allocate a MemoryError instance */
1136 args = Py_BuildValue("()");
1137 if (!args ||
1138 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1139 {
1140 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1141 }
1142 Py_DECREF(args);
1143
1144 /* We're done with __builtin__ */
1145 Py_DECREF(bltinmod);
1146}
1147
1148
Mark Hammonda2905272002-07-29 13:42:14 +00001149void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001150_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001151{
1152 int i;
1153
1154 Py_XDECREF(PyExc_MemoryErrorInst);
1155 PyExc_MemoryErrorInst = NULL;
1156
1157 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001158 /* clear the class's dictionary, freeing up circular references
1159 * between the class and its methods.
1160 */
1161 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1162 PyDict_Clear(cdict);
1163 Py_DECREF(cdict);
1164
1165 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001166 Py_XDECREF(*exctable[i].exc);
1167 *exctable[i].exc = NULL;
1168 }
1169}