blob: 16b67384cd98a321ab32023b340d48a8855e0139 [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\
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000070 | | +-- VMSError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +000071 | |\n\
72 | +-- EOFError\n\
73 | +-- RuntimeError\n\
74 | | |\n\
75 | | +-- NotImplementedError\n\
76 | |\n\
77 | +-- NameError\n\
78 | | |\n\
79 | | +-- UnboundLocalError\n\
80 | |\n\
81 | +-- AttributeError\n\
82 | +-- SyntaxError\n\
83 | | |\n\
84 | | +-- IndentationError\n\
85 | | |\n\
86 | | +-- TabError\n\
87 | |\n\
88 | +-- TypeError\n\
89 | +-- AssertionError\n\
90 | +-- LookupError\n\
91 | | |\n\
92 | | +-- IndexError\n\
93 | | +-- KeyError\n\
94 | |\n\
95 | +-- ArithmeticError\n\
96 | | |\n\
97 | | +-- OverflowError\n\
98 | | +-- ZeroDivisionError\n\
99 | | +-- FloatingPointError\n\
100 | |\n\
101 | +-- ValueError\n\
102 | | |\n\
103 | | +-- UnicodeError\n\
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000104 | | |\n\
105 | | +-- UnicodeEncodeError\n\
106 | | +-- UnicodeDecodeError\n\
107 | | +-- UnicodeTranslateError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000108 | |\n\
Fred Drakebb9fa212001-10-05 21:50:08 +0000109 | +-- ReferenceError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000110 | +-- SystemError\n\
111 | +-- MemoryError\n\
112 |\n\
113 +---Warning\n\
Barry Warsaw675ac282000-05-26 19:05:16 +0000114 |\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000115 +-- UserWarning\n\
116 +-- DeprecationWarning\n\
Neal Norwitzd68f5172002-05-29 15:54:55 +0000117 +-- PendingDeprecationWarning\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000118 +-- SyntaxWarning\n\
Guido van Rossumae347b32001-08-23 02:56:07 +0000119 +-- OverflowWarning\n\
Barry Warsaw9f007392002-08-14 15:51:29 +0000120 +-- RuntimeWarning\n\
121 +-- FutureWarning"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000122);
Barry Warsaw675ac282000-05-26 19:05:16 +0000123
124
125/* Helper function for populating a dictionary with method wrappers. */
126static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000127populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +0000128{
129 if (!methods)
130 return 0;
131
132 while (methods->ml_name) {
133 /* get a wrapper for the built-in function */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000134 PyObject *func = PyCFunction_New(methods, NULL);
135 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +0000136 int status;
137
138 if (!func)
139 return -1;
140
141 /* turn the function into an unbound method */
142 if (!(meth = PyMethod_New(func, NULL, klass))) {
143 Py_DECREF(func);
144 return -1;
145 }
Barry Warsaw9667ed22001-01-23 16:08:34 +0000146
Barry Warsaw675ac282000-05-26 19:05:16 +0000147 /* add method to dictionary */
148 status = PyDict_SetItemString(dict, methods->ml_name, meth);
149 Py_DECREF(meth);
150 Py_DECREF(func);
151
152 /* stop now if an error occurred, otherwise do the next method */
153 if (status)
154 return status;
155
156 methods++;
157 }
158 return 0;
159}
160
Barry Warsaw9667ed22001-01-23 16:08:34 +0000161
Barry Warsaw675ac282000-05-26 19:05:16 +0000162
163/* This function is used to create all subsequent exception classes. */
164static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000165make_class(PyObject **klass, PyObject *base,
166 char *name, PyMethodDef *methods,
167 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +0000168{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000169 PyObject *dict = PyDict_New();
170 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000171 int status = -1;
172
173 if (!dict)
174 return -1;
175
176 /* If an error occurs from here on, goto finally instead of explicitly
177 * returning NULL.
178 */
179
180 if (docstr) {
181 if (!(str = PyString_FromString(docstr)))
182 goto finally;
183 if (PyDict_SetItemString(dict, "__doc__", str))
184 goto finally;
185 }
186
187 if (!(*klass = PyErr_NewException(name, base, dict)))
188 goto finally;
189
190 if (populate_methods(*klass, dict, methods)) {
191 Py_DECREF(*klass);
192 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000193 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000194 }
195
196 status = 0;
197
198 finally:
199 Py_XDECREF(dict);
200 Py_XDECREF(str);
201 return status;
202}
203
204
205/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000206static PyObject *
207get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000208{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000209 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000210 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000211 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000212 if (PyExc_TypeError) {
213 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000214 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000215 }
216 return NULL;
217 }
218 return self;
219}
220
221
222
223/* Notes on bootstrapping the exception classes.
224 *
225 * First thing we create is the base class for all exceptions, called
226 * appropriately enough: Exception. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000227 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000228 * for TypeError, which can conditionally exist.
229 *
230 * Next, StandardError is created (which is quite simple) followed by
231 * TypeError, because the instantiation of other exceptions can potentially
232 * throw a TypeError. Once these exceptions are created, all the others
233 * can be created in any order. See the static exctable below for the
234 * explicit bootstrap order.
235 *
236 * All classes after Exception can be created using PyErr_NewException().
237 */
238
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000239PyDoc_STRVAR(Exception__doc__, "Common base class for all exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000240
241
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000242static PyObject *
243Exception__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000244{
245 int status;
246
247 if (!(self = get_self(args)))
248 return NULL;
249
250 /* set args attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000251 /* XXX size is only a hint */
252 args = PySequence_GetSlice(args, 1, PySequence_Size(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000253 if (!args)
254 return NULL;
255 status = PyObject_SetAttrString(self, "args", args);
256 Py_DECREF(args);
257 if (status < 0)
258 return NULL;
259
260 Py_INCREF(Py_None);
261 return Py_None;
262}
263
264
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000265static PyObject *
266Exception__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000267{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000268 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000269
Fred Drake1aba5772000-08-15 15:46:16 +0000270 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000271 return NULL;
272
273 args = PyObject_GetAttrString(self, "args");
274 if (!args)
275 return NULL;
276
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000277 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000278 case 0:
279 out = PyString_FromString("");
280 break;
281 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000282 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000283 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000284 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000285 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000286 Py_DECREF(tmp);
287 }
288 else
289 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000290 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000291 }
Guido van Rossum98b2a422002-09-18 04:06:32 +0000292 case -1:
293 PyErr_Clear();
294 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000295 default:
296 out = PyObject_Str(args);
297 break;
298 }
299
300 Py_DECREF(args);
301 return out;
302}
303
304
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000305static PyObject *
306Exception__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000307{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000308 PyObject *out;
309 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000310
Fred Drake1aba5772000-08-15 15:46:16 +0000311 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000312 return NULL;
313
314 args = PyObject_GetAttrString(self, "args");
315 if (!args)
316 return NULL;
317
318 out = PyObject_GetItem(args, index);
319 Py_DECREF(args);
320 return out;
321}
322
323
324static PyMethodDef
325Exception_methods[] = {
326 /* methods for the Exception class */
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000327 { "__getitem__", Exception__getitem__, METH_VARARGS},
328 { "__str__", Exception__str__, METH_VARARGS},
329 { "__init__", Exception__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000330 { NULL, NULL }
331};
332
333
334static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000335make_Exception(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000336{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000337 PyObject *dict = PyDict_New();
338 PyObject *str = NULL;
339 PyObject *name = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000340 int status = -1;
341
342 if (!dict)
343 return -1;
344
345 /* If an error occurs from here on, goto finally instead of explicitly
346 * returning NULL.
347 */
348
349 if (!(str = PyString_FromString(modulename)))
350 goto finally;
351 if (PyDict_SetItemString(dict, "__module__", str))
352 goto finally;
353 Py_DECREF(str);
354 if (!(str = PyString_FromString(Exception__doc__)))
355 goto finally;
356 if (PyDict_SetItemString(dict, "__doc__", str))
357 goto finally;
358
359 if (!(name = PyString_FromString("Exception")))
360 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000361
Barry Warsaw675ac282000-05-26 19:05:16 +0000362 if (!(PyExc_Exception = PyClass_New(NULL, dict, name)))
363 goto finally;
364
365 /* Now populate the dictionary with the method suite */
366 if (populate_methods(PyExc_Exception, dict, Exception_methods))
367 /* Don't need to reclaim PyExc_Exception here because that'll
368 * happen during interpreter shutdown.
369 */
370 goto finally;
371
372 status = 0;
373
374 finally:
375 Py_XDECREF(dict);
376 Py_XDECREF(str);
377 Py_XDECREF(name);
378 return status;
379}
380
381
382
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000383PyDoc_STRVAR(StandardError__doc__,
384"Base class for all standard Python exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000385
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000386PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000387
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000388PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000389
Barry Warsaw675ac282000-05-26 19:05:16 +0000390
391
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000392PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000393
394
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000395static PyObject *
396SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000397{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000398 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000399 int status;
400
401 if (!(self = get_self(args)))
402 return NULL;
403
404 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000405 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000406 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000407
Barry Warsaw675ac282000-05-26 19:05:16 +0000408 status = PyObject_SetAttrString(self, "args", args);
409 if (status < 0) {
410 Py_DECREF(args);
411 return NULL;
412 }
413
414 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000415 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000416 case 0:
417 Py_INCREF(Py_None);
418 code = Py_None;
419 break;
420 case 1:
421 code = PySequence_GetItem(args, 0);
422 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000423 case -1:
424 PyErr_Clear();
425 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000426 default:
427 Py_INCREF(args);
428 code = args;
429 break;
430 }
431
432 status = PyObject_SetAttrString(self, "code", code);
433 Py_DECREF(code);
434 Py_DECREF(args);
435 if (status < 0)
436 return NULL;
437
438 Py_INCREF(Py_None);
439 return Py_None;
440}
441
442
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000443static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000444 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000445 {NULL, NULL}
446};
447
448
449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000450PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000451
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000452PyDoc_STRVAR(ImportError__doc__,
453"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000454
455
456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000457PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000458
459
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000460static PyObject *
461EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000462{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000463 PyObject *item0 = NULL;
464 PyObject *item1 = NULL;
465 PyObject *item2 = NULL;
466 PyObject *subslice = NULL;
467 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000468
469 if (!(self = get_self(args)))
470 return NULL;
471
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000472 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000473 return NULL;
474
475 if (PyObject_SetAttrString(self, "args", args) ||
476 PyObject_SetAttrString(self, "errno", Py_None) ||
477 PyObject_SetAttrString(self, "strerror", Py_None) ||
478 PyObject_SetAttrString(self, "filename", Py_None))
479 {
480 goto finally;
481 }
482
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000483 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000484 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000485 /* Where a function has a single filename, such as open() or some
486 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
487 * called, giving a third argument which is the filename. But, so
488 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000489 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000490 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000491 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000492 * we hack args so that it only contains two items. This also
493 * means we need our own __str__() which prints out the filename
494 * when it was supplied.
495 */
496 item0 = PySequence_GetItem(args, 0);
497 item1 = PySequence_GetItem(args, 1);
498 item2 = PySequence_GetItem(args, 2);
499 if (!item0 || !item1 || !item2)
500 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000501
Barry Warsaw675ac282000-05-26 19:05:16 +0000502 if (PyObject_SetAttrString(self, "errno", item0) ||
503 PyObject_SetAttrString(self, "strerror", item1) ||
504 PyObject_SetAttrString(self, "filename", item2))
505 {
506 goto finally;
507 }
508
509 subslice = PySequence_GetSlice(args, 0, 2);
510 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
511 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000512 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000513
514 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000515 /* Used when PyErr_SetFromErrno() is called and no filename
516 * argument is given.
517 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000518 item0 = PySequence_GetItem(args, 0);
519 item1 = PySequence_GetItem(args, 1);
520 if (!item0 || !item1)
521 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000522
Barry Warsaw675ac282000-05-26 19:05:16 +0000523 if (PyObject_SetAttrString(self, "errno", item0) ||
524 PyObject_SetAttrString(self, "strerror", item1))
525 {
526 goto finally;
527 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000528 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000529
530 case -1:
531 PyErr_Clear();
532 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000533 }
534
535 Py_INCREF(Py_None);
536 rtnval = Py_None;
537
538 finally:
539 Py_DECREF(args);
540 Py_XDECREF(item0);
541 Py_XDECREF(item1);
542 Py_XDECREF(item2);
543 Py_XDECREF(subslice);
544 return rtnval;
545}
546
547
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000548static PyObject *
549EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000550{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000551 PyObject *originalself = self;
552 PyObject *filename;
553 PyObject *serrno;
554 PyObject *strerror;
555 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000556
Fred Drake1aba5772000-08-15 15:46:16 +0000557 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000558 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000559
Barry Warsaw675ac282000-05-26 19:05:16 +0000560 filename = PyObject_GetAttrString(self, "filename");
561 serrno = PyObject_GetAttrString(self, "errno");
562 strerror = PyObject_GetAttrString(self, "strerror");
563 if (!filename || !serrno || !strerror)
564 goto finally;
565
566 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000567 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
568 PyObject *repr = PyObject_Repr(filename);
569 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000570
571 if (!fmt || !repr || !tuple) {
572 Py_XDECREF(fmt);
573 Py_XDECREF(repr);
574 Py_XDECREF(tuple);
575 goto finally;
576 }
577
578 PyTuple_SET_ITEM(tuple, 0, serrno);
579 PyTuple_SET_ITEM(tuple, 1, strerror);
580 PyTuple_SET_ITEM(tuple, 2, repr);
581
582 rtnval = PyString_Format(fmt, tuple);
583
584 Py_DECREF(fmt);
585 Py_DECREF(tuple);
586 /* already freed because tuple owned only reference */
587 serrno = NULL;
588 strerror = NULL;
589 }
590 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000591 PyObject *fmt = PyString_FromString("[Errno %s] %s");
592 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000593
594 if (!fmt || !tuple) {
595 Py_XDECREF(fmt);
596 Py_XDECREF(tuple);
597 goto finally;
598 }
599
600 PyTuple_SET_ITEM(tuple, 0, serrno);
601 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000602
Barry Warsaw675ac282000-05-26 19:05:16 +0000603 rtnval = PyString_Format(fmt, tuple);
604
605 Py_DECREF(fmt);
606 Py_DECREF(tuple);
607 /* already freed because tuple owned only reference */
608 serrno = NULL;
609 strerror = NULL;
610 }
611 else
612 /* The original Python code said:
613 *
614 * return StandardError.__str__(self)
615 *
616 * but there is no StandardError__str__() function; we happen to
617 * know that's just a pass through to Exception__str__().
618 */
619 rtnval = Exception__str__(originalself, args);
620
621 finally:
622 Py_XDECREF(filename);
623 Py_XDECREF(serrno);
624 Py_XDECREF(strerror);
625 return rtnval;
626}
627
628
629static
630PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000631 {"__init__", EnvironmentError__init__, METH_VARARGS},
632 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000633 {NULL, NULL}
634};
635
636
637
638
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000639PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000640
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000641PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000642
643#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000644PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000645#endif /* MS_WINDOWS */
646
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000647#ifdef __VMS
648static char
649VMSError__doc__[] = "OpenVMS OS system call failed.";
650#endif
651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000652PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000653
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000654PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000655
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000656PyDoc_STRVAR(NotImplementedError__doc__,
657"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000658
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000659PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000661PyDoc_STRVAR(UnboundLocalError__doc__,
662"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000664PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000665
666
667
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000668PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000669
670
671static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000672SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000673{
Barry Warsaw87bec352000-08-18 05:05:37 +0000674 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000675 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000676
677 /* Additional class-creation time initializations */
678 if (!emptystring ||
679 PyObject_SetAttrString(klass, "msg", emptystring) ||
680 PyObject_SetAttrString(klass, "filename", Py_None) ||
681 PyObject_SetAttrString(klass, "lineno", Py_None) ||
682 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000683 PyObject_SetAttrString(klass, "text", Py_None) ||
684 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000685 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000686 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000687 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000688 Py_XDECREF(emptystring);
689 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000690}
691
692
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000693static PyObject *
694SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000695{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000696 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000697 int lenargs;
698
699 if (!(self = get_self(args)))
700 return NULL;
701
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000702 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000703 return NULL;
704
705 if (PyObject_SetAttrString(self, "args", args))
706 goto finally;
707
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000708 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000709 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000710 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000711 int status;
712
713 if (!item0)
714 goto finally;
715 status = PyObject_SetAttrString(self, "msg", item0);
716 Py_DECREF(item0);
717 if (status)
718 goto finally;
719 }
720 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000721 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000722 PyObject *filename = NULL, *lineno = NULL;
723 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000724 int status = 1;
725
726 if (!info)
727 goto finally;
728
729 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000730 if (filename != NULL) {
731 lineno = PySequence_GetItem(info, 1);
732 if (lineno != NULL) {
733 offset = PySequence_GetItem(info, 2);
734 if (offset != NULL) {
735 text = PySequence_GetItem(info, 3);
736 if (text != NULL) {
737 status =
738 PyObject_SetAttrString(self, "filename", filename)
739 || PyObject_SetAttrString(self, "lineno", lineno)
740 || PyObject_SetAttrString(self, "offset", offset)
741 || PyObject_SetAttrString(self, "text", text);
742 Py_DECREF(text);
743 }
744 Py_DECREF(offset);
745 }
746 Py_DECREF(lineno);
747 }
748 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000749 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000750 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000751
752 if (status)
753 goto finally;
754 }
755 Py_INCREF(Py_None);
756 rtnval = Py_None;
757
758 finally:
759 Py_DECREF(args);
760 return rtnval;
761}
762
763
Fred Drake185a29b2000-08-15 16:20:36 +0000764/* This is called "my_basename" instead of just "basename" to avoid name
765 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
766 defined, and Python does define that. */
767static char *
768my_basename(char *name)
769{
770 char *cp = name;
771 char *result = name;
772
773 if (name == NULL)
774 return "???";
775 while (*cp != '\0') {
776 if (*cp == SEP)
777 result = cp + 1;
778 ++cp;
779 }
780 return result;
781}
782
783
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000784static PyObject *
785SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000786{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000787 PyObject *msg;
788 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000789 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000790
Fred Drake1aba5772000-08-15 15:46:16 +0000791 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000792 return NULL;
793
794 if (!(msg = PyObject_GetAttrString(self, "msg")))
795 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000796
Barry Warsaw675ac282000-05-26 19:05:16 +0000797 str = PyObject_Str(msg);
798 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000799 result = str;
800
801 /* XXX -- do all the additional formatting with filename and
802 lineno here */
803
Guido van Rossum602d4512002-09-03 20:24:09 +0000804 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000805 int have_filename = 0;
806 int have_lineno = 0;
807 char *buffer = NULL;
808
Barry Warsaw77c9f502000-08-16 19:43:17 +0000809 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000810 have_filename = PyString_Check(filename);
811 else
812 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000813
814 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000815 have_lineno = PyInt_Check(lineno);
816 else
817 PyErr_Clear();
818
819 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000820 int bufsize = PyString_GET_SIZE(str) + 64;
821 if (have_filename)
822 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000823
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000824 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000825 if (buffer != NULL) {
826 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000827 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
828 PyString_AS_STRING(str),
829 my_basename(PyString_AS_STRING(filename)),
830 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000831 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000832 PyOS_snprintf(buffer, bufsize, "%s (%s)",
833 PyString_AS_STRING(str),
834 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000835 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000836 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
837 PyString_AS_STRING(str),
838 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000839
Fred Drake1aba5772000-08-15 15:46:16 +0000840 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000841 PyMem_FREE(buffer);
842
Fred Drake1aba5772000-08-15 15:46:16 +0000843 if (result == NULL)
844 result = str;
845 else
846 Py_DECREF(str);
847 }
848 }
849 Py_XDECREF(filename);
850 Py_XDECREF(lineno);
851 }
852 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000853}
854
855
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000856static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000857 {"__init__", SyntaxError__init__, METH_VARARGS},
858 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000859 {NULL, NULL}
860};
861
862
Guido van Rossum602d4512002-09-03 20:24:09 +0000863static PyObject *
864KeyError__str__(PyObject *self, PyObject *args)
865{
866 PyObject *argsattr;
867 PyObject *result;
868
869 if (!PyArg_ParseTuple(args, "O:__str__", &self))
870 return NULL;
871
872 if (!(argsattr = PyObject_GetAttrString(self, "args")))
873 return NULL;
874
875 /* If args is a tuple of exactly one item, apply repr to args[0].
876 This is done so that e.g. the exception raised by {}[''] prints
877 KeyError: ''
878 rather than the confusing
879 KeyError
880 alone. The downside is that if KeyError is raised with an explanatory
881 string, that string will be displayed in quotes. Too bad.
882 If args is anything else, use the default Exception__str__().
883 */
884 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
885 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
886 result = PyObject_Repr(key);
887 }
888 else
889 result = Exception__str__(self, args);
890
891 Py_DECREF(argsattr);
892 return result;
893}
894
895static PyMethodDef KeyError_methods[] = {
896 {"__str__", KeyError__str__, METH_VARARGS},
897 {NULL, NULL}
898};
899
900
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000901#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000902static
903int get_int(PyObject *exc, const char *name, int *value)
904{
905 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
906
907 if (!attr)
908 return -1;
909 if (!PyInt_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000910 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000911 Py_DECREF(attr);
912 return -1;
913 }
914 *value = PyInt_AS_LONG(attr);
915 Py_DECREF(attr);
916 return 0;
917}
918
919
920static
921int set_int(PyObject *exc, const char *name, int value)
922{
923 PyObject *obj = PyInt_FromLong(value);
924 int result;
925
926 if (!obj)
927 return -1;
928 result = PyObject_SetAttrString(exc, (char *)name, obj);
929 Py_DECREF(obj);
930 return result;
931}
932
933
934static
935PyObject *get_string(PyObject *exc, const char *name)
936{
937 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
938
939 if (!attr)
940 return NULL;
941 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000942 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000943 Py_DECREF(attr);
944 return NULL;
945 }
946 return attr;
947}
948
949
950static
951int set_string(PyObject *exc, const char *name, const char *value)
952{
953 PyObject *obj = PyString_FromString(value);
954 int result;
955
956 if (!obj)
957 return -1;
958 result = PyObject_SetAttrString(exc, (char *)name, obj);
959 Py_DECREF(obj);
960 return result;
961}
962
963
964static
965PyObject *get_unicode(PyObject *exc, const char *name)
966{
967 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
968
969 if (!attr)
970 return NULL;
971 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000972 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000973 Py_DECREF(attr);
974 return NULL;
975 }
976 return attr;
977}
978
979PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
980{
981 return get_string(exc, "encoding");
982}
983
984PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
985{
986 return get_string(exc, "encoding");
987}
988
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000989PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
990{
991 return get_unicode(exc, "object");
992}
993
994PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
995{
996 return get_string(exc, "object");
997}
998
999PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
1000{
1001 return get_unicode(exc, "object");
1002}
1003
1004int PyUnicodeEncodeError_GetStart(PyObject *exc, int *start)
1005{
1006 if (!get_int(exc, "start", start)) {
1007 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
1008 int size;
1009 if (!object)
1010 return -1;
1011 size = PyUnicode_GET_SIZE(object);
1012 if (*start<0)
1013 *start = 0;
1014 if (*start>=size)
1015 *start = size-1;
1016 Py_DECREF(object);
1017 return 0;
1018 }
1019 return -1;
1020}
1021
1022
1023int PyUnicodeDecodeError_GetStart(PyObject *exc, int *start)
1024{
1025 if (!get_int(exc, "start", start)) {
1026 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
1027 int size;
1028 if (!object)
1029 return -1;
1030 size = PyString_GET_SIZE(object);
1031 if (*start<0)
1032 *start = 0;
1033 if (*start>=size)
1034 *start = size-1;
1035 Py_DECREF(object);
1036 return 0;
1037 }
1038 return -1;
1039}
1040
1041
1042int PyUnicodeTranslateError_GetStart(PyObject *exc, int *start)
1043{
1044 return PyUnicodeEncodeError_GetStart(exc, start);
1045}
1046
1047
1048int PyUnicodeEncodeError_SetStart(PyObject *exc, int start)
1049{
1050 return set_int(exc, "start", start);
1051}
1052
1053
1054int PyUnicodeDecodeError_SetStart(PyObject *exc, int start)
1055{
1056 return set_int(exc, "start", start);
1057}
1058
1059
1060int PyUnicodeTranslateError_SetStart(PyObject *exc, int start)
1061{
1062 return set_int(exc, "start", start);
1063}
1064
1065
1066int PyUnicodeEncodeError_GetEnd(PyObject *exc, int *end)
1067{
1068 if (!get_int(exc, "end", end)) {
1069 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
1070 int size;
1071 if (!object)
1072 return -1;
1073 size = PyUnicode_GET_SIZE(object);
1074 if (*end<1)
1075 *end = 1;
1076 if (*end>size)
1077 *end = size;
1078 Py_DECREF(object);
1079 return 0;
1080 }
1081 return -1;
1082}
1083
1084
1085int PyUnicodeDecodeError_GetEnd(PyObject *exc, int *end)
1086{
1087 if (!get_int(exc, "end", end)) {
1088 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
1089 int size;
1090 if (!object)
1091 return -1;
1092 size = PyString_GET_SIZE(object);
1093 if (*end<1)
1094 *end = 1;
1095 if (*end>size)
1096 *end = size;
1097 Py_DECREF(object);
1098 return 0;
1099 }
1100 return -1;
1101}
1102
1103
1104int PyUnicodeTranslateError_GetEnd(PyObject *exc, int *start)
1105{
1106 return PyUnicodeEncodeError_GetEnd(exc, start);
1107}
1108
1109
1110int PyUnicodeEncodeError_SetEnd(PyObject *exc, int end)
1111{
1112 return set_int(exc, "end", end);
1113}
1114
1115
1116int PyUnicodeDecodeError_SetEnd(PyObject *exc, int end)
1117{
1118 return set_int(exc, "end", end);
1119}
1120
1121
1122int PyUnicodeTranslateError_SetEnd(PyObject *exc, int end)
1123{
1124 return set_int(exc, "end", end);
1125}
1126
1127
1128PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1129{
1130 return get_string(exc, "reason");
1131}
1132
1133
1134PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1135{
1136 return get_string(exc, "reason");
1137}
1138
1139
1140PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1141{
1142 return get_string(exc, "reason");
1143}
1144
1145
1146int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1147{
1148 return set_string(exc, "reason", reason);
1149}
1150
1151
1152int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1153{
1154 return set_string(exc, "reason", reason);
1155}
1156
1157
1158int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1159{
1160 return set_string(exc, "reason", reason);
1161}
1162
1163
1164static PyObject *
1165UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1166{
1167 PyObject *rtnval = NULL;
1168 PyObject *encoding;
1169 PyObject *object;
1170 PyObject *start;
1171 PyObject *end;
1172 PyObject *reason;
1173
1174 if (!(self = get_self(args)))
1175 return NULL;
1176
1177 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1178 return NULL;
1179
1180 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1181 &PyString_Type, &encoding,
1182 objecttype, &object,
1183 &PyInt_Type, &start,
1184 &PyInt_Type, &end,
1185 &PyString_Type, &reason))
1186 return NULL;
1187
1188 if (PyObject_SetAttrString(self, "args", args))
1189 goto finally;
1190
1191 if (PyObject_SetAttrString(self, "encoding", encoding))
1192 goto finally;
1193 if (PyObject_SetAttrString(self, "object", object))
1194 goto finally;
1195 if (PyObject_SetAttrString(self, "start", start))
1196 goto finally;
1197 if (PyObject_SetAttrString(self, "end", end))
1198 goto finally;
1199 if (PyObject_SetAttrString(self, "reason", reason))
1200 goto finally;
1201
1202 Py_INCREF(Py_None);
1203 rtnval = Py_None;
1204
1205 finally:
1206 Py_DECREF(args);
1207 return rtnval;
1208}
1209
1210
1211static PyObject *
1212UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1213{
1214 return UnicodeError__init__(self, args, &PyUnicode_Type);
1215}
1216
1217static PyObject *
1218UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1219{
1220 PyObject *encodingObj = NULL;
1221 PyObject *objectObj = NULL;
1222 int length;
1223 int start;
1224 int end;
1225 PyObject *reasonObj = NULL;
1226 char buffer[1000];
1227 PyObject *result = NULL;
1228
1229 self = arg;
1230
1231 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1232 goto error;
1233
1234 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1235 goto error;
1236
1237 length = PyUnicode_GET_SIZE(objectObj);
1238
1239 if (PyUnicodeEncodeError_GetStart(self, &start))
1240 goto error;
1241
1242 if (PyUnicodeEncodeError_GetEnd(self, &end))
1243 goto error;
1244
1245 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1246 goto error;
1247
1248 if (end==start+1) {
1249 PyOS_snprintf(buffer, sizeof(buffer),
1250 "'%.400s' codec can't encode character '\\u%x' in position %d: %.400s",
1251 PyString_AS_STRING(encodingObj),
1252 (int)PyUnicode_AS_UNICODE(objectObj)[start],
1253 start,
1254 PyString_AS_STRING(reasonObj)
1255 );
1256 }
1257 else {
1258 PyOS_snprintf(buffer, sizeof(buffer),
1259 "'%.400s' codec can't encode characters in position %d-%d: %.400s",
1260 PyString_AS_STRING(encodingObj),
1261 start,
1262 end-1,
1263 PyString_AS_STRING(reasonObj)
1264 );
1265 }
1266 result = PyString_FromString(buffer);
1267
1268error:
1269 Py_XDECREF(reasonObj);
1270 Py_XDECREF(objectObj);
1271 Py_XDECREF(encodingObj);
1272 return result;
1273}
1274
1275static PyMethodDef UnicodeEncodeError_methods[] = {
1276 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1277 {"__str__", UnicodeEncodeError__str__, METH_O},
1278 {NULL, NULL}
1279};
1280
1281
1282PyObject * PyUnicodeEncodeError_Create(
1283 const char *encoding, const Py_UNICODE *object, int length,
1284 int start, int end, const char *reason)
1285{
1286 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#iis",
1287 encoding, object, length, start, end, reason);
1288}
1289
1290
1291static PyObject *
1292UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1293{
1294 return UnicodeError__init__(self, args, &PyString_Type);
1295}
1296
1297static PyObject *
1298UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1299{
1300 PyObject *encodingObj = NULL;
1301 PyObject *objectObj = NULL;
1302 int length;
1303 int start;
1304 int end;
1305 PyObject *reasonObj = NULL;
1306 char buffer[1000];
1307 PyObject *result = NULL;
1308
1309 self = arg;
1310
1311 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1312 goto error;
1313
1314 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1315 goto error;
1316
1317 length = PyString_GET_SIZE(objectObj);
1318
1319 if (PyUnicodeDecodeError_GetStart(self, &start))
1320 goto error;
1321
1322 if (PyUnicodeDecodeError_GetEnd(self, &end))
1323 goto error;
1324
1325 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1326 goto error;
1327
1328 if (end==start+1) {
1329 PyOS_snprintf(buffer, sizeof(buffer),
1330 "'%.400s' codec can't decode byte 0x%x in position %d: %.400s",
1331 PyString_AS_STRING(encodingObj),
1332 ((int)PyString_AS_STRING(objectObj)[start])&0xff,
1333 start,
1334 PyString_AS_STRING(reasonObj)
1335 );
1336 }
1337 else {
1338 PyOS_snprintf(buffer, sizeof(buffer),
1339 "'%.400s' codec can't decode bytes in position %d-%d: %.400s",
1340 PyString_AS_STRING(encodingObj),
1341 start,
1342 end-1,
1343 PyString_AS_STRING(reasonObj)
1344 );
1345 }
1346 result = PyString_FromString(buffer);
1347
1348error:
1349 Py_XDECREF(reasonObj);
1350 Py_XDECREF(objectObj);
1351 Py_XDECREF(encodingObj);
1352 return result;
1353}
1354
1355static PyMethodDef UnicodeDecodeError_methods[] = {
1356 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1357 {"__str__", UnicodeDecodeError__str__, METH_O},
1358 {NULL, NULL}
1359};
1360
1361
1362PyObject * PyUnicodeDecodeError_Create(
1363 const char *encoding, const char *object, int length,
1364 int start, int end, const char *reason)
1365{
1366 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
1367 encoding, object, length, start, end, reason);
1368}
1369
1370
1371static PyObject *
1372UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1373{
1374 PyObject *rtnval = NULL;
1375 PyObject *object;
1376 PyObject *start;
1377 PyObject *end;
1378 PyObject *reason;
1379
1380 if (!(self = get_self(args)))
1381 return NULL;
1382
1383 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1384 return NULL;
1385
1386 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1387 &PyUnicode_Type, &object,
1388 &PyInt_Type, &start,
1389 &PyInt_Type, &end,
1390 &PyString_Type, &reason))
1391 goto finally;
1392
1393 if (PyObject_SetAttrString(self, "args", args))
1394 goto finally;
1395
1396 if (PyObject_SetAttrString(self, "object", object))
1397 goto finally;
1398 if (PyObject_SetAttrString(self, "start", start))
1399 goto finally;
1400 if (PyObject_SetAttrString(self, "end", end))
1401 goto finally;
1402 if (PyObject_SetAttrString(self, "reason", reason))
1403 goto finally;
1404
1405 Py_INCREF(Py_None);
1406 rtnval = Py_None;
1407
1408 finally:
1409 Py_DECREF(args);
1410 return rtnval;
1411}
1412
1413
1414static PyObject *
1415UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1416{
1417 PyObject *objectObj = NULL;
1418 int length;
1419 int start;
1420 int end;
1421 PyObject *reasonObj = NULL;
1422 char buffer[1000];
1423 PyObject *result = NULL;
1424
1425 self = arg;
1426
1427 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1428 goto error;
1429
1430 length = PyUnicode_GET_SIZE(objectObj);
1431
1432 if (PyUnicodeTranslateError_GetStart(self, &start))
1433 goto error;
1434
1435 if (PyUnicodeTranslateError_GetEnd(self, &end))
1436 goto error;
1437
1438 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1439 goto error;
1440
1441 if (end==start+1) {
1442 PyOS_snprintf(buffer, sizeof(buffer),
1443 "can't translate character '\\u%x' in position %d: %.400s",
1444 (int)PyUnicode_AS_UNICODE(objectObj)[start],
1445 start,
1446 PyString_AS_STRING(reasonObj)
1447 );
1448 }
1449 else {
1450 PyOS_snprintf(buffer, sizeof(buffer),
1451 "can't translate characters in position %d-%d: %.400s",
1452 start,
1453 end-1,
1454 PyString_AS_STRING(reasonObj)
1455 );
1456 }
1457 result = PyString_FromString(buffer);
1458
1459error:
1460 Py_XDECREF(reasonObj);
1461 Py_XDECREF(objectObj);
1462 return result;
1463}
1464
1465static PyMethodDef UnicodeTranslateError_methods[] = {
1466 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1467 {"__str__", UnicodeTranslateError__str__, METH_O},
1468 {NULL, NULL}
1469};
1470
1471
1472PyObject * PyUnicodeTranslateError_Create(
1473 const Py_UNICODE *object, int length,
1474 int start, int end, const char *reason)
1475{
1476 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
1477 object, length, start, end, reason);
1478}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001479#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001480
1481
Barry Warsaw675ac282000-05-26 19:05:16 +00001482
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001483/* Exception doc strings */
1484
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001485PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001487PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001488
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001489PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001490
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001491PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001492
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001493PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001495PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001496
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001497PyDoc_STRVAR(ZeroDivisionError__doc__,
1498"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001499
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001500PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001502PyDoc_STRVAR(ValueError__doc__,
1503"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001504
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001505PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001506
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001507#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001508PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1509
1510PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1511
1512PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001513#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001515PyDoc_STRVAR(SystemError__doc__,
1516"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001517\n\
1518Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001519the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001520
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001521PyDoc_STRVAR(ReferenceError__doc__,
1522"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001523
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001524PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001526PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001528PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001529
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001530/* Warning category docstrings */
1531
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001532PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001534PyDoc_STRVAR(UserWarning__doc__,
1535"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001536
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001537PyDoc_STRVAR(DeprecationWarning__doc__,
1538"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001539
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001540PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001541"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001542"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001543
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001544PyDoc_STRVAR(SyntaxWarning__doc__,
1545"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001546
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001547PyDoc_STRVAR(OverflowWarning__doc__,
1548"Base class for warnings about numeric overflow.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001550PyDoc_STRVAR(RuntimeWarning__doc__,
1551"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001552
Barry Warsaw9f007392002-08-14 15:51:29 +00001553PyDoc_STRVAR(FutureWarning__doc__,
1554"Base class for warnings about constructs that will change semantically "
1555"in the future.");
1556
Barry Warsaw675ac282000-05-26 19:05:16 +00001557
1558
1559/* module global functions */
1560static PyMethodDef functions[] = {
1561 /* Sentinel */
1562 {NULL, NULL}
1563};
1564
1565
1566
1567/* Global C API defined exceptions */
1568
1569PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001570PyObject *PyExc_StopIteration;
Barry Warsaw675ac282000-05-26 19:05:16 +00001571PyObject *PyExc_StandardError;
1572PyObject *PyExc_ArithmeticError;
1573PyObject *PyExc_LookupError;
1574
1575PyObject *PyExc_AssertionError;
1576PyObject *PyExc_AttributeError;
1577PyObject *PyExc_EOFError;
1578PyObject *PyExc_FloatingPointError;
1579PyObject *PyExc_EnvironmentError;
1580PyObject *PyExc_IOError;
1581PyObject *PyExc_OSError;
1582PyObject *PyExc_ImportError;
1583PyObject *PyExc_IndexError;
1584PyObject *PyExc_KeyError;
1585PyObject *PyExc_KeyboardInterrupt;
1586PyObject *PyExc_MemoryError;
1587PyObject *PyExc_NameError;
1588PyObject *PyExc_OverflowError;
1589PyObject *PyExc_RuntimeError;
1590PyObject *PyExc_NotImplementedError;
1591PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001592PyObject *PyExc_IndentationError;
1593PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001594PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001595PyObject *PyExc_SystemError;
1596PyObject *PyExc_SystemExit;
1597PyObject *PyExc_UnboundLocalError;
1598PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001599PyObject *PyExc_UnicodeEncodeError;
1600PyObject *PyExc_UnicodeDecodeError;
1601PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001602PyObject *PyExc_TypeError;
1603PyObject *PyExc_ValueError;
1604PyObject *PyExc_ZeroDivisionError;
1605#ifdef MS_WINDOWS
1606PyObject *PyExc_WindowsError;
1607#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001608#ifdef __VMS
1609PyObject *PyExc_VMSError;
1610#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001611
1612/* Pre-computed MemoryError instance. Best to create this as early as
1613 * possibly and not wait until a MemoryError is actually raised!
1614 */
1615PyObject *PyExc_MemoryErrorInst;
1616
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001617/* Predefined warning categories */
1618PyObject *PyExc_Warning;
1619PyObject *PyExc_UserWarning;
1620PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001621PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001622PyObject *PyExc_SyntaxWarning;
Guido van Rossumae347b32001-08-23 02:56:07 +00001623PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001624PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001625PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001626
Barry Warsaw675ac282000-05-26 19:05:16 +00001627
1628
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001629/* mapping between exception names and their PyObject ** */
1630static struct {
1631 char *name;
1632 PyObject **exc;
1633 PyObject **base; /* NULL == PyExc_StandardError */
1634 char *docstr;
1635 PyMethodDef *methods;
1636 int (*classinit)(PyObject *);
1637} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001638 /*
1639 * The first three classes MUST appear in exactly this order
1640 */
1641 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001642 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1643 StopIteration__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001644 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1645 StandardError__doc__},
1646 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1647 /*
1648 * The rest appear in depth-first order of the hierarchy
1649 */
1650 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1651 SystemExit_methods},
1652 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1653 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1654 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1655 EnvironmentError_methods},
1656 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1657 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1658#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001659 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001660 WindowsError__doc__},
1661#endif /* MS_WINDOWS */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001662#ifdef __VMS
1663 {"VMSError", &PyExc_VMSError, &PyExc_OSError,
1664 VMSError__doc__},
1665#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001666 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1667 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1668 {"NotImplementedError", &PyExc_NotImplementedError,
1669 &PyExc_RuntimeError, NotImplementedError__doc__},
1670 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1671 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1672 UnboundLocalError__doc__},
1673 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1674 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1675 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001676 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1677 IndentationError__doc__},
1678 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1679 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001680 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1681 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1682 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1683 IndexError__doc__},
1684 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001685 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001686 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1687 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1688 OverflowError__doc__},
1689 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1690 ZeroDivisionError__doc__},
1691 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1692 FloatingPointError__doc__},
1693 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1694 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001695#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001696 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1697 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1698 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1699 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1700 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1701 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001702#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001703 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001704 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1705 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001706 /* Warning categories */
1707 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1708 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1709 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1710 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001711 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1712 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001713 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Guido van Rossumae347b32001-08-23 02:56:07 +00001714 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1715 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001716 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1717 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001718 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1719 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001720 /* Sentinel */
1721 {NULL}
1722};
1723
1724
1725
Mark Hammonda2905272002-07-29 13:42:14 +00001726void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001727_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001728{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001729 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001730 int modnamesz = strlen(modulename);
1731 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001732 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001733
Tim Peters6d6c1a32001-08-02 04:15:00 +00001734 me = Py_InitModule(modulename, functions);
1735 if (me == NULL)
1736 goto err;
1737 mydict = PyModule_GetDict(me);
1738 if (mydict == NULL)
1739 goto err;
1740 bltinmod = PyImport_ImportModule("__builtin__");
1741 if (bltinmod == NULL)
1742 goto err;
1743 bdict = PyModule_GetDict(bltinmod);
1744 if (bdict == NULL)
1745 goto err;
1746 doc = PyString_FromString(module__doc__);
1747 if (doc == NULL)
1748 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001749
Tim Peters6d6c1a32001-08-02 04:15:00 +00001750 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001751 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001752 if (i < 0) {
1753 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001754 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001755 return;
1756 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001757
1758 /* This is the base class of all exceptions, so make it first. */
1759 if (make_Exception(modulename) ||
1760 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1761 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1762 {
1763 Py_FatalError("Base class `Exception' could not be created.");
1764 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001765
Barry Warsaw675ac282000-05-26 19:05:16 +00001766 /* Now we can programmatically create all the remaining exceptions.
1767 * Remember to start the loop at 1 to skip Exceptions.
1768 */
1769 for (i=1; exctable[i].name; i++) {
1770 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001771 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1772 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001773
1774 (void)strcpy(cname, modulename);
1775 (void)strcat(cname, ".");
1776 (void)strcat(cname, exctable[i].name);
1777
1778 if (exctable[i].base == 0)
1779 base = PyExc_StandardError;
1780 else
1781 base = *exctable[i].base;
1782
1783 status = make_class(exctable[i].exc, base, cname,
1784 exctable[i].methods,
1785 exctable[i].docstr);
1786
1787 PyMem_DEL(cname);
1788
1789 if (status)
1790 Py_FatalError("Standard exception classes could not be created.");
1791
1792 if (exctable[i].classinit) {
1793 status = (*exctable[i].classinit)(*exctable[i].exc);
1794 if (status)
1795 Py_FatalError("An exception class could not be initialized.");
1796 }
1797
1798 /* Now insert the class into both this module and the __builtin__
1799 * module.
1800 */
1801 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1802 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1803 {
1804 Py_FatalError("Module dictionary insertion problem.");
1805 }
1806 }
1807
1808 /* Now we need to pre-allocate a MemoryError instance */
1809 args = Py_BuildValue("()");
1810 if (!args ||
1811 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1812 {
1813 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1814 }
1815 Py_DECREF(args);
1816
1817 /* We're done with __builtin__ */
1818 Py_DECREF(bltinmod);
1819}
1820
1821
Mark Hammonda2905272002-07-29 13:42:14 +00001822void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001823_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001824{
1825 int i;
1826
1827 Py_XDECREF(PyExc_MemoryErrorInst);
1828 PyExc_MemoryErrorInst = NULL;
1829
1830 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001831 /* clear the class's dictionary, freeing up circular references
1832 * between the class and its methods.
1833 */
1834 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1835 PyDict_Clear(cdict);
1836 Py_DECREF(cdict);
1837
1838 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001839 Py_XDECREF(*exctable[i].exc);
1840 *exctable[i].exc = NULL;
1841 }
1842}