blob: 03affdc8431a7e763836bf8a43305c045ff331b0 [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\
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000103 | | |\n\
104 | | +-- UnicodeEncodeError\n\
105 | | +-- UnicodeDecodeError\n\
106 | | +-- UnicodeTranslateError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000107 | |\n\
Fred Drakebb9fa212001-10-05 21:50:08 +0000108 | +-- ReferenceError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000109 | +-- SystemError\n\
110 | +-- MemoryError\n\
111 |\n\
112 +---Warning\n\
Barry Warsaw675ac282000-05-26 19:05:16 +0000113 |\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000114 +-- UserWarning\n\
115 +-- DeprecationWarning\n\
Neal Norwitzd68f5172002-05-29 15:54:55 +0000116 +-- PendingDeprecationWarning\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000117 +-- SyntaxWarning\n\
Guido van Rossumae347b32001-08-23 02:56:07 +0000118 +-- OverflowWarning\n\
Barry Warsaw9f007392002-08-14 15:51:29 +0000119 +-- RuntimeWarning\n\
120 +-- FutureWarning"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000121);
Barry Warsaw675ac282000-05-26 19:05:16 +0000122
123
124/* Helper function for populating a dictionary with method wrappers. */
125static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000126populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +0000127{
128 if (!methods)
129 return 0;
130
131 while (methods->ml_name) {
132 /* get a wrapper for the built-in function */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000133 PyObject *func = PyCFunction_New(methods, NULL);
134 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +0000135 int status;
136
137 if (!func)
138 return -1;
139
140 /* turn the function into an unbound method */
141 if (!(meth = PyMethod_New(func, NULL, klass))) {
142 Py_DECREF(func);
143 return -1;
144 }
Barry Warsaw9667ed22001-01-23 16:08:34 +0000145
Barry Warsaw675ac282000-05-26 19:05:16 +0000146 /* add method to dictionary */
147 status = PyDict_SetItemString(dict, methods->ml_name, meth);
148 Py_DECREF(meth);
149 Py_DECREF(func);
150
151 /* stop now if an error occurred, otherwise do the next method */
152 if (status)
153 return status;
154
155 methods++;
156 }
157 return 0;
158}
159
Barry Warsaw9667ed22001-01-23 16:08:34 +0000160
Barry Warsaw675ac282000-05-26 19:05:16 +0000161
162/* This function is used to create all subsequent exception classes. */
163static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000164make_class(PyObject **klass, PyObject *base,
165 char *name, PyMethodDef *methods,
166 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +0000167{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000168 PyObject *dict = PyDict_New();
169 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000170 int status = -1;
171
172 if (!dict)
173 return -1;
174
175 /* If an error occurs from here on, goto finally instead of explicitly
176 * returning NULL.
177 */
178
179 if (docstr) {
180 if (!(str = PyString_FromString(docstr)))
181 goto finally;
182 if (PyDict_SetItemString(dict, "__doc__", str))
183 goto finally;
184 }
185
186 if (!(*klass = PyErr_NewException(name, base, dict)))
187 goto finally;
188
189 if (populate_methods(*klass, dict, methods)) {
190 Py_DECREF(*klass);
191 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000192 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000193 }
194
195 status = 0;
196
197 finally:
198 Py_XDECREF(dict);
199 Py_XDECREF(str);
200 return status;
201}
202
203
204/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000205static PyObject *
206get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000207{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000208 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000209 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000210 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000211 if (PyExc_TypeError) {
212 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000213 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000214 }
215 return NULL;
216 }
217 return self;
218}
219
220
221
222/* Notes on bootstrapping the exception classes.
223 *
224 * First thing we create is the base class for all exceptions, called
225 * appropriately enough: Exception. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000226 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000227 * for TypeError, which can conditionally exist.
228 *
229 * Next, StandardError is created (which is quite simple) followed by
230 * TypeError, because the instantiation of other exceptions can potentially
231 * throw a TypeError. Once these exceptions are created, all the others
232 * can be created in any order. See the static exctable below for the
233 * explicit bootstrap order.
234 *
235 * All classes after Exception can be created using PyErr_NewException().
236 */
237
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000238PyDoc_STRVAR(Exception__doc__, "Common base class for all exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000239
240
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000241static PyObject *
242Exception__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000243{
244 int status;
245
246 if (!(self = get_self(args)))
247 return NULL;
248
249 /* set args attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000250 /* XXX size is only a hint */
251 args = PySequence_GetSlice(args, 1, PySequence_Size(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000252 if (!args)
253 return NULL;
254 status = PyObject_SetAttrString(self, "args", args);
255 Py_DECREF(args);
256 if (status < 0)
257 return NULL;
258
259 Py_INCREF(Py_None);
260 return Py_None;
261}
262
263
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000264static PyObject *
265Exception__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000266{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000267 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000268
Fred Drake1aba5772000-08-15 15:46:16 +0000269 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000270 return NULL;
271
272 args = PyObject_GetAttrString(self, "args");
273 if (!args)
274 return NULL;
275
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000276 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000277 case 0:
278 out = PyString_FromString("");
279 break;
280 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000281 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000282 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000283 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000284 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000285 Py_DECREF(tmp);
286 }
287 else
288 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000289 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000290 }
Guido van Rossum98b2a422002-09-18 04:06:32 +0000291 case -1:
292 PyErr_Clear();
293 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000294 default:
295 out = PyObject_Str(args);
296 break;
297 }
298
299 Py_DECREF(args);
300 return out;
301}
302
303
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000304static PyObject *
305Exception__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000306{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000307 PyObject *out;
308 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000309
Fred Drake1aba5772000-08-15 15:46:16 +0000310 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000311 return NULL;
312
313 args = PyObject_GetAttrString(self, "args");
314 if (!args)
315 return NULL;
316
317 out = PyObject_GetItem(args, index);
318 Py_DECREF(args);
319 return out;
320}
321
322
323static PyMethodDef
324Exception_methods[] = {
325 /* methods for the Exception class */
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000326 { "__getitem__", Exception__getitem__, METH_VARARGS},
327 { "__str__", Exception__str__, METH_VARARGS},
328 { "__init__", Exception__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000329 { NULL, NULL }
330};
331
332
333static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000334make_Exception(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000335{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000336 PyObject *dict = PyDict_New();
337 PyObject *str = NULL;
338 PyObject *name = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000339 int status = -1;
340
341 if (!dict)
342 return -1;
343
344 /* If an error occurs from here on, goto finally instead of explicitly
345 * returning NULL.
346 */
347
348 if (!(str = PyString_FromString(modulename)))
349 goto finally;
350 if (PyDict_SetItemString(dict, "__module__", str))
351 goto finally;
352 Py_DECREF(str);
353 if (!(str = PyString_FromString(Exception__doc__)))
354 goto finally;
355 if (PyDict_SetItemString(dict, "__doc__", str))
356 goto finally;
357
358 if (!(name = PyString_FromString("Exception")))
359 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000360
Barry Warsaw675ac282000-05-26 19:05:16 +0000361 if (!(PyExc_Exception = PyClass_New(NULL, dict, name)))
362 goto finally;
363
364 /* Now populate the dictionary with the method suite */
365 if (populate_methods(PyExc_Exception, dict, Exception_methods))
366 /* Don't need to reclaim PyExc_Exception here because that'll
367 * happen during interpreter shutdown.
368 */
369 goto finally;
370
371 status = 0;
372
373 finally:
374 Py_XDECREF(dict);
375 Py_XDECREF(str);
376 Py_XDECREF(name);
377 return status;
378}
379
380
381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000382PyDoc_STRVAR(StandardError__doc__,
383"Base class for all standard Python exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000384
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000385PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000387PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000388
Barry Warsaw675ac282000-05-26 19:05:16 +0000389
390
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000391PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000392
393
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000394static PyObject *
395SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000396{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000397 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000398 int status;
399
400 if (!(self = get_self(args)))
401 return NULL;
402
403 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000404 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000405 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000406
Barry Warsaw675ac282000-05-26 19:05:16 +0000407 status = PyObject_SetAttrString(self, "args", args);
408 if (status < 0) {
409 Py_DECREF(args);
410 return NULL;
411 }
412
413 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000414 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000415 case 0:
416 Py_INCREF(Py_None);
417 code = Py_None;
418 break;
419 case 1:
420 code = PySequence_GetItem(args, 0);
421 break;
422 default:
423 Py_INCREF(args);
424 code = args;
425 break;
426 }
427
428 status = PyObject_SetAttrString(self, "code", code);
429 Py_DECREF(code);
430 Py_DECREF(args);
431 if (status < 0)
432 return NULL;
433
434 Py_INCREF(Py_None);
435 return Py_None;
436}
437
438
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000439static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000440 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000441 {NULL, NULL}
442};
443
444
445
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000446PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000448PyDoc_STRVAR(ImportError__doc__,
449"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000450
451
452
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000453PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000454
455
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000456static PyObject *
457EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000458{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000459 PyObject *item0 = NULL;
460 PyObject *item1 = NULL;
461 PyObject *item2 = NULL;
462 PyObject *subslice = NULL;
463 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000464
465 if (!(self = get_self(args)))
466 return NULL;
467
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000468 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000469 return NULL;
470
471 if (PyObject_SetAttrString(self, "args", args) ||
472 PyObject_SetAttrString(self, "errno", Py_None) ||
473 PyObject_SetAttrString(self, "strerror", Py_None) ||
474 PyObject_SetAttrString(self, "filename", Py_None))
475 {
476 goto finally;
477 }
478
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000479 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000480 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000481 /* Where a function has a single filename, such as open() or some
482 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
483 * called, giving a third argument which is the filename. But, so
484 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000485 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000486 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000487 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000488 * we hack args so that it only contains two items. This also
489 * means we need our own __str__() which prints out the filename
490 * when it was supplied.
491 */
492 item0 = PySequence_GetItem(args, 0);
493 item1 = PySequence_GetItem(args, 1);
494 item2 = PySequence_GetItem(args, 2);
495 if (!item0 || !item1 || !item2)
496 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000497
Barry Warsaw675ac282000-05-26 19:05:16 +0000498 if (PyObject_SetAttrString(self, "errno", item0) ||
499 PyObject_SetAttrString(self, "strerror", item1) ||
500 PyObject_SetAttrString(self, "filename", item2))
501 {
502 goto finally;
503 }
504
505 subslice = PySequence_GetSlice(args, 0, 2);
506 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
507 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000508 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000509
510 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000511 /* Used when PyErr_SetFromErrno() is called and no filename
512 * argument is given.
513 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000514 item0 = PySequence_GetItem(args, 0);
515 item1 = PySequence_GetItem(args, 1);
516 if (!item0 || !item1)
517 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000518
Barry Warsaw675ac282000-05-26 19:05:16 +0000519 if (PyObject_SetAttrString(self, "errno", item0) ||
520 PyObject_SetAttrString(self, "strerror", item1))
521 {
522 goto finally;
523 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000524 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000525 }
526
527 Py_INCREF(Py_None);
528 rtnval = Py_None;
529
530 finally:
531 Py_DECREF(args);
532 Py_XDECREF(item0);
533 Py_XDECREF(item1);
534 Py_XDECREF(item2);
535 Py_XDECREF(subslice);
536 return rtnval;
537}
538
539
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000540static PyObject *
541EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000542{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000543 PyObject *originalself = self;
544 PyObject *filename;
545 PyObject *serrno;
546 PyObject *strerror;
547 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000548
Fred Drake1aba5772000-08-15 15:46:16 +0000549 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000550 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000551
Barry Warsaw675ac282000-05-26 19:05:16 +0000552 filename = PyObject_GetAttrString(self, "filename");
553 serrno = PyObject_GetAttrString(self, "errno");
554 strerror = PyObject_GetAttrString(self, "strerror");
555 if (!filename || !serrno || !strerror)
556 goto finally;
557
558 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000559 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
560 PyObject *repr = PyObject_Repr(filename);
561 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000562
563 if (!fmt || !repr || !tuple) {
564 Py_XDECREF(fmt);
565 Py_XDECREF(repr);
566 Py_XDECREF(tuple);
567 goto finally;
568 }
569
570 PyTuple_SET_ITEM(tuple, 0, serrno);
571 PyTuple_SET_ITEM(tuple, 1, strerror);
572 PyTuple_SET_ITEM(tuple, 2, repr);
573
574 rtnval = PyString_Format(fmt, tuple);
575
576 Py_DECREF(fmt);
577 Py_DECREF(tuple);
578 /* already freed because tuple owned only reference */
579 serrno = NULL;
580 strerror = NULL;
581 }
582 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000583 PyObject *fmt = PyString_FromString("[Errno %s] %s");
584 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000585
586 if (!fmt || !tuple) {
587 Py_XDECREF(fmt);
588 Py_XDECREF(tuple);
589 goto finally;
590 }
591
592 PyTuple_SET_ITEM(tuple, 0, serrno);
593 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000594
Barry Warsaw675ac282000-05-26 19:05:16 +0000595 rtnval = PyString_Format(fmt, tuple);
596
597 Py_DECREF(fmt);
598 Py_DECREF(tuple);
599 /* already freed because tuple owned only reference */
600 serrno = NULL;
601 strerror = NULL;
602 }
603 else
604 /* The original Python code said:
605 *
606 * return StandardError.__str__(self)
607 *
608 * but there is no StandardError__str__() function; we happen to
609 * know that's just a pass through to Exception__str__().
610 */
611 rtnval = Exception__str__(originalself, args);
612
613 finally:
614 Py_XDECREF(filename);
615 Py_XDECREF(serrno);
616 Py_XDECREF(strerror);
617 return rtnval;
618}
619
620
621static
622PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000623 {"__init__", EnvironmentError__init__, METH_VARARGS},
624 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000625 {NULL, NULL}
626};
627
628
629
630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000631PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000632
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000633PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000634
635#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000636PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000637#endif /* MS_WINDOWS */
638
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000639PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000640
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000641PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000643PyDoc_STRVAR(NotImplementedError__doc__,
644"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000646PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000648PyDoc_STRVAR(UnboundLocalError__doc__,
649"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000650
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000651PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000652
653
654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000655PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000656
657
658static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000659SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000660{
Barry Warsaw87bec352000-08-18 05:05:37 +0000661 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000662 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000663
664 /* Additional class-creation time initializations */
665 if (!emptystring ||
666 PyObject_SetAttrString(klass, "msg", emptystring) ||
667 PyObject_SetAttrString(klass, "filename", Py_None) ||
668 PyObject_SetAttrString(klass, "lineno", Py_None) ||
669 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000670 PyObject_SetAttrString(klass, "text", Py_None) ||
671 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000672 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000673 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000674 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000675 Py_XDECREF(emptystring);
676 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000677}
678
679
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000680static PyObject *
681SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000682{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000683 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000684 int lenargs;
685
686 if (!(self = get_self(args)))
687 return NULL;
688
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000689 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000690 return NULL;
691
692 if (PyObject_SetAttrString(self, "args", args))
693 goto finally;
694
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000695 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000696 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000697 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000698 int status;
699
700 if (!item0)
701 goto finally;
702 status = PyObject_SetAttrString(self, "msg", item0);
703 Py_DECREF(item0);
704 if (status)
705 goto finally;
706 }
707 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000708 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000709 PyObject *filename = NULL, *lineno = NULL;
710 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000711 int status = 1;
712
713 if (!info)
714 goto finally;
715
716 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000717 if (filename != NULL) {
718 lineno = PySequence_GetItem(info, 1);
719 if (lineno != NULL) {
720 offset = PySequence_GetItem(info, 2);
721 if (offset != NULL) {
722 text = PySequence_GetItem(info, 3);
723 if (text != NULL) {
724 status =
725 PyObject_SetAttrString(self, "filename", filename)
726 || PyObject_SetAttrString(self, "lineno", lineno)
727 || PyObject_SetAttrString(self, "offset", offset)
728 || PyObject_SetAttrString(self, "text", text);
729 Py_DECREF(text);
730 }
731 Py_DECREF(offset);
732 }
733 Py_DECREF(lineno);
734 }
735 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000736 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000737 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000738
739 if (status)
740 goto finally;
741 }
742 Py_INCREF(Py_None);
743 rtnval = Py_None;
744
745 finally:
746 Py_DECREF(args);
747 return rtnval;
748}
749
750
Fred Drake185a29b2000-08-15 16:20:36 +0000751/* This is called "my_basename" instead of just "basename" to avoid name
752 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
753 defined, and Python does define that. */
754static char *
755my_basename(char *name)
756{
757 char *cp = name;
758 char *result = name;
759
760 if (name == NULL)
761 return "???";
762 while (*cp != '\0') {
763 if (*cp == SEP)
764 result = cp + 1;
765 ++cp;
766 }
767 return result;
768}
769
770
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000771static PyObject *
772SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000773{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000774 PyObject *msg;
775 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000776 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000777
Fred Drake1aba5772000-08-15 15:46:16 +0000778 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000779 return NULL;
780
781 if (!(msg = PyObject_GetAttrString(self, "msg")))
782 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000783
Barry Warsaw675ac282000-05-26 19:05:16 +0000784 str = PyObject_Str(msg);
785 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000786 result = str;
787
788 /* XXX -- do all the additional formatting with filename and
789 lineno here */
790
Guido van Rossum602d4512002-09-03 20:24:09 +0000791 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000792 int have_filename = 0;
793 int have_lineno = 0;
794 char *buffer = NULL;
795
Barry Warsaw77c9f502000-08-16 19:43:17 +0000796 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000797 have_filename = PyString_Check(filename);
798 else
799 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000800
801 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000802 have_lineno = PyInt_Check(lineno);
803 else
804 PyErr_Clear();
805
806 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000807 int bufsize = PyString_GET_SIZE(str) + 64;
808 if (have_filename)
809 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000810
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000811 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000812 if (buffer != NULL) {
813 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000814 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
815 PyString_AS_STRING(str),
816 my_basename(PyString_AS_STRING(filename)),
817 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000818 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000819 PyOS_snprintf(buffer, bufsize, "%s (%s)",
820 PyString_AS_STRING(str),
821 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000822 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000823 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
824 PyString_AS_STRING(str),
825 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000826
Fred Drake1aba5772000-08-15 15:46:16 +0000827 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000828 PyMem_FREE(buffer);
829
Fred Drake1aba5772000-08-15 15:46:16 +0000830 if (result == NULL)
831 result = str;
832 else
833 Py_DECREF(str);
834 }
835 }
836 Py_XDECREF(filename);
837 Py_XDECREF(lineno);
838 }
839 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000840}
841
842
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000843static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000844 {"__init__", SyntaxError__init__, METH_VARARGS},
845 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000846 {NULL, NULL}
847};
848
849
Guido van Rossum602d4512002-09-03 20:24:09 +0000850static PyObject *
851KeyError__str__(PyObject *self, PyObject *args)
852{
853 PyObject *argsattr;
854 PyObject *result;
855
856 if (!PyArg_ParseTuple(args, "O:__str__", &self))
857 return NULL;
858
859 if (!(argsattr = PyObject_GetAttrString(self, "args")))
860 return NULL;
861
862 /* If args is a tuple of exactly one item, apply repr to args[0].
863 This is done so that e.g. the exception raised by {}[''] prints
864 KeyError: ''
865 rather than the confusing
866 KeyError
867 alone. The downside is that if KeyError is raised with an explanatory
868 string, that string will be displayed in quotes. Too bad.
869 If args is anything else, use the default Exception__str__().
870 */
871 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
872 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
873 result = PyObject_Repr(key);
874 }
875 else
876 result = Exception__str__(self, args);
877
878 Py_DECREF(argsattr);
879 return result;
880}
881
882static PyMethodDef KeyError_methods[] = {
883 {"__str__", KeyError__str__, METH_VARARGS},
884 {NULL, NULL}
885};
886
887
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000888static
889int get_int(PyObject *exc, const char *name, int *value)
890{
891 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
892
893 if (!attr)
894 return -1;
895 if (!PyInt_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000896 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000897 Py_DECREF(attr);
898 return -1;
899 }
900 *value = PyInt_AS_LONG(attr);
901 Py_DECREF(attr);
902 return 0;
903}
904
905
906static
907int set_int(PyObject *exc, const char *name, int value)
908{
909 PyObject *obj = PyInt_FromLong(value);
910 int result;
911
912 if (!obj)
913 return -1;
914 result = PyObject_SetAttrString(exc, (char *)name, obj);
915 Py_DECREF(obj);
916 return result;
917}
918
919
920static
921PyObject *get_string(PyObject *exc, const char *name)
922{
923 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
924
925 if (!attr)
926 return NULL;
927 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000928 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000929 Py_DECREF(attr);
930 return NULL;
931 }
932 return attr;
933}
934
935
936static
937int set_string(PyObject *exc, const char *name, const char *value)
938{
939 PyObject *obj = PyString_FromString(value);
940 int result;
941
942 if (!obj)
943 return -1;
944 result = PyObject_SetAttrString(exc, (char *)name, obj);
945 Py_DECREF(obj);
946 return result;
947}
948
949
950static
951PyObject *get_unicode(PyObject *exc, const char *name)
952{
953 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
954
955 if (!attr)
956 return NULL;
957 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000958 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000959 Py_DECREF(attr);
960 return NULL;
961 }
962 return attr;
963}
964
965PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
966{
967 return get_string(exc, "encoding");
968}
969
970PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
971{
972 return get_string(exc, "encoding");
973}
974
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000975PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
976{
977 return get_unicode(exc, "object");
978}
979
980PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
981{
982 return get_string(exc, "object");
983}
984
985PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
986{
987 return get_unicode(exc, "object");
988}
989
990int PyUnicodeEncodeError_GetStart(PyObject *exc, int *start)
991{
992 if (!get_int(exc, "start", start)) {
993 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
994 int size;
995 if (!object)
996 return -1;
997 size = PyUnicode_GET_SIZE(object);
998 if (*start<0)
999 *start = 0;
1000 if (*start>=size)
1001 *start = size-1;
1002 Py_DECREF(object);
1003 return 0;
1004 }
1005 return -1;
1006}
1007
1008
1009int PyUnicodeDecodeError_GetStart(PyObject *exc, int *start)
1010{
1011 if (!get_int(exc, "start", start)) {
1012 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
1013 int size;
1014 if (!object)
1015 return -1;
1016 size = PyString_GET_SIZE(object);
1017 if (*start<0)
1018 *start = 0;
1019 if (*start>=size)
1020 *start = size-1;
1021 Py_DECREF(object);
1022 return 0;
1023 }
1024 return -1;
1025}
1026
1027
1028int PyUnicodeTranslateError_GetStart(PyObject *exc, int *start)
1029{
1030 return PyUnicodeEncodeError_GetStart(exc, start);
1031}
1032
1033
1034int PyUnicodeEncodeError_SetStart(PyObject *exc, int start)
1035{
1036 return set_int(exc, "start", start);
1037}
1038
1039
1040int PyUnicodeDecodeError_SetStart(PyObject *exc, int start)
1041{
1042 return set_int(exc, "start", start);
1043}
1044
1045
1046int PyUnicodeTranslateError_SetStart(PyObject *exc, int start)
1047{
1048 return set_int(exc, "start", start);
1049}
1050
1051
1052int PyUnicodeEncodeError_GetEnd(PyObject *exc, int *end)
1053{
1054 if (!get_int(exc, "end", end)) {
1055 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
1056 int size;
1057 if (!object)
1058 return -1;
1059 size = PyUnicode_GET_SIZE(object);
1060 if (*end<1)
1061 *end = 1;
1062 if (*end>size)
1063 *end = size;
1064 Py_DECREF(object);
1065 return 0;
1066 }
1067 return -1;
1068}
1069
1070
1071int PyUnicodeDecodeError_GetEnd(PyObject *exc, int *end)
1072{
1073 if (!get_int(exc, "end", end)) {
1074 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
1075 int size;
1076 if (!object)
1077 return -1;
1078 size = PyString_GET_SIZE(object);
1079 if (*end<1)
1080 *end = 1;
1081 if (*end>size)
1082 *end = size;
1083 Py_DECREF(object);
1084 return 0;
1085 }
1086 return -1;
1087}
1088
1089
1090int PyUnicodeTranslateError_GetEnd(PyObject *exc, int *start)
1091{
1092 return PyUnicodeEncodeError_GetEnd(exc, start);
1093}
1094
1095
1096int PyUnicodeEncodeError_SetEnd(PyObject *exc, int end)
1097{
1098 return set_int(exc, "end", end);
1099}
1100
1101
1102int PyUnicodeDecodeError_SetEnd(PyObject *exc, int end)
1103{
1104 return set_int(exc, "end", end);
1105}
1106
1107
1108int PyUnicodeTranslateError_SetEnd(PyObject *exc, int end)
1109{
1110 return set_int(exc, "end", end);
1111}
1112
1113
1114PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1115{
1116 return get_string(exc, "reason");
1117}
1118
1119
1120PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1121{
1122 return get_string(exc, "reason");
1123}
1124
1125
1126PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1127{
1128 return get_string(exc, "reason");
1129}
1130
1131
1132int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1133{
1134 return set_string(exc, "reason", reason);
1135}
1136
1137
1138int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1139{
1140 return set_string(exc, "reason", reason);
1141}
1142
1143
1144int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1145{
1146 return set_string(exc, "reason", reason);
1147}
1148
1149
1150static PyObject *
1151UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1152{
1153 PyObject *rtnval = NULL;
1154 PyObject *encoding;
1155 PyObject *object;
1156 PyObject *start;
1157 PyObject *end;
1158 PyObject *reason;
1159
1160 if (!(self = get_self(args)))
1161 return NULL;
1162
1163 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1164 return NULL;
1165
1166 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1167 &PyString_Type, &encoding,
1168 objecttype, &object,
1169 &PyInt_Type, &start,
1170 &PyInt_Type, &end,
1171 &PyString_Type, &reason))
1172 return NULL;
1173
1174 if (PyObject_SetAttrString(self, "args", args))
1175 goto finally;
1176
1177 if (PyObject_SetAttrString(self, "encoding", encoding))
1178 goto finally;
1179 if (PyObject_SetAttrString(self, "object", object))
1180 goto finally;
1181 if (PyObject_SetAttrString(self, "start", start))
1182 goto finally;
1183 if (PyObject_SetAttrString(self, "end", end))
1184 goto finally;
1185 if (PyObject_SetAttrString(self, "reason", reason))
1186 goto finally;
1187
1188 Py_INCREF(Py_None);
1189 rtnval = Py_None;
1190
1191 finally:
1192 Py_DECREF(args);
1193 return rtnval;
1194}
1195
1196
1197static PyObject *
1198UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1199{
1200 return UnicodeError__init__(self, args, &PyUnicode_Type);
1201}
1202
1203static PyObject *
1204UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1205{
1206 PyObject *encodingObj = NULL;
1207 PyObject *objectObj = NULL;
1208 int length;
1209 int start;
1210 int end;
1211 PyObject *reasonObj = NULL;
1212 char buffer[1000];
1213 PyObject *result = NULL;
1214
1215 self = arg;
1216
1217 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1218 goto error;
1219
1220 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1221 goto error;
1222
1223 length = PyUnicode_GET_SIZE(objectObj);
1224
1225 if (PyUnicodeEncodeError_GetStart(self, &start))
1226 goto error;
1227
1228 if (PyUnicodeEncodeError_GetEnd(self, &end))
1229 goto error;
1230
1231 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1232 goto error;
1233
1234 if (end==start+1) {
1235 PyOS_snprintf(buffer, sizeof(buffer),
1236 "'%.400s' codec can't encode character '\\u%x' in position %d: %.400s",
1237 PyString_AS_STRING(encodingObj),
1238 (int)PyUnicode_AS_UNICODE(objectObj)[start],
1239 start,
1240 PyString_AS_STRING(reasonObj)
1241 );
1242 }
1243 else {
1244 PyOS_snprintf(buffer, sizeof(buffer),
1245 "'%.400s' codec can't encode characters in position %d-%d: %.400s",
1246 PyString_AS_STRING(encodingObj),
1247 start,
1248 end-1,
1249 PyString_AS_STRING(reasonObj)
1250 );
1251 }
1252 result = PyString_FromString(buffer);
1253
1254error:
1255 Py_XDECREF(reasonObj);
1256 Py_XDECREF(objectObj);
1257 Py_XDECREF(encodingObj);
1258 return result;
1259}
1260
1261static PyMethodDef UnicodeEncodeError_methods[] = {
1262 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1263 {"__str__", UnicodeEncodeError__str__, METH_O},
1264 {NULL, NULL}
1265};
1266
1267
1268PyObject * PyUnicodeEncodeError_Create(
1269 const char *encoding, const Py_UNICODE *object, int length,
1270 int start, int end, const char *reason)
1271{
1272 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#iis",
1273 encoding, object, length, start, end, reason);
1274}
1275
1276
1277static PyObject *
1278UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1279{
1280 return UnicodeError__init__(self, args, &PyString_Type);
1281}
1282
1283static PyObject *
1284UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1285{
1286 PyObject *encodingObj = NULL;
1287 PyObject *objectObj = NULL;
1288 int length;
1289 int start;
1290 int end;
1291 PyObject *reasonObj = NULL;
1292 char buffer[1000];
1293 PyObject *result = NULL;
1294
1295 self = arg;
1296
1297 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1298 goto error;
1299
1300 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1301 goto error;
1302
1303 length = PyString_GET_SIZE(objectObj);
1304
1305 if (PyUnicodeDecodeError_GetStart(self, &start))
1306 goto error;
1307
1308 if (PyUnicodeDecodeError_GetEnd(self, &end))
1309 goto error;
1310
1311 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1312 goto error;
1313
1314 if (end==start+1) {
1315 PyOS_snprintf(buffer, sizeof(buffer),
1316 "'%.400s' codec can't decode byte 0x%x in position %d: %.400s",
1317 PyString_AS_STRING(encodingObj),
1318 ((int)PyString_AS_STRING(objectObj)[start])&0xff,
1319 start,
1320 PyString_AS_STRING(reasonObj)
1321 );
1322 }
1323 else {
1324 PyOS_snprintf(buffer, sizeof(buffer),
1325 "'%.400s' codec can't decode bytes in position %d-%d: %.400s",
1326 PyString_AS_STRING(encodingObj),
1327 start,
1328 end-1,
1329 PyString_AS_STRING(reasonObj)
1330 );
1331 }
1332 result = PyString_FromString(buffer);
1333
1334error:
1335 Py_XDECREF(reasonObj);
1336 Py_XDECREF(objectObj);
1337 Py_XDECREF(encodingObj);
1338 return result;
1339}
1340
1341static PyMethodDef UnicodeDecodeError_methods[] = {
1342 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1343 {"__str__", UnicodeDecodeError__str__, METH_O},
1344 {NULL, NULL}
1345};
1346
1347
1348PyObject * PyUnicodeDecodeError_Create(
1349 const char *encoding, const char *object, int length,
1350 int start, int end, const char *reason)
1351{
1352 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
1353 encoding, object, length, start, end, reason);
1354}
1355
1356
1357static PyObject *
1358UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1359{
1360 PyObject *rtnval = NULL;
1361 PyObject *object;
1362 PyObject *start;
1363 PyObject *end;
1364 PyObject *reason;
1365
1366 if (!(self = get_self(args)))
1367 return NULL;
1368
1369 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1370 return NULL;
1371
1372 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1373 &PyUnicode_Type, &object,
1374 &PyInt_Type, &start,
1375 &PyInt_Type, &end,
1376 &PyString_Type, &reason))
1377 goto finally;
1378
1379 if (PyObject_SetAttrString(self, "args", args))
1380 goto finally;
1381
1382 if (PyObject_SetAttrString(self, "object", object))
1383 goto finally;
1384 if (PyObject_SetAttrString(self, "start", start))
1385 goto finally;
1386 if (PyObject_SetAttrString(self, "end", end))
1387 goto finally;
1388 if (PyObject_SetAttrString(self, "reason", reason))
1389 goto finally;
1390
1391 Py_INCREF(Py_None);
1392 rtnval = Py_None;
1393
1394 finally:
1395 Py_DECREF(args);
1396 return rtnval;
1397}
1398
1399
1400static PyObject *
1401UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1402{
1403 PyObject *objectObj = NULL;
1404 int length;
1405 int start;
1406 int end;
1407 PyObject *reasonObj = NULL;
1408 char buffer[1000];
1409 PyObject *result = NULL;
1410
1411 self = arg;
1412
1413 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1414 goto error;
1415
1416 length = PyUnicode_GET_SIZE(objectObj);
1417
1418 if (PyUnicodeTranslateError_GetStart(self, &start))
1419 goto error;
1420
1421 if (PyUnicodeTranslateError_GetEnd(self, &end))
1422 goto error;
1423
1424 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1425 goto error;
1426
1427 if (end==start+1) {
1428 PyOS_snprintf(buffer, sizeof(buffer),
1429 "can't translate character '\\u%x' in position %d: %.400s",
1430 (int)PyUnicode_AS_UNICODE(objectObj)[start],
1431 start,
1432 PyString_AS_STRING(reasonObj)
1433 );
1434 }
1435 else {
1436 PyOS_snprintf(buffer, sizeof(buffer),
1437 "can't translate characters in position %d-%d: %.400s",
1438 start,
1439 end-1,
1440 PyString_AS_STRING(reasonObj)
1441 );
1442 }
1443 result = PyString_FromString(buffer);
1444
1445error:
1446 Py_XDECREF(reasonObj);
1447 Py_XDECREF(objectObj);
1448 return result;
1449}
1450
1451static PyMethodDef UnicodeTranslateError_methods[] = {
1452 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1453 {"__str__", UnicodeTranslateError__str__, METH_O},
1454 {NULL, NULL}
1455};
1456
1457
1458PyObject * PyUnicodeTranslateError_Create(
1459 const Py_UNICODE *object, int length,
1460 int start, int end, const char *reason)
1461{
1462 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
1463 object, length, start, end, reason);
1464}
1465
1466
Barry Warsaw675ac282000-05-26 19:05:16 +00001467
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001468/* Exception doc strings */
1469
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001470PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001472PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001473
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001474PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001475
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001476PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001478PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001479
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001480PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001482PyDoc_STRVAR(ZeroDivisionError__doc__,
1483"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001484
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001485PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001487PyDoc_STRVAR(ValueError__doc__,
1488"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001489
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001490PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001491
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001492PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1493
1494PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1495
1496PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
1497
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001498PyDoc_STRVAR(SystemError__doc__,
1499"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001500\n\
1501Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001502the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001504PyDoc_STRVAR(ReferenceError__doc__,
1505"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001506
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001507PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001508
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001509PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001510
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001511PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001512
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001513/* Warning category docstrings */
1514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001515PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001517PyDoc_STRVAR(UserWarning__doc__,
1518"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001519
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001520PyDoc_STRVAR(DeprecationWarning__doc__,
1521"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001522
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001523PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001524"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001525"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001526
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001527PyDoc_STRVAR(SyntaxWarning__doc__,
1528"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001529
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001530PyDoc_STRVAR(OverflowWarning__doc__,
1531"Base class for warnings about numeric overflow.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001532
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001533PyDoc_STRVAR(RuntimeWarning__doc__,
1534"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001535
Barry Warsaw9f007392002-08-14 15:51:29 +00001536PyDoc_STRVAR(FutureWarning__doc__,
1537"Base class for warnings about constructs that will change semantically "
1538"in the future.");
1539
Barry Warsaw675ac282000-05-26 19:05:16 +00001540
1541
1542/* module global functions */
1543static PyMethodDef functions[] = {
1544 /* Sentinel */
1545 {NULL, NULL}
1546};
1547
1548
1549
1550/* Global C API defined exceptions */
1551
1552PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001553PyObject *PyExc_StopIteration;
Barry Warsaw675ac282000-05-26 19:05:16 +00001554PyObject *PyExc_StandardError;
1555PyObject *PyExc_ArithmeticError;
1556PyObject *PyExc_LookupError;
1557
1558PyObject *PyExc_AssertionError;
1559PyObject *PyExc_AttributeError;
1560PyObject *PyExc_EOFError;
1561PyObject *PyExc_FloatingPointError;
1562PyObject *PyExc_EnvironmentError;
1563PyObject *PyExc_IOError;
1564PyObject *PyExc_OSError;
1565PyObject *PyExc_ImportError;
1566PyObject *PyExc_IndexError;
1567PyObject *PyExc_KeyError;
1568PyObject *PyExc_KeyboardInterrupt;
1569PyObject *PyExc_MemoryError;
1570PyObject *PyExc_NameError;
1571PyObject *PyExc_OverflowError;
1572PyObject *PyExc_RuntimeError;
1573PyObject *PyExc_NotImplementedError;
1574PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001575PyObject *PyExc_IndentationError;
1576PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001577PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001578PyObject *PyExc_SystemError;
1579PyObject *PyExc_SystemExit;
1580PyObject *PyExc_UnboundLocalError;
1581PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001582PyObject *PyExc_UnicodeEncodeError;
1583PyObject *PyExc_UnicodeDecodeError;
1584PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001585PyObject *PyExc_TypeError;
1586PyObject *PyExc_ValueError;
1587PyObject *PyExc_ZeroDivisionError;
1588#ifdef MS_WINDOWS
1589PyObject *PyExc_WindowsError;
1590#endif
1591
1592/* Pre-computed MemoryError instance. Best to create this as early as
1593 * possibly and not wait until a MemoryError is actually raised!
1594 */
1595PyObject *PyExc_MemoryErrorInst;
1596
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001597/* Predefined warning categories */
1598PyObject *PyExc_Warning;
1599PyObject *PyExc_UserWarning;
1600PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001601PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001602PyObject *PyExc_SyntaxWarning;
Guido van Rossumae347b32001-08-23 02:56:07 +00001603PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001604PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001605PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001606
Barry Warsaw675ac282000-05-26 19:05:16 +00001607
1608
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001609/* mapping between exception names and their PyObject ** */
1610static struct {
1611 char *name;
1612 PyObject **exc;
1613 PyObject **base; /* NULL == PyExc_StandardError */
1614 char *docstr;
1615 PyMethodDef *methods;
1616 int (*classinit)(PyObject *);
1617} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001618 /*
1619 * The first three classes MUST appear in exactly this order
1620 */
1621 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001622 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1623 StopIteration__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001624 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1625 StandardError__doc__},
1626 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1627 /*
1628 * The rest appear in depth-first order of the hierarchy
1629 */
1630 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1631 SystemExit_methods},
1632 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1633 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1634 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1635 EnvironmentError_methods},
1636 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1637 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1638#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001639 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001640 WindowsError__doc__},
1641#endif /* MS_WINDOWS */
1642 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1643 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1644 {"NotImplementedError", &PyExc_NotImplementedError,
1645 &PyExc_RuntimeError, NotImplementedError__doc__},
1646 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1647 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1648 UnboundLocalError__doc__},
1649 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1650 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1651 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001652 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1653 IndentationError__doc__},
1654 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1655 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001656 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1657 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1658 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1659 IndexError__doc__},
1660 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001661 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001662 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1663 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1664 OverflowError__doc__},
1665 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1666 ZeroDivisionError__doc__},
1667 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1668 FloatingPointError__doc__},
1669 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1670 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001671 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1672 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1673 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1674 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1675 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1676 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Fred Drakebb9fa212001-10-05 21:50:08 +00001677 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001678 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1679 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001680 /* Warning categories */
1681 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1682 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1683 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1684 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001685 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1686 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001687 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Guido van Rossumae347b32001-08-23 02:56:07 +00001688 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1689 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001690 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1691 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001692 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1693 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001694 /* Sentinel */
1695 {NULL}
1696};
1697
1698
1699
Mark Hammonda2905272002-07-29 13:42:14 +00001700void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001701_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001702{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001703 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001704 int modnamesz = strlen(modulename);
1705 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001706 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001707
Tim Peters6d6c1a32001-08-02 04:15:00 +00001708 me = Py_InitModule(modulename, functions);
1709 if (me == NULL)
1710 goto err;
1711 mydict = PyModule_GetDict(me);
1712 if (mydict == NULL)
1713 goto err;
1714 bltinmod = PyImport_ImportModule("__builtin__");
1715 if (bltinmod == NULL)
1716 goto err;
1717 bdict = PyModule_GetDict(bltinmod);
1718 if (bdict == NULL)
1719 goto err;
1720 doc = PyString_FromString(module__doc__);
1721 if (doc == NULL)
1722 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001723
Tim Peters6d6c1a32001-08-02 04:15:00 +00001724 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001725 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001726 if (i < 0) {
1727 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001728 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001729 return;
1730 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001731
1732 /* This is the base class of all exceptions, so make it first. */
1733 if (make_Exception(modulename) ||
1734 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1735 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1736 {
1737 Py_FatalError("Base class `Exception' could not be created.");
1738 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001739
Barry Warsaw675ac282000-05-26 19:05:16 +00001740 /* Now we can programmatically create all the remaining exceptions.
1741 * Remember to start the loop at 1 to skip Exceptions.
1742 */
1743 for (i=1; exctable[i].name; i++) {
1744 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001745 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1746 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001747
1748 (void)strcpy(cname, modulename);
1749 (void)strcat(cname, ".");
1750 (void)strcat(cname, exctable[i].name);
1751
1752 if (exctable[i].base == 0)
1753 base = PyExc_StandardError;
1754 else
1755 base = *exctable[i].base;
1756
1757 status = make_class(exctable[i].exc, base, cname,
1758 exctable[i].methods,
1759 exctable[i].docstr);
1760
1761 PyMem_DEL(cname);
1762
1763 if (status)
1764 Py_FatalError("Standard exception classes could not be created.");
1765
1766 if (exctable[i].classinit) {
1767 status = (*exctable[i].classinit)(*exctable[i].exc);
1768 if (status)
1769 Py_FatalError("An exception class could not be initialized.");
1770 }
1771
1772 /* Now insert the class into both this module and the __builtin__
1773 * module.
1774 */
1775 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1776 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1777 {
1778 Py_FatalError("Module dictionary insertion problem.");
1779 }
1780 }
1781
1782 /* Now we need to pre-allocate a MemoryError instance */
1783 args = Py_BuildValue("()");
1784 if (!args ||
1785 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1786 {
1787 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1788 }
1789 Py_DECREF(args);
1790
1791 /* We're done with __builtin__ */
1792 Py_DECREF(bltinmod);
1793}
1794
1795
Mark Hammonda2905272002-07-29 13:42:14 +00001796void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001797_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001798{
1799 int i;
1800
1801 Py_XDECREF(PyExc_MemoryErrorInst);
1802 PyExc_MemoryErrorInst = NULL;
1803
1804 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001805 /* clear the class's dictionary, freeing up circular references
1806 * between the class and its methods.
1807 */
1808 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1809 PyDict_Clear(cdict);
1810 Py_DECREF(cdict);
1811
1812 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001813 Py_XDECREF(*exctable[i].exc);
1814 *exctable[i].exc = NULL;
1815 }
1816}