blob: ba0c6bd851f879b0bf98815f5b48b740b5b713f1 [file] [log] [blame]
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001/*
2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
3 *
4 * Thanks go to Tim Peters and Michael Hudson for debugging.
5 */
6
Thomas Wouters477c8d52006-05-27 19:21:47 +00007#define PY_SSIZE_T_CLEAN
8#include <Python.h>
9#include "structmember.h"
10#include "osdefs.h"
11
12#define MAKE_IT_NONE(x) (x) = Py_None; Py_INCREF(Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +000013
14/* NOTE: If the exception class hierarchy changes, don't forget to update
15 * Lib/test/exception_hierarchy.txt
16 */
17
Thomas Wouters477c8d52006-05-27 19:21:47 +000018/*
19 * BaseException
20 */
21static PyObject *
22BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
23{
24 PyBaseExceptionObject *self;
25
26 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +000027 if (!self)
28 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000029 /* the dict is created on the fly in PyObject_GenericSetAttr */
Guido van Rossumebe3e162007-05-17 18:20:34 +000030 self->dict = NULL;
Collin Winter1966f1c2007-09-01 20:26:44 +000031 self->traceback = self->cause = self->context = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000032
33 self->args = PyTuple_New(0);
34 if (!self->args) {
35 Py_DECREF(self);
36 return NULL;
37 }
38
Thomas Wouters477c8d52006-05-27 19:21:47 +000039 return (PyObject *)self;
40}
41
42static int
43BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
44{
Christian Heimes90aa7642007-12-19 02:45:37 +000045 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000046 return -1;
47
Thomas Wouters477c8d52006-05-27 19:21:47 +000048 Py_DECREF(self->args);
49 self->args = args;
50 Py_INCREF(self->args);
51
Thomas Wouters477c8d52006-05-27 19:21:47 +000052 return 0;
53}
54
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000055static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000056BaseException_clear(PyBaseExceptionObject *self)
57{
58 Py_CLEAR(self->dict);
59 Py_CLEAR(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000060 Py_CLEAR(self->traceback);
Collin Winter1966f1c2007-09-01 20:26:44 +000061 Py_CLEAR(self->cause);
62 Py_CLEAR(self->context);
Thomas Wouters477c8d52006-05-27 19:21:47 +000063 return 0;
64}
65
66static void
67BaseException_dealloc(PyBaseExceptionObject *self)
68{
Thomas Wouters89f507f2006-12-13 04:49:30 +000069 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000070 BaseException_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +000071 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000072}
73
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000074static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000075BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
76{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000077 Py_VISIT(self->dict);
Thomas Wouters477c8d52006-05-27 19:21:47 +000078 Py_VISIT(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000079 Py_VISIT(self->traceback);
Collin Winter1966f1c2007-09-01 20:26:44 +000080 Py_VISIT(self->cause);
81 Py_VISIT(self->context);
Thomas Wouters477c8d52006-05-27 19:21:47 +000082 return 0;
83}
84
85static PyObject *
86BaseException_str(PyBaseExceptionObject *self)
87{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000088 switch (PyTuple_GET_SIZE(self->args)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000089 case 0:
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +000090 return PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +000091 case 1:
Thomas Heller519a0422007-11-15 20:48:54 +000092 return PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +000093 default:
Thomas Heller519a0422007-11-15 20:48:54 +000094 return PyObject_Str(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +000095 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000096}
97
98static PyObject *
99BaseException_repr(PyBaseExceptionObject *self)
100{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000101 char *name;
102 char *dot;
103
Christian Heimes90aa7642007-12-19 02:45:37 +0000104 name = (char *)Py_TYPE(self)->tp_name;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000105 dot = strrchr(name, '.');
106 if (dot != NULL) name = dot+1;
107
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000108 return PyUnicode_FromFormat("%s%R", name, self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000109}
110
111/* Pickling support */
112static PyObject *
113BaseException_reduce(PyBaseExceptionObject *self)
114{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000115 if (self->args && self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000116 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000117 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000118 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000119}
120
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000121/*
122 * Needed for backward compatibility, since exceptions used to store
123 * all their attributes in the __dict__. Code is taken from cPickle's
124 * load_build function.
125 */
126static PyObject *
127BaseException_setstate(PyObject *self, PyObject *state)
128{
129 PyObject *d_key, *d_value;
130 Py_ssize_t i = 0;
131
132 if (state != Py_None) {
133 if (!PyDict_Check(state)) {
134 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
135 return NULL;
136 }
137 while (PyDict_Next(state, &i, &d_key, &d_value)) {
138 if (PyObject_SetAttr(self, d_key, d_value) < 0)
139 return NULL;
140 }
141 }
142 Py_RETURN_NONE;
143}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000144
Collin Winter828f04a2007-08-31 00:04:24 +0000145static PyObject *
146BaseException_with_traceback(PyObject *self, PyObject *tb) {
147 if (PyException_SetTraceback(self, tb))
148 return NULL;
149
150 Py_INCREF(self);
151 return self;
152}
153
Thomas Wouters477c8d52006-05-27 19:21:47 +0000154
155static PyMethodDef BaseException_methods[] = {
156 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000157 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Collin Winter828f04a2007-08-31 00:04:24 +0000158 {"with_traceback", (PyCFunction)BaseException_with_traceback, METH_O },
Thomas Wouters477c8d52006-05-27 19:21:47 +0000159 {NULL, NULL, 0, NULL},
160};
161
162
Thomas Wouters477c8d52006-05-27 19:21:47 +0000163static PyObject *
164BaseException_get_dict(PyBaseExceptionObject *self)
165{
166 if (self->dict == NULL) {
167 self->dict = PyDict_New();
168 if (!self->dict)
169 return NULL;
170 }
171 Py_INCREF(self->dict);
172 return self->dict;
173}
174
175static int
176BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
177{
178 if (val == NULL) {
179 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
180 return -1;
181 }
182 if (!PyDict_Check(val)) {
183 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
184 return -1;
185 }
186 Py_CLEAR(self->dict);
187 Py_INCREF(val);
188 self->dict = val;
189 return 0;
190}
191
192static PyObject *
193BaseException_get_args(PyBaseExceptionObject *self)
194{
195 if (self->args == NULL) {
196 Py_INCREF(Py_None);
197 return Py_None;
198 }
199 Py_INCREF(self->args);
200 return self->args;
201}
202
203static int
204BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
205{
206 PyObject *seq;
207 if (val == NULL) {
208 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
209 return -1;
210 }
211 seq = PySequence_Tuple(val);
212 if (!seq) return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000213 Py_CLEAR(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000214 self->args = seq;
215 return 0;
216}
217
Collin Winter828f04a2007-08-31 00:04:24 +0000218static PyObject *
219BaseException_get_tb(PyBaseExceptionObject *self)
220{
221 if (self->traceback == NULL) {
222 Py_INCREF(Py_None);
223 return Py_None;
224 }
225 Py_INCREF(self->traceback);
226 return self->traceback;
227}
228
229static int
230BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb)
231{
232 if (tb == NULL) {
233 PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted");
234 return -1;
235 }
236 else if (!(tb == Py_None || PyTraceBack_Check(tb))) {
237 PyErr_SetString(PyExc_TypeError,
238 "__traceback__ must be a traceback or None");
239 return -1;
240 }
241
242 Py_XINCREF(tb);
243 Py_XDECREF(self->traceback);
244 self->traceback = tb;
245 return 0;
246}
247
Guido van Rossum360e4b82007-05-14 22:51:27 +0000248
Thomas Wouters477c8d52006-05-27 19:21:47 +0000249static PyGetSetDef BaseException_getset[] = {
250 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
251 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Collin Winter828f04a2007-08-31 00:04:24 +0000252 {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000253 {NULL},
254};
255
256
Collin Winter828f04a2007-08-31 00:04:24 +0000257PyObject *
258PyException_GetTraceback(PyObject *self) {
259 PyBaseExceptionObject *base_self = (PyBaseExceptionObject *)self;
260 Py_XINCREF(base_self->traceback);
261 return base_self->traceback;
262}
263
264
265int
266PyException_SetTraceback(PyObject *self, PyObject *tb) {
267 return BaseException_set_tb((PyBaseExceptionObject *)self, tb);
268}
269
270PyObject *
271PyException_GetCause(PyObject *self) {
272 PyObject *cause = ((PyBaseExceptionObject *)self)->cause;
273 Py_XINCREF(cause);
274 return cause;
275}
276
277/* Steals a reference to cause */
278void
279PyException_SetCause(PyObject *self, PyObject *cause) {
280 PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause;
281 ((PyBaseExceptionObject *)self)->cause = cause;
282 Py_XDECREF(old_cause);
283}
284
285PyObject *
286PyException_GetContext(PyObject *self) {
287 PyObject *context = ((PyBaseExceptionObject *)self)->context;
288 Py_XINCREF(context);
289 return context;
290}
291
292/* Steals a reference to context */
293void
294PyException_SetContext(PyObject *self, PyObject *context) {
295 PyObject *old_context = ((PyBaseExceptionObject *)self)->context;
296 ((PyBaseExceptionObject *)self)->context = context;
297 Py_XDECREF(old_context);
298}
299
300
301static PyMemberDef BaseException_members[] = {
302 {"__context__", T_OBJECT, offsetof(PyBaseExceptionObject, context), 0,
303 PyDoc_STR("exception context")},
304 {"__cause__", T_OBJECT, offsetof(PyBaseExceptionObject, cause), 0,
305 PyDoc_STR("exception cause")},
306 {NULL} /* Sentinel */
307};
308
Thomas Wouters477c8d52006-05-27 19:21:47 +0000309static PyTypeObject _PyExc_BaseException = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000310 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +0000311 "BaseException", /*tp_name*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000312 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
313 0, /*tp_itemsize*/
314 (destructor)BaseException_dealloc, /*tp_dealloc*/
315 0, /*tp_print*/
316 0, /*tp_getattr*/
317 0, /*tp_setattr*/
318 0, /* tp_compare; */
319 (reprfunc)BaseException_repr, /*tp_repr*/
320 0, /*tp_as_number*/
Brett Cannonba7bf492007-02-27 00:15:55 +0000321 0, /*tp_as_sequence*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000322 0, /*tp_as_mapping*/
323 0, /*tp_hash */
324 0, /*tp_call*/
325 (reprfunc)BaseException_str, /*tp_str*/
326 PyObject_GenericGetAttr, /*tp_getattro*/
327 PyObject_GenericSetAttr, /*tp_setattro*/
328 0, /*tp_as_buffer*/
Thomas Wouters27d517b2007-02-25 20:39:11 +0000329 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
330 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000331 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
332 (traverseproc)BaseException_traverse, /* tp_traverse */
333 (inquiry)BaseException_clear, /* tp_clear */
334 0, /* tp_richcompare */
335 0, /* tp_weaklistoffset */
336 0, /* tp_iter */
337 0, /* tp_iternext */
338 BaseException_methods, /* tp_methods */
Collin Winter828f04a2007-08-31 00:04:24 +0000339 BaseException_members, /* tp_members */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000340 BaseException_getset, /* tp_getset */
341 0, /* tp_base */
342 0, /* tp_dict */
343 0, /* tp_descr_get */
344 0, /* tp_descr_set */
345 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
346 (initproc)BaseException_init, /* tp_init */
347 0, /* tp_alloc */
348 BaseException_new, /* tp_new */
349};
350/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
351from the previous implmentation and also allowing Python objects to be used
352in the API */
353PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
354
355/* note these macros omit the last semicolon so the macro invocation may
356 * include it and not look strange.
357 */
358#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
359static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000360 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000361 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000362 sizeof(PyBaseExceptionObject), \
363 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
364 0, 0, 0, 0, 0, 0, 0, \
365 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
366 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
367 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
368 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
369 (initproc)BaseException_init, 0, BaseException_new,\
370}; \
371PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
372
373#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
374static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000375 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000376 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000377 sizeof(Py ## EXCSTORE ## Object), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000378 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000379 0, 0, 0, 0, 0, \
380 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000381 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
382 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000383 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000384 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000385}; \
386PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
387
388#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
389static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000390 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000391 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000392 sizeof(Py ## EXCSTORE ## Object), 0, \
393 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
394 (reprfunc)EXCSTR, 0, 0, 0, \
395 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
396 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
397 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
398 EXCMEMBERS, 0, &_ ## EXCBASE, \
399 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000400 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000401}; \
402PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
403
404
405/*
406 * Exception extends BaseException
407 */
408SimpleExtendsException(PyExc_BaseException, Exception,
409 "Common base class for all non-exit exceptions.");
410
411
412/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000413 * TypeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000414 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000415SimpleExtendsException(PyExc_Exception, TypeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000416 "Inappropriate argument type.");
417
418
419/*
420 * StopIteration extends Exception
421 */
422SimpleExtendsException(PyExc_Exception, StopIteration,
Georg Brandla18af4e2007-04-21 15:47:16 +0000423 "Signal the end from iterator.__next__().");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000424
425
426/*
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000427 * GeneratorExit extends BaseException
Thomas Wouters477c8d52006-05-27 19:21:47 +0000428 */
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000429SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000430 "Request that a generator exit.");
431
432
433/*
434 * SystemExit extends BaseException
435 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000436
437static int
438SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
439{
440 Py_ssize_t size = PyTuple_GET_SIZE(args);
441
442 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
443 return -1;
444
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000445 if (size == 0)
446 return 0;
447 Py_CLEAR(self->code);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000448 if (size == 1)
449 self->code = PyTuple_GET_ITEM(args, 0);
450 else if (size > 1)
451 self->code = args;
452 Py_INCREF(self->code);
453 return 0;
454}
455
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000456static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000457SystemExit_clear(PySystemExitObject *self)
458{
459 Py_CLEAR(self->code);
460 return BaseException_clear((PyBaseExceptionObject *)self);
461}
462
463static void
464SystemExit_dealloc(PySystemExitObject *self)
465{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000466 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000467 SystemExit_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000468 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000469}
470
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000471static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000472SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
473{
474 Py_VISIT(self->code);
475 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
476}
477
478static PyMemberDef SystemExit_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000479 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
480 PyDoc_STR("exception code")},
481 {NULL} /* Sentinel */
482};
483
484ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
485 SystemExit_dealloc, 0, SystemExit_members, 0,
486 "Request to exit from the interpreter.");
487
488/*
489 * KeyboardInterrupt extends BaseException
490 */
491SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
492 "Program interrupted by user.");
493
494
495/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000496 * ImportError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000497 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000498SimpleExtendsException(PyExc_Exception, ImportError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000499 "Import can't find module, or can't find name in module.");
500
501
502/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000503 * EnvironmentError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000504 */
505
Thomas Wouters477c8d52006-05-27 19:21:47 +0000506/* Where a function has a single filename, such as open() or some
507 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
508 * called, giving a third argument which is the filename. But, so
509 * that old code using in-place unpacking doesn't break, e.g.:
510 *
511 * except IOError, (errno, strerror):
512 *
513 * we hack args so that it only contains two items. This also
514 * means we need our own __str__() which prints out the filename
515 * when it was supplied.
516 */
517static int
518EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
519 PyObject *kwds)
520{
521 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
522 PyObject *subslice = NULL;
523
524 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
525 return -1;
526
Thomas Wouters89f507f2006-12-13 04:49:30 +0000527 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000528 return 0;
529 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000530
531 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000532 &myerrno, &strerror, &filename)) {
533 return -1;
534 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000535 Py_CLEAR(self->myerrno); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000536 self->myerrno = myerrno;
537 Py_INCREF(self->myerrno);
538
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000539 Py_CLEAR(self->strerror); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000540 self->strerror = strerror;
541 Py_INCREF(self->strerror);
542
543 /* self->filename will remain Py_None otherwise */
544 if (filename != NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000545 Py_CLEAR(self->filename); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000546 self->filename = filename;
547 Py_INCREF(self->filename);
548
549 subslice = PyTuple_GetSlice(args, 0, 2);
550 if (!subslice)
551 return -1;
552
553 Py_DECREF(self->args); /* replacing args */
554 self->args = subslice;
555 }
556 return 0;
557}
558
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000559static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000560EnvironmentError_clear(PyEnvironmentErrorObject *self)
561{
562 Py_CLEAR(self->myerrno);
563 Py_CLEAR(self->strerror);
564 Py_CLEAR(self->filename);
565 return BaseException_clear((PyBaseExceptionObject *)self);
566}
567
568static void
569EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
570{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000571 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000572 EnvironmentError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000573 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000574}
575
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000576static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000577EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
578 void *arg)
579{
580 Py_VISIT(self->myerrno);
581 Py_VISIT(self->strerror);
582 Py_VISIT(self->filename);
583 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
584}
585
586static PyObject *
587EnvironmentError_str(PyEnvironmentErrorObject *self)
588{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000589 if (self->filename)
590 return PyUnicode_FromFormat("[Errno %S] %S: %R",
591 self->myerrno ? self->myerrno: Py_None,
592 self->strerror ? self->strerror: Py_None,
593 self->filename);
594 else if (self->myerrno && self->strerror)
595 return PyUnicode_FromFormat("[Errno %S] %S",
596 self->myerrno ? self->myerrno: Py_None,
597 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000598 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000599 return BaseException_str((PyBaseExceptionObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000600}
601
602static PyMemberDef EnvironmentError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000603 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
604 PyDoc_STR("exception errno")},
605 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
606 PyDoc_STR("exception strerror")},
607 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
608 PyDoc_STR("exception filename")},
609 {NULL} /* Sentinel */
610};
611
612
613static PyObject *
614EnvironmentError_reduce(PyEnvironmentErrorObject *self)
615{
616 PyObject *args = self->args;
617 PyObject *res = NULL, *tmp;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000618
Thomas Wouters477c8d52006-05-27 19:21:47 +0000619 /* self->args is only the first two real arguments if there was a
620 * file name given to EnvironmentError. */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000621 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000622 args = PyTuple_New(3);
623 if (!args) return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000624
625 tmp = PyTuple_GET_ITEM(self->args, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000626 Py_INCREF(tmp);
627 PyTuple_SET_ITEM(args, 0, tmp);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000628
629 tmp = PyTuple_GET_ITEM(self->args, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000630 Py_INCREF(tmp);
631 PyTuple_SET_ITEM(args, 1, tmp);
632
633 Py_INCREF(self->filename);
634 PyTuple_SET_ITEM(args, 2, self->filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000635 } else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000636 Py_INCREF(args);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000637
638 if (self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000639 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000640 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000641 res = PyTuple_Pack(2, Py_TYPE(self), args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000642 Py_DECREF(args);
643 return res;
644}
645
646
647static PyMethodDef EnvironmentError_methods[] = {
648 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
649 {NULL}
650};
651
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000652ComplexExtendsException(PyExc_Exception, EnvironmentError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653 EnvironmentError, EnvironmentError_dealloc,
654 EnvironmentError_methods, EnvironmentError_members,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000655 EnvironmentError_str,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000656 "Base class for I/O related errors.");
657
658
659/*
660 * IOError extends EnvironmentError
661 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000662MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000663 EnvironmentError, "I/O operation failed.");
664
665
666/*
667 * OSError extends EnvironmentError
668 */
669MiddlingExtendsException(PyExc_EnvironmentError, OSError,
670 EnvironmentError, "OS system call failed.");
671
672
673/*
674 * WindowsError extends OSError
675 */
676#ifdef MS_WINDOWS
677#include "errmap.h"
678
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000679static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000680WindowsError_clear(PyWindowsErrorObject *self)
681{
682 Py_CLEAR(self->myerrno);
683 Py_CLEAR(self->strerror);
684 Py_CLEAR(self->filename);
685 Py_CLEAR(self->winerror);
686 return BaseException_clear((PyBaseExceptionObject *)self);
687}
688
689static void
690WindowsError_dealloc(PyWindowsErrorObject *self)
691{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000692 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000693 WindowsError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000694 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000695}
696
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000697static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000698WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
699{
700 Py_VISIT(self->myerrno);
701 Py_VISIT(self->strerror);
702 Py_VISIT(self->filename);
703 Py_VISIT(self->winerror);
704 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
705}
706
Thomas Wouters477c8d52006-05-27 19:21:47 +0000707static int
708WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
709{
710 PyObject *o_errcode = NULL;
711 long errcode;
712 long posix_errno;
713
714 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
715 == -1)
716 return -1;
717
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000718 if (self->myerrno == NULL)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000719 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000720
721 /* Set errno to the POSIX errno, and winerror to the Win32
722 error code. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000723 errcode = PyLong_AsLong(self->myerrno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000724 if (errcode == -1 && PyErr_Occurred())
725 return -1;
726 posix_errno = winerror_to_errno(errcode);
727
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000728 Py_CLEAR(self->winerror);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000729 self->winerror = self->myerrno;
730
Christian Heimes217cfd12007-12-02 14:31:20 +0000731 o_errcode = PyLong_FromLong(posix_errno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000732 if (!o_errcode)
733 return -1;
734
735 self->myerrno = o_errcode;
736
737 return 0;
738}
739
740
741static PyObject *
742WindowsError_str(PyWindowsErrorObject *self)
743{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000744 if (self->filename)
745 return PyUnicode_FromFormat("[Error %S] %S: %R",
746 self->winerror ? self->winerror: Py_None,
747 self->strerror ? self->strerror: Py_None,
748 self->filename);
749 else if (self->winerror && self->strerror)
750 return PyUnicode_FromFormat("[Error %S] %S",
751 self->winerror ? self->winerror: Py_None,
752 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000753 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000754 return EnvironmentError_str((PyEnvironmentErrorObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000755}
756
757static PyMemberDef WindowsError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000758 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
759 PyDoc_STR("POSIX exception code")},
760 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
761 PyDoc_STR("exception strerror")},
762 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
763 PyDoc_STR("exception filename")},
764 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
765 PyDoc_STR("Win32 exception code")},
766 {NULL} /* Sentinel */
767};
768
769ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
770 WindowsError_dealloc, 0, WindowsError_members,
771 WindowsError_str, "MS-Windows OS system call failed.");
772
773#endif /* MS_WINDOWS */
774
775
776/*
777 * VMSError extends OSError (I think)
778 */
779#ifdef __VMS
780MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
781 "OpenVMS OS system call failed.");
782#endif
783
784
785/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000786 * EOFError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000787 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000788SimpleExtendsException(PyExc_Exception, EOFError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000789 "Read beyond end of file.");
790
791
792/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000793 * RuntimeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000794 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000795SimpleExtendsException(PyExc_Exception, RuntimeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000796 "Unspecified run-time error.");
797
798
799/*
800 * NotImplementedError extends RuntimeError
801 */
802SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
803 "Method or function hasn't been implemented yet.");
804
805/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000806 * NameError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000807 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000808SimpleExtendsException(PyExc_Exception, NameError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000809 "Name not found globally.");
810
811/*
812 * UnboundLocalError extends NameError
813 */
814SimpleExtendsException(PyExc_NameError, UnboundLocalError,
815 "Local name referenced but not bound to a value.");
816
817/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000818 * AttributeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000819 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000820SimpleExtendsException(PyExc_Exception, AttributeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000821 "Attribute not found.");
822
823
824/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000825 * SyntaxError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000826 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000827
828static int
829SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
830{
831 PyObject *info = NULL;
832 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
833
834 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
835 return -1;
836
837 if (lenargs >= 1) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000838 Py_CLEAR(self->msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000839 self->msg = PyTuple_GET_ITEM(args, 0);
840 Py_INCREF(self->msg);
841 }
842 if (lenargs == 2) {
843 info = PyTuple_GET_ITEM(args, 1);
844 info = PySequence_Tuple(info);
845 if (!info) return -1;
846
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000847 if (PyTuple_GET_SIZE(info) != 4) {
848 /* not a very good error message, but it's what Python 2.4 gives */
849 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
850 Py_DECREF(info);
851 return -1;
852 }
853
854 Py_CLEAR(self->filename);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000855 self->filename = PyTuple_GET_ITEM(info, 0);
856 Py_INCREF(self->filename);
857
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000858 Py_CLEAR(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859 self->lineno = PyTuple_GET_ITEM(info, 1);
860 Py_INCREF(self->lineno);
861
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000862 Py_CLEAR(self->offset);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000863 self->offset = PyTuple_GET_ITEM(info, 2);
864 Py_INCREF(self->offset);
865
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000866 Py_CLEAR(self->text);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000867 self->text = PyTuple_GET_ITEM(info, 3);
868 Py_INCREF(self->text);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000869
870 Py_DECREF(info);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000871 }
872 return 0;
873}
874
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000875static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000876SyntaxError_clear(PySyntaxErrorObject *self)
877{
878 Py_CLEAR(self->msg);
879 Py_CLEAR(self->filename);
880 Py_CLEAR(self->lineno);
881 Py_CLEAR(self->offset);
882 Py_CLEAR(self->text);
883 Py_CLEAR(self->print_file_and_line);
884 return BaseException_clear((PyBaseExceptionObject *)self);
885}
886
887static void
888SyntaxError_dealloc(PySyntaxErrorObject *self)
889{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000890 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000891 SyntaxError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000892 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000893}
894
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000895static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000896SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
897{
898 Py_VISIT(self->msg);
899 Py_VISIT(self->filename);
900 Py_VISIT(self->lineno);
901 Py_VISIT(self->offset);
902 Py_VISIT(self->text);
903 Py_VISIT(self->print_file_and_line);
904 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
905}
906
907/* This is called "my_basename" instead of just "basename" to avoid name
908 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
909 defined, and Python does define that. */
910static char *
911my_basename(char *name)
912{
913 char *cp = name;
914 char *result = name;
915
916 if (name == NULL)
917 return "???";
918 while (*cp != '\0') {
919 if (*cp == SEP)
920 result = cp + 1;
921 ++cp;
922 }
923 return result;
924}
925
926
927static PyObject *
928SyntaxError_str(PySyntaxErrorObject *self)
929{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000930 int have_lineno = 0;
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000931 char *filename = 0;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000932 /* Below, we always ignore overflow errors, just printing -1.
933 Still, we cannot allow an OverflowError to be raised, so
934 we need to call PyLong_AsLongAndOverflow. */
935 int overflow;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000936
937 /* XXX -- do all the additional formatting with filename and
938 lineno here */
939
Neal Norwitzed2b7392007-08-26 04:51:10 +0000940 if (self->filename && PyUnicode_Check(self->filename)) {
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000941 filename = PyUnicode_AsString(self->filename);
942 }
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000943 have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000944
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000945 if (!filename && !have_lineno)
Thomas Heller519a0422007-11-15 20:48:54 +0000946 return PyObject_Str(self->msg ? self->msg : Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000947
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000948 if (filename && have_lineno)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000949 return PyUnicode_FromFormat("%S (%s, line %ld)",
950 self->msg ? self->msg : Py_None,
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000951 my_basename(filename),
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000952 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000953 else if (filename)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000954 return PyUnicode_FromFormat("%S (%s)",
955 self->msg ? self->msg : Py_None,
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000956 my_basename(filename));
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000957 else /* only have_lineno */
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000958 return PyUnicode_FromFormat("%S (line %ld)",
959 self->msg ? self->msg : Py_None,
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000960 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000961}
962
963static PyMemberDef SyntaxError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000964 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
965 PyDoc_STR("exception msg")},
966 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
967 PyDoc_STR("exception filename")},
968 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
969 PyDoc_STR("exception lineno")},
970 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
971 PyDoc_STR("exception offset")},
972 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
973 PyDoc_STR("exception text")},
974 {"print_file_and_line", T_OBJECT,
975 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
976 PyDoc_STR("exception print_file_and_line")},
977 {NULL} /* Sentinel */
978};
979
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000980ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000981 SyntaxError_dealloc, 0, SyntaxError_members,
982 SyntaxError_str, "Invalid syntax.");
983
984
985/*
986 * IndentationError extends SyntaxError
987 */
988MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
989 "Improper indentation.");
990
991
992/*
993 * TabError extends IndentationError
994 */
995MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
996 "Improper mixture of spaces and tabs.");
997
998
999/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001000 * LookupError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001002SimpleExtendsException(PyExc_Exception, LookupError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001003 "Base class for lookup errors.");
1004
1005
1006/*
1007 * IndexError extends LookupError
1008 */
1009SimpleExtendsException(PyExc_LookupError, IndexError,
1010 "Sequence index out of range.");
1011
1012
1013/*
1014 * KeyError extends LookupError
1015 */
1016static PyObject *
1017KeyError_str(PyBaseExceptionObject *self)
1018{
1019 /* If args is a tuple of exactly one item, apply repr to args[0].
1020 This is done so that e.g. the exception raised by {}[''] prints
1021 KeyError: ''
1022 rather than the confusing
1023 KeyError
1024 alone. The downside is that if KeyError is raised with an explanatory
1025 string, that string will be displayed in quotes. Too bad.
1026 If args is anything else, use the default BaseException__str__().
1027 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001028 if (PyTuple_GET_SIZE(self->args) == 1) {
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001029 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001030 }
1031 return BaseException_str(self);
1032}
1033
1034ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
1035 0, 0, 0, KeyError_str, "Mapping key not found.");
1036
1037
1038/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001039 * ValueError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001040 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001041SimpleExtendsException(PyExc_Exception, ValueError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001042 "Inappropriate argument value (of correct type).");
1043
1044/*
1045 * UnicodeError extends ValueError
1046 */
1047
1048SimpleExtendsException(PyExc_ValueError, UnicodeError,
1049 "Unicode related error.");
1050
Thomas Wouters477c8d52006-05-27 19:21:47 +00001051static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001052get_string(PyObject *attr, const char *name)
Walter Dörwald612344f2007-05-04 19:28:21 +00001053{
1054 if (!attr) {
1055 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1056 return NULL;
1057 }
1058
Guido van Rossum98297ee2007-11-06 21:34:58 +00001059 if (!PyString_Check(attr)) {
Walter Dörwald612344f2007-05-04 19:28:21 +00001060 PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
1061 return NULL;
1062 }
1063 Py_INCREF(attr);
1064 return attr;
1065}
1066
1067static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001068get_unicode(PyObject *attr, const char *name)
1069{
1070 if (!attr) {
1071 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1072 return NULL;
1073 }
1074
1075 if (!PyUnicode_Check(attr)) {
1076 PyErr_Format(PyExc_TypeError,
1077 "%.200s attribute must be unicode", name);
1078 return NULL;
1079 }
1080 Py_INCREF(attr);
1081 return attr;
1082}
1083
Walter Dörwaldd2034312007-05-18 16:29:38 +00001084static int
1085set_unicodefromstring(PyObject **attr, const char *value)
1086{
1087 PyObject *obj = PyUnicode_FromString(value);
1088 if (!obj)
1089 return -1;
1090 Py_CLEAR(*attr);
1091 *attr = obj;
1092 return 0;
1093}
1094
Thomas Wouters477c8d52006-05-27 19:21:47 +00001095PyObject *
1096PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1097{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001098 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001099}
1100
1101PyObject *
1102PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1103{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001104 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001105}
1106
1107PyObject *
1108PyUnicodeEncodeError_GetObject(PyObject *exc)
1109{
1110 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1111}
1112
1113PyObject *
1114PyUnicodeDecodeError_GetObject(PyObject *exc)
1115{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001116 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001117}
1118
1119PyObject *
1120PyUnicodeTranslateError_GetObject(PyObject *exc)
1121{
1122 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1123}
1124
1125int
1126PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1127{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001128 Py_ssize_t size;
1129 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1130 "object");
1131 if (!obj)
1132 return -1;
1133 *start = ((PyUnicodeErrorObject *)exc)->start;
1134 size = PyUnicode_GET_SIZE(obj);
1135 if (*start<0)
1136 *start = 0; /*XXX check for values <0*/
1137 if (*start>=size)
1138 *start = size-1;
1139 Py_DECREF(obj);
1140 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001141}
1142
1143
1144int
1145PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1146{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001147 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001148 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001149 if (!obj)
1150 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001151 size = PyString_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001152 *start = ((PyUnicodeErrorObject *)exc)->start;
1153 if (*start<0)
1154 *start = 0;
1155 if (*start>=size)
1156 *start = size-1;
1157 Py_DECREF(obj);
1158 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001159}
1160
1161
1162int
1163PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1164{
1165 return PyUnicodeEncodeError_GetStart(exc, start);
1166}
1167
1168
1169int
1170PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1171{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001172 ((PyUnicodeErrorObject *)exc)->start = start;
1173 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001174}
1175
1176
1177int
1178PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1179{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001180 ((PyUnicodeErrorObject *)exc)->start = start;
1181 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001182}
1183
1184
1185int
1186PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1187{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001188 ((PyUnicodeErrorObject *)exc)->start = start;
1189 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001190}
1191
1192
1193int
1194PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1195{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001196 Py_ssize_t size;
1197 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1198 "object");
1199 if (!obj)
1200 return -1;
1201 *end = ((PyUnicodeErrorObject *)exc)->end;
1202 size = PyUnicode_GET_SIZE(obj);
1203 if (*end<1)
1204 *end = 1;
1205 if (*end>size)
1206 *end = size;
1207 Py_DECREF(obj);
1208 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001209}
1210
1211
1212int
1213PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1214{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001215 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001216 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001217 if (!obj)
1218 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001219 size = PyString_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001220 *end = ((PyUnicodeErrorObject *)exc)->end;
1221 if (*end<1)
1222 *end = 1;
1223 if (*end>size)
1224 *end = size;
1225 Py_DECREF(obj);
1226 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001227}
1228
1229
1230int
1231PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1232{
1233 return PyUnicodeEncodeError_GetEnd(exc, start);
1234}
1235
1236
1237int
1238PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1239{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001240 ((PyUnicodeErrorObject *)exc)->end = end;
1241 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001242}
1243
1244
1245int
1246PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1247{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001248 ((PyUnicodeErrorObject *)exc)->end = end;
1249 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001250}
1251
1252
1253int
1254PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1255{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001256 ((PyUnicodeErrorObject *)exc)->end = end;
1257 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001258}
1259
1260PyObject *
1261PyUnicodeEncodeError_GetReason(PyObject *exc)
1262{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001263 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001264}
1265
1266
1267PyObject *
1268PyUnicodeDecodeError_GetReason(PyObject *exc)
1269{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001270 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001271}
1272
1273
1274PyObject *
1275PyUnicodeTranslateError_GetReason(PyObject *exc)
1276{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001277 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001278}
1279
1280
1281int
1282PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1283{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001284 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1285 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001286}
1287
1288
1289int
1290PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1291{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001292 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1293 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001294}
1295
1296
1297int
1298PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1299{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001300 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1301 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001302}
1303
1304
Thomas Wouters477c8d52006-05-27 19:21:47 +00001305static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001306UnicodeError_clear(PyUnicodeErrorObject *self)
1307{
1308 Py_CLEAR(self->encoding);
1309 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001310 Py_CLEAR(self->reason);
1311 return BaseException_clear((PyBaseExceptionObject *)self);
1312}
1313
1314static void
1315UnicodeError_dealloc(PyUnicodeErrorObject *self)
1316{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001317 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001318 UnicodeError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +00001319 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001320}
1321
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001322static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001323UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1324{
1325 Py_VISIT(self->encoding);
1326 Py_VISIT(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001327 Py_VISIT(self->reason);
1328 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1329}
1330
1331static PyMemberDef UnicodeError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001332 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1333 PyDoc_STR("exception encoding")},
1334 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1335 PyDoc_STR("exception object")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001336 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001337 PyDoc_STR("exception start")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001338 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001339 PyDoc_STR("exception end")},
1340 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1341 PyDoc_STR("exception reason")},
1342 {NULL} /* Sentinel */
1343};
1344
1345
1346/*
1347 * UnicodeEncodeError extends UnicodeError
1348 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001349
1350static int
1351UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1352{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001353 PyUnicodeErrorObject *err;
1354
Thomas Wouters477c8d52006-05-27 19:21:47 +00001355 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1356 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001357
1358 err = (PyUnicodeErrorObject *)self;
1359
1360 Py_CLEAR(err->encoding);
1361 Py_CLEAR(err->object);
1362 Py_CLEAR(err->reason);
1363
1364 if (!PyArg_ParseTuple(args, "O!O!nnO!",
1365 &PyUnicode_Type, &err->encoding,
1366 &PyUnicode_Type, &err->object,
1367 &err->start,
1368 &err->end,
1369 &PyUnicode_Type, &err->reason)) {
1370 err->encoding = err->object = err->reason = NULL;
1371 return -1;
1372 }
1373
1374 Py_INCREF(err->encoding);
1375 Py_INCREF(err->object);
1376 Py_INCREF(err->reason);
1377
1378 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001379}
1380
1381static PyObject *
1382UnicodeEncodeError_str(PyObject *self)
1383{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001384 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001385
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001386 if (uself->end==uself->start+1) {
1387 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001388 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001389 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001390 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001391 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001392 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001393 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001394 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001395 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001396 fmt,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001397 ((PyUnicodeErrorObject *)self)->encoding,
Walter Dörwald787b03b2007-06-05 13:29:29 +00001398 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001399 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001400 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001401 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001402 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001403 return PyUnicode_FromFormat(
1404 "'%U' codec can't encode characters in position %zd-%zd: %U",
1405 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001406 uself->start,
1407 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001408 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001409 );
1410}
1411
1412static PyTypeObject _PyExc_UnicodeEncodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001413 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001414 "UnicodeEncodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001415 sizeof(PyUnicodeErrorObject), 0,
1416 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1417 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1418 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001419 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1420 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001421 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001422 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001423};
1424PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1425
1426PyObject *
1427PyUnicodeEncodeError_Create(
1428 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1429 Py_ssize_t start, Py_ssize_t end, const char *reason)
1430{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001431 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "Uu#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001432 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001433}
1434
1435
1436/*
1437 * UnicodeDecodeError extends UnicodeError
1438 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001439
1440static int
1441UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1442{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001443 PyUnicodeErrorObject *ude;
1444 const char *data;
1445 Py_ssize_t size;
1446
Thomas Wouters477c8d52006-05-27 19:21:47 +00001447 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1448 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001449
1450 ude = (PyUnicodeErrorObject *)self;
1451
1452 Py_CLEAR(ude->encoding);
1453 Py_CLEAR(ude->object);
1454 Py_CLEAR(ude->reason);
1455
1456 if (!PyArg_ParseTuple(args, "O!OnnO!",
1457 &PyUnicode_Type, &ude->encoding,
1458 &ude->object,
1459 &ude->start,
1460 &ude->end,
1461 &PyUnicode_Type, &ude->reason)) {
1462 ude->encoding = ude->object = ude->reason = NULL;
1463 return -1;
1464 }
1465
1466 if (!PyString_Check(ude->object)) {
1467 if (PyObject_AsReadBuffer(ude->object, (const void **)&data, &size)) {
1468 ude->encoding = ude->object = ude->reason = NULL;
1469 return -1;
1470 }
1471 ude->object = PyString_FromStringAndSize(data, size);
1472 }
1473 else {
1474 Py_INCREF(ude->object);
1475 }
1476
1477 Py_INCREF(ude->encoding);
1478 Py_INCREF(ude->reason);
1479
1480 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001481}
1482
1483static PyObject *
1484UnicodeDecodeError_str(PyObject *self)
1485{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001486 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001487
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001488 if (uself->end==uself->start+1) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00001489 int byte = (int)(PyString_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001490 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001491 "'%U' codec can't decode byte 0x%02x in position %zd: %U",
Walter Dörwaldd2034312007-05-18 16:29:38 +00001492 ((PyUnicodeErrorObject *)self)->encoding,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001493 byte,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001494 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001495 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001496 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001497 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001498 return PyUnicode_FromFormat(
1499 "'%U' codec can't decode bytes in position %zd-%zd: %U",
1500 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001501 uself->start,
1502 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001503 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001504 );
1505}
1506
1507static PyTypeObject _PyExc_UnicodeDecodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001508 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001509 "UnicodeDecodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001510 sizeof(PyUnicodeErrorObject), 0,
1511 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1512 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1513 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001514 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1515 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001516 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001517 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001518};
1519PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1520
1521PyObject *
1522PyUnicodeDecodeError_Create(
1523 const char *encoding, const char *object, Py_ssize_t length,
1524 Py_ssize_t start, Py_ssize_t end, const char *reason)
1525{
1526 assert(length < INT_MAX);
1527 assert(start < INT_MAX);
1528 assert(end < INT_MAX);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001529 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "Uy#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001530 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001531}
1532
1533
1534/*
1535 * UnicodeTranslateError extends UnicodeError
1536 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001537
1538static int
1539UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1540 PyObject *kwds)
1541{
1542 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1543 return -1;
1544
1545 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001546 Py_CLEAR(self->reason);
1547
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001548 if (!PyArg_ParseTuple(args, "O!nnO!",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001549 &PyUnicode_Type, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001550 &self->start,
1551 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001552 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001553 self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001554 return -1;
1555 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001556
Thomas Wouters477c8d52006-05-27 19:21:47 +00001557 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001558 Py_INCREF(self->reason);
1559
1560 return 0;
1561}
1562
1563
1564static PyObject *
1565UnicodeTranslateError_str(PyObject *self)
1566{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001567 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001568
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001569 if (uself->end==uself->start+1) {
1570 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001571 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001572 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001573 fmt = "can't translate character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001574 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001575 fmt = "can't translate character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001576 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001577 fmt = "can't translate character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001578 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001579 fmt,
1580 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001581 uself->start,
1582 uself->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001583 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001584 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001585 return PyUnicode_FromFormat(
1586 "can't translate characters in position %zd-%zd: %U",
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001587 uself->start,
1588 uself->end-1,
1589 uself->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001590 );
1591}
1592
1593static PyTypeObject _PyExc_UnicodeTranslateError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001594 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001595 "UnicodeTranslateError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001596 sizeof(PyUnicodeErrorObject), 0,
1597 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1598 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1599 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001600 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001601 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1602 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001603 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001604};
1605PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1606
1607PyObject *
1608PyUnicodeTranslateError_Create(
1609 const Py_UNICODE *object, Py_ssize_t length,
1610 Py_ssize_t start, Py_ssize_t end, const char *reason)
1611{
1612 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001613 object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001614}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001615
1616
1617/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001618 * AssertionError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001619 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001620SimpleExtendsException(PyExc_Exception, AssertionError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001621 "Assertion failed.");
1622
1623
1624/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001625 * ArithmeticError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001626 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001627SimpleExtendsException(PyExc_Exception, ArithmeticError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001628 "Base class for arithmetic errors.");
1629
1630
1631/*
1632 * FloatingPointError extends ArithmeticError
1633 */
1634SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
1635 "Floating point operation failed.");
1636
1637
1638/*
1639 * OverflowError extends ArithmeticError
1640 */
1641SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
1642 "Result too large to be represented.");
1643
1644
1645/*
1646 * ZeroDivisionError extends ArithmeticError
1647 */
1648SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
1649 "Second argument to a division or modulo operation was zero.");
1650
1651
1652/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001653 * SystemError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001654 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001655SimpleExtendsException(PyExc_Exception, SystemError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001656 "Internal error in the Python interpreter.\n"
1657 "\n"
1658 "Please report this to the Python maintainer, along with the traceback,\n"
1659 "the Python version, and the hardware/OS platform and version.");
1660
1661
1662/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001663 * ReferenceError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001664 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001665SimpleExtendsException(PyExc_Exception, ReferenceError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001666 "Weak ref proxy used after referent went away.");
1667
1668
1669/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001670 * MemoryError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001671 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001672SimpleExtendsException(PyExc_Exception, MemoryError, "Out of memory.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001673
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001674/*
1675 * BufferError extends Exception
1676 */
1677SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error.");
1678
Thomas Wouters477c8d52006-05-27 19:21:47 +00001679
1680/* Warning category docstrings */
1681
1682/*
1683 * Warning extends Exception
1684 */
1685SimpleExtendsException(PyExc_Exception, Warning,
1686 "Base class for warning categories.");
1687
1688
1689/*
1690 * UserWarning extends Warning
1691 */
1692SimpleExtendsException(PyExc_Warning, UserWarning,
1693 "Base class for warnings generated by user code.");
1694
1695
1696/*
1697 * DeprecationWarning extends Warning
1698 */
1699SimpleExtendsException(PyExc_Warning, DeprecationWarning,
1700 "Base class for warnings about deprecated features.");
1701
1702
1703/*
1704 * PendingDeprecationWarning extends Warning
1705 */
1706SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1707 "Base class for warnings about features which will be deprecated\n"
1708 "in the future.");
1709
1710
1711/*
1712 * SyntaxWarning extends Warning
1713 */
1714SimpleExtendsException(PyExc_Warning, SyntaxWarning,
1715 "Base class for warnings about dubious syntax.");
1716
1717
1718/*
1719 * RuntimeWarning extends Warning
1720 */
1721SimpleExtendsException(PyExc_Warning, RuntimeWarning,
1722 "Base class for warnings about dubious runtime behavior.");
1723
1724
1725/*
1726 * FutureWarning extends Warning
1727 */
1728SimpleExtendsException(PyExc_Warning, FutureWarning,
1729 "Base class for warnings about constructs that will change semantically\n"
1730 "in the future.");
1731
1732
1733/*
1734 * ImportWarning extends Warning
1735 */
1736SimpleExtendsException(PyExc_Warning, ImportWarning,
1737 "Base class for warnings about probable mistakes in module imports");
1738
1739
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001740/*
1741 * UnicodeWarning extends Warning
1742 */
1743SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1744 "Base class for warnings about Unicode related problems, mostly\n"
1745 "related to conversion problems.");
1746
Guido van Rossum98297ee2007-11-06 21:34:58 +00001747/*
1748 * BytesWarning extends Warning
1749 */
1750SimpleExtendsException(PyExc_Warning, BytesWarning,
1751 "Base class for warnings about bytes and buffer related problems, mostly\n"
1752 "related to conversion from str or comparing to str.");
1753
1754
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001755
Thomas Wouters477c8d52006-05-27 19:21:47 +00001756/* Pre-computed MemoryError instance. Best to create this as early as
1757 * possible and not wait until a MemoryError is actually raised!
1758 */
1759PyObject *PyExc_MemoryErrorInst=NULL;
1760
Thomas Wouters89d996e2007-09-08 17:39:28 +00001761/* Pre-computed RuntimeError instance for when recursion depth is reached.
1762 Meant to be used when normalizing the exception for exceeding the recursion
1763 depth will cause its own infinite recursion.
1764*/
1765PyObject *PyExc_RecursionErrorInst = NULL;
1766
Thomas Wouters477c8d52006-05-27 19:21:47 +00001767#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1768 Py_FatalError("exceptions bootstrapping error.");
1769
1770#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001771 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1772 Py_FatalError("Module dictionary insertion problem.");
1773
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001774#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1775/* crt variable checking in VisualStudio .NET 2005 */
1776#include <crtdbg.h>
1777
1778static int prevCrtReportMode;
1779static _invalid_parameter_handler prevCrtHandler;
1780
1781/* Invalid parameter handler. Sets a ValueError exception */
1782static void
1783InvalidParameterHandler(
1784 const wchar_t * expression,
1785 const wchar_t * function,
1786 const wchar_t * file,
1787 unsigned int line,
1788 uintptr_t pReserved)
1789{
1790 /* Do nothing, allow execution to continue. Usually this
1791 * means that the CRT will set errno to EINVAL
1792 */
1793}
1794#endif
1795
1796
Thomas Wouters477c8d52006-05-27 19:21:47 +00001797PyMODINIT_FUNC
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001798_PyExc_Init(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001799{
Neal Norwitz2633c692007-02-26 22:22:47 +00001800 PyObject *bltinmod, *bdict;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001801
1802 PRE_INIT(BaseException)
1803 PRE_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001804 PRE_INIT(TypeError)
1805 PRE_INIT(StopIteration)
1806 PRE_INIT(GeneratorExit)
1807 PRE_INIT(SystemExit)
1808 PRE_INIT(KeyboardInterrupt)
1809 PRE_INIT(ImportError)
1810 PRE_INIT(EnvironmentError)
1811 PRE_INIT(IOError)
1812 PRE_INIT(OSError)
1813#ifdef MS_WINDOWS
1814 PRE_INIT(WindowsError)
1815#endif
1816#ifdef __VMS
1817 PRE_INIT(VMSError)
1818#endif
1819 PRE_INIT(EOFError)
1820 PRE_INIT(RuntimeError)
1821 PRE_INIT(NotImplementedError)
1822 PRE_INIT(NameError)
1823 PRE_INIT(UnboundLocalError)
1824 PRE_INIT(AttributeError)
1825 PRE_INIT(SyntaxError)
1826 PRE_INIT(IndentationError)
1827 PRE_INIT(TabError)
1828 PRE_INIT(LookupError)
1829 PRE_INIT(IndexError)
1830 PRE_INIT(KeyError)
1831 PRE_INIT(ValueError)
1832 PRE_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001833 PRE_INIT(UnicodeEncodeError)
1834 PRE_INIT(UnicodeDecodeError)
1835 PRE_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001836 PRE_INIT(AssertionError)
1837 PRE_INIT(ArithmeticError)
1838 PRE_INIT(FloatingPointError)
1839 PRE_INIT(OverflowError)
1840 PRE_INIT(ZeroDivisionError)
1841 PRE_INIT(SystemError)
1842 PRE_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001843 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001844 PRE_INIT(MemoryError)
1845 PRE_INIT(Warning)
1846 PRE_INIT(UserWarning)
1847 PRE_INIT(DeprecationWarning)
1848 PRE_INIT(PendingDeprecationWarning)
1849 PRE_INIT(SyntaxWarning)
1850 PRE_INIT(RuntimeWarning)
1851 PRE_INIT(FutureWarning)
1852 PRE_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001853 PRE_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001854 PRE_INIT(BytesWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001855
Georg Brandl1a3284e2007-12-02 09:40:06 +00001856 bltinmod = PyImport_ImportModule("builtins");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001857 if (bltinmod == NULL)
1858 Py_FatalError("exceptions bootstrapping error.");
1859 bdict = PyModule_GetDict(bltinmod);
1860 if (bdict == NULL)
1861 Py_FatalError("exceptions bootstrapping error.");
1862
1863 POST_INIT(BaseException)
1864 POST_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001865 POST_INIT(TypeError)
1866 POST_INIT(StopIteration)
1867 POST_INIT(GeneratorExit)
1868 POST_INIT(SystemExit)
1869 POST_INIT(KeyboardInterrupt)
1870 POST_INIT(ImportError)
1871 POST_INIT(EnvironmentError)
1872 POST_INIT(IOError)
1873 POST_INIT(OSError)
1874#ifdef MS_WINDOWS
1875 POST_INIT(WindowsError)
1876#endif
1877#ifdef __VMS
1878 POST_INIT(VMSError)
1879#endif
1880 POST_INIT(EOFError)
1881 POST_INIT(RuntimeError)
1882 POST_INIT(NotImplementedError)
1883 POST_INIT(NameError)
1884 POST_INIT(UnboundLocalError)
1885 POST_INIT(AttributeError)
1886 POST_INIT(SyntaxError)
1887 POST_INIT(IndentationError)
1888 POST_INIT(TabError)
1889 POST_INIT(LookupError)
1890 POST_INIT(IndexError)
1891 POST_INIT(KeyError)
1892 POST_INIT(ValueError)
1893 POST_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001894 POST_INIT(UnicodeEncodeError)
1895 POST_INIT(UnicodeDecodeError)
1896 POST_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001897 POST_INIT(AssertionError)
1898 POST_INIT(ArithmeticError)
1899 POST_INIT(FloatingPointError)
1900 POST_INIT(OverflowError)
1901 POST_INIT(ZeroDivisionError)
1902 POST_INIT(SystemError)
1903 POST_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001904 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001905 POST_INIT(MemoryError)
1906 POST_INIT(Warning)
1907 POST_INIT(UserWarning)
1908 POST_INIT(DeprecationWarning)
1909 POST_INIT(PendingDeprecationWarning)
1910 POST_INIT(SyntaxWarning)
1911 POST_INIT(RuntimeWarning)
1912 POST_INIT(FutureWarning)
1913 POST_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001914 POST_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001915 POST_INIT(BytesWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001916
1917 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
1918 if (!PyExc_MemoryErrorInst)
1919 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1920
Thomas Wouters89d996e2007-09-08 17:39:28 +00001921 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
1922 if (!PyExc_RecursionErrorInst)
1923 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
1924 "recursion errors");
1925 else {
1926 PyBaseExceptionObject *err_inst =
1927 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
1928 PyObject *args_tuple;
1929 PyObject *exc_message;
Neal Norwitzbed67842007-10-27 04:00:45 +00001930 exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
Thomas Wouters89d996e2007-09-08 17:39:28 +00001931 if (!exc_message)
1932 Py_FatalError("cannot allocate argument for RuntimeError "
1933 "pre-allocation");
1934 args_tuple = PyTuple_Pack(1, exc_message);
1935 if (!args_tuple)
1936 Py_FatalError("cannot allocate tuple for RuntimeError "
1937 "pre-allocation");
1938 Py_DECREF(exc_message);
1939 if (BaseException_init(err_inst, args_tuple, NULL))
1940 Py_FatalError("init of pre-allocated RuntimeError failed");
1941 Py_DECREF(args_tuple);
1942 }
1943
Thomas Wouters477c8d52006-05-27 19:21:47 +00001944 Py_DECREF(bltinmod);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001945
1946#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1947 /* Set CRT argument error handler */
1948 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
1949 /* turn off assertions in debug mode */
1950 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
1951#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001952}
1953
1954void
1955_PyExc_Fini(void)
1956{
1957 Py_XDECREF(PyExc_MemoryErrorInst);
1958 PyExc_MemoryErrorInst = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001959#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1960 /* reset CRT error handling */
1961 _set_invalid_parameter_handler(prevCrtHandler);
1962 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
1963#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001964}