blob: cbcda7b06602d57b83b44f94dea931e6cd2a952e [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{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000116 return PyTuple_Pack(3, Py_Type(self), self->args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000117 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000639 res = PyTuple_Pack(3, Py_Type(self), args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000640 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000932
933 /* XXX -- do all the additional formatting with filename and
934 lineno here */
935
Neal Norwitzed2b7392007-08-26 04:51:10 +0000936 if (self->filename && PyUnicode_Check(self->filename)) {
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000937 filename = PyUnicode_AsString(self->filename);
938 }
Guido van Rossumddefaf32007-01-14 03:31:43 +0000939 have_lineno = (self->lineno != NULL) && PyInt_CheckExact(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000940
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000941 if (!filename && !have_lineno)
Thomas Heller519a0422007-11-15 20:48:54 +0000942 return PyObject_Str(self->msg ? self->msg : Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000943
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000944 if (filename && have_lineno)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000945 return PyUnicode_FromFormat("%S (%s, line %ld)",
946 self->msg ? self->msg : Py_None,
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000947 my_basename(filename),
Christian Heimes217cfd12007-12-02 14:31:20 +0000948 PyLong_AsLong(self->lineno));
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000949 else if (filename)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000950 return PyUnicode_FromFormat("%S (%s)",
951 self->msg ? self->msg : Py_None,
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000952 my_basename(filename));
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000953 else /* only have_lineno */
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000954 return PyUnicode_FromFormat("%S (line %ld)",
955 self->msg ? self->msg : Py_None,
Christian Heimes217cfd12007-12-02 14:31:20 +0000956 PyLong_AsLong(self->lineno));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957}
958
959static PyMemberDef SyntaxError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000960 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
961 PyDoc_STR("exception msg")},
962 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
963 PyDoc_STR("exception filename")},
964 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
965 PyDoc_STR("exception lineno")},
966 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
967 PyDoc_STR("exception offset")},
968 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
969 PyDoc_STR("exception text")},
970 {"print_file_and_line", T_OBJECT,
971 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
972 PyDoc_STR("exception print_file_and_line")},
973 {NULL} /* Sentinel */
974};
975
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000976ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000977 SyntaxError_dealloc, 0, SyntaxError_members,
978 SyntaxError_str, "Invalid syntax.");
979
980
981/*
982 * IndentationError extends SyntaxError
983 */
984MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
985 "Improper indentation.");
986
987
988/*
989 * TabError extends IndentationError
990 */
991MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
992 "Improper mixture of spaces and tabs.");
993
994
995/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000996 * LookupError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000997 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000998SimpleExtendsException(PyExc_Exception, LookupError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000999 "Base class for lookup errors.");
1000
1001
1002/*
1003 * IndexError extends LookupError
1004 */
1005SimpleExtendsException(PyExc_LookupError, IndexError,
1006 "Sequence index out of range.");
1007
1008
1009/*
1010 * KeyError extends LookupError
1011 */
1012static PyObject *
1013KeyError_str(PyBaseExceptionObject *self)
1014{
1015 /* If args is a tuple of exactly one item, apply repr to args[0].
1016 This is done so that e.g. the exception raised by {}[''] prints
1017 KeyError: ''
1018 rather than the confusing
1019 KeyError
1020 alone. The downside is that if KeyError is raised with an explanatory
1021 string, that string will be displayed in quotes. Too bad.
1022 If args is anything else, use the default BaseException__str__().
1023 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001024 if (PyTuple_GET_SIZE(self->args) == 1) {
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001025 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001026 }
1027 return BaseException_str(self);
1028}
1029
1030ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
1031 0, 0, 0, KeyError_str, "Mapping key not found.");
1032
1033
1034/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001035 * ValueError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001036 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001037SimpleExtendsException(PyExc_Exception, ValueError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001038 "Inappropriate argument value (of correct type).");
1039
1040/*
1041 * UnicodeError extends ValueError
1042 */
1043
1044SimpleExtendsException(PyExc_ValueError, UnicodeError,
1045 "Unicode related error.");
1046
Thomas Wouters477c8d52006-05-27 19:21:47 +00001047static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001048get_string(PyObject *attr, const char *name)
Walter Dörwald612344f2007-05-04 19:28:21 +00001049{
1050 if (!attr) {
1051 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1052 return NULL;
1053 }
1054
Guido van Rossum98297ee2007-11-06 21:34:58 +00001055 if (!PyString_Check(attr)) {
Walter Dörwald612344f2007-05-04 19:28:21 +00001056 PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
1057 return NULL;
1058 }
1059 Py_INCREF(attr);
1060 return attr;
1061}
1062
1063static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001064get_unicode(PyObject *attr, const char *name)
1065{
1066 if (!attr) {
1067 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1068 return NULL;
1069 }
1070
1071 if (!PyUnicode_Check(attr)) {
1072 PyErr_Format(PyExc_TypeError,
1073 "%.200s attribute must be unicode", name);
1074 return NULL;
1075 }
1076 Py_INCREF(attr);
1077 return attr;
1078}
1079
Walter Dörwaldd2034312007-05-18 16:29:38 +00001080static int
1081set_unicodefromstring(PyObject **attr, const char *value)
1082{
1083 PyObject *obj = PyUnicode_FromString(value);
1084 if (!obj)
1085 return -1;
1086 Py_CLEAR(*attr);
1087 *attr = obj;
1088 return 0;
1089}
1090
Thomas Wouters477c8d52006-05-27 19:21:47 +00001091PyObject *
1092PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1093{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001094 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001095}
1096
1097PyObject *
1098PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1099{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001100 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001101}
1102
1103PyObject *
1104PyUnicodeEncodeError_GetObject(PyObject *exc)
1105{
1106 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1107}
1108
1109PyObject *
1110PyUnicodeDecodeError_GetObject(PyObject *exc)
1111{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001112 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001113}
1114
1115PyObject *
1116PyUnicodeTranslateError_GetObject(PyObject *exc)
1117{
1118 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1119}
1120
1121int
1122PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1123{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001124 Py_ssize_t size;
1125 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1126 "object");
1127 if (!obj)
1128 return -1;
1129 *start = ((PyUnicodeErrorObject *)exc)->start;
1130 size = PyUnicode_GET_SIZE(obj);
1131 if (*start<0)
1132 *start = 0; /*XXX check for values <0*/
1133 if (*start>=size)
1134 *start = size-1;
1135 Py_DECREF(obj);
1136 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001137}
1138
1139
1140int
1141PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1142{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001143 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001144 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001145 if (!obj)
1146 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001147 size = PyString_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001148 *start = ((PyUnicodeErrorObject *)exc)->start;
1149 if (*start<0)
1150 *start = 0;
1151 if (*start>=size)
1152 *start = size-1;
1153 Py_DECREF(obj);
1154 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001155}
1156
1157
1158int
1159PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1160{
1161 return PyUnicodeEncodeError_GetStart(exc, start);
1162}
1163
1164
1165int
1166PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1167{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001168 ((PyUnicodeErrorObject *)exc)->start = start;
1169 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001170}
1171
1172
1173int
1174PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1175{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001176 ((PyUnicodeErrorObject *)exc)->start = start;
1177 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001178}
1179
1180
1181int
1182PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1183{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001184 ((PyUnicodeErrorObject *)exc)->start = start;
1185 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001186}
1187
1188
1189int
1190PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1191{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001192 Py_ssize_t size;
1193 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1194 "object");
1195 if (!obj)
1196 return -1;
1197 *end = ((PyUnicodeErrorObject *)exc)->end;
1198 size = PyUnicode_GET_SIZE(obj);
1199 if (*end<1)
1200 *end = 1;
1201 if (*end>size)
1202 *end = size;
1203 Py_DECREF(obj);
1204 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001205}
1206
1207
1208int
1209PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1210{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001211 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001212 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001213 if (!obj)
1214 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001215 size = PyString_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001216 *end = ((PyUnicodeErrorObject *)exc)->end;
1217 if (*end<1)
1218 *end = 1;
1219 if (*end>size)
1220 *end = size;
1221 Py_DECREF(obj);
1222 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001223}
1224
1225
1226int
1227PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1228{
1229 return PyUnicodeEncodeError_GetEnd(exc, start);
1230}
1231
1232
1233int
1234PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1235{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001236 ((PyUnicodeErrorObject *)exc)->end = end;
1237 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001238}
1239
1240
1241int
1242PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1243{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001244 ((PyUnicodeErrorObject *)exc)->end = end;
1245 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001246}
1247
1248
1249int
1250PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1251{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001252 ((PyUnicodeErrorObject *)exc)->end = end;
1253 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001254}
1255
1256PyObject *
1257PyUnicodeEncodeError_GetReason(PyObject *exc)
1258{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001259 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001260}
1261
1262
1263PyObject *
1264PyUnicodeDecodeError_GetReason(PyObject *exc)
1265{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001266 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001267}
1268
1269
1270PyObject *
1271PyUnicodeTranslateError_GetReason(PyObject *exc)
1272{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001273 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001274}
1275
1276
1277int
1278PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1279{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001280 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1281 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001282}
1283
1284
1285int
1286PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1287{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001288 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1289 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001290}
1291
1292
1293int
1294PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1295{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001296 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1297 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001298}
1299
1300
Thomas Wouters477c8d52006-05-27 19:21:47 +00001301static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001302UnicodeError_clear(PyUnicodeErrorObject *self)
1303{
1304 Py_CLEAR(self->encoding);
1305 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001306 Py_CLEAR(self->reason);
1307 return BaseException_clear((PyBaseExceptionObject *)self);
1308}
1309
1310static void
1311UnicodeError_dealloc(PyUnicodeErrorObject *self)
1312{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001313 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001314 UnicodeError_clear(self);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001315 Py_Type(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001316}
1317
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001318static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001319UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1320{
1321 Py_VISIT(self->encoding);
1322 Py_VISIT(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001323 Py_VISIT(self->reason);
1324 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1325}
1326
1327static PyMemberDef UnicodeError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001328 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1329 PyDoc_STR("exception encoding")},
1330 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1331 PyDoc_STR("exception object")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001332 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001333 PyDoc_STR("exception start")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001334 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001335 PyDoc_STR("exception end")},
1336 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1337 PyDoc_STR("exception reason")},
1338 {NULL} /* Sentinel */
1339};
1340
1341
1342/*
1343 * UnicodeEncodeError extends UnicodeError
1344 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001345
1346static int
1347UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1348{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001349 PyUnicodeErrorObject *err;
1350
Thomas Wouters477c8d52006-05-27 19:21:47 +00001351 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1352 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001353
1354 err = (PyUnicodeErrorObject *)self;
1355
1356 Py_CLEAR(err->encoding);
1357 Py_CLEAR(err->object);
1358 Py_CLEAR(err->reason);
1359
1360 if (!PyArg_ParseTuple(args, "O!O!nnO!",
1361 &PyUnicode_Type, &err->encoding,
1362 &PyUnicode_Type, &err->object,
1363 &err->start,
1364 &err->end,
1365 &PyUnicode_Type, &err->reason)) {
1366 err->encoding = err->object = err->reason = NULL;
1367 return -1;
1368 }
1369
1370 Py_INCREF(err->encoding);
1371 Py_INCREF(err->object);
1372 Py_INCREF(err->reason);
1373
1374 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001375}
1376
1377static PyObject *
1378UnicodeEncodeError_str(PyObject *self)
1379{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001380 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001381
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001382 if (uself->end==uself->start+1) {
1383 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001384 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001385 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001386 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001387 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001388 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001389 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001390 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001391 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001392 fmt,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001393 ((PyUnicodeErrorObject *)self)->encoding,
Walter Dörwald787b03b2007-06-05 13:29:29 +00001394 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001395 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001396 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001397 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001398 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001399 return PyUnicode_FromFormat(
1400 "'%U' codec can't encode characters in position %zd-%zd: %U",
1401 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001402 uself->start,
1403 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001404 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001405 );
1406}
1407
1408static PyTypeObject _PyExc_UnicodeEncodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001409 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001410 "UnicodeEncodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001411 sizeof(PyUnicodeErrorObject), 0,
1412 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1413 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1414 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001415 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1416 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001417 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001418 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001419};
1420PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1421
1422PyObject *
1423PyUnicodeEncodeError_Create(
1424 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1425 Py_ssize_t start, Py_ssize_t end, const char *reason)
1426{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001427 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "Uu#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001428 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001429}
1430
1431
1432/*
1433 * UnicodeDecodeError extends UnicodeError
1434 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001435
1436static int
1437UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1438{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001439 PyUnicodeErrorObject *ude;
1440 const char *data;
1441 Py_ssize_t size;
1442
Thomas Wouters477c8d52006-05-27 19:21:47 +00001443 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1444 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001445
1446 ude = (PyUnicodeErrorObject *)self;
1447
1448 Py_CLEAR(ude->encoding);
1449 Py_CLEAR(ude->object);
1450 Py_CLEAR(ude->reason);
1451
1452 if (!PyArg_ParseTuple(args, "O!OnnO!",
1453 &PyUnicode_Type, &ude->encoding,
1454 &ude->object,
1455 &ude->start,
1456 &ude->end,
1457 &PyUnicode_Type, &ude->reason)) {
1458 ude->encoding = ude->object = ude->reason = NULL;
1459 return -1;
1460 }
1461
1462 if (!PyString_Check(ude->object)) {
1463 if (PyObject_AsReadBuffer(ude->object, (const void **)&data, &size)) {
1464 ude->encoding = ude->object = ude->reason = NULL;
1465 return -1;
1466 }
1467 ude->object = PyString_FromStringAndSize(data, size);
1468 }
1469 else {
1470 Py_INCREF(ude->object);
1471 }
1472
1473 Py_INCREF(ude->encoding);
1474 Py_INCREF(ude->reason);
1475
1476 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001477}
1478
1479static PyObject *
1480UnicodeDecodeError_str(PyObject *self)
1481{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001482 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001483
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001484 if (uself->end==uself->start+1) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00001485 int byte = (int)(PyString_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001486 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001487 "'%U' codec can't decode byte 0x%02x in position %zd: %U",
Walter Dörwaldd2034312007-05-18 16:29:38 +00001488 ((PyUnicodeErrorObject *)self)->encoding,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001489 byte,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001490 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001491 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001492 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001493 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001494 return PyUnicode_FromFormat(
1495 "'%U' codec can't decode bytes in position %zd-%zd: %U",
1496 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001497 uself->start,
1498 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001499 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001500 );
1501}
1502
1503static PyTypeObject _PyExc_UnicodeDecodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001504 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001505 "UnicodeDecodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001506 sizeof(PyUnicodeErrorObject), 0,
1507 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1508 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1509 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001510 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1511 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001512 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001513 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001514};
1515PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1516
1517PyObject *
1518PyUnicodeDecodeError_Create(
1519 const char *encoding, const char *object, Py_ssize_t length,
1520 Py_ssize_t start, Py_ssize_t end, const char *reason)
1521{
1522 assert(length < INT_MAX);
1523 assert(start < INT_MAX);
1524 assert(end < INT_MAX);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001525 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "Uy#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001526 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001527}
1528
1529
1530/*
1531 * UnicodeTranslateError extends UnicodeError
1532 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001533
1534static int
1535UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1536 PyObject *kwds)
1537{
1538 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1539 return -1;
1540
1541 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001542 Py_CLEAR(self->reason);
1543
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001544 if (!PyArg_ParseTuple(args, "O!nnO!",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001545 &PyUnicode_Type, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001546 &self->start,
1547 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001548 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001549 self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001550 return -1;
1551 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001552
Thomas Wouters477c8d52006-05-27 19:21:47 +00001553 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001554 Py_INCREF(self->reason);
1555
1556 return 0;
1557}
1558
1559
1560static PyObject *
1561UnicodeTranslateError_str(PyObject *self)
1562{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001563 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001564
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001565 if (uself->end==uself->start+1) {
1566 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001567 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001568 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001569 fmt = "can't translate character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001570 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001571 fmt = "can't translate character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001572 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001573 fmt = "can't translate character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001574 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001575 fmt,
1576 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001577 uself->start,
1578 uself->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001579 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001580 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001581 return PyUnicode_FromFormat(
1582 "can't translate characters in position %zd-%zd: %U",
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001583 uself->start,
1584 uself->end-1,
1585 uself->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001586 );
1587}
1588
1589static PyTypeObject _PyExc_UnicodeTranslateError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001590 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001591 "UnicodeTranslateError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001592 sizeof(PyUnicodeErrorObject), 0,
1593 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1594 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1595 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001596 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001597 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1598 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001599 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001600};
1601PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1602
1603PyObject *
1604PyUnicodeTranslateError_Create(
1605 const Py_UNICODE *object, Py_ssize_t length,
1606 Py_ssize_t start, Py_ssize_t end, const char *reason)
1607{
1608 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001609 object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001610}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001611
1612
1613/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001614 * AssertionError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001615 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001616SimpleExtendsException(PyExc_Exception, AssertionError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001617 "Assertion failed.");
1618
1619
1620/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001621 * ArithmeticError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001622 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001623SimpleExtendsException(PyExc_Exception, ArithmeticError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001624 "Base class for arithmetic errors.");
1625
1626
1627/*
1628 * FloatingPointError extends ArithmeticError
1629 */
1630SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
1631 "Floating point operation failed.");
1632
1633
1634/*
1635 * OverflowError extends ArithmeticError
1636 */
1637SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
1638 "Result too large to be represented.");
1639
1640
1641/*
1642 * ZeroDivisionError extends ArithmeticError
1643 */
1644SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
1645 "Second argument to a division or modulo operation was zero.");
1646
1647
1648/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001649 * SystemError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001650 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001651SimpleExtendsException(PyExc_Exception, SystemError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001652 "Internal error in the Python interpreter.\n"
1653 "\n"
1654 "Please report this to the Python maintainer, along with the traceback,\n"
1655 "the Python version, and the hardware/OS platform and version.");
1656
1657
1658/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001659 * ReferenceError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001660 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001661SimpleExtendsException(PyExc_Exception, ReferenceError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001662 "Weak ref proxy used after referent went away.");
1663
1664
1665/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001666 * MemoryError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001667 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001668SimpleExtendsException(PyExc_Exception, MemoryError, "Out of memory.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001669
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001670/*
1671 * BufferError extends Exception
1672 */
1673SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error.");
1674
Thomas Wouters477c8d52006-05-27 19:21:47 +00001675
1676/* Warning category docstrings */
1677
1678/*
1679 * Warning extends Exception
1680 */
1681SimpleExtendsException(PyExc_Exception, Warning,
1682 "Base class for warning categories.");
1683
1684
1685/*
1686 * UserWarning extends Warning
1687 */
1688SimpleExtendsException(PyExc_Warning, UserWarning,
1689 "Base class for warnings generated by user code.");
1690
1691
1692/*
1693 * DeprecationWarning extends Warning
1694 */
1695SimpleExtendsException(PyExc_Warning, DeprecationWarning,
1696 "Base class for warnings about deprecated features.");
1697
1698
1699/*
1700 * PendingDeprecationWarning extends Warning
1701 */
1702SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1703 "Base class for warnings about features which will be deprecated\n"
1704 "in the future.");
1705
1706
1707/*
1708 * SyntaxWarning extends Warning
1709 */
1710SimpleExtendsException(PyExc_Warning, SyntaxWarning,
1711 "Base class for warnings about dubious syntax.");
1712
1713
1714/*
1715 * RuntimeWarning extends Warning
1716 */
1717SimpleExtendsException(PyExc_Warning, RuntimeWarning,
1718 "Base class for warnings about dubious runtime behavior.");
1719
1720
1721/*
1722 * FutureWarning extends Warning
1723 */
1724SimpleExtendsException(PyExc_Warning, FutureWarning,
1725 "Base class for warnings about constructs that will change semantically\n"
1726 "in the future.");
1727
1728
1729/*
1730 * ImportWarning extends Warning
1731 */
1732SimpleExtendsException(PyExc_Warning, ImportWarning,
1733 "Base class for warnings about probable mistakes in module imports");
1734
1735
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001736/*
1737 * UnicodeWarning extends Warning
1738 */
1739SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1740 "Base class for warnings about Unicode related problems, mostly\n"
1741 "related to conversion problems.");
1742
Guido van Rossum98297ee2007-11-06 21:34:58 +00001743/*
1744 * BytesWarning extends Warning
1745 */
1746SimpleExtendsException(PyExc_Warning, BytesWarning,
1747 "Base class for warnings about bytes and buffer related problems, mostly\n"
1748 "related to conversion from str or comparing to str.");
1749
1750
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001751
Thomas Wouters477c8d52006-05-27 19:21:47 +00001752/* Pre-computed MemoryError instance. Best to create this as early as
1753 * possible and not wait until a MemoryError is actually raised!
1754 */
1755PyObject *PyExc_MemoryErrorInst=NULL;
1756
Thomas Wouters89d996e2007-09-08 17:39:28 +00001757/* Pre-computed RuntimeError instance for when recursion depth is reached.
1758 Meant to be used when normalizing the exception for exceeding the recursion
1759 depth will cause its own infinite recursion.
1760*/
1761PyObject *PyExc_RecursionErrorInst = NULL;
1762
Thomas Wouters477c8d52006-05-27 19:21:47 +00001763#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1764 Py_FatalError("exceptions bootstrapping error.");
1765
1766#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001767 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1768 Py_FatalError("Module dictionary insertion problem.");
1769
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001770#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1771/* crt variable checking in VisualStudio .NET 2005 */
1772#include <crtdbg.h>
1773
1774static int prevCrtReportMode;
1775static _invalid_parameter_handler prevCrtHandler;
1776
1777/* Invalid parameter handler. Sets a ValueError exception */
1778static void
1779InvalidParameterHandler(
1780 const wchar_t * expression,
1781 const wchar_t * function,
1782 const wchar_t * file,
1783 unsigned int line,
1784 uintptr_t pReserved)
1785{
1786 /* Do nothing, allow execution to continue. Usually this
1787 * means that the CRT will set errno to EINVAL
1788 */
1789}
1790#endif
1791
1792
Thomas Wouters477c8d52006-05-27 19:21:47 +00001793PyMODINIT_FUNC
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001794_PyExc_Init(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001795{
Neal Norwitz2633c692007-02-26 22:22:47 +00001796 PyObject *bltinmod, *bdict;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001797
1798 PRE_INIT(BaseException)
1799 PRE_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001800 PRE_INIT(TypeError)
1801 PRE_INIT(StopIteration)
1802 PRE_INIT(GeneratorExit)
1803 PRE_INIT(SystemExit)
1804 PRE_INIT(KeyboardInterrupt)
1805 PRE_INIT(ImportError)
1806 PRE_INIT(EnvironmentError)
1807 PRE_INIT(IOError)
1808 PRE_INIT(OSError)
1809#ifdef MS_WINDOWS
1810 PRE_INIT(WindowsError)
1811#endif
1812#ifdef __VMS
1813 PRE_INIT(VMSError)
1814#endif
1815 PRE_INIT(EOFError)
1816 PRE_INIT(RuntimeError)
1817 PRE_INIT(NotImplementedError)
1818 PRE_INIT(NameError)
1819 PRE_INIT(UnboundLocalError)
1820 PRE_INIT(AttributeError)
1821 PRE_INIT(SyntaxError)
1822 PRE_INIT(IndentationError)
1823 PRE_INIT(TabError)
1824 PRE_INIT(LookupError)
1825 PRE_INIT(IndexError)
1826 PRE_INIT(KeyError)
1827 PRE_INIT(ValueError)
1828 PRE_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001829 PRE_INIT(UnicodeEncodeError)
1830 PRE_INIT(UnicodeDecodeError)
1831 PRE_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001832 PRE_INIT(AssertionError)
1833 PRE_INIT(ArithmeticError)
1834 PRE_INIT(FloatingPointError)
1835 PRE_INIT(OverflowError)
1836 PRE_INIT(ZeroDivisionError)
1837 PRE_INIT(SystemError)
1838 PRE_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001839 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001840 PRE_INIT(MemoryError)
1841 PRE_INIT(Warning)
1842 PRE_INIT(UserWarning)
1843 PRE_INIT(DeprecationWarning)
1844 PRE_INIT(PendingDeprecationWarning)
1845 PRE_INIT(SyntaxWarning)
1846 PRE_INIT(RuntimeWarning)
1847 PRE_INIT(FutureWarning)
1848 PRE_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001849 PRE_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001850 PRE_INIT(BytesWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001851
Georg Brandl1a3284e2007-12-02 09:40:06 +00001852 bltinmod = PyImport_ImportModule("builtins");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001853 if (bltinmod == NULL)
1854 Py_FatalError("exceptions bootstrapping error.");
1855 bdict = PyModule_GetDict(bltinmod);
1856 if (bdict == NULL)
1857 Py_FatalError("exceptions bootstrapping error.");
1858
1859 POST_INIT(BaseException)
1860 POST_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001861 POST_INIT(TypeError)
1862 POST_INIT(StopIteration)
1863 POST_INIT(GeneratorExit)
1864 POST_INIT(SystemExit)
1865 POST_INIT(KeyboardInterrupt)
1866 POST_INIT(ImportError)
1867 POST_INIT(EnvironmentError)
1868 POST_INIT(IOError)
1869 POST_INIT(OSError)
1870#ifdef MS_WINDOWS
1871 POST_INIT(WindowsError)
1872#endif
1873#ifdef __VMS
1874 POST_INIT(VMSError)
1875#endif
1876 POST_INIT(EOFError)
1877 POST_INIT(RuntimeError)
1878 POST_INIT(NotImplementedError)
1879 POST_INIT(NameError)
1880 POST_INIT(UnboundLocalError)
1881 POST_INIT(AttributeError)
1882 POST_INIT(SyntaxError)
1883 POST_INIT(IndentationError)
1884 POST_INIT(TabError)
1885 POST_INIT(LookupError)
1886 POST_INIT(IndexError)
1887 POST_INIT(KeyError)
1888 POST_INIT(ValueError)
1889 POST_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001890 POST_INIT(UnicodeEncodeError)
1891 POST_INIT(UnicodeDecodeError)
1892 POST_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001893 POST_INIT(AssertionError)
1894 POST_INIT(ArithmeticError)
1895 POST_INIT(FloatingPointError)
1896 POST_INIT(OverflowError)
1897 POST_INIT(ZeroDivisionError)
1898 POST_INIT(SystemError)
1899 POST_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001900 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001901 POST_INIT(MemoryError)
1902 POST_INIT(Warning)
1903 POST_INIT(UserWarning)
1904 POST_INIT(DeprecationWarning)
1905 POST_INIT(PendingDeprecationWarning)
1906 POST_INIT(SyntaxWarning)
1907 POST_INIT(RuntimeWarning)
1908 POST_INIT(FutureWarning)
1909 POST_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001910 POST_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001911 POST_INIT(BytesWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001912
1913 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
1914 if (!PyExc_MemoryErrorInst)
1915 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1916
Thomas Wouters89d996e2007-09-08 17:39:28 +00001917 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
1918 if (!PyExc_RecursionErrorInst)
1919 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
1920 "recursion errors");
1921 else {
1922 PyBaseExceptionObject *err_inst =
1923 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
1924 PyObject *args_tuple;
1925 PyObject *exc_message;
Neal Norwitzbed67842007-10-27 04:00:45 +00001926 exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
Thomas Wouters89d996e2007-09-08 17:39:28 +00001927 if (!exc_message)
1928 Py_FatalError("cannot allocate argument for RuntimeError "
1929 "pre-allocation");
1930 args_tuple = PyTuple_Pack(1, exc_message);
1931 if (!args_tuple)
1932 Py_FatalError("cannot allocate tuple for RuntimeError "
1933 "pre-allocation");
1934 Py_DECREF(exc_message);
1935 if (BaseException_init(err_inst, args_tuple, NULL))
1936 Py_FatalError("init of pre-allocated RuntimeError failed");
1937 Py_DECREF(args_tuple);
1938 }
1939
Thomas Wouters477c8d52006-05-27 19:21:47 +00001940 Py_DECREF(bltinmod);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001941
1942#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1943 /* Set CRT argument error handler */
1944 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
1945 /* turn off assertions in debug mode */
1946 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
1947#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001948}
1949
1950void
1951_PyExc_Fini(void)
1952{
1953 Py_XDECREF(PyExc_MemoryErrorInst);
1954 PyExc_MemoryErrorInst = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001955#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1956 /* reset CRT error handling */
1957 _set_invalid_parameter_handler(prevCrtHandler);
1958 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
1959#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001960}