blob: c4bd626dedf079ee8463d67561c7d55702b72d65 [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\
Barry Warsaw9f007392002-08-14 15:51:29 +0000115 +-- RuntimeWarning\n\
116 +-- FutureWarning"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000117);
Barry Warsaw675ac282000-05-26 19:05:16 +0000118
119
120/* Helper function for populating a dictionary with method wrappers. */
121static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000122populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +0000123{
124 if (!methods)
125 return 0;
126
127 while (methods->ml_name) {
128 /* get a wrapper for the built-in function */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000129 PyObject *func = PyCFunction_New(methods, NULL);
130 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +0000131 int status;
132
133 if (!func)
134 return -1;
135
136 /* turn the function into an unbound method */
137 if (!(meth = PyMethod_New(func, NULL, klass))) {
138 Py_DECREF(func);
139 return -1;
140 }
Barry Warsaw9667ed22001-01-23 16:08:34 +0000141
Barry Warsaw675ac282000-05-26 19:05:16 +0000142 /* add method to dictionary */
143 status = PyDict_SetItemString(dict, methods->ml_name, meth);
144 Py_DECREF(meth);
145 Py_DECREF(func);
146
147 /* stop now if an error occurred, otherwise do the next method */
148 if (status)
149 return status;
150
151 methods++;
152 }
153 return 0;
154}
155
Barry Warsaw9667ed22001-01-23 16:08:34 +0000156
Barry Warsaw675ac282000-05-26 19:05:16 +0000157
158/* This function is used to create all subsequent exception classes. */
159static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000160make_class(PyObject **klass, PyObject *base,
161 char *name, PyMethodDef *methods,
162 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +0000163{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000164 PyObject *dict = PyDict_New();
165 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000166 int status = -1;
167
168 if (!dict)
169 return -1;
170
171 /* If an error occurs from here on, goto finally instead of explicitly
172 * returning NULL.
173 */
174
175 if (docstr) {
176 if (!(str = PyString_FromString(docstr)))
177 goto finally;
178 if (PyDict_SetItemString(dict, "__doc__", str))
179 goto finally;
180 }
181
182 if (!(*klass = PyErr_NewException(name, base, dict)))
183 goto finally;
184
185 if (populate_methods(*klass, dict, methods)) {
186 Py_DECREF(*klass);
187 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000188 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000189 }
190
191 status = 0;
192
193 finally:
194 Py_XDECREF(dict);
195 Py_XDECREF(str);
196 return status;
197}
198
199
200/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000201static PyObject *
202get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000203{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000204 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000205 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000206 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000207 if (PyExc_TypeError) {
208 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000209 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000210 }
211 return NULL;
212 }
213 return self;
214}
215
216
217
218/* Notes on bootstrapping the exception classes.
219 *
220 * First thing we create is the base class for all exceptions, called
221 * appropriately enough: Exception. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000222 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000223 * for TypeError, which can conditionally exist.
224 *
225 * Next, StandardError is created (which is quite simple) followed by
226 * TypeError, because the instantiation of other exceptions can potentially
227 * throw a TypeError. Once these exceptions are created, all the others
228 * can be created in any order. See the static exctable below for the
229 * explicit bootstrap order.
230 *
231 * All classes after Exception can be created using PyErr_NewException().
232 */
233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000234PyDoc_STRVAR(Exception__doc__, "Common base class for all exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000235
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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000375PyDoc_STRVAR(StandardError__doc__,
376"Base class for all standard Python exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000377
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000378PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000379
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000380PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000381
Barry Warsaw675ac282000-05-26 19:05:16 +0000382
383
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000384PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000385
386
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000387static PyObject *
388SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000389{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000390 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000391 int status;
392
393 if (!(self = get_self(args)))
394 return NULL;
395
396 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000397 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000398 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000399
Barry Warsaw675ac282000-05-26 19:05:16 +0000400 status = PyObject_SetAttrString(self, "args", args);
401 if (status < 0) {
402 Py_DECREF(args);
403 return NULL;
404 }
405
406 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000407 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000408 case 0:
409 Py_INCREF(Py_None);
410 code = Py_None;
411 break;
412 case 1:
413 code = PySequence_GetItem(args, 0);
414 break;
415 default:
416 Py_INCREF(args);
417 code = args;
418 break;
419 }
420
421 status = PyObject_SetAttrString(self, "code", code);
422 Py_DECREF(code);
423 Py_DECREF(args);
424 if (status < 0)
425 return NULL;
426
427 Py_INCREF(Py_None);
428 return Py_None;
429}
430
431
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000432static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000433 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000434 {NULL, NULL}
435};
436
437
438
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000439PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000441PyDoc_STRVAR(ImportError__doc__,
442"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000443
444
445
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000446PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000447
448
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000449static PyObject *
450EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000451{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000452 PyObject *item0 = NULL;
453 PyObject *item1 = NULL;
454 PyObject *item2 = NULL;
455 PyObject *subslice = NULL;
456 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000457
458 if (!(self = get_self(args)))
459 return NULL;
460
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000461 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000462 return NULL;
463
464 if (PyObject_SetAttrString(self, "args", args) ||
465 PyObject_SetAttrString(self, "errno", Py_None) ||
466 PyObject_SetAttrString(self, "strerror", Py_None) ||
467 PyObject_SetAttrString(self, "filename", Py_None))
468 {
469 goto finally;
470 }
471
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000472 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000473 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000474 /* Where a function has a single filename, such as open() or some
475 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
476 * called, giving a third argument which is the filename. But, so
477 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000478 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000479 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000480 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000481 * we hack args so that it only contains two items. This also
482 * means we need our own __str__() which prints out the filename
483 * when it was supplied.
484 */
485 item0 = PySequence_GetItem(args, 0);
486 item1 = PySequence_GetItem(args, 1);
487 item2 = PySequence_GetItem(args, 2);
488 if (!item0 || !item1 || !item2)
489 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000490
Barry Warsaw675ac282000-05-26 19:05:16 +0000491 if (PyObject_SetAttrString(self, "errno", item0) ||
492 PyObject_SetAttrString(self, "strerror", item1) ||
493 PyObject_SetAttrString(self, "filename", item2))
494 {
495 goto finally;
496 }
497
498 subslice = PySequence_GetSlice(args, 0, 2);
499 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
500 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000501 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000502
503 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000504 /* Used when PyErr_SetFromErrno() is called and no filename
505 * argument is given.
506 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000507 item0 = PySequence_GetItem(args, 0);
508 item1 = PySequence_GetItem(args, 1);
509 if (!item0 || !item1)
510 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000511
Barry Warsaw675ac282000-05-26 19:05:16 +0000512 if (PyObject_SetAttrString(self, "errno", item0) ||
513 PyObject_SetAttrString(self, "strerror", item1))
514 {
515 goto finally;
516 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000517 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000518 }
519
520 Py_INCREF(Py_None);
521 rtnval = Py_None;
522
523 finally:
524 Py_DECREF(args);
525 Py_XDECREF(item0);
526 Py_XDECREF(item1);
527 Py_XDECREF(item2);
528 Py_XDECREF(subslice);
529 return rtnval;
530}
531
532
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000533static PyObject *
534EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000535{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000536 PyObject *originalself = self;
537 PyObject *filename;
538 PyObject *serrno;
539 PyObject *strerror;
540 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000541
Fred Drake1aba5772000-08-15 15:46:16 +0000542 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000543 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000544
Barry Warsaw675ac282000-05-26 19:05:16 +0000545 filename = PyObject_GetAttrString(self, "filename");
546 serrno = PyObject_GetAttrString(self, "errno");
547 strerror = PyObject_GetAttrString(self, "strerror");
548 if (!filename || !serrno || !strerror)
549 goto finally;
550
551 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000552 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
553 PyObject *repr = PyObject_Repr(filename);
554 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000555
556 if (!fmt || !repr || !tuple) {
557 Py_XDECREF(fmt);
558 Py_XDECREF(repr);
559 Py_XDECREF(tuple);
560 goto finally;
561 }
562
563 PyTuple_SET_ITEM(tuple, 0, serrno);
564 PyTuple_SET_ITEM(tuple, 1, strerror);
565 PyTuple_SET_ITEM(tuple, 2, repr);
566
567 rtnval = PyString_Format(fmt, tuple);
568
569 Py_DECREF(fmt);
570 Py_DECREF(tuple);
571 /* already freed because tuple owned only reference */
572 serrno = NULL;
573 strerror = NULL;
574 }
575 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000576 PyObject *fmt = PyString_FromString("[Errno %s] %s");
577 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000578
579 if (!fmt || !tuple) {
580 Py_XDECREF(fmt);
581 Py_XDECREF(tuple);
582 goto finally;
583 }
584
585 PyTuple_SET_ITEM(tuple, 0, serrno);
586 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000587
Barry Warsaw675ac282000-05-26 19:05:16 +0000588 rtnval = PyString_Format(fmt, tuple);
589
590 Py_DECREF(fmt);
591 Py_DECREF(tuple);
592 /* already freed because tuple owned only reference */
593 serrno = NULL;
594 strerror = NULL;
595 }
596 else
597 /* The original Python code said:
598 *
599 * return StandardError.__str__(self)
600 *
601 * but there is no StandardError__str__() function; we happen to
602 * know that's just a pass through to Exception__str__().
603 */
604 rtnval = Exception__str__(originalself, args);
605
606 finally:
607 Py_XDECREF(filename);
608 Py_XDECREF(serrno);
609 Py_XDECREF(strerror);
610 return rtnval;
611}
612
613
614static
615PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000616 {"__init__", EnvironmentError__init__, METH_VARARGS},
617 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000618 {NULL, NULL}
619};
620
621
622
623
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000624PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000625
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000626PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000627
628#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000629PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000630#endif /* MS_WINDOWS */
631
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000632PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000633
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000634PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000635
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000636PyDoc_STRVAR(NotImplementedError__doc__,
637"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000638
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000639PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000640
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000641PyDoc_STRVAR(UnboundLocalError__doc__,
642"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000643
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000644PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000645
646
647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000648PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000649
650
651static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000652SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000653{
Barry Warsaw87bec352000-08-18 05:05:37 +0000654 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000655 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000656
657 /* Additional class-creation time initializations */
658 if (!emptystring ||
659 PyObject_SetAttrString(klass, "msg", emptystring) ||
660 PyObject_SetAttrString(klass, "filename", Py_None) ||
661 PyObject_SetAttrString(klass, "lineno", Py_None) ||
662 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000663 PyObject_SetAttrString(klass, "text", Py_None) ||
664 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000665 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000666 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000667 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000668 Py_XDECREF(emptystring);
669 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000670}
671
672
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000673static PyObject *
674SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000675{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000676 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000677 int lenargs;
678
679 if (!(self = get_self(args)))
680 return NULL;
681
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000682 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000683 return NULL;
684
685 if (PyObject_SetAttrString(self, "args", args))
686 goto finally;
687
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000688 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000689 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000690 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000691 int status;
692
693 if (!item0)
694 goto finally;
695 status = PyObject_SetAttrString(self, "msg", item0);
696 Py_DECREF(item0);
697 if (status)
698 goto finally;
699 }
700 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000701 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000702 PyObject *filename = NULL, *lineno = NULL;
703 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000704 int status = 1;
705
706 if (!info)
707 goto finally;
708
709 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000710 if (filename != NULL) {
711 lineno = PySequence_GetItem(info, 1);
712 if (lineno != NULL) {
713 offset = PySequence_GetItem(info, 2);
714 if (offset != NULL) {
715 text = PySequence_GetItem(info, 3);
716 if (text != NULL) {
717 status =
718 PyObject_SetAttrString(self, "filename", filename)
719 || PyObject_SetAttrString(self, "lineno", lineno)
720 || PyObject_SetAttrString(self, "offset", offset)
721 || PyObject_SetAttrString(self, "text", text);
722 Py_DECREF(text);
723 }
724 Py_DECREF(offset);
725 }
726 Py_DECREF(lineno);
727 }
728 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000729 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000730 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000731
732 if (status)
733 goto finally;
734 }
735 Py_INCREF(Py_None);
736 rtnval = Py_None;
737
738 finally:
739 Py_DECREF(args);
740 return rtnval;
741}
742
743
Fred Drake185a29b2000-08-15 16:20:36 +0000744/* This is called "my_basename" instead of just "basename" to avoid name
745 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
746 defined, and Python does define that. */
747static char *
748my_basename(char *name)
749{
750 char *cp = name;
751 char *result = name;
752
753 if (name == NULL)
754 return "???";
755 while (*cp != '\0') {
756 if (*cp == SEP)
757 result = cp + 1;
758 ++cp;
759 }
760 return result;
761}
762
763
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000764static PyObject *
765SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000766{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000767 PyObject *msg;
768 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000769 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000770
Fred Drake1aba5772000-08-15 15:46:16 +0000771 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000772 return NULL;
773
774 if (!(msg = PyObject_GetAttrString(self, "msg")))
775 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000776
Barry Warsaw675ac282000-05-26 19:05:16 +0000777 str = PyObject_Str(msg);
778 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000779 result = str;
780
781 /* XXX -- do all the additional formatting with filename and
782 lineno here */
783
784 if (PyString_Check(str)) {
785 int have_filename = 0;
786 int have_lineno = 0;
787 char *buffer = NULL;
788
Barry Warsaw77c9f502000-08-16 19:43:17 +0000789 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000790 have_filename = PyString_Check(filename);
791 else
792 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000793
794 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000795 have_lineno = PyInt_Check(lineno);
796 else
797 PyErr_Clear();
798
799 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000800 int bufsize = PyString_GET_SIZE(str) + 64;
801 if (have_filename)
802 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000803
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000804 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000805 if (buffer != NULL) {
806 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000807 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
808 PyString_AS_STRING(str),
809 my_basename(PyString_AS_STRING(filename)),
810 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000811 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000812 PyOS_snprintf(buffer, bufsize, "%s (%s)",
813 PyString_AS_STRING(str),
814 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000815 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000816 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
817 PyString_AS_STRING(str),
818 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000819
Fred Drake1aba5772000-08-15 15:46:16 +0000820 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000821 PyMem_FREE(buffer);
822
Fred Drake1aba5772000-08-15 15:46:16 +0000823 if (result == NULL)
824 result = str;
825 else
826 Py_DECREF(str);
827 }
828 }
829 Py_XDECREF(filename);
830 Py_XDECREF(lineno);
831 }
832 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000833}
834
835
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000836static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000837 {"__init__", SyntaxError__init__, METH_VARARGS},
838 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000839 {NULL, NULL}
840};
841
842
843
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000844/* Exception doc strings */
845
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000846PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000847
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000848PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000849
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000850PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000851
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000852PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000853
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000854PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000855
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000856PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000857
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000858PyDoc_STRVAR(ZeroDivisionError__doc__,
859"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000860
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000861PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000863PyDoc_STRVAR(ValueError__doc__,
864"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +0000865
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000866PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000867
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000868PyDoc_STRVAR(SystemError__doc__,
869"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +0000870\n\
871Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000872the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000873
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000874PyDoc_STRVAR(ReferenceError__doc__,
875"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +0000876
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000877PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000878
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000879PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +0000880
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000881PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +0000882
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000883/* Warning category docstrings */
884
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000885PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000886
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000887PyDoc_STRVAR(UserWarning__doc__,
888"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000890PyDoc_STRVAR(DeprecationWarning__doc__,
891"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000892
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000893PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +0000894"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000895"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +0000896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000897PyDoc_STRVAR(SyntaxWarning__doc__,
898"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000899
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000900PyDoc_STRVAR(OverflowWarning__doc__,
901"Base class for warnings about numeric overflow.");
Guido van Rossumae347b32001-08-23 02:56:07 +0000902
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000903PyDoc_STRVAR(RuntimeWarning__doc__,
904"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000905
Barry Warsaw9f007392002-08-14 15:51:29 +0000906PyDoc_STRVAR(FutureWarning__doc__,
907"Base class for warnings about constructs that will change semantically "
908"in the future.");
909
Barry Warsaw675ac282000-05-26 19:05:16 +0000910
911
912/* module global functions */
913static PyMethodDef functions[] = {
914 /* Sentinel */
915 {NULL, NULL}
916};
917
918
919
920/* Global C API defined exceptions */
921
922PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000923PyObject *PyExc_StopIteration;
Barry Warsaw675ac282000-05-26 19:05:16 +0000924PyObject *PyExc_StandardError;
925PyObject *PyExc_ArithmeticError;
926PyObject *PyExc_LookupError;
927
928PyObject *PyExc_AssertionError;
929PyObject *PyExc_AttributeError;
930PyObject *PyExc_EOFError;
931PyObject *PyExc_FloatingPointError;
932PyObject *PyExc_EnvironmentError;
933PyObject *PyExc_IOError;
934PyObject *PyExc_OSError;
935PyObject *PyExc_ImportError;
936PyObject *PyExc_IndexError;
937PyObject *PyExc_KeyError;
938PyObject *PyExc_KeyboardInterrupt;
939PyObject *PyExc_MemoryError;
940PyObject *PyExc_NameError;
941PyObject *PyExc_OverflowError;
942PyObject *PyExc_RuntimeError;
943PyObject *PyExc_NotImplementedError;
944PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +0000945PyObject *PyExc_IndentationError;
946PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +0000947PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +0000948PyObject *PyExc_SystemError;
949PyObject *PyExc_SystemExit;
950PyObject *PyExc_UnboundLocalError;
951PyObject *PyExc_UnicodeError;
952PyObject *PyExc_TypeError;
953PyObject *PyExc_ValueError;
954PyObject *PyExc_ZeroDivisionError;
955#ifdef MS_WINDOWS
956PyObject *PyExc_WindowsError;
957#endif
958
959/* Pre-computed MemoryError instance. Best to create this as early as
960 * possibly and not wait until a MemoryError is actually raised!
961 */
962PyObject *PyExc_MemoryErrorInst;
963
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000964/* Predefined warning categories */
965PyObject *PyExc_Warning;
966PyObject *PyExc_UserWarning;
967PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +0000968PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000969PyObject *PyExc_SyntaxWarning;
Guido van Rossumae347b32001-08-23 02:56:07 +0000970PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000971PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +0000972PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +0000973
Barry Warsaw675ac282000-05-26 19:05:16 +0000974
975
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000976/* mapping between exception names and their PyObject ** */
977static struct {
978 char *name;
979 PyObject **exc;
980 PyObject **base; /* NULL == PyExc_StandardError */
981 char *docstr;
982 PyMethodDef *methods;
983 int (*classinit)(PyObject *);
984} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +0000985 /*
986 * The first three classes MUST appear in exactly this order
987 */
988 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000989 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
990 StopIteration__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +0000991 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
992 StandardError__doc__},
993 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
994 /*
995 * The rest appear in depth-first order of the hierarchy
996 */
997 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
998 SystemExit_methods},
999 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1000 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1001 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1002 EnvironmentError_methods},
1003 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1004 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1005#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001006 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001007 WindowsError__doc__},
1008#endif /* MS_WINDOWS */
1009 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1010 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1011 {"NotImplementedError", &PyExc_NotImplementedError,
1012 &PyExc_RuntimeError, NotImplementedError__doc__},
1013 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1014 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1015 UnboundLocalError__doc__},
1016 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1017 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1018 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001019 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1020 IndentationError__doc__},
1021 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1022 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001023 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1024 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1025 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1026 IndexError__doc__},
1027 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
1028 KeyError__doc__},
1029 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1030 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1031 OverflowError__doc__},
1032 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1033 ZeroDivisionError__doc__},
1034 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1035 FloatingPointError__doc__},
1036 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1037 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Fred Drakebb9fa212001-10-05 21:50:08 +00001038 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001039 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1040 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001041 /* Warning categories */
1042 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1043 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1044 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1045 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001046 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1047 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001048 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Guido van Rossumae347b32001-08-23 02:56:07 +00001049 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1050 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001051 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1052 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001053 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1054 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001055 /* Sentinel */
1056 {NULL}
1057};
1058
1059
1060
Mark Hammonda2905272002-07-29 13:42:14 +00001061void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001062_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001063{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001064 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001065 int modnamesz = strlen(modulename);
1066 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001067 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001068
Tim Peters6d6c1a32001-08-02 04:15:00 +00001069 me = Py_InitModule(modulename, functions);
1070 if (me == NULL)
1071 goto err;
1072 mydict = PyModule_GetDict(me);
1073 if (mydict == NULL)
1074 goto err;
1075 bltinmod = PyImport_ImportModule("__builtin__");
1076 if (bltinmod == NULL)
1077 goto err;
1078 bdict = PyModule_GetDict(bltinmod);
1079 if (bdict == NULL)
1080 goto err;
1081 doc = PyString_FromString(module__doc__);
1082 if (doc == NULL)
1083 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001084
Tim Peters6d6c1a32001-08-02 04:15:00 +00001085 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001086 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001087 if (i < 0) {
1088 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001089 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001090 return;
1091 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001092
1093 /* This is the base class of all exceptions, so make it first. */
1094 if (make_Exception(modulename) ||
1095 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1096 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1097 {
1098 Py_FatalError("Base class `Exception' could not be created.");
1099 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001100
Barry Warsaw675ac282000-05-26 19:05:16 +00001101 /* Now we can programmatically create all the remaining exceptions.
1102 * Remember to start the loop at 1 to skip Exceptions.
1103 */
1104 for (i=1; exctable[i].name; i++) {
1105 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001106 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1107 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001108
1109 (void)strcpy(cname, modulename);
1110 (void)strcat(cname, ".");
1111 (void)strcat(cname, exctable[i].name);
1112
1113 if (exctable[i].base == 0)
1114 base = PyExc_StandardError;
1115 else
1116 base = *exctable[i].base;
1117
1118 status = make_class(exctable[i].exc, base, cname,
1119 exctable[i].methods,
1120 exctable[i].docstr);
1121
1122 PyMem_DEL(cname);
1123
1124 if (status)
1125 Py_FatalError("Standard exception classes could not be created.");
1126
1127 if (exctable[i].classinit) {
1128 status = (*exctable[i].classinit)(*exctable[i].exc);
1129 if (status)
1130 Py_FatalError("An exception class could not be initialized.");
1131 }
1132
1133 /* Now insert the class into both this module and the __builtin__
1134 * module.
1135 */
1136 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1137 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1138 {
1139 Py_FatalError("Module dictionary insertion problem.");
1140 }
1141 }
1142
1143 /* Now we need to pre-allocate a MemoryError instance */
1144 args = Py_BuildValue("()");
1145 if (!args ||
1146 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1147 {
1148 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1149 }
1150 Py_DECREF(args);
1151
1152 /* We're done with __builtin__ */
1153 Py_DECREF(bltinmod);
1154}
1155
1156
Mark Hammonda2905272002-07-29 13:42:14 +00001157void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001158_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001159{
1160 int i;
1161
1162 Py_XDECREF(PyExc_MemoryErrorInst);
1163 PyExc_MemoryErrorInst = NULL;
1164
1165 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001166 /* clear the class's dictionary, freeing up circular references
1167 * between the class and its methods.
1168 */
1169 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1170 PyDict_Clear(cdict);
1171 Py_DECREF(cdict);
1172
1173 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001174 Py_XDECREF(*exctable[i].exc);
1175 *exctable[i].exc = NULL;
1176 }
1177}