blob: 015d45a7586584d50375d81699727cef061a4f38 [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;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000422 case -1:
423 PyErr_Clear();
424 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000425 default:
426 Py_INCREF(args);
427 code = args;
428 break;
429 }
430
431 status = PyObject_SetAttrString(self, "code", code);
432 Py_DECREF(code);
433 Py_DECREF(args);
434 if (status < 0)
435 return NULL;
436
437 Py_INCREF(Py_None);
438 return Py_None;
439}
440
441
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000442static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000443 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000444 {NULL, NULL}
445};
446
447
448
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000449PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000450
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000451PyDoc_STRVAR(ImportError__doc__,
452"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000453
454
455
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000456PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000457
458
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000459static PyObject *
460EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000461{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000462 PyObject *item0 = NULL;
463 PyObject *item1 = NULL;
464 PyObject *item2 = NULL;
465 PyObject *subslice = NULL;
466 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000467
468 if (!(self = get_self(args)))
469 return NULL;
470
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000471 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000472 return NULL;
473
474 if (PyObject_SetAttrString(self, "args", args) ||
475 PyObject_SetAttrString(self, "errno", Py_None) ||
476 PyObject_SetAttrString(self, "strerror", Py_None) ||
477 PyObject_SetAttrString(self, "filename", Py_None))
478 {
479 goto finally;
480 }
481
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000482 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000483 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000484 /* Where a function has a single filename, such as open() or some
485 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
486 * called, giving a third argument which is the filename. But, so
487 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000488 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000489 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000490 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000491 * we hack args so that it only contains two items. This also
492 * means we need our own __str__() which prints out the filename
493 * when it was supplied.
494 */
495 item0 = PySequence_GetItem(args, 0);
496 item1 = PySequence_GetItem(args, 1);
497 item2 = PySequence_GetItem(args, 2);
498 if (!item0 || !item1 || !item2)
499 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000500
Barry Warsaw675ac282000-05-26 19:05:16 +0000501 if (PyObject_SetAttrString(self, "errno", item0) ||
502 PyObject_SetAttrString(self, "strerror", item1) ||
503 PyObject_SetAttrString(self, "filename", item2))
504 {
505 goto finally;
506 }
507
508 subslice = PySequence_GetSlice(args, 0, 2);
509 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
510 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000511 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000512
513 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000514 /* Used when PyErr_SetFromErrno() is called and no filename
515 * argument is given.
516 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000517 item0 = PySequence_GetItem(args, 0);
518 item1 = PySequence_GetItem(args, 1);
519 if (!item0 || !item1)
520 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000521
Barry Warsaw675ac282000-05-26 19:05:16 +0000522 if (PyObject_SetAttrString(self, "errno", item0) ||
523 PyObject_SetAttrString(self, "strerror", item1))
524 {
525 goto finally;
526 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000527 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000528
529 case -1:
530 PyErr_Clear();
531 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000532 }
533
534 Py_INCREF(Py_None);
535 rtnval = Py_None;
536
537 finally:
538 Py_DECREF(args);
539 Py_XDECREF(item0);
540 Py_XDECREF(item1);
541 Py_XDECREF(item2);
542 Py_XDECREF(subslice);
543 return rtnval;
544}
545
546
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000547static PyObject *
548EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000549{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000550 PyObject *originalself = self;
551 PyObject *filename;
552 PyObject *serrno;
553 PyObject *strerror;
554 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000555
Fred Drake1aba5772000-08-15 15:46:16 +0000556 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000557 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000558
Barry Warsaw675ac282000-05-26 19:05:16 +0000559 filename = PyObject_GetAttrString(self, "filename");
560 serrno = PyObject_GetAttrString(self, "errno");
561 strerror = PyObject_GetAttrString(self, "strerror");
562 if (!filename || !serrno || !strerror)
563 goto finally;
564
565 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000566 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
567 PyObject *repr = PyObject_Repr(filename);
568 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000569
570 if (!fmt || !repr || !tuple) {
571 Py_XDECREF(fmt);
572 Py_XDECREF(repr);
573 Py_XDECREF(tuple);
574 goto finally;
575 }
576
577 PyTuple_SET_ITEM(tuple, 0, serrno);
578 PyTuple_SET_ITEM(tuple, 1, strerror);
579 PyTuple_SET_ITEM(tuple, 2, repr);
580
581 rtnval = PyString_Format(fmt, tuple);
582
583 Py_DECREF(fmt);
584 Py_DECREF(tuple);
585 /* already freed because tuple owned only reference */
586 serrno = NULL;
587 strerror = NULL;
588 }
589 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000590 PyObject *fmt = PyString_FromString("[Errno %s] %s");
591 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000592
593 if (!fmt || !tuple) {
594 Py_XDECREF(fmt);
595 Py_XDECREF(tuple);
596 goto finally;
597 }
598
599 PyTuple_SET_ITEM(tuple, 0, serrno);
600 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000601
Barry Warsaw675ac282000-05-26 19:05:16 +0000602 rtnval = PyString_Format(fmt, tuple);
603
604 Py_DECREF(fmt);
605 Py_DECREF(tuple);
606 /* already freed because tuple owned only reference */
607 serrno = NULL;
608 strerror = NULL;
609 }
610 else
611 /* The original Python code said:
612 *
613 * return StandardError.__str__(self)
614 *
615 * but there is no StandardError__str__() function; we happen to
616 * know that's just a pass through to Exception__str__().
617 */
618 rtnval = Exception__str__(originalself, args);
619
620 finally:
621 Py_XDECREF(filename);
622 Py_XDECREF(serrno);
623 Py_XDECREF(strerror);
624 return rtnval;
625}
626
627
628static
629PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000630 {"__init__", EnvironmentError__init__, METH_VARARGS},
631 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000632 {NULL, NULL}
633};
634
635
636
637
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000638PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000639
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000640PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000641
642#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000643PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000644#endif /* MS_WINDOWS */
645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000646PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000648PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000649
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000650PyDoc_STRVAR(NotImplementedError__doc__,
651"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000652
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000653PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000655PyDoc_STRVAR(UnboundLocalError__doc__,
656"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000657
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000658PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000659
660
661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000662PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000663
664
665static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000666SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000667{
Barry Warsaw87bec352000-08-18 05:05:37 +0000668 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000669 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000670
671 /* Additional class-creation time initializations */
672 if (!emptystring ||
673 PyObject_SetAttrString(klass, "msg", emptystring) ||
674 PyObject_SetAttrString(klass, "filename", Py_None) ||
675 PyObject_SetAttrString(klass, "lineno", Py_None) ||
676 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000677 PyObject_SetAttrString(klass, "text", Py_None) ||
678 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000679 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000680 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000681 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000682 Py_XDECREF(emptystring);
683 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000684}
685
686
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000687static PyObject *
688SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000689{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000690 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000691 int lenargs;
692
693 if (!(self = get_self(args)))
694 return NULL;
695
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000696 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000697 return NULL;
698
699 if (PyObject_SetAttrString(self, "args", args))
700 goto finally;
701
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000702 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000703 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000704 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000705 int status;
706
707 if (!item0)
708 goto finally;
709 status = PyObject_SetAttrString(self, "msg", item0);
710 Py_DECREF(item0);
711 if (status)
712 goto finally;
713 }
714 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000715 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000716 PyObject *filename = NULL, *lineno = NULL;
717 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000718 int status = 1;
719
720 if (!info)
721 goto finally;
722
723 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000724 if (filename != NULL) {
725 lineno = PySequence_GetItem(info, 1);
726 if (lineno != NULL) {
727 offset = PySequence_GetItem(info, 2);
728 if (offset != NULL) {
729 text = PySequence_GetItem(info, 3);
730 if (text != NULL) {
731 status =
732 PyObject_SetAttrString(self, "filename", filename)
733 || PyObject_SetAttrString(self, "lineno", lineno)
734 || PyObject_SetAttrString(self, "offset", offset)
735 || PyObject_SetAttrString(self, "text", text);
736 Py_DECREF(text);
737 }
738 Py_DECREF(offset);
739 }
740 Py_DECREF(lineno);
741 }
742 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000743 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000744 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000745
746 if (status)
747 goto finally;
748 }
749 Py_INCREF(Py_None);
750 rtnval = Py_None;
751
752 finally:
753 Py_DECREF(args);
754 return rtnval;
755}
756
757
Fred Drake185a29b2000-08-15 16:20:36 +0000758/* This is called "my_basename" instead of just "basename" to avoid name
759 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
760 defined, and Python does define that. */
761static char *
762my_basename(char *name)
763{
764 char *cp = name;
765 char *result = name;
766
767 if (name == NULL)
768 return "???";
769 while (*cp != '\0') {
770 if (*cp == SEP)
771 result = cp + 1;
772 ++cp;
773 }
774 return result;
775}
776
777
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000778static PyObject *
779SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000780{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000781 PyObject *msg;
782 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000783 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000784
Fred Drake1aba5772000-08-15 15:46:16 +0000785 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000786 return NULL;
787
788 if (!(msg = PyObject_GetAttrString(self, "msg")))
789 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000790
Barry Warsaw675ac282000-05-26 19:05:16 +0000791 str = PyObject_Str(msg);
792 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000793 result = str;
794
795 /* XXX -- do all the additional formatting with filename and
796 lineno here */
797
Guido van Rossum602d4512002-09-03 20:24:09 +0000798 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000799 int have_filename = 0;
800 int have_lineno = 0;
801 char *buffer = NULL;
802
Barry Warsaw77c9f502000-08-16 19:43:17 +0000803 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000804 have_filename = PyString_Check(filename);
805 else
806 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000807
808 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000809 have_lineno = PyInt_Check(lineno);
810 else
811 PyErr_Clear();
812
813 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000814 int bufsize = PyString_GET_SIZE(str) + 64;
815 if (have_filename)
816 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000817
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000818 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000819 if (buffer != NULL) {
820 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000821 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
822 PyString_AS_STRING(str),
823 my_basename(PyString_AS_STRING(filename)),
824 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000825 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000826 PyOS_snprintf(buffer, bufsize, "%s (%s)",
827 PyString_AS_STRING(str),
828 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000829 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000830 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
831 PyString_AS_STRING(str),
832 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000833
Fred Drake1aba5772000-08-15 15:46:16 +0000834 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000835 PyMem_FREE(buffer);
836
Fred Drake1aba5772000-08-15 15:46:16 +0000837 if (result == NULL)
838 result = str;
839 else
840 Py_DECREF(str);
841 }
842 }
843 Py_XDECREF(filename);
844 Py_XDECREF(lineno);
845 }
846 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000847}
848
849
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000850static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000851 {"__init__", SyntaxError__init__, METH_VARARGS},
852 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000853 {NULL, NULL}
854};
855
856
Guido van Rossum602d4512002-09-03 20:24:09 +0000857static PyObject *
858KeyError__str__(PyObject *self, PyObject *args)
859{
860 PyObject *argsattr;
861 PyObject *result;
862
863 if (!PyArg_ParseTuple(args, "O:__str__", &self))
864 return NULL;
865
866 if (!(argsattr = PyObject_GetAttrString(self, "args")))
867 return NULL;
868
869 /* If args is a tuple of exactly one item, apply repr to args[0].
870 This is done so that e.g. the exception raised by {}[''] prints
871 KeyError: ''
872 rather than the confusing
873 KeyError
874 alone. The downside is that if KeyError is raised with an explanatory
875 string, that string will be displayed in quotes. Too bad.
876 If args is anything else, use the default Exception__str__().
877 */
878 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
879 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
880 result = PyObject_Repr(key);
881 }
882 else
883 result = Exception__str__(self, args);
884
885 Py_DECREF(argsattr);
886 return result;
887}
888
889static PyMethodDef KeyError_methods[] = {
890 {"__str__", KeyError__str__, METH_VARARGS},
891 {NULL, NULL}
892};
893
894
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000895#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000896static
897int get_int(PyObject *exc, const char *name, int *value)
898{
899 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
900
901 if (!attr)
902 return -1;
903 if (!PyInt_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000904 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000905 Py_DECREF(attr);
906 return -1;
907 }
908 *value = PyInt_AS_LONG(attr);
909 Py_DECREF(attr);
910 return 0;
911}
912
913
914static
915int set_int(PyObject *exc, const char *name, int value)
916{
917 PyObject *obj = PyInt_FromLong(value);
918 int result;
919
920 if (!obj)
921 return -1;
922 result = PyObject_SetAttrString(exc, (char *)name, obj);
923 Py_DECREF(obj);
924 return result;
925}
926
927
928static
929PyObject *get_string(PyObject *exc, const char *name)
930{
931 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
932
933 if (!attr)
934 return NULL;
935 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000936 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000937 Py_DECREF(attr);
938 return NULL;
939 }
940 return attr;
941}
942
943
944static
945int set_string(PyObject *exc, const char *name, const char *value)
946{
947 PyObject *obj = PyString_FromString(value);
948 int result;
949
950 if (!obj)
951 return -1;
952 result = PyObject_SetAttrString(exc, (char *)name, obj);
953 Py_DECREF(obj);
954 return result;
955}
956
957
958static
959PyObject *get_unicode(PyObject *exc, const char *name)
960{
961 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
962
963 if (!attr)
964 return NULL;
965 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000966 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000967 Py_DECREF(attr);
968 return NULL;
969 }
970 return attr;
971}
972
973PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
974{
975 return get_string(exc, "encoding");
976}
977
978PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
979{
980 return get_string(exc, "encoding");
981}
982
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000983PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
984{
985 return get_unicode(exc, "object");
986}
987
988PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
989{
990 return get_string(exc, "object");
991}
992
993PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
994{
995 return get_unicode(exc, "object");
996}
997
998int PyUnicodeEncodeError_GetStart(PyObject *exc, int *start)
999{
1000 if (!get_int(exc, "start", start)) {
1001 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
1002 int size;
1003 if (!object)
1004 return -1;
1005 size = PyUnicode_GET_SIZE(object);
1006 if (*start<0)
1007 *start = 0;
1008 if (*start>=size)
1009 *start = size-1;
1010 Py_DECREF(object);
1011 return 0;
1012 }
1013 return -1;
1014}
1015
1016
1017int PyUnicodeDecodeError_GetStart(PyObject *exc, int *start)
1018{
1019 if (!get_int(exc, "start", start)) {
1020 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
1021 int size;
1022 if (!object)
1023 return -1;
1024 size = PyString_GET_SIZE(object);
1025 if (*start<0)
1026 *start = 0;
1027 if (*start>=size)
1028 *start = size-1;
1029 Py_DECREF(object);
1030 return 0;
1031 }
1032 return -1;
1033}
1034
1035
1036int PyUnicodeTranslateError_GetStart(PyObject *exc, int *start)
1037{
1038 return PyUnicodeEncodeError_GetStart(exc, start);
1039}
1040
1041
1042int PyUnicodeEncodeError_SetStart(PyObject *exc, int start)
1043{
1044 return set_int(exc, "start", start);
1045}
1046
1047
1048int PyUnicodeDecodeError_SetStart(PyObject *exc, int start)
1049{
1050 return set_int(exc, "start", start);
1051}
1052
1053
1054int PyUnicodeTranslateError_SetStart(PyObject *exc, int start)
1055{
1056 return set_int(exc, "start", start);
1057}
1058
1059
1060int PyUnicodeEncodeError_GetEnd(PyObject *exc, int *end)
1061{
1062 if (!get_int(exc, "end", end)) {
1063 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
1064 int size;
1065 if (!object)
1066 return -1;
1067 size = PyUnicode_GET_SIZE(object);
1068 if (*end<1)
1069 *end = 1;
1070 if (*end>size)
1071 *end = size;
1072 Py_DECREF(object);
1073 return 0;
1074 }
1075 return -1;
1076}
1077
1078
1079int PyUnicodeDecodeError_GetEnd(PyObject *exc, int *end)
1080{
1081 if (!get_int(exc, "end", end)) {
1082 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
1083 int size;
1084 if (!object)
1085 return -1;
1086 size = PyString_GET_SIZE(object);
1087 if (*end<1)
1088 *end = 1;
1089 if (*end>size)
1090 *end = size;
1091 Py_DECREF(object);
1092 return 0;
1093 }
1094 return -1;
1095}
1096
1097
1098int PyUnicodeTranslateError_GetEnd(PyObject *exc, int *start)
1099{
1100 return PyUnicodeEncodeError_GetEnd(exc, start);
1101}
1102
1103
1104int PyUnicodeEncodeError_SetEnd(PyObject *exc, int end)
1105{
1106 return set_int(exc, "end", end);
1107}
1108
1109
1110int PyUnicodeDecodeError_SetEnd(PyObject *exc, int end)
1111{
1112 return set_int(exc, "end", end);
1113}
1114
1115
1116int PyUnicodeTranslateError_SetEnd(PyObject *exc, int end)
1117{
1118 return set_int(exc, "end", end);
1119}
1120
1121
1122PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1123{
1124 return get_string(exc, "reason");
1125}
1126
1127
1128PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1129{
1130 return get_string(exc, "reason");
1131}
1132
1133
1134PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1135{
1136 return get_string(exc, "reason");
1137}
1138
1139
1140int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1141{
1142 return set_string(exc, "reason", reason);
1143}
1144
1145
1146int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1147{
1148 return set_string(exc, "reason", reason);
1149}
1150
1151
1152int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1153{
1154 return set_string(exc, "reason", reason);
1155}
1156
1157
1158static PyObject *
1159UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1160{
1161 PyObject *rtnval = NULL;
1162 PyObject *encoding;
1163 PyObject *object;
1164 PyObject *start;
1165 PyObject *end;
1166 PyObject *reason;
1167
1168 if (!(self = get_self(args)))
1169 return NULL;
1170
1171 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1172 return NULL;
1173
1174 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1175 &PyString_Type, &encoding,
1176 objecttype, &object,
1177 &PyInt_Type, &start,
1178 &PyInt_Type, &end,
1179 &PyString_Type, &reason))
1180 return NULL;
1181
1182 if (PyObject_SetAttrString(self, "args", args))
1183 goto finally;
1184
1185 if (PyObject_SetAttrString(self, "encoding", encoding))
1186 goto finally;
1187 if (PyObject_SetAttrString(self, "object", object))
1188 goto finally;
1189 if (PyObject_SetAttrString(self, "start", start))
1190 goto finally;
1191 if (PyObject_SetAttrString(self, "end", end))
1192 goto finally;
1193 if (PyObject_SetAttrString(self, "reason", reason))
1194 goto finally;
1195
1196 Py_INCREF(Py_None);
1197 rtnval = Py_None;
1198
1199 finally:
1200 Py_DECREF(args);
1201 return rtnval;
1202}
1203
1204
1205static PyObject *
1206UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1207{
1208 return UnicodeError__init__(self, args, &PyUnicode_Type);
1209}
1210
1211static PyObject *
1212UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1213{
1214 PyObject *encodingObj = NULL;
1215 PyObject *objectObj = NULL;
1216 int length;
1217 int start;
1218 int end;
1219 PyObject *reasonObj = NULL;
1220 char buffer[1000];
1221 PyObject *result = NULL;
1222
1223 self = arg;
1224
1225 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1226 goto error;
1227
1228 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1229 goto error;
1230
1231 length = PyUnicode_GET_SIZE(objectObj);
1232
1233 if (PyUnicodeEncodeError_GetStart(self, &start))
1234 goto error;
1235
1236 if (PyUnicodeEncodeError_GetEnd(self, &end))
1237 goto error;
1238
1239 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1240 goto error;
1241
1242 if (end==start+1) {
1243 PyOS_snprintf(buffer, sizeof(buffer),
1244 "'%.400s' codec can't encode character '\\u%x' in position %d: %.400s",
1245 PyString_AS_STRING(encodingObj),
1246 (int)PyUnicode_AS_UNICODE(objectObj)[start],
1247 start,
1248 PyString_AS_STRING(reasonObj)
1249 );
1250 }
1251 else {
1252 PyOS_snprintf(buffer, sizeof(buffer),
1253 "'%.400s' codec can't encode characters in position %d-%d: %.400s",
1254 PyString_AS_STRING(encodingObj),
1255 start,
1256 end-1,
1257 PyString_AS_STRING(reasonObj)
1258 );
1259 }
1260 result = PyString_FromString(buffer);
1261
1262error:
1263 Py_XDECREF(reasonObj);
1264 Py_XDECREF(objectObj);
1265 Py_XDECREF(encodingObj);
1266 return result;
1267}
1268
1269static PyMethodDef UnicodeEncodeError_methods[] = {
1270 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1271 {"__str__", UnicodeEncodeError__str__, METH_O},
1272 {NULL, NULL}
1273};
1274
1275
1276PyObject * PyUnicodeEncodeError_Create(
1277 const char *encoding, const Py_UNICODE *object, int length,
1278 int start, int end, const char *reason)
1279{
1280 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#iis",
1281 encoding, object, length, start, end, reason);
1282}
1283
1284
1285static PyObject *
1286UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1287{
1288 return UnicodeError__init__(self, args, &PyString_Type);
1289}
1290
1291static PyObject *
1292UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1293{
1294 PyObject *encodingObj = NULL;
1295 PyObject *objectObj = NULL;
1296 int length;
1297 int start;
1298 int end;
1299 PyObject *reasonObj = NULL;
1300 char buffer[1000];
1301 PyObject *result = NULL;
1302
1303 self = arg;
1304
1305 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1306 goto error;
1307
1308 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1309 goto error;
1310
1311 length = PyString_GET_SIZE(objectObj);
1312
1313 if (PyUnicodeDecodeError_GetStart(self, &start))
1314 goto error;
1315
1316 if (PyUnicodeDecodeError_GetEnd(self, &end))
1317 goto error;
1318
1319 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1320 goto error;
1321
1322 if (end==start+1) {
1323 PyOS_snprintf(buffer, sizeof(buffer),
1324 "'%.400s' codec can't decode byte 0x%x in position %d: %.400s",
1325 PyString_AS_STRING(encodingObj),
1326 ((int)PyString_AS_STRING(objectObj)[start])&0xff,
1327 start,
1328 PyString_AS_STRING(reasonObj)
1329 );
1330 }
1331 else {
1332 PyOS_snprintf(buffer, sizeof(buffer),
1333 "'%.400s' codec can't decode bytes in position %d-%d: %.400s",
1334 PyString_AS_STRING(encodingObj),
1335 start,
1336 end-1,
1337 PyString_AS_STRING(reasonObj)
1338 );
1339 }
1340 result = PyString_FromString(buffer);
1341
1342error:
1343 Py_XDECREF(reasonObj);
1344 Py_XDECREF(objectObj);
1345 Py_XDECREF(encodingObj);
1346 return result;
1347}
1348
1349static PyMethodDef UnicodeDecodeError_methods[] = {
1350 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1351 {"__str__", UnicodeDecodeError__str__, METH_O},
1352 {NULL, NULL}
1353};
1354
1355
1356PyObject * PyUnicodeDecodeError_Create(
1357 const char *encoding, const char *object, int length,
1358 int start, int end, const char *reason)
1359{
1360 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
1361 encoding, object, length, start, end, reason);
1362}
1363
1364
1365static PyObject *
1366UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1367{
1368 PyObject *rtnval = NULL;
1369 PyObject *object;
1370 PyObject *start;
1371 PyObject *end;
1372 PyObject *reason;
1373
1374 if (!(self = get_self(args)))
1375 return NULL;
1376
1377 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1378 return NULL;
1379
1380 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1381 &PyUnicode_Type, &object,
1382 &PyInt_Type, &start,
1383 &PyInt_Type, &end,
1384 &PyString_Type, &reason))
1385 goto finally;
1386
1387 if (PyObject_SetAttrString(self, "args", args))
1388 goto finally;
1389
1390 if (PyObject_SetAttrString(self, "object", object))
1391 goto finally;
1392 if (PyObject_SetAttrString(self, "start", start))
1393 goto finally;
1394 if (PyObject_SetAttrString(self, "end", end))
1395 goto finally;
1396 if (PyObject_SetAttrString(self, "reason", reason))
1397 goto finally;
1398
1399 Py_INCREF(Py_None);
1400 rtnval = Py_None;
1401
1402 finally:
1403 Py_DECREF(args);
1404 return rtnval;
1405}
1406
1407
1408static PyObject *
1409UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1410{
1411 PyObject *objectObj = NULL;
1412 int length;
1413 int start;
1414 int end;
1415 PyObject *reasonObj = NULL;
1416 char buffer[1000];
1417 PyObject *result = NULL;
1418
1419 self = arg;
1420
1421 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1422 goto error;
1423
1424 length = PyUnicode_GET_SIZE(objectObj);
1425
1426 if (PyUnicodeTranslateError_GetStart(self, &start))
1427 goto error;
1428
1429 if (PyUnicodeTranslateError_GetEnd(self, &end))
1430 goto error;
1431
1432 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1433 goto error;
1434
1435 if (end==start+1) {
1436 PyOS_snprintf(buffer, sizeof(buffer),
1437 "can't translate character '\\u%x' in position %d: %.400s",
1438 (int)PyUnicode_AS_UNICODE(objectObj)[start],
1439 start,
1440 PyString_AS_STRING(reasonObj)
1441 );
1442 }
1443 else {
1444 PyOS_snprintf(buffer, sizeof(buffer),
1445 "can't translate characters in position %d-%d: %.400s",
1446 start,
1447 end-1,
1448 PyString_AS_STRING(reasonObj)
1449 );
1450 }
1451 result = PyString_FromString(buffer);
1452
1453error:
1454 Py_XDECREF(reasonObj);
1455 Py_XDECREF(objectObj);
1456 return result;
1457}
1458
1459static PyMethodDef UnicodeTranslateError_methods[] = {
1460 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1461 {"__str__", UnicodeTranslateError__str__, METH_O},
1462 {NULL, NULL}
1463};
1464
1465
1466PyObject * PyUnicodeTranslateError_Create(
1467 const Py_UNICODE *object, int length,
1468 int start, int end, const char *reason)
1469{
1470 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
1471 object, length, start, end, reason);
1472}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001473#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001474
1475
Barry Warsaw675ac282000-05-26 19:05:16 +00001476
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001477/* Exception doc strings */
1478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001479PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001480
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001481PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001482
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001483PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001484
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001485PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001487PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001488
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001489PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001490
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001491PyDoc_STRVAR(ZeroDivisionError__doc__,
1492"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001494PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001495
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001496PyDoc_STRVAR(ValueError__doc__,
1497"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001499PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001500
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001501#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001502PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1503
1504PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1505
1506PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001507#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001508
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001509PyDoc_STRVAR(SystemError__doc__,
1510"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001511\n\
1512Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001513the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001515PyDoc_STRVAR(ReferenceError__doc__,
1516"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001518PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001519
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001520PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001522PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001523
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001524/* Warning category docstrings */
1525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001526PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001528PyDoc_STRVAR(UserWarning__doc__,
1529"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001530
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001531PyDoc_STRVAR(DeprecationWarning__doc__,
1532"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001534PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001535"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001536"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001537
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001538PyDoc_STRVAR(SyntaxWarning__doc__,
1539"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001540
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001541PyDoc_STRVAR(OverflowWarning__doc__,
1542"Base class for warnings about numeric overflow.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001543
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001544PyDoc_STRVAR(RuntimeWarning__doc__,
1545"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001546
Barry Warsaw9f007392002-08-14 15:51:29 +00001547PyDoc_STRVAR(FutureWarning__doc__,
1548"Base class for warnings about constructs that will change semantically "
1549"in the future.");
1550
Barry Warsaw675ac282000-05-26 19:05:16 +00001551
1552
1553/* module global functions */
1554static PyMethodDef functions[] = {
1555 /* Sentinel */
1556 {NULL, NULL}
1557};
1558
1559
1560
1561/* Global C API defined exceptions */
1562
1563PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001564PyObject *PyExc_StopIteration;
Barry Warsaw675ac282000-05-26 19:05:16 +00001565PyObject *PyExc_StandardError;
1566PyObject *PyExc_ArithmeticError;
1567PyObject *PyExc_LookupError;
1568
1569PyObject *PyExc_AssertionError;
1570PyObject *PyExc_AttributeError;
1571PyObject *PyExc_EOFError;
1572PyObject *PyExc_FloatingPointError;
1573PyObject *PyExc_EnvironmentError;
1574PyObject *PyExc_IOError;
1575PyObject *PyExc_OSError;
1576PyObject *PyExc_ImportError;
1577PyObject *PyExc_IndexError;
1578PyObject *PyExc_KeyError;
1579PyObject *PyExc_KeyboardInterrupt;
1580PyObject *PyExc_MemoryError;
1581PyObject *PyExc_NameError;
1582PyObject *PyExc_OverflowError;
1583PyObject *PyExc_RuntimeError;
1584PyObject *PyExc_NotImplementedError;
1585PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001586PyObject *PyExc_IndentationError;
1587PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001588PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001589PyObject *PyExc_SystemError;
1590PyObject *PyExc_SystemExit;
1591PyObject *PyExc_UnboundLocalError;
1592PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001593PyObject *PyExc_UnicodeEncodeError;
1594PyObject *PyExc_UnicodeDecodeError;
1595PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001596PyObject *PyExc_TypeError;
1597PyObject *PyExc_ValueError;
1598PyObject *PyExc_ZeroDivisionError;
1599#ifdef MS_WINDOWS
1600PyObject *PyExc_WindowsError;
1601#endif
1602
1603/* Pre-computed MemoryError instance. Best to create this as early as
1604 * possibly and not wait until a MemoryError is actually raised!
1605 */
1606PyObject *PyExc_MemoryErrorInst;
1607
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001608/* Predefined warning categories */
1609PyObject *PyExc_Warning;
1610PyObject *PyExc_UserWarning;
1611PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001612PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001613PyObject *PyExc_SyntaxWarning;
Guido van Rossumae347b32001-08-23 02:56:07 +00001614PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001615PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001616PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001617
Barry Warsaw675ac282000-05-26 19:05:16 +00001618
1619
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001620/* mapping between exception names and their PyObject ** */
1621static struct {
1622 char *name;
1623 PyObject **exc;
1624 PyObject **base; /* NULL == PyExc_StandardError */
1625 char *docstr;
1626 PyMethodDef *methods;
1627 int (*classinit)(PyObject *);
1628} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001629 /*
1630 * The first three classes MUST appear in exactly this order
1631 */
1632 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001633 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1634 StopIteration__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001635 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1636 StandardError__doc__},
1637 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1638 /*
1639 * The rest appear in depth-first order of the hierarchy
1640 */
1641 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1642 SystemExit_methods},
1643 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1644 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1645 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1646 EnvironmentError_methods},
1647 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1648 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1649#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001650 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001651 WindowsError__doc__},
1652#endif /* MS_WINDOWS */
1653 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1654 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1655 {"NotImplementedError", &PyExc_NotImplementedError,
1656 &PyExc_RuntimeError, NotImplementedError__doc__},
1657 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1658 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1659 UnboundLocalError__doc__},
1660 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1661 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1662 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001663 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1664 IndentationError__doc__},
1665 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1666 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001667 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1668 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1669 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1670 IndexError__doc__},
1671 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001672 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001673 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1674 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1675 OverflowError__doc__},
1676 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1677 ZeroDivisionError__doc__},
1678 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1679 FloatingPointError__doc__},
1680 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1681 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001682#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001683 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1684 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1685 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1686 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1687 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1688 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001689#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001690 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001691 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1692 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001693 /* Warning categories */
1694 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1695 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1696 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1697 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001698 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1699 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001700 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Guido van Rossumae347b32001-08-23 02:56:07 +00001701 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1702 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001703 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1704 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001705 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1706 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001707 /* Sentinel */
1708 {NULL}
1709};
1710
1711
1712
Mark Hammonda2905272002-07-29 13:42:14 +00001713void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001714_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001715{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001716 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001717 int modnamesz = strlen(modulename);
1718 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001719 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001720
Tim Peters6d6c1a32001-08-02 04:15:00 +00001721 me = Py_InitModule(modulename, functions);
1722 if (me == NULL)
1723 goto err;
1724 mydict = PyModule_GetDict(me);
1725 if (mydict == NULL)
1726 goto err;
1727 bltinmod = PyImport_ImportModule("__builtin__");
1728 if (bltinmod == NULL)
1729 goto err;
1730 bdict = PyModule_GetDict(bltinmod);
1731 if (bdict == NULL)
1732 goto err;
1733 doc = PyString_FromString(module__doc__);
1734 if (doc == NULL)
1735 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001736
Tim Peters6d6c1a32001-08-02 04:15:00 +00001737 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001738 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001739 if (i < 0) {
1740 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001741 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001742 return;
1743 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001744
1745 /* This is the base class of all exceptions, so make it first. */
1746 if (make_Exception(modulename) ||
1747 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1748 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1749 {
1750 Py_FatalError("Base class `Exception' could not be created.");
1751 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001752
Barry Warsaw675ac282000-05-26 19:05:16 +00001753 /* Now we can programmatically create all the remaining exceptions.
1754 * Remember to start the loop at 1 to skip Exceptions.
1755 */
1756 for (i=1; exctable[i].name; i++) {
1757 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001758 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1759 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001760
1761 (void)strcpy(cname, modulename);
1762 (void)strcat(cname, ".");
1763 (void)strcat(cname, exctable[i].name);
1764
1765 if (exctable[i].base == 0)
1766 base = PyExc_StandardError;
1767 else
1768 base = *exctable[i].base;
1769
1770 status = make_class(exctable[i].exc, base, cname,
1771 exctable[i].methods,
1772 exctable[i].docstr);
1773
1774 PyMem_DEL(cname);
1775
1776 if (status)
1777 Py_FatalError("Standard exception classes could not be created.");
1778
1779 if (exctable[i].classinit) {
1780 status = (*exctable[i].classinit)(*exctable[i].exc);
1781 if (status)
1782 Py_FatalError("An exception class could not be initialized.");
1783 }
1784
1785 /* Now insert the class into both this module and the __builtin__
1786 * module.
1787 */
1788 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1789 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1790 {
1791 Py_FatalError("Module dictionary insertion problem.");
1792 }
1793 }
1794
1795 /* Now we need to pre-allocate a MemoryError instance */
1796 args = Py_BuildValue("()");
1797 if (!args ||
1798 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1799 {
1800 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1801 }
1802 Py_DECREF(args);
1803
1804 /* We're done with __builtin__ */
1805 Py_DECREF(bltinmod);
1806}
1807
1808
Mark Hammonda2905272002-07-29 13:42:14 +00001809void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001810_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001811{
1812 int i;
1813
1814 Py_XDECREF(PyExc_MemoryErrorInst);
1815 PyExc_MemoryErrorInst = NULL;
1816
1817 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001818 /* clear the class's dictionary, freeing up circular references
1819 * between the class and its methods.
1820 */
1821 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1822 PyDict_Clear(cdict);
1823 Py_DECREF(cdict);
1824
1825 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001826 Py_XDECREF(*exctable[i].exc);
1827 *exctable[i].exc = NULL;
1828 }
1829}