blob: 2e7c820bf1e96e40ab4e90ecc1a7532740a676ed [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\
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000060 +-- GeneratorExit\n\
Barry Warsaw675ac282000-05-26 19:05:16 +000061 +-- StandardError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +000062 | |\n\
63 | +-- KeyboardInterrupt\n\
64 | +-- ImportError\n\
65 | +-- EnvironmentError\n\
66 | | |\n\
67 | | +-- IOError\n\
68 | | +-- OSError\n\
69 | | |\n\
70 | | +-- WindowsError\n\
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000071 | | +-- VMSError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +000072 | |\n\
73 | +-- EOFError\n\
74 | +-- RuntimeError\n\
75 | | |\n\
76 | | +-- NotImplementedError\n\
77 | |\n\
78 | +-- NameError\n\
79 | | |\n\
80 | | +-- UnboundLocalError\n\
81 | |\n\
82 | +-- AttributeError\n\
83 | +-- SyntaxError\n\
84 | | |\n\
85 | | +-- IndentationError\n\
86 | | |\n\
87 | | +-- TabError\n\
88 | |\n\
89 | +-- TypeError\n\
90 | +-- AssertionError\n\
91 | +-- LookupError\n\
92 | | |\n\
93 | | +-- IndexError\n\
94 | | +-- KeyError\n\
95 | |\n\
96 | +-- ArithmeticError\n\
97 | | |\n\
98 | | +-- OverflowError\n\
99 | | +-- ZeroDivisionError\n\
100 | | +-- FloatingPointError\n\
101 | |\n\
102 | +-- ValueError\n\
103 | | |\n\
104 | | +-- UnicodeError\n\
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000105 | | |\n\
106 | | +-- UnicodeEncodeError\n\
107 | | +-- UnicodeDecodeError\n\
108 | | +-- UnicodeTranslateError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000109 | |\n\
Fred Drakebb9fa212001-10-05 21:50:08 +0000110 | +-- ReferenceError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000111 | +-- SystemError\n\
112 | +-- MemoryError\n\
113 |\n\
114 +---Warning\n\
Barry Warsaw675ac282000-05-26 19:05:16 +0000115 |\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000116 +-- UserWarning\n\
117 +-- DeprecationWarning\n\
Neal Norwitzd68f5172002-05-29 15:54:55 +0000118 +-- PendingDeprecationWarning\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000119 +-- SyntaxWarning\n\
Guido van Rossumae347b32001-08-23 02:56:07 +0000120 +-- OverflowWarning\n\
Barry Warsaw9f007392002-08-14 15:51:29 +0000121 +-- RuntimeWarning\n\
122 +-- FutureWarning"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000123);
Barry Warsaw675ac282000-05-26 19:05:16 +0000124
125
126/* Helper function for populating a dictionary with method wrappers. */
127static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000128populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +0000129{
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000130 PyObject *module;
131 int status = -1;
132
Barry Warsaw675ac282000-05-26 19:05:16 +0000133 if (!methods)
134 return 0;
135
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000136 module = PyString_FromString("exceptions");
137 if (!module)
138 return 0;
Barry Warsaw675ac282000-05-26 19:05:16 +0000139 while (methods->ml_name) {
140 /* get a wrapper for the built-in function */
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000141 PyObject *func = PyCFunction_NewEx(methods, NULL, module);
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000142 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +0000143
144 if (!func)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000145 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +0000146
147 /* turn the function into an unbound method */
148 if (!(meth = PyMethod_New(func, NULL, klass))) {
149 Py_DECREF(func);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000150 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +0000151 }
Barry Warsaw9667ed22001-01-23 16:08:34 +0000152
Barry Warsaw675ac282000-05-26 19:05:16 +0000153 /* add method to dictionary */
154 status = PyDict_SetItemString(dict, methods->ml_name, meth);
155 Py_DECREF(meth);
156 Py_DECREF(func);
157
158 /* stop now if an error occurred, otherwise do the next method */
159 if (status)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000160 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +0000161
162 methods++;
163 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000164 status = 0;
165 status:
166 Py_DECREF(module);
167 return status;
Barry Warsaw675ac282000-05-26 19:05:16 +0000168}
169
Barry Warsaw9667ed22001-01-23 16:08:34 +0000170
Barry Warsaw675ac282000-05-26 19:05:16 +0000171
172/* This function is used to create all subsequent exception classes. */
173static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000174make_class(PyObject **klass, PyObject *base,
175 char *name, PyMethodDef *methods,
176 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +0000177{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000178 PyObject *dict = PyDict_New();
179 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000180 int status = -1;
181
182 if (!dict)
183 return -1;
184
185 /* If an error occurs from here on, goto finally instead of explicitly
186 * returning NULL.
187 */
188
189 if (docstr) {
190 if (!(str = PyString_FromString(docstr)))
191 goto finally;
192 if (PyDict_SetItemString(dict, "__doc__", str))
193 goto finally;
194 }
195
196 if (!(*klass = PyErr_NewException(name, base, dict)))
197 goto finally;
198
199 if (populate_methods(*klass, dict, methods)) {
200 Py_DECREF(*klass);
201 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000202 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000203 }
204
205 status = 0;
206
207 finally:
208 Py_XDECREF(dict);
209 Py_XDECREF(str);
210 return status;
211}
212
213
214/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000215static PyObject *
216get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000217{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000218 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000219 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000220 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000221 if (PyExc_TypeError) {
222 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000223 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000224 }
225 return NULL;
226 }
227 return self;
228}
229
230
231
232/* Notes on bootstrapping the exception classes.
233 *
234 * First thing we create is the base class for all exceptions, called
235 * appropriately enough: Exception. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000236 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000237 * for TypeError, which can conditionally exist.
238 *
239 * Next, StandardError is created (which is quite simple) followed by
240 * TypeError, because the instantiation of other exceptions can potentially
241 * throw a TypeError. Once these exceptions are created, all the others
242 * can be created in any order. See the static exctable below for the
243 * explicit bootstrap order.
244 *
245 * All classes after Exception can be created using PyErr_NewException().
246 */
247
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000248PyDoc_STRVAR(Exception__doc__, "Common base class for all exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000249
250
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000251static PyObject *
252Exception__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000253{
254 int status;
255
256 if (!(self = get_self(args)))
257 return NULL;
258
259 /* set args attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000260 /* XXX size is only a hint */
261 args = PySequence_GetSlice(args, 1, PySequence_Size(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000262 if (!args)
263 return NULL;
264 status = PyObject_SetAttrString(self, "args", args);
265 Py_DECREF(args);
266 if (status < 0)
267 return NULL;
268
269 Py_INCREF(Py_None);
270 return Py_None;
271}
272
273
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000274static PyObject *
275Exception__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000276{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000277 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000278
Fred Drake1aba5772000-08-15 15:46:16 +0000279 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000280 return NULL;
281
282 args = PyObject_GetAttrString(self, "args");
283 if (!args)
284 return NULL;
285
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000286 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000287 case 0:
288 out = PyString_FromString("");
289 break;
290 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000291 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000292 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000293 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000294 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000295 Py_DECREF(tmp);
296 }
297 else
298 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000299 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000300 }
Guido van Rossum98b2a422002-09-18 04:06:32 +0000301 case -1:
302 PyErr_Clear();
303 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000304 default:
305 out = PyObject_Str(args);
306 break;
307 }
308
309 Py_DECREF(args);
310 return out;
311}
312
313
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000314static PyObject *
315Exception__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000316{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000317 PyObject *out;
318 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000319
Fred Drake1aba5772000-08-15 15:46:16 +0000320 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000321 return NULL;
322
323 args = PyObject_GetAttrString(self, "args");
324 if (!args)
325 return NULL;
326
327 out = PyObject_GetItem(args, index);
328 Py_DECREF(args);
329 return out;
330}
331
332
333static PyMethodDef
334Exception_methods[] = {
335 /* methods for the Exception class */
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000336 { "__getitem__", Exception__getitem__, METH_VARARGS},
337 { "__str__", Exception__str__, METH_VARARGS},
338 { "__init__", Exception__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000339 { NULL, NULL }
340};
341
342
343static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000344make_Exception(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000345{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000346 PyObject *dict = PyDict_New();
347 PyObject *str = NULL;
348 PyObject *name = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000349 int status = -1;
350
351 if (!dict)
352 return -1;
353
354 /* If an error occurs from here on, goto finally instead of explicitly
355 * returning NULL.
356 */
357
358 if (!(str = PyString_FromString(modulename)))
359 goto finally;
360 if (PyDict_SetItemString(dict, "__module__", str))
361 goto finally;
362 Py_DECREF(str);
363 if (!(str = PyString_FromString(Exception__doc__)))
364 goto finally;
365 if (PyDict_SetItemString(dict, "__doc__", str))
366 goto finally;
367
368 if (!(name = PyString_FromString("Exception")))
369 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000370
Barry Warsaw675ac282000-05-26 19:05:16 +0000371 if (!(PyExc_Exception = PyClass_New(NULL, dict, name)))
372 goto finally;
373
374 /* Now populate the dictionary with the method suite */
375 if (populate_methods(PyExc_Exception, dict, Exception_methods))
376 /* Don't need to reclaim PyExc_Exception here because that'll
377 * happen during interpreter shutdown.
378 */
379 goto finally;
380
381 status = 0;
382
383 finally:
384 Py_XDECREF(dict);
385 Py_XDECREF(str);
386 Py_XDECREF(name);
387 return status;
388}
389
390
391
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000392PyDoc_STRVAR(StandardError__doc__,
393"Base class for all standard Python exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000395PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000397PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000398PyDoc_STRVAR(GeneratorExit__doc__, "Request that a generator exit.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000399
Barry Warsaw675ac282000-05-26 19:05:16 +0000400
401
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000402PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000403
404
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000405static PyObject *
406SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000407{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000408 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000409 int status;
410
411 if (!(self = get_self(args)))
412 return NULL;
413
414 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000415 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000416 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000417
Barry Warsaw675ac282000-05-26 19:05:16 +0000418 status = PyObject_SetAttrString(self, "args", args);
419 if (status < 0) {
420 Py_DECREF(args);
421 return NULL;
422 }
423
424 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000425 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000426 case 0:
427 Py_INCREF(Py_None);
428 code = Py_None;
429 break;
430 case 1:
431 code = PySequence_GetItem(args, 0);
432 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000433 case -1:
434 PyErr_Clear();
435 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000436 default:
437 Py_INCREF(args);
438 code = args;
439 break;
440 }
441
442 status = PyObject_SetAttrString(self, "code", code);
443 Py_DECREF(code);
444 Py_DECREF(args);
445 if (status < 0)
446 return NULL;
447
448 Py_INCREF(Py_None);
449 return Py_None;
450}
451
452
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000453static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000454 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000455 {NULL, NULL}
456};
457
458
459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000460PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000461
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000462PyDoc_STRVAR(ImportError__doc__,
463"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000464
465
466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000467PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000468
469
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000470static PyObject *
471EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000472{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000473 PyObject *item0 = NULL;
474 PyObject *item1 = NULL;
475 PyObject *item2 = NULL;
476 PyObject *subslice = NULL;
477 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000478
479 if (!(self = get_self(args)))
480 return NULL;
481
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000482 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000483 return NULL;
484
485 if (PyObject_SetAttrString(self, "args", args) ||
486 PyObject_SetAttrString(self, "errno", Py_None) ||
487 PyObject_SetAttrString(self, "strerror", Py_None) ||
488 PyObject_SetAttrString(self, "filename", Py_None))
489 {
490 goto finally;
491 }
492
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000493 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000494 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000495 /* Where a function has a single filename, such as open() or some
496 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
497 * called, giving a third argument which is the filename. But, so
498 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000499 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000500 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000501 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000502 * we hack args so that it only contains two items. This also
503 * means we need our own __str__() which prints out the filename
504 * when it was supplied.
505 */
506 item0 = PySequence_GetItem(args, 0);
507 item1 = PySequence_GetItem(args, 1);
508 item2 = PySequence_GetItem(args, 2);
509 if (!item0 || !item1 || !item2)
510 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000511
Barry Warsaw675ac282000-05-26 19:05:16 +0000512 if (PyObject_SetAttrString(self, "errno", item0) ||
513 PyObject_SetAttrString(self, "strerror", item1) ||
514 PyObject_SetAttrString(self, "filename", item2))
515 {
516 goto finally;
517 }
518
519 subslice = PySequence_GetSlice(args, 0, 2);
520 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
521 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000522 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000523
524 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000525 /* Used when PyErr_SetFromErrno() is called and no filename
526 * argument is given.
527 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000528 item0 = PySequence_GetItem(args, 0);
529 item1 = PySequence_GetItem(args, 1);
530 if (!item0 || !item1)
531 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000532
Barry Warsaw675ac282000-05-26 19:05:16 +0000533 if (PyObject_SetAttrString(self, "errno", item0) ||
534 PyObject_SetAttrString(self, "strerror", item1))
535 {
536 goto finally;
537 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000538 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000539
540 case -1:
541 PyErr_Clear();
542 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000543 }
544
545 Py_INCREF(Py_None);
546 rtnval = Py_None;
547
548 finally:
549 Py_DECREF(args);
550 Py_XDECREF(item0);
551 Py_XDECREF(item1);
552 Py_XDECREF(item2);
553 Py_XDECREF(subslice);
554 return rtnval;
555}
556
557
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000558static PyObject *
559EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000560{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000561 PyObject *originalself = self;
562 PyObject *filename;
563 PyObject *serrno;
564 PyObject *strerror;
565 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000566
Fred Drake1aba5772000-08-15 15:46:16 +0000567 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000568 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000569
Barry Warsaw675ac282000-05-26 19:05:16 +0000570 filename = PyObject_GetAttrString(self, "filename");
571 serrno = PyObject_GetAttrString(self, "errno");
572 strerror = PyObject_GetAttrString(self, "strerror");
573 if (!filename || !serrno || !strerror)
574 goto finally;
575
576 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000577 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
578 PyObject *repr = PyObject_Repr(filename);
579 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000580
581 if (!fmt || !repr || !tuple) {
582 Py_XDECREF(fmt);
583 Py_XDECREF(repr);
584 Py_XDECREF(tuple);
585 goto finally;
586 }
587
588 PyTuple_SET_ITEM(tuple, 0, serrno);
589 PyTuple_SET_ITEM(tuple, 1, strerror);
590 PyTuple_SET_ITEM(tuple, 2, repr);
591
592 rtnval = PyString_Format(fmt, tuple);
593
594 Py_DECREF(fmt);
595 Py_DECREF(tuple);
596 /* already freed because tuple owned only reference */
597 serrno = NULL;
598 strerror = NULL;
599 }
600 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000601 PyObject *fmt = PyString_FromString("[Errno %s] %s");
602 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000603
604 if (!fmt || !tuple) {
605 Py_XDECREF(fmt);
606 Py_XDECREF(tuple);
607 goto finally;
608 }
609
610 PyTuple_SET_ITEM(tuple, 0, serrno);
611 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000612
Barry Warsaw675ac282000-05-26 19:05:16 +0000613 rtnval = PyString_Format(fmt, tuple);
614
615 Py_DECREF(fmt);
616 Py_DECREF(tuple);
617 /* already freed because tuple owned only reference */
618 serrno = NULL;
619 strerror = NULL;
620 }
621 else
622 /* The original Python code said:
623 *
624 * return StandardError.__str__(self)
625 *
626 * but there is no StandardError__str__() function; we happen to
627 * know that's just a pass through to Exception__str__().
628 */
629 rtnval = Exception__str__(originalself, args);
630
631 finally:
632 Py_XDECREF(filename);
633 Py_XDECREF(serrno);
634 Py_XDECREF(strerror);
635 return rtnval;
636}
637
638
639static
640PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000641 {"__init__", EnvironmentError__init__, METH_VARARGS},
642 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000643 {NULL, NULL}
644};
645
646
647
648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000649PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000650
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000651PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000652
653#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000654PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000655#endif /* MS_WINDOWS */
656
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000657#ifdef __VMS
658static char
659VMSError__doc__[] = "OpenVMS OS system call failed.";
660#endif
661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000662PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000664PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000665
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000666PyDoc_STRVAR(NotImplementedError__doc__,
667"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000668
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000669PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000671PyDoc_STRVAR(UnboundLocalError__doc__,
672"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000673
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000674PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000675
676
677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000678PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000679
680
681static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000682SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000683{
Barry Warsaw87bec352000-08-18 05:05:37 +0000684 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000685 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000686
687 /* Additional class-creation time initializations */
688 if (!emptystring ||
689 PyObject_SetAttrString(klass, "msg", emptystring) ||
690 PyObject_SetAttrString(klass, "filename", Py_None) ||
691 PyObject_SetAttrString(klass, "lineno", Py_None) ||
692 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000693 PyObject_SetAttrString(klass, "text", Py_None) ||
694 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000695 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000696 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000697 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000698 Py_XDECREF(emptystring);
699 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000700}
701
702
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000703static PyObject *
704SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000705{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000706 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000707 int lenargs;
708
709 if (!(self = get_self(args)))
710 return NULL;
711
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000712 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000713 return NULL;
714
715 if (PyObject_SetAttrString(self, "args", args))
716 goto finally;
717
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000718 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000719 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000720 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000721 int status;
722
723 if (!item0)
724 goto finally;
725 status = PyObject_SetAttrString(self, "msg", item0);
726 Py_DECREF(item0);
727 if (status)
728 goto finally;
729 }
730 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000731 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000732 PyObject *filename = NULL, *lineno = NULL;
733 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000734 int status = 1;
735
736 if (!info)
737 goto finally;
738
739 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000740 if (filename != NULL) {
741 lineno = PySequence_GetItem(info, 1);
742 if (lineno != NULL) {
743 offset = PySequence_GetItem(info, 2);
744 if (offset != NULL) {
745 text = PySequence_GetItem(info, 3);
746 if (text != NULL) {
747 status =
748 PyObject_SetAttrString(self, "filename", filename)
749 || PyObject_SetAttrString(self, "lineno", lineno)
750 || PyObject_SetAttrString(self, "offset", offset)
751 || PyObject_SetAttrString(self, "text", text);
752 Py_DECREF(text);
753 }
754 Py_DECREF(offset);
755 }
756 Py_DECREF(lineno);
757 }
758 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000759 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000760 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000761
762 if (status)
763 goto finally;
764 }
765 Py_INCREF(Py_None);
766 rtnval = Py_None;
767
768 finally:
769 Py_DECREF(args);
770 return rtnval;
771}
772
773
Fred Drake185a29b2000-08-15 16:20:36 +0000774/* This is called "my_basename" instead of just "basename" to avoid name
775 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
776 defined, and Python does define that. */
777static char *
778my_basename(char *name)
779{
780 char *cp = name;
781 char *result = name;
782
783 if (name == NULL)
784 return "???";
785 while (*cp != '\0') {
786 if (*cp == SEP)
787 result = cp + 1;
788 ++cp;
789 }
790 return result;
791}
792
793
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000794static PyObject *
795SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000796{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000797 PyObject *msg;
798 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000799 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000800
Fred Drake1aba5772000-08-15 15:46:16 +0000801 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000802 return NULL;
803
804 if (!(msg = PyObject_GetAttrString(self, "msg")))
805 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000806
Barry Warsaw675ac282000-05-26 19:05:16 +0000807 str = PyObject_Str(msg);
808 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000809 result = str;
810
811 /* XXX -- do all the additional formatting with filename and
812 lineno here */
813
Guido van Rossum602d4512002-09-03 20:24:09 +0000814 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000815 int have_filename = 0;
816 int have_lineno = 0;
817 char *buffer = NULL;
818
Barry Warsaw77c9f502000-08-16 19:43:17 +0000819 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000820 have_filename = PyString_Check(filename);
821 else
822 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000823
824 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000825 have_lineno = PyInt_Check(lineno);
826 else
827 PyErr_Clear();
828
829 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000830 int bufsize = PyString_GET_SIZE(str) + 64;
831 if (have_filename)
832 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000833
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000834 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000835 if (buffer != NULL) {
836 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000837 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
838 PyString_AS_STRING(str),
839 my_basename(PyString_AS_STRING(filename)),
840 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000841 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000842 PyOS_snprintf(buffer, bufsize, "%s (%s)",
843 PyString_AS_STRING(str),
844 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000845 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000846 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
847 PyString_AS_STRING(str),
848 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000849
Fred Drake1aba5772000-08-15 15:46:16 +0000850 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000851 PyMem_FREE(buffer);
852
Fred Drake1aba5772000-08-15 15:46:16 +0000853 if (result == NULL)
854 result = str;
855 else
856 Py_DECREF(str);
857 }
858 }
859 Py_XDECREF(filename);
860 Py_XDECREF(lineno);
861 }
862 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000863}
864
865
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000866static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000867 {"__init__", SyntaxError__init__, METH_VARARGS},
868 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000869 {NULL, NULL}
870};
871
872
Guido van Rossum602d4512002-09-03 20:24:09 +0000873static PyObject *
874KeyError__str__(PyObject *self, PyObject *args)
875{
876 PyObject *argsattr;
877 PyObject *result;
878
879 if (!PyArg_ParseTuple(args, "O:__str__", &self))
880 return NULL;
881
882 if (!(argsattr = PyObject_GetAttrString(self, "args")))
883 return NULL;
884
885 /* If args is a tuple of exactly one item, apply repr to args[0].
886 This is done so that e.g. the exception raised by {}[''] prints
887 KeyError: ''
888 rather than the confusing
889 KeyError
890 alone. The downside is that if KeyError is raised with an explanatory
891 string, that string will be displayed in quotes. Too bad.
892 If args is anything else, use the default Exception__str__().
893 */
894 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
895 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
896 result = PyObject_Repr(key);
897 }
898 else
899 result = Exception__str__(self, args);
900
901 Py_DECREF(argsattr);
902 return result;
903}
904
905static PyMethodDef KeyError_methods[] = {
906 {"__str__", KeyError__str__, METH_VARARGS},
907 {NULL, NULL}
908};
909
910
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000911#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000912static
913int get_int(PyObject *exc, const char *name, int *value)
914{
915 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
916
917 if (!attr)
918 return -1;
919 if (!PyInt_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000920 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000921 Py_DECREF(attr);
922 return -1;
923 }
924 *value = PyInt_AS_LONG(attr);
925 Py_DECREF(attr);
926 return 0;
927}
928
929
930static
931int set_int(PyObject *exc, const char *name, int value)
932{
933 PyObject *obj = PyInt_FromLong(value);
934 int result;
935
936 if (!obj)
937 return -1;
938 result = PyObject_SetAttrString(exc, (char *)name, obj);
939 Py_DECREF(obj);
940 return result;
941}
942
943
944static
945PyObject *get_string(PyObject *exc, const char *name)
946{
947 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
948
949 if (!attr)
950 return NULL;
951 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000952 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000953 Py_DECREF(attr);
954 return NULL;
955 }
956 return attr;
957}
958
959
960static
961int set_string(PyObject *exc, const char *name, const char *value)
962{
963 PyObject *obj = PyString_FromString(value);
964 int result;
965
966 if (!obj)
967 return -1;
968 result = PyObject_SetAttrString(exc, (char *)name, obj);
969 Py_DECREF(obj);
970 return result;
971}
972
973
974static
975PyObject *get_unicode(PyObject *exc, const char *name)
976{
977 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
978
979 if (!attr)
980 return NULL;
981 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000982 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000983 Py_DECREF(attr);
984 return NULL;
985 }
986 return attr;
987}
988
989PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
990{
991 return get_string(exc, "encoding");
992}
993
994PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
995{
996 return get_string(exc, "encoding");
997}
998
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000999PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
1000{
1001 return get_unicode(exc, "object");
1002}
1003
1004PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
1005{
1006 return get_string(exc, "object");
1007}
1008
1009PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
1010{
1011 return get_unicode(exc, "object");
1012}
1013
1014int PyUnicodeEncodeError_GetStart(PyObject *exc, int *start)
1015{
1016 if (!get_int(exc, "start", start)) {
1017 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
1018 int size;
1019 if (!object)
1020 return -1;
1021 size = PyUnicode_GET_SIZE(object);
1022 if (*start<0)
1023 *start = 0;
1024 if (*start>=size)
1025 *start = size-1;
1026 Py_DECREF(object);
1027 return 0;
1028 }
1029 return -1;
1030}
1031
1032
1033int PyUnicodeDecodeError_GetStart(PyObject *exc, int *start)
1034{
1035 if (!get_int(exc, "start", start)) {
1036 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
1037 int size;
1038 if (!object)
1039 return -1;
1040 size = PyString_GET_SIZE(object);
1041 if (*start<0)
1042 *start = 0;
1043 if (*start>=size)
1044 *start = size-1;
1045 Py_DECREF(object);
1046 return 0;
1047 }
1048 return -1;
1049}
1050
1051
1052int PyUnicodeTranslateError_GetStart(PyObject *exc, int *start)
1053{
1054 return PyUnicodeEncodeError_GetStart(exc, start);
1055}
1056
1057
1058int PyUnicodeEncodeError_SetStart(PyObject *exc, int start)
1059{
1060 return set_int(exc, "start", start);
1061}
1062
1063
1064int PyUnicodeDecodeError_SetStart(PyObject *exc, int start)
1065{
1066 return set_int(exc, "start", start);
1067}
1068
1069
1070int PyUnicodeTranslateError_SetStart(PyObject *exc, int start)
1071{
1072 return set_int(exc, "start", start);
1073}
1074
1075
1076int PyUnicodeEncodeError_GetEnd(PyObject *exc, int *end)
1077{
1078 if (!get_int(exc, "end", end)) {
1079 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
1080 int size;
1081 if (!object)
1082 return -1;
1083 size = PyUnicode_GET_SIZE(object);
1084 if (*end<1)
1085 *end = 1;
1086 if (*end>size)
1087 *end = size;
1088 Py_DECREF(object);
1089 return 0;
1090 }
1091 return -1;
1092}
1093
1094
1095int PyUnicodeDecodeError_GetEnd(PyObject *exc, int *end)
1096{
1097 if (!get_int(exc, "end", end)) {
1098 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
1099 int size;
1100 if (!object)
1101 return -1;
1102 size = PyString_GET_SIZE(object);
1103 if (*end<1)
1104 *end = 1;
1105 if (*end>size)
1106 *end = size;
1107 Py_DECREF(object);
1108 return 0;
1109 }
1110 return -1;
1111}
1112
1113
1114int PyUnicodeTranslateError_GetEnd(PyObject *exc, int *start)
1115{
1116 return PyUnicodeEncodeError_GetEnd(exc, start);
1117}
1118
1119
1120int PyUnicodeEncodeError_SetEnd(PyObject *exc, int end)
1121{
1122 return set_int(exc, "end", end);
1123}
1124
1125
1126int PyUnicodeDecodeError_SetEnd(PyObject *exc, int end)
1127{
1128 return set_int(exc, "end", end);
1129}
1130
1131
1132int PyUnicodeTranslateError_SetEnd(PyObject *exc, int end)
1133{
1134 return set_int(exc, "end", end);
1135}
1136
1137
1138PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1139{
1140 return get_string(exc, "reason");
1141}
1142
1143
1144PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1145{
1146 return get_string(exc, "reason");
1147}
1148
1149
1150PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1151{
1152 return get_string(exc, "reason");
1153}
1154
1155
1156int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1157{
1158 return set_string(exc, "reason", reason);
1159}
1160
1161
1162int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1163{
1164 return set_string(exc, "reason", reason);
1165}
1166
1167
1168int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1169{
1170 return set_string(exc, "reason", reason);
1171}
1172
1173
1174static PyObject *
1175UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1176{
1177 PyObject *rtnval = NULL;
1178 PyObject *encoding;
1179 PyObject *object;
1180 PyObject *start;
1181 PyObject *end;
1182 PyObject *reason;
1183
1184 if (!(self = get_self(args)))
1185 return NULL;
1186
1187 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1188 return NULL;
1189
1190 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1191 &PyString_Type, &encoding,
1192 objecttype, &object,
1193 &PyInt_Type, &start,
1194 &PyInt_Type, &end,
1195 &PyString_Type, &reason))
Walter Dörwalde98147a2003-08-14 20:59:07 +00001196 goto finally;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001197
1198 if (PyObject_SetAttrString(self, "args", args))
1199 goto finally;
1200
1201 if (PyObject_SetAttrString(self, "encoding", encoding))
1202 goto finally;
1203 if (PyObject_SetAttrString(self, "object", object))
1204 goto finally;
1205 if (PyObject_SetAttrString(self, "start", start))
1206 goto finally;
1207 if (PyObject_SetAttrString(self, "end", end))
1208 goto finally;
1209 if (PyObject_SetAttrString(self, "reason", reason))
1210 goto finally;
1211
1212 Py_INCREF(Py_None);
1213 rtnval = Py_None;
1214
1215 finally:
1216 Py_DECREF(args);
1217 return rtnval;
1218}
1219
1220
1221static PyObject *
1222UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1223{
1224 return UnicodeError__init__(self, args, &PyUnicode_Type);
1225}
1226
1227static PyObject *
1228UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1229{
1230 PyObject *encodingObj = NULL;
1231 PyObject *objectObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001232 int start;
1233 int end;
1234 PyObject *reasonObj = NULL;
1235 char buffer[1000];
1236 PyObject *result = NULL;
1237
1238 self = arg;
1239
1240 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1241 goto error;
1242
1243 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1244 goto error;
1245
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001246 if (PyUnicodeEncodeError_GetStart(self, &start))
1247 goto error;
1248
1249 if (PyUnicodeEncodeError_GetEnd(self, &end))
1250 goto error;
1251
1252 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1253 goto error;
1254
1255 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001256 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
1257 char *format;
1258 if (badchar <= 0xff)
Walter Dörwalda54b92b2003-08-12 17:34:49 +00001259 format = "'%.400s' codec can't encode character u'\\x%02x' in position %d: %.400s";
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001260 else if (badchar <= 0xffff)
Walter Dörwalda54b92b2003-08-12 17:34:49 +00001261 format = "'%.400s' codec can't encode character u'\\u%04x' in position %d: %.400s";
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001262 else
Walter Dörwalda54b92b2003-08-12 17:34:49 +00001263 format = "'%.400s' codec can't encode character u'\\U%08x' in position %d: %.400s";
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001264 PyOS_snprintf(buffer, sizeof(buffer),
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001265 format,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001266 PyString_AS_STRING(encodingObj),
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001267 badchar,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001268 start,
1269 PyString_AS_STRING(reasonObj)
1270 );
1271 }
1272 else {
1273 PyOS_snprintf(buffer, sizeof(buffer),
1274 "'%.400s' codec can't encode characters in position %d-%d: %.400s",
1275 PyString_AS_STRING(encodingObj),
1276 start,
1277 end-1,
1278 PyString_AS_STRING(reasonObj)
1279 );
1280 }
1281 result = PyString_FromString(buffer);
1282
1283error:
1284 Py_XDECREF(reasonObj);
1285 Py_XDECREF(objectObj);
1286 Py_XDECREF(encodingObj);
1287 return result;
1288}
1289
1290static PyMethodDef UnicodeEncodeError_methods[] = {
1291 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1292 {"__str__", UnicodeEncodeError__str__, METH_O},
1293 {NULL, NULL}
1294};
1295
1296
1297PyObject * PyUnicodeEncodeError_Create(
1298 const char *encoding, const Py_UNICODE *object, int length,
1299 int start, int end, const char *reason)
1300{
1301 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#iis",
1302 encoding, object, length, start, end, reason);
1303}
1304
1305
1306static PyObject *
1307UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1308{
1309 return UnicodeError__init__(self, args, &PyString_Type);
1310}
1311
1312static PyObject *
1313UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1314{
1315 PyObject *encodingObj = NULL;
1316 PyObject *objectObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001317 int start;
1318 int end;
1319 PyObject *reasonObj = NULL;
1320 char buffer[1000];
1321 PyObject *result = NULL;
1322
1323 self = arg;
1324
1325 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1326 goto error;
1327
1328 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1329 goto error;
1330
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001331 if (PyUnicodeDecodeError_GetStart(self, &start))
1332 goto error;
1333
1334 if (PyUnicodeDecodeError_GetEnd(self, &end))
1335 goto error;
1336
1337 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1338 goto error;
1339
1340 if (end==start+1) {
1341 PyOS_snprintf(buffer, sizeof(buffer),
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001342 "'%.400s' codec can't decode byte 0x%02x in position %d: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001343 PyString_AS_STRING(encodingObj),
1344 ((int)PyString_AS_STRING(objectObj)[start])&0xff,
1345 start,
1346 PyString_AS_STRING(reasonObj)
1347 );
1348 }
1349 else {
1350 PyOS_snprintf(buffer, sizeof(buffer),
1351 "'%.400s' codec can't decode bytes in position %d-%d: %.400s",
1352 PyString_AS_STRING(encodingObj),
1353 start,
1354 end-1,
1355 PyString_AS_STRING(reasonObj)
1356 );
1357 }
1358 result = PyString_FromString(buffer);
1359
1360error:
1361 Py_XDECREF(reasonObj);
1362 Py_XDECREF(objectObj);
1363 Py_XDECREF(encodingObj);
1364 return result;
1365}
1366
1367static PyMethodDef UnicodeDecodeError_methods[] = {
1368 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1369 {"__str__", UnicodeDecodeError__str__, METH_O},
1370 {NULL, NULL}
1371};
1372
1373
1374PyObject * PyUnicodeDecodeError_Create(
1375 const char *encoding, const char *object, int length,
1376 int start, int end, const char *reason)
1377{
1378 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
1379 encoding, object, length, start, end, reason);
1380}
1381
1382
1383static PyObject *
1384UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1385{
1386 PyObject *rtnval = NULL;
1387 PyObject *object;
1388 PyObject *start;
1389 PyObject *end;
1390 PyObject *reason;
1391
1392 if (!(self = get_self(args)))
1393 return NULL;
1394
1395 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1396 return NULL;
1397
1398 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1399 &PyUnicode_Type, &object,
1400 &PyInt_Type, &start,
1401 &PyInt_Type, &end,
1402 &PyString_Type, &reason))
1403 goto finally;
1404
1405 if (PyObject_SetAttrString(self, "args", args))
1406 goto finally;
1407
1408 if (PyObject_SetAttrString(self, "object", object))
1409 goto finally;
1410 if (PyObject_SetAttrString(self, "start", start))
1411 goto finally;
1412 if (PyObject_SetAttrString(self, "end", end))
1413 goto finally;
1414 if (PyObject_SetAttrString(self, "reason", reason))
1415 goto finally;
1416
1417 Py_INCREF(Py_None);
1418 rtnval = Py_None;
1419
1420 finally:
1421 Py_DECREF(args);
1422 return rtnval;
1423}
1424
1425
1426static PyObject *
1427UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1428{
1429 PyObject *objectObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001430 int start;
1431 int end;
1432 PyObject *reasonObj = NULL;
1433 char buffer[1000];
1434 PyObject *result = NULL;
1435
1436 self = arg;
1437
1438 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1439 goto error;
1440
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001441 if (PyUnicodeTranslateError_GetStart(self, &start))
1442 goto error;
1443
1444 if (PyUnicodeTranslateError_GetEnd(self, &end))
1445 goto error;
1446
1447 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1448 goto error;
1449
1450 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001451 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
1452 char *format;
1453 if (badchar <= 0xff)
Walter Dörwalda54b92b2003-08-12 17:34:49 +00001454 format = "can't translate character u'\\x%02x' in position %d: %.400s";
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001455 else if (badchar <= 0xffff)
Walter Dörwalda54b92b2003-08-12 17:34:49 +00001456 format = "can't translate character u'\\u%04x' in position %d: %.400s";
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001457 else
Walter Dörwalda54b92b2003-08-12 17:34:49 +00001458 format = "can't translate character u'\\U%08x' in position %d: %.400s";
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001459 PyOS_snprintf(buffer, sizeof(buffer),
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001460 format,
1461 badchar,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001462 start,
1463 PyString_AS_STRING(reasonObj)
1464 );
1465 }
1466 else {
1467 PyOS_snprintf(buffer, sizeof(buffer),
1468 "can't translate characters in position %d-%d: %.400s",
1469 start,
1470 end-1,
1471 PyString_AS_STRING(reasonObj)
1472 );
1473 }
1474 result = PyString_FromString(buffer);
1475
1476error:
1477 Py_XDECREF(reasonObj);
1478 Py_XDECREF(objectObj);
1479 return result;
1480}
1481
1482static PyMethodDef UnicodeTranslateError_methods[] = {
1483 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1484 {"__str__", UnicodeTranslateError__str__, METH_O},
1485 {NULL, NULL}
1486};
1487
1488
1489PyObject * PyUnicodeTranslateError_Create(
1490 const Py_UNICODE *object, int length,
1491 int start, int end, const char *reason)
1492{
1493 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
1494 object, length, start, end, reason);
1495}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001496#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001497
1498
Barry Warsaw675ac282000-05-26 19:05:16 +00001499
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001500/* Exception doc strings */
1501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001502PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001504PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001505
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001506PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001507
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001508PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001510PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001511
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001512PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001513
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001514PyDoc_STRVAR(ZeroDivisionError__doc__,
1515"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001517PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001518
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001519PyDoc_STRVAR(ValueError__doc__,
1520"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001522PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001523
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001524#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001525PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1526
1527PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1528
1529PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001530#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001531
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001532PyDoc_STRVAR(SystemError__doc__,
1533"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001534\n\
1535Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001536the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001537
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001538PyDoc_STRVAR(ReferenceError__doc__,
1539"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001540
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001541PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001543PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001544
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001545PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001546
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001547/* Warning category docstrings */
1548
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001549PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001550
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001551PyDoc_STRVAR(UserWarning__doc__,
1552"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001553
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001554PyDoc_STRVAR(DeprecationWarning__doc__,
1555"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001557PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001558"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001559"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001561PyDoc_STRVAR(SyntaxWarning__doc__,
1562"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001563
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001564PyDoc_STRVAR(OverflowWarning__doc__,
Tim Petersc8854432004-08-25 02:14:08 +00001565"Base class for warnings about numeric overflow. Won't exist in Python 2.5.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001566
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001567PyDoc_STRVAR(RuntimeWarning__doc__,
1568"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001569
Barry Warsaw9f007392002-08-14 15:51:29 +00001570PyDoc_STRVAR(FutureWarning__doc__,
1571"Base class for warnings about constructs that will change semantically "
1572"in the future.");
1573
Barry Warsaw675ac282000-05-26 19:05:16 +00001574
1575
1576/* module global functions */
1577static PyMethodDef functions[] = {
1578 /* Sentinel */
1579 {NULL, NULL}
1580};
1581
1582
1583
1584/* Global C API defined exceptions */
1585
1586PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001587PyObject *PyExc_StopIteration;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001588PyObject *PyExc_GeneratorExit;
Barry Warsaw675ac282000-05-26 19:05:16 +00001589PyObject *PyExc_StandardError;
1590PyObject *PyExc_ArithmeticError;
1591PyObject *PyExc_LookupError;
1592
1593PyObject *PyExc_AssertionError;
1594PyObject *PyExc_AttributeError;
1595PyObject *PyExc_EOFError;
1596PyObject *PyExc_FloatingPointError;
1597PyObject *PyExc_EnvironmentError;
1598PyObject *PyExc_IOError;
1599PyObject *PyExc_OSError;
1600PyObject *PyExc_ImportError;
1601PyObject *PyExc_IndexError;
1602PyObject *PyExc_KeyError;
1603PyObject *PyExc_KeyboardInterrupt;
1604PyObject *PyExc_MemoryError;
1605PyObject *PyExc_NameError;
1606PyObject *PyExc_OverflowError;
1607PyObject *PyExc_RuntimeError;
1608PyObject *PyExc_NotImplementedError;
1609PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001610PyObject *PyExc_IndentationError;
1611PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001612PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001613PyObject *PyExc_SystemError;
1614PyObject *PyExc_SystemExit;
1615PyObject *PyExc_UnboundLocalError;
1616PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001617PyObject *PyExc_UnicodeEncodeError;
1618PyObject *PyExc_UnicodeDecodeError;
1619PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001620PyObject *PyExc_TypeError;
1621PyObject *PyExc_ValueError;
1622PyObject *PyExc_ZeroDivisionError;
1623#ifdef MS_WINDOWS
1624PyObject *PyExc_WindowsError;
1625#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001626#ifdef __VMS
1627PyObject *PyExc_VMSError;
1628#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001629
1630/* Pre-computed MemoryError instance. Best to create this as early as
1631 * possibly and not wait until a MemoryError is actually raised!
1632 */
1633PyObject *PyExc_MemoryErrorInst;
1634
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001635/* Predefined warning categories */
1636PyObject *PyExc_Warning;
1637PyObject *PyExc_UserWarning;
1638PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001639PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001640PyObject *PyExc_SyntaxWarning;
Tim Petersc8854432004-08-25 02:14:08 +00001641/* PyExc_OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001642PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001643PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001644PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001645
Barry Warsaw675ac282000-05-26 19:05:16 +00001646
1647
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001648/* mapping between exception names and their PyObject ** */
1649static struct {
1650 char *name;
1651 PyObject **exc;
1652 PyObject **base; /* NULL == PyExc_StandardError */
1653 char *docstr;
1654 PyMethodDef *methods;
1655 int (*classinit)(PyObject *);
1656} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001657 /*
1658 * The first three classes MUST appear in exactly this order
1659 */
1660 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001661 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1662 StopIteration__doc__},
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001663 {"GeneratorExit", &PyExc_GeneratorExit, &PyExc_Exception,
1664 GeneratorExit__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001665 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1666 StandardError__doc__},
1667 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1668 /*
1669 * The rest appear in depth-first order of the hierarchy
1670 */
1671 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1672 SystemExit_methods},
1673 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1674 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1675 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1676 EnvironmentError_methods},
1677 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1678 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1679#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001680 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001681 WindowsError__doc__},
1682#endif /* MS_WINDOWS */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001683#ifdef __VMS
1684 {"VMSError", &PyExc_VMSError, &PyExc_OSError,
1685 VMSError__doc__},
1686#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001687 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1688 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1689 {"NotImplementedError", &PyExc_NotImplementedError,
1690 &PyExc_RuntimeError, NotImplementedError__doc__},
1691 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1692 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1693 UnboundLocalError__doc__},
1694 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1695 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1696 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001697 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1698 IndentationError__doc__},
1699 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1700 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001701 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1702 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1703 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1704 IndexError__doc__},
1705 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001706 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001707 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1708 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1709 OverflowError__doc__},
1710 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1711 ZeroDivisionError__doc__},
1712 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1713 FloatingPointError__doc__},
1714 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1715 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001716#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001717 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1718 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1719 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1720 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1721 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1722 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001723#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001724 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001725 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1726 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001727 /* Warning categories */
1728 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1729 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1730 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1731 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001732 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1733 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001734 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Tim Petersc8854432004-08-25 02:14:08 +00001735 /* OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001736 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1737 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001738 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1739 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001740 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1741 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001742 /* Sentinel */
1743 {NULL}
1744};
1745
1746
1747
Mark Hammonda2905272002-07-29 13:42:14 +00001748void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001749_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001750{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001751 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001752 int modnamesz = strlen(modulename);
1753 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001754 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001755
Tim Peters6d6c1a32001-08-02 04:15:00 +00001756 me = Py_InitModule(modulename, functions);
1757 if (me == NULL)
1758 goto err;
1759 mydict = PyModule_GetDict(me);
1760 if (mydict == NULL)
1761 goto err;
1762 bltinmod = PyImport_ImportModule("__builtin__");
1763 if (bltinmod == NULL)
1764 goto err;
1765 bdict = PyModule_GetDict(bltinmod);
1766 if (bdict == NULL)
1767 goto err;
1768 doc = PyString_FromString(module__doc__);
1769 if (doc == NULL)
1770 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001771
Tim Peters6d6c1a32001-08-02 04:15:00 +00001772 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001773 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001774 if (i < 0) {
1775 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001776 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001777 return;
1778 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001779
1780 /* This is the base class of all exceptions, so make it first. */
1781 if (make_Exception(modulename) ||
1782 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1783 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1784 {
1785 Py_FatalError("Base class `Exception' could not be created.");
1786 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001787
Barry Warsaw675ac282000-05-26 19:05:16 +00001788 /* Now we can programmatically create all the remaining exceptions.
1789 * Remember to start the loop at 1 to skip Exceptions.
1790 */
1791 for (i=1; exctable[i].name; i++) {
1792 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001793 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1794 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001795
1796 (void)strcpy(cname, modulename);
1797 (void)strcat(cname, ".");
1798 (void)strcat(cname, exctable[i].name);
1799
1800 if (exctable[i].base == 0)
1801 base = PyExc_StandardError;
1802 else
1803 base = *exctable[i].base;
1804
1805 status = make_class(exctable[i].exc, base, cname,
1806 exctable[i].methods,
1807 exctable[i].docstr);
1808
1809 PyMem_DEL(cname);
1810
1811 if (status)
1812 Py_FatalError("Standard exception classes could not be created.");
1813
1814 if (exctable[i].classinit) {
1815 status = (*exctable[i].classinit)(*exctable[i].exc);
1816 if (status)
1817 Py_FatalError("An exception class could not be initialized.");
1818 }
1819
1820 /* Now insert the class into both this module and the __builtin__
1821 * module.
1822 */
1823 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1824 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1825 {
1826 Py_FatalError("Module dictionary insertion problem.");
1827 }
1828 }
1829
1830 /* Now we need to pre-allocate a MemoryError instance */
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001831 args = PyTuple_New(0);
Barry Warsaw675ac282000-05-26 19:05:16 +00001832 if (!args ||
1833 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1834 {
1835 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1836 }
1837 Py_DECREF(args);
1838
1839 /* We're done with __builtin__ */
1840 Py_DECREF(bltinmod);
1841}
1842
1843
Mark Hammonda2905272002-07-29 13:42:14 +00001844void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001845_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001846{
1847 int i;
1848
1849 Py_XDECREF(PyExc_MemoryErrorInst);
1850 PyExc_MemoryErrorInst = NULL;
1851
1852 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001853 /* clear the class's dictionary, freeing up circular references
1854 * between the class and its methods.
1855 */
1856 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1857 PyDict_Clear(cdict);
1858 Py_DECREF(cdict);
1859
1860 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001861 Py_XDECREF(*exctable[i].exc);
1862 *exctable[i].exc = NULL;
1863 }
1864}