blob: 0080694cd8f4ceb93b406a41a66fba769e56d557 [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{
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000129 PyObject *module;
130 int status = -1;
131
Barry Warsaw675ac282000-05-26 19:05:16 +0000132 if (!methods)
133 return 0;
134
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000135 module = PyString_FromString("exceptions");
136 if (!module)
137 return 0;
Barry Warsaw675ac282000-05-26 19:05:16 +0000138 while (methods->ml_name) {
139 /* get a wrapper for the built-in function */
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000140 PyObject *func = PyCFunction_NewEx(methods, NULL, module);
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000141 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +0000142
143 if (!func)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000144 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +0000145
146 /* turn the function into an unbound method */
147 if (!(meth = PyMethod_New(func, NULL, klass))) {
148 Py_DECREF(func);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000149 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +0000150 }
Barry Warsaw9667ed22001-01-23 16:08:34 +0000151
Barry Warsaw675ac282000-05-26 19:05:16 +0000152 /* add method to dictionary */
153 status = PyDict_SetItemString(dict, methods->ml_name, meth);
154 Py_DECREF(meth);
155 Py_DECREF(func);
156
157 /* stop now if an error occurred, otherwise do the next method */
158 if (status)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000159 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +0000160
161 methods++;
162 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000163 status = 0;
164 status:
165 Py_DECREF(module);
166 return status;
Barry Warsaw675ac282000-05-26 19:05:16 +0000167}
168
Barry Warsaw9667ed22001-01-23 16:08:34 +0000169
Barry Warsaw675ac282000-05-26 19:05:16 +0000170
171/* This function is used to create all subsequent exception classes. */
172static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000173make_class(PyObject **klass, PyObject *base,
174 char *name, PyMethodDef *methods,
175 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +0000176{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000177 PyObject *dict = PyDict_New();
178 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000179 int status = -1;
180
181 if (!dict)
182 return -1;
183
184 /* If an error occurs from here on, goto finally instead of explicitly
185 * returning NULL.
186 */
187
188 if (docstr) {
189 if (!(str = PyString_FromString(docstr)))
190 goto finally;
191 if (PyDict_SetItemString(dict, "__doc__", str))
192 goto finally;
193 }
194
195 if (!(*klass = PyErr_NewException(name, base, dict)))
196 goto finally;
197
198 if (populate_methods(*klass, dict, methods)) {
199 Py_DECREF(*klass);
200 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000201 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000202 }
203
204 status = 0;
205
206 finally:
207 Py_XDECREF(dict);
208 Py_XDECREF(str);
209 return status;
210}
211
212
213/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000214static PyObject *
215get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000216{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000217 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000218 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000219 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000220 if (PyExc_TypeError) {
221 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000222 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000223 }
224 return NULL;
225 }
226 return self;
227}
228
229
230
231/* Notes on bootstrapping the exception classes.
232 *
233 * First thing we create is the base class for all exceptions, called
234 * appropriately enough: Exception. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000235 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000236 * for TypeError, which can conditionally exist.
237 *
238 * Next, StandardError is created (which is quite simple) followed by
239 * TypeError, because the instantiation of other exceptions can potentially
240 * throw a TypeError. Once these exceptions are created, all the others
241 * can be created in any order. See the static exctable below for the
242 * explicit bootstrap order.
243 *
244 * All classes after Exception can be created using PyErr_NewException().
245 */
246
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000247PyDoc_STRVAR(Exception__doc__, "Common base class for all exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000248
249
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000250static PyObject *
251Exception__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000252{
253 int status;
254
255 if (!(self = get_self(args)))
256 return NULL;
257
258 /* set args attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000259 /* XXX size is only a hint */
260 args = PySequence_GetSlice(args, 1, PySequence_Size(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000261 if (!args)
262 return NULL;
263 status = PyObject_SetAttrString(self, "args", args);
264 Py_DECREF(args);
265 if (status < 0)
266 return NULL;
267
268 Py_INCREF(Py_None);
269 return Py_None;
270}
271
272
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000273static PyObject *
274Exception__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000275{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000276 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000277
Fred Drake1aba5772000-08-15 15:46:16 +0000278 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000279 return NULL;
280
281 args = PyObject_GetAttrString(self, "args");
282 if (!args)
283 return NULL;
284
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000285 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000286 case 0:
287 out = PyString_FromString("");
288 break;
289 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000290 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000291 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000292 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000293 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000294 Py_DECREF(tmp);
295 }
296 else
297 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000298 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000299 }
Guido van Rossum98b2a422002-09-18 04:06:32 +0000300 case -1:
301 PyErr_Clear();
302 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000303 default:
304 out = PyObject_Str(args);
305 break;
306 }
307
308 Py_DECREF(args);
309 return out;
310}
311
312
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000313static PyObject *
314Exception__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000315{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000316 PyObject *out;
317 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000318
Fred Drake1aba5772000-08-15 15:46:16 +0000319 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000320 return NULL;
321
322 args = PyObject_GetAttrString(self, "args");
323 if (!args)
324 return NULL;
325
326 out = PyObject_GetItem(args, index);
327 Py_DECREF(args);
328 return out;
329}
330
331
332static PyMethodDef
333Exception_methods[] = {
334 /* methods for the Exception class */
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000335 { "__getitem__", Exception__getitem__, METH_VARARGS},
336 { "__str__", Exception__str__, METH_VARARGS},
337 { "__init__", Exception__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000338 { NULL, NULL }
339};
340
341
342static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000343make_Exception(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000344{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000345 PyObject *dict = PyDict_New();
346 PyObject *str = NULL;
347 PyObject *name = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000348 int status = -1;
349
350 if (!dict)
351 return -1;
352
353 /* If an error occurs from here on, goto finally instead of explicitly
354 * returning NULL.
355 */
356
357 if (!(str = PyString_FromString(modulename)))
358 goto finally;
359 if (PyDict_SetItemString(dict, "__module__", str))
360 goto finally;
361 Py_DECREF(str);
362 if (!(str = PyString_FromString(Exception__doc__)))
363 goto finally;
364 if (PyDict_SetItemString(dict, "__doc__", str))
365 goto finally;
366
367 if (!(name = PyString_FromString("Exception")))
368 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000369
Barry Warsaw675ac282000-05-26 19:05:16 +0000370 if (!(PyExc_Exception = PyClass_New(NULL, dict, name)))
371 goto finally;
372
373 /* Now populate the dictionary with the method suite */
374 if (populate_methods(PyExc_Exception, dict, Exception_methods))
375 /* Don't need to reclaim PyExc_Exception here because that'll
376 * happen during interpreter shutdown.
377 */
378 goto finally;
379
380 status = 0;
381
382 finally:
383 Py_XDECREF(dict);
384 Py_XDECREF(str);
385 Py_XDECREF(name);
386 return status;
387}
388
389
390
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000391PyDoc_STRVAR(StandardError__doc__,
392"Base class for all standard Python exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000393
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000394PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000395
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000396PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000397
Barry Warsaw675ac282000-05-26 19:05:16 +0000398
399
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000400PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000401
402
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000403static PyObject *
404SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000405{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000406 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000407 int status;
408
409 if (!(self = get_self(args)))
410 return NULL;
411
412 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000413 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000414 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000415
Barry Warsaw675ac282000-05-26 19:05:16 +0000416 status = PyObject_SetAttrString(self, "args", args);
417 if (status < 0) {
418 Py_DECREF(args);
419 return NULL;
420 }
421
422 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000423 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000424 case 0:
425 Py_INCREF(Py_None);
426 code = Py_None;
427 break;
428 case 1:
429 code = PySequence_GetItem(args, 0);
430 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000431 case -1:
432 PyErr_Clear();
433 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000434 default:
435 Py_INCREF(args);
436 code = args;
437 break;
438 }
439
440 status = PyObject_SetAttrString(self, "code", code);
441 Py_DECREF(code);
442 Py_DECREF(args);
443 if (status < 0)
444 return NULL;
445
446 Py_INCREF(Py_None);
447 return Py_None;
448}
449
450
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000451static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000452 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000453 {NULL, NULL}
454};
455
456
457
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000458PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000460PyDoc_STRVAR(ImportError__doc__,
461"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000462
463
464
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000465PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000466
467
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000468static PyObject *
469EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000470{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000471 PyObject *item0 = NULL;
472 PyObject *item1 = NULL;
473 PyObject *item2 = NULL;
474 PyObject *subslice = NULL;
475 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000476
477 if (!(self = get_self(args)))
478 return NULL;
479
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000480 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000481 return NULL;
482
483 if (PyObject_SetAttrString(self, "args", args) ||
484 PyObject_SetAttrString(self, "errno", Py_None) ||
485 PyObject_SetAttrString(self, "strerror", Py_None) ||
486 PyObject_SetAttrString(self, "filename", Py_None))
487 {
488 goto finally;
489 }
490
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000491 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000492 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000493 /* Where a function has a single filename, such as open() or some
494 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
495 * called, giving a third argument which is the filename. But, so
496 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000497 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000498 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000499 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000500 * we hack args so that it only contains two items. This also
501 * means we need our own __str__() which prints out the filename
502 * when it was supplied.
503 */
504 item0 = PySequence_GetItem(args, 0);
505 item1 = PySequence_GetItem(args, 1);
506 item2 = PySequence_GetItem(args, 2);
507 if (!item0 || !item1 || !item2)
508 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000509
Barry Warsaw675ac282000-05-26 19:05:16 +0000510 if (PyObject_SetAttrString(self, "errno", item0) ||
511 PyObject_SetAttrString(self, "strerror", item1) ||
512 PyObject_SetAttrString(self, "filename", item2))
513 {
514 goto finally;
515 }
516
517 subslice = PySequence_GetSlice(args, 0, 2);
518 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
519 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000520 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000521
522 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000523 /* Used when PyErr_SetFromErrno() is called and no filename
524 * argument is given.
525 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000526 item0 = PySequence_GetItem(args, 0);
527 item1 = PySequence_GetItem(args, 1);
528 if (!item0 || !item1)
529 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000530
Barry Warsaw675ac282000-05-26 19:05:16 +0000531 if (PyObject_SetAttrString(self, "errno", item0) ||
532 PyObject_SetAttrString(self, "strerror", item1))
533 {
534 goto finally;
535 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000536 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000537
538 case -1:
539 PyErr_Clear();
540 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000541 }
542
543 Py_INCREF(Py_None);
544 rtnval = Py_None;
545
546 finally:
547 Py_DECREF(args);
548 Py_XDECREF(item0);
549 Py_XDECREF(item1);
550 Py_XDECREF(item2);
551 Py_XDECREF(subslice);
552 return rtnval;
553}
554
555
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000556static PyObject *
557EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000558{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000559 PyObject *originalself = self;
560 PyObject *filename;
561 PyObject *serrno;
562 PyObject *strerror;
563 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000564
Fred Drake1aba5772000-08-15 15:46:16 +0000565 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000566 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000567
Barry Warsaw675ac282000-05-26 19:05:16 +0000568 filename = PyObject_GetAttrString(self, "filename");
569 serrno = PyObject_GetAttrString(self, "errno");
570 strerror = PyObject_GetAttrString(self, "strerror");
571 if (!filename || !serrno || !strerror)
572 goto finally;
573
574 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000575 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
576 PyObject *repr = PyObject_Repr(filename);
577 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000578
579 if (!fmt || !repr || !tuple) {
580 Py_XDECREF(fmt);
581 Py_XDECREF(repr);
582 Py_XDECREF(tuple);
583 goto finally;
584 }
585
586 PyTuple_SET_ITEM(tuple, 0, serrno);
587 PyTuple_SET_ITEM(tuple, 1, strerror);
588 PyTuple_SET_ITEM(tuple, 2, repr);
589
590 rtnval = PyString_Format(fmt, tuple);
591
592 Py_DECREF(fmt);
593 Py_DECREF(tuple);
594 /* already freed because tuple owned only reference */
595 serrno = NULL;
596 strerror = NULL;
597 }
598 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000599 PyObject *fmt = PyString_FromString("[Errno %s] %s");
600 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000601
602 if (!fmt || !tuple) {
603 Py_XDECREF(fmt);
604 Py_XDECREF(tuple);
605 goto finally;
606 }
607
608 PyTuple_SET_ITEM(tuple, 0, serrno);
609 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000610
Barry Warsaw675ac282000-05-26 19:05:16 +0000611 rtnval = PyString_Format(fmt, tuple);
612
613 Py_DECREF(fmt);
614 Py_DECREF(tuple);
615 /* already freed because tuple owned only reference */
616 serrno = NULL;
617 strerror = NULL;
618 }
619 else
620 /* The original Python code said:
621 *
622 * return StandardError.__str__(self)
623 *
624 * but there is no StandardError__str__() function; we happen to
625 * know that's just a pass through to Exception__str__().
626 */
627 rtnval = Exception__str__(originalself, args);
628
629 finally:
630 Py_XDECREF(filename);
631 Py_XDECREF(serrno);
632 Py_XDECREF(strerror);
633 return rtnval;
634}
635
636
637static
638PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000639 {"__init__", EnvironmentError__init__, METH_VARARGS},
640 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000641 {NULL, NULL}
642};
643
644
645
646
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000647PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000649PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000650
651#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000652PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000653#endif /* MS_WINDOWS */
654
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000655#ifdef __VMS
656static char
657VMSError__doc__[] = "OpenVMS OS system call failed.";
658#endif
659
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000660PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000662PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000664PyDoc_STRVAR(NotImplementedError__doc__,
665"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000666
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000667PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000668
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000669PyDoc_STRVAR(UnboundLocalError__doc__,
670"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000671
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000672PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000673
674
675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000676PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000677
678
679static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000680SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000681{
Barry Warsaw87bec352000-08-18 05:05:37 +0000682 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000683 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000684
685 /* Additional class-creation time initializations */
686 if (!emptystring ||
687 PyObject_SetAttrString(klass, "msg", emptystring) ||
688 PyObject_SetAttrString(klass, "filename", Py_None) ||
689 PyObject_SetAttrString(klass, "lineno", Py_None) ||
690 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000691 PyObject_SetAttrString(klass, "text", Py_None) ||
692 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000693 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000694 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000695 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000696 Py_XDECREF(emptystring);
697 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000698}
699
700
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000701static PyObject *
702SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000703{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000704 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000705 int lenargs;
706
707 if (!(self = get_self(args)))
708 return NULL;
709
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000710 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000711 return NULL;
712
713 if (PyObject_SetAttrString(self, "args", args))
714 goto finally;
715
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000716 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000717 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000718 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000719 int status;
720
721 if (!item0)
722 goto finally;
723 status = PyObject_SetAttrString(self, "msg", item0);
724 Py_DECREF(item0);
725 if (status)
726 goto finally;
727 }
728 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000729 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000730 PyObject *filename = NULL, *lineno = NULL;
731 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000732 int status = 1;
733
734 if (!info)
735 goto finally;
736
737 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000738 if (filename != NULL) {
739 lineno = PySequence_GetItem(info, 1);
740 if (lineno != NULL) {
741 offset = PySequence_GetItem(info, 2);
742 if (offset != NULL) {
743 text = PySequence_GetItem(info, 3);
744 if (text != NULL) {
745 status =
746 PyObject_SetAttrString(self, "filename", filename)
747 || PyObject_SetAttrString(self, "lineno", lineno)
748 || PyObject_SetAttrString(self, "offset", offset)
749 || PyObject_SetAttrString(self, "text", text);
750 Py_DECREF(text);
751 }
752 Py_DECREF(offset);
753 }
754 Py_DECREF(lineno);
755 }
756 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000757 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000758 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000759
760 if (status)
761 goto finally;
762 }
763 Py_INCREF(Py_None);
764 rtnval = Py_None;
765
766 finally:
767 Py_DECREF(args);
768 return rtnval;
769}
770
771
Fred Drake185a29b2000-08-15 16:20:36 +0000772/* This is called "my_basename" instead of just "basename" to avoid name
773 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
774 defined, and Python does define that. */
775static char *
776my_basename(char *name)
777{
778 char *cp = name;
779 char *result = name;
780
781 if (name == NULL)
782 return "???";
783 while (*cp != '\0') {
784 if (*cp == SEP)
785 result = cp + 1;
786 ++cp;
787 }
788 return result;
789}
790
791
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000792static PyObject *
793SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000794{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000795 PyObject *msg;
796 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000797 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000798
Fred Drake1aba5772000-08-15 15:46:16 +0000799 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000800 return NULL;
801
802 if (!(msg = PyObject_GetAttrString(self, "msg")))
803 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000804
Barry Warsaw675ac282000-05-26 19:05:16 +0000805 str = PyObject_Str(msg);
806 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000807 result = str;
808
809 /* XXX -- do all the additional formatting with filename and
810 lineno here */
811
Guido van Rossum602d4512002-09-03 20:24:09 +0000812 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000813 int have_filename = 0;
814 int have_lineno = 0;
815 char *buffer = NULL;
816
Barry Warsaw77c9f502000-08-16 19:43:17 +0000817 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000818 have_filename = PyString_Check(filename);
819 else
820 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000821
822 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000823 have_lineno = PyInt_Check(lineno);
824 else
825 PyErr_Clear();
826
827 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000828 int bufsize = PyString_GET_SIZE(str) + 64;
829 if (have_filename)
830 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000831
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000832 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000833 if (buffer != NULL) {
834 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000835 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
836 PyString_AS_STRING(str),
837 my_basename(PyString_AS_STRING(filename)),
838 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000839 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000840 PyOS_snprintf(buffer, bufsize, "%s (%s)",
841 PyString_AS_STRING(str),
842 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000843 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000844 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
845 PyString_AS_STRING(str),
846 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000847
Fred Drake1aba5772000-08-15 15:46:16 +0000848 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000849 PyMem_FREE(buffer);
850
Fred Drake1aba5772000-08-15 15:46:16 +0000851 if (result == NULL)
852 result = str;
853 else
854 Py_DECREF(str);
855 }
856 }
857 Py_XDECREF(filename);
858 Py_XDECREF(lineno);
859 }
860 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000861}
862
863
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000864static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000865 {"__init__", SyntaxError__init__, METH_VARARGS},
866 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000867 {NULL, NULL}
868};
869
870
Guido van Rossum602d4512002-09-03 20:24:09 +0000871static PyObject *
872KeyError__str__(PyObject *self, PyObject *args)
873{
874 PyObject *argsattr;
875 PyObject *result;
876
877 if (!PyArg_ParseTuple(args, "O:__str__", &self))
878 return NULL;
879
880 if (!(argsattr = PyObject_GetAttrString(self, "args")))
881 return NULL;
882
883 /* If args is a tuple of exactly one item, apply repr to args[0].
884 This is done so that e.g. the exception raised by {}[''] prints
885 KeyError: ''
886 rather than the confusing
887 KeyError
888 alone. The downside is that if KeyError is raised with an explanatory
889 string, that string will be displayed in quotes. Too bad.
890 If args is anything else, use the default Exception__str__().
891 */
892 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
893 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
894 result = PyObject_Repr(key);
895 }
896 else
897 result = Exception__str__(self, args);
898
899 Py_DECREF(argsattr);
900 return result;
901}
902
903static PyMethodDef KeyError_methods[] = {
904 {"__str__", KeyError__str__, METH_VARARGS},
905 {NULL, NULL}
906};
907
908
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000909#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000910static
911int get_int(PyObject *exc, const char *name, int *value)
912{
913 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
914
915 if (!attr)
916 return -1;
917 if (!PyInt_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000918 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000919 Py_DECREF(attr);
920 return -1;
921 }
922 *value = PyInt_AS_LONG(attr);
923 Py_DECREF(attr);
924 return 0;
925}
926
927
928static
929int set_int(PyObject *exc, const char *name, int value)
930{
931 PyObject *obj = PyInt_FromLong(value);
932 int result;
933
934 if (!obj)
935 return -1;
936 result = PyObject_SetAttrString(exc, (char *)name, obj);
937 Py_DECREF(obj);
938 return result;
939}
940
941
942static
943PyObject *get_string(PyObject *exc, const char *name)
944{
945 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
946
947 if (!attr)
948 return NULL;
949 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000950 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000951 Py_DECREF(attr);
952 return NULL;
953 }
954 return attr;
955}
956
957
958static
959int set_string(PyObject *exc, const char *name, const char *value)
960{
961 PyObject *obj = PyString_FromString(value);
962 int result;
963
964 if (!obj)
965 return -1;
966 result = PyObject_SetAttrString(exc, (char *)name, obj);
967 Py_DECREF(obj);
968 return result;
969}
970
971
972static
973PyObject *get_unicode(PyObject *exc, const char *name)
974{
975 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
976
977 if (!attr)
978 return NULL;
979 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000980 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000981 Py_DECREF(attr);
982 return NULL;
983 }
984 return attr;
985}
986
987PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
988{
989 return get_string(exc, "encoding");
990}
991
992PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
993{
994 return get_string(exc, "encoding");
995}
996
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000997PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
998{
999 return get_unicode(exc, "object");
1000}
1001
1002PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
1003{
1004 return get_string(exc, "object");
1005}
1006
1007PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
1008{
1009 return get_unicode(exc, "object");
1010}
1011
1012int PyUnicodeEncodeError_GetStart(PyObject *exc, int *start)
1013{
1014 if (!get_int(exc, "start", start)) {
1015 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
1016 int size;
1017 if (!object)
1018 return -1;
1019 size = PyUnicode_GET_SIZE(object);
1020 if (*start<0)
1021 *start = 0;
1022 if (*start>=size)
1023 *start = size-1;
1024 Py_DECREF(object);
1025 return 0;
1026 }
1027 return -1;
1028}
1029
1030
1031int PyUnicodeDecodeError_GetStart(PyObject *exc, int *start)
1032{
1033 if (!get_int(exc, "start", start)) {
1034 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
1035 int size;
1036 if (!object)
1037 return -1;
1038 size = PyString_GET_SIZE(object);
1039 if (*start<0)
1040 *start = 0;
1041 if (*start>=size)
1042 *start = size-1;
1043 Py_DECREF(object);
1044 return 0;
1045 }
1046 return -1;
1047}
1048
1049
1050int PyUnicodeTranslateError_GetStart(PyObject *exc, int *start)
1051{
1052 return PyUnicodeEncodeError_GetStart(exc, start);
1053}
1054
1055
1056int PyUnicodeEncodeError_SetStart(PyObject *exc, int start)
1057{
1058 return set_int(exc, "start", start);
1059}
1060
1061
1062int PyUnicodeDecodeError_SetStart(PyObject *exc, int start)
1063{
1064 return set_int(exc, "start", start);
1065}
1066
1067
1068int PyUnicodeTranslateError_SetStart(PyObject *exc, int start)
1069{
1070 return set_int(exc, "start", start);
1071}
1072
1073
1074int PyUnicodeEncodeError_GetEnd(PyObject *exc, int *end)
1075{
1076 if (!get_int(exc, "end", end)) {
1077 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
1078 int size;
1079 if (!object)
1080 return -1;
1081 size = PyUnicode_GET_SIZE(object);
1082 if (*end<1)
1083 *end = 1;
1084 if (*end>size)
1085 *end = size;
1086 Py_DECREF(object);
1087 return 0;
1088 }
1089 return -1;
1090}
1091
1092
1093int PyUnicodeDecodeError_GetEnd(PyObject *exc, int *end)
1094{
1095 if (!get_int(exc, "end", end)) {
1096 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
1097 int size;
1098 if (!object)
1099 return -1;
1100 size = PyString_GET_SIZE(object);
1101 if (*end<1)
1102 *end = 1;
1103 if (*end>size)
1104 *end = size;
1105 Py_DECREF(object);
1106 return 0;
1107 }
1108 return -1;
1109}
1110
1111
1112int PyUnicodeTranslateError_GetEnd(PyObject *exc, int *start)
1113{
1114 return PyUnicodeEncodeError_GetEnd(exc, start);
1115}
1116
1117
1118int PyUnicodeEncodeError_SetEnd(PyObject *exc, int end)
1119{
1120 return set_int(exc, "end", end);
1121}
1122
1123
1124int PyUnicodeDecodeError_SetEnd(PyObject *exc, int end)
1125{
1126 return set_int(exc, "end", end);
1127}
1128
1129
1130int PyUnicodeTranslateError_SetEnd(PyObject *exc, int end)
1131{
1132 return set_int(exc, "end", end);
1133}
1134
1135
1136PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1137{
1138 return get_string(exc, "reason");
1139}
1140
1141
1142PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1143{
1144 return get_string(exc, "reason");
1145}
1146
1147
1148PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1149{
1150 return get_string(exc, "reason");
1151}
1152
1153
1154int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1155{
1156 return set_string(exc, "reason", reason);
1157}
1158
1159
1160int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1161{
1162 return set_string(exc, "reason", reason);
1163}
1164
1165
1166int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1167{
1168 return set_string(exc, "reason", reason);
1169}
1170
1171
1172static PyObject *
1173UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1174{
1175 PyObject *rtnval = NULL;
1176 PyObject *encoding;
1177 PyObject *object;
1178 PyObject *start;
1179 PyObject *end;
1180 PyObject *reason;
1181
1182 if (!(self = get_self(args)))
1183 return NULL;
1184
1185 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1186 return NULL;
1187
1188 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1189 &PyString_Type, &encoding,
1190 objecttype, &object,
1191 &PyInt_Type, &start,
1192 &PyInt_Type, &end,
1193 &PyString_Type, &reason))
1194 return NULL;
1195
1196 if (PyObject_SetAttrString(self, "args", args))
1197 goto finally;
1198
1199 if (PyObject_SetAttrString(self, "encoding", encoding))
1200 goto finally;
1201 if (PyObject_SetAttrString(self, "object", object))
1202 goto finally;
1203 if (PyObject_SetAttrString(self, "start", start))
1204 goto finally;
1205 if (PyObject_SetAttrString(self, "end", end))
1206 goto finally;
1207 if (PyObject_SetAttrString(self, "reason", reason))
1208 goto finally;
1209
1210 Py_INCREF(Py_None);
1211 rtnval = Py_None;
1212
1213 finally:
1214 Py_DECREF(args);
1215 return rtnval;
1216}
1217
1218
1219static PyObject *
1220UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1221{
1222 return UnicodeError__init__(self, args, &PyUnicode_Type);
1223}
1224
1225static PyObject *
1226UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1227{
1228 PyObject *encodingObj = NULL;
1229 PyObject *objectObj = NULL;
1230 int length;
1231 int start;
1232 int end;
1233 PyObject *reasonObj = NULL;
1234 char buffer[1000];
1235 PyObject *result = NULL;
1236
1237 self = arg;
1238
1239 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1240 goto error;
1241
1242 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1243 goto error;
1244
1245 length = PyUnicode_GET_SIZE(objectObj);
1246
1247 if (PyUnicodeEncodeError_GetStart(self, &start))
1248 goto error;
1249
1250 if (PyUnicodeEncodeError_GetEnd(self, &end))
1251 goto error;
1252
1253 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1254 goto error;
1255
1256 if (end==start+1) {
1257 PyOS_snprintf(buffer, sizeof(buffer),
1258 "'%.400s' codec can't encode character '\\u%x' in position %d: %.400s",
1259 PyString_AS_STRING(encodingObj),
1260 (int)PyUnicode_AS_UNICODE(objectObj)[start],
1261 start,
1262 PyString_AS_STRING(reasonObj)
1263 );
1264 }
1265 else {
1266 PyOS_snprintf(buffer, sizeof(buffer),
1267 "'%.400s' codec can't encode characters in position %d-%d: %.400s",
1268 PyString_AS_STRING(encodingObj),
1269 start,
1270 end-1,
1271 PyString_AS_STRING(reasonObj)
1272 );
1273 }
1274 result = PyString_FromString(buffer);
1275
1276error:
1277 Py_XDECREF(reasonObj);
1278 Py_XDECREF(objectObj);
1279 Py_XDECREF(encodingObj);
1280 return result;
1281}
1282
1283static PyMethodDef UnicodeEncodeError_methods[] = {
1284 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1285 {"__str__", UnicodeEncodeError__str__, METH_O},
1286 {NULL, NULL}
1287};
1288
1289
1290PyObject * PyUnicodeEncodeError_Create(
1291 const char *encoding, const Py_UNICODE *object, int length,
1292 int start, int end, const char *reason)
1293{
1294 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#iis",
1295 encoding, object, length, start, end, reason);
1296}
1297
1298
1299static PyObject *
1300UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1301{
1302 return UnicodeError__init__(self, args, &PyString_Type);
1303}
1304
1305static PyObject *
1306UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1307{
1308 PyObject *encodingObj = NULL;
1309 PyObject *objectObj = NULL;
1310 int length;
1311 int start;
1312 int end;
1313 PyObject *reasonObj = NULL;
1314 char buffer[1000];
1315 PyObject *result = NULL;
1316
1317 self = arg;
1318
1319 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1320 goto error;
1321
1322 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1323 goto error;
1324
1325 length = PyString_GET_SIZE(objectObj);
1326
1327 if (PyUnicodeDecodeError_GetStart(self, &start))
1328 goto error;
1329
1330 if (PyUnicodeDecodeError_GetEnd(self, &end))
1331 goto error;
1332
1333 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1334 goto error;
1335
1336 if (end==start+1) {
1337 PyOS_snprintf(buffer, sizeof(buffer),
1338 "'%.400s' codec can't decode byte 0x%x in position %d: %.400s",
1339 PyString_AS_STRING(encodingObj),
1340 ((int)PyString_AS_STRING(objectObj)[start])&0xff,
1341 start,
1342 PyString_AS_STRING(reasonObj)
1343 );
1344 }
1345 else {
1346 PyOS_snprintf(buffer, sizeof(buffer),
1347 "'%.400s' codec can't decode bytes in position %d-%d: %.400s",
1348 PyString_AS_STRING(encodingObj),
1349 start,
1350 end-1,
1351 PyString_AS_STRING(reasonObj)
1352 );
1353 }
1354 result = PyString_FromString(buffer);
1355
1356error:
1357 Py_XDECREF(reasonObj);
1358 Py_XDECREF(objectObj);
1359 Py_XDECREF(encodingObj);
1360 return result;
1361}
1362
1363static PyMethodDef UnicodeDecodeError_methods[] = {
1364 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1365 {"__str__", UnicodeDecodeError__str__, METH_O},
1366 {NULL, NULL}
1367};
1368
1369
1370PyObject * PyUnicodeDecodeError_Create(
1371 const char *encoding, const char *object, int length,
1372 int start, int end, const char *reason)
1373{
1374 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
1375 encoding, object, length, start, end, reason);
1376}
1377
1378
1379static PyObject *
1380UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1381{
1382 PyObject *rtnval = NULL;
1383 PyObject *object;
1384 PyObject *start;
1385 PyObject *end;
1386 PyObject *reason;
1387
1388 if (!(self = get_self(args)))
1389 return NULL;
1390
1391 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1392 return NULL;
1393
1394 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1395 &PyUnicode_Type, &object,
1396 &PyInt_Type, &start,
1397 &PyInt_Type, &end,
1398 &PyString_Type, &reason))
1399 goto finally;
1400
1401 if (PyObject_SetAttrString(self, "args", args))
1402 goto finally;
1403
1404 if (PyObject_SetAttrString(self, "object", object))
1405 goto finally;
1406 if (PyObject_SetAttrString(self, "start", start))
1407 goto finally;
1408 if (PyObject_SetAttrString(self, "end", end))
1409 goto finally;
1410 if (PyObject_SetAttrString(self, "reason", reason))
1411 goto finally;
1412
1413 Py_INCREF(Py_None);
1414 rtnval = Py_None;
1415
1416 finally:
1417 Py_DECREF(args);
1418 return rtnval;
1419}
1420
1421
1422static PyObject *
1423UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1424{
1425 PyObject *objectObj = NULL;
1426 int length;
1427 int start;
1428 int end;
1429 PyObject *reasonObj = NULL;
1430 char buffer[1000];
1431 PyObject *result = NULL;
1432
1433 self = arg;
1434
1435 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1436 goto error;
1437
1438 length = PyUnicode_GET_SIZE(objectObj);
1439
1440 if (PyUnicodeTranslateError_GetStart(self, &start))
1441 goto error;
1442
1443 if (PyUnicodeTranslateError_GetEnd(self, &end))
1444 goto error;
1445
1446 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1447 goto error;
1448
1449 if (end==start+1) {
1450 PyOS_snprintf(buffer, sizeof(buffer),
1451 "can't translate character '\\u%x' in position %d: %.400s",
1452 (int)PyUnicode_AS_UNICODE(objectObj)[start],
1453 start,
1454 PyString_AS_STRING(reasonObj)
1455 );
1456 }
1457 else {
1458 PyOS_snprintf(buffer, sizeof(buffer),
1459 "can't translate characters in position %d-%d: %.400s",
1460 start,
1461 end-1,
1462 PyString_AS_STRING(reasonObj)
1463 );
1464 }
1465 result = PyString_FromString(buffer);
1466
1467error:
1468 Py_XDECREF(reasonObj);
1469 Py_XDECREF(objectObj);
1470 return result;
1471}
1472
1473static PyMethodDef UnicodeTranslateError_methods[] = {
1474 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1475 {"__str__", UnicodeTranslateError__str__, METH_O},
1476 {NULL, NULL}
1477};
1478
1479
1480PyObject * PyUnicodeTranslateError_Create(
1481 const Py_UNICODE *object, int length,
1482 int start, int end, const char *reason)
1483{
1484 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
1485 object, length, start, end, reason);
1486}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001487#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001488
1489
Barry Warsaw675ac282000-05-26 19:05:16 +00001490
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001491/* Exception doc strings */
1492
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001493PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001495PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001496
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001497PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001499PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001500
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001501PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001502
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001503PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001504
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001505PyDoc_STRVAR(ZeroDivisionError__doc__,
1506"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001507
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001508PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001510PyDoc_STRVAR(ValueError__doc__,
1511"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001512
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001513PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001514
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001515#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001516PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1517
1518PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1519
1520PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001521#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001522
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001523PyDoc_STRVAR(SystemError__doc__,
1524"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001525\n\
1526Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001527the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001529PyDoc_STRVAR(ReferenceError__doc__,
1530"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001531
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001532PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001534PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001536PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001537
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001538/* Warning category docstrings */
1539
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001540PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001541
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001542PyDoc_STRVAR(UserWarning__doc__,
1543"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001544
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001545PyDoc_STRVAR(DeprecationWarning__doc__,
1546"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001548PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001549"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001550"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001551
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001552PyDoc_STRVAR(SyntaxWarning__doc__,
1553"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001554
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001555PyDoc_STRVAR(OverflowWarning__doc__,
1556"Base class for warnings about numeric overflow.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001557
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001558PyDoc_STRVAR(RuntimeWarning__doc__,
1559"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001560
Barry Warsaw9f007392002-08-14 15:51:29 +00001561PyDoc_STRVAR(FutureWarning__doc__,
1562"Base class for warnings about constructs that will change semantically "
1563"in the future.");
1564
Barry Warsaw675ac282000-05-26 19:05:16 +00001565
1566
1567/* module global functions */
1568static PyMethodDef functions[] = {
1569 /* Sentinel */
1570 {NULL, NULL}
1571};
1572
1573
1574
1575/* Global C API defined exceptions */
1576
1577PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001578PyObject *PyExc_StopIteration;
Barry Warsaw675ac282000-05-26 19:05:16 +00001579PyObject *PyExc_StandardError;
1580PyObject *PyExc_ArithmeticError;
1581PyObject *PyExc_LookupError;
1582
1583PyObject *PyExc_AssertionError;
1584PyObject *PyExc_AttributeError;
1585PyObject *PyExc_EOFError;
1586PyObject *PyExc_FloatingPointError;
1587PyObject *PyExc_EnvironmentError;
1588PyObject *PyExc_IOError;
1589PyObject *PyExc_OSError;
1590PyObject *PyExc_ImportError;
1591PyObject *PyExc_IndexError;
1592PyObject *PyExc_KeyError;
1593PyObject *PyExc_KeyboardInterrupt;
1594PyObject *PyExc_MemoryError;
1595PyObject *PyExc_NameError;
1596PyObject *PyExc_OverflowError;
1597PyObject *PyExc_RuntimeError;
1598PyObject *PyExc_NotImplementedError;
1599PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001600PyObject *PyExc_IndentationError;
1601PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001602PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001603PyObject *PyExc_SystemError;
1604PyObject *PyExc_SystemExit;
1605PyObject *PyExc_UnboundLocalError;
1606PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001607PyObject *PyExc_UnicodeEncodeError;
1608PyObject *PyExc_UnicodeDecodeError;
1609PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001610PyObject *PyExc_TypeError;
1611PyObject *PyExc_ValueError;
1612PyObject *PyExc_ZeroDivisionError;
1613#ifdef MS_WINDOWS
1614PyObject *PyExc_WindowsError;
1615#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001616#ifdef __VMS
1617PyObject *PyExc_VMSError;
1618#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001619
1620/* Pre-computed MemoryError instance. Best to create this as early as
1621 * possibly and not wait until a MemoryError is actually raised!
1622 */
1623PyObject *PyExc_MemoryErrorInst;
1624
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001625/* Predefined warning categories */
1626PyObject *PyExc_Warning;
1627PyObject *PyExc_UserWarning;
1628PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001629PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001630PyObject *PyExc_SyntaxWarning;
Guido van Rossumae347b32001-08-23 02:56:07 +00001631PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001632PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001633PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001634
Barry Warsaw675ac282000-05-26 19:05:16 +00001635
1636
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001637/* mapping between exception names and their PyObject ** */
1638static struct {
1639 char *name;
1640 PyObject **exc;
1641 PyObject **base; /* NULL == PyExc_StandardError */
1642 char *docstr;
1643 PyMethodDef *methods;
1644 int (*classinit)(PyObject *);
1645} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001646 /*
1647 * The first three classes MUST appear in exactly this order
1648 */
1649 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001650 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1651 StopIteration__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001652 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1653 StandardError__doc__},
1654 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1655 /*
1656 * The rest appear in depth-first order of the hierarchy
1657 */
1658 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1659 SystemExit_methods},
1660 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1661 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1662 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1663 EnvironmentError_methods},
1664 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1665 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1666#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001667 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001668 WindowsError__doc__},
1669#endif /* MS_WINDOWS */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001670#ifdef __VMS
1671 {"VMSError", &PyExc_VMSError, &PyExc_OSError,
1672 VMSError__doc__},
1673#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001674 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1675 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1676 {"NotImplementedError", &PyExc_NotImplementedError,
1677 &PyExc_RuntimeError, NotImplementedError__doc__},
1678 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1679 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1680 UnboundLocalError__doc__},
1681 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1682 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1683 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001684 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1685 IndentationError__doc__},
1686 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1687 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001688 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1689 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1690 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1691 IndexError__doc__},
1692 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001693 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001694 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1695 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1696 OverflowError__doc__},
1697 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1698 ZeroDivisionError__doc__},
1699 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1700 FloatingPointError__doc__},
1701 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1702 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001703#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001704 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1705 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1706 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1707 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1708 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1709 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001710#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001711 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001712 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1713 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001714 /* Warning categories */
1715 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1716 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1717 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1718 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001719 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1720 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001721 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Guido van Rossumae347b32001-08-23 02:56:07 +00001722 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1723 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001724 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1725 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001726 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1727 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001728 /* Sentinel */
1729 {NULL}
1730};
1731
1732
1733
Mark Hammonda2905272002-07-29 13:42:14 +00001734void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001735_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001736{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001737 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001738 int modnamesz = strlen(modulename);
1739 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001740 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001741
Tim Peters6d6c1a32001-08-02 04:15:00 +00001742 me = Py_InitModule(modulename, functions);
1743 if (me == NULL)
1744 goto err;
1745 mydict = PyModule_GetDict(me);
1746 if (mydict == NULL)
1747 goto err;
1748 bltinmod = PyImport_ImportModule("__builtin__");
1749 if (bltinmod == NULL)
1750 goto err;
1751 bdict = PyModule_GetDict(bltinmod);
1752 if (bdict == NULL)
1753 goto err;
1754 doc = PyString_FromString(module__doc__);
1755 if (doc == NULL)
1756 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001757
Tim Peters6d6c1a32001-08-02 04:15:00 +00001758 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001759 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001760 if (i < 0) {
1761 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001762 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001763 return;
1764 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001765
1766 /* This is the base class of all exceptions, so make it first. */
1767 if (make_Exception(modulename) ||
1768 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1769 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1770 {
1771 Py_FatalError("Base class `Exception' could not be created.");
1772 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001773
Barry Warsaw675ac282000-05-26 19:05:16 +00001774 /* Now we can programmatically create all the remaining exceptions.
1775 * Remember to start the loop at 1 to skip Exceptions.
1776 */
1777 for (i=1; exctable[i].name; i++) {
1778 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001779 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1780 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001781
1782 (void)strcpy(cname, modulename);
1783 (void)strcat(cname, ".");
1784 (void)strcat(cname, exctable[i].name);
1785
1786 if (exctable[i].base == 0)
1787 base = PyExc_StandardError;
1788 else
1789 base = *exctable[i].base;
1790
1791 status = make_class(exctable[i].exc, base, cname,
1792 exctable[i].methods,
1793 exctable[i].docstr);
1794
1795 PyMem_DEL(cname);
1796
1797 if (status)
1798 Py_FatalError("Standard exception classes could not be created.");
1799
1800 if (exctable[i].classinit) {
1801 status = (*exctable[i].classinit)(*exctable[i].exc);
1802 if (status)
1803 Py_FatalError("An exception class could not be initialized.");
1804 }
1805
1806 /* Now insert the class into both this module and the __builtin__
1807 * module.
1808 */
1809 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1810 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1811 {
1812 Py_FatalError("Module dictionary insertion problem.");
1813 }
1814 }
1815
1816 /* Now we need to pre-allocate a MemoryError instance */
1817 args = Py_BuildValue("()");
1818 if (!args ||
1819 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1820 {
1821 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1822 }
1823 Py_DECREF(args);
1824
1825 /* We're done with __builtin__ */
1826 Py_DECREF(bltinmod);
1827}
1828
1829
Mark Hammonda2905272002-07-29 13:42:14 +00001830void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001831_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001832{
1833 int i;
1834
1835 Py_XDECREF(PyExc_MemoryErrorInst);
1836 PyExc_MemoryErrorInst = NULL;
1837
1838 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001839 /* clear the class's dictionary, freeing up circular references
1840 * between the class and its methods.
1841 */
1842 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1843 PyDict_Clear(cdict);
1844 Py_DECREF(cdict);
1845
1846 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001847 Py_XDECREF(*exctable[i].exc);
1848 *exctable[i].exc = NULL;
1849 }
1850}