blob: 041cf9d10494770c68648dc9bbf0b3b252cbc45b [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:
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +000092 return PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +000093 default:
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +000094 return PyObject_Unicode(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/*
427 * GeneratorExit extends Exception
428 */
429SimpleExtendsException(PyExc_Exception, GeneratorExit,
430 "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. */
723 errcode = PyInt_AsLong(self->myerrno);
724 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
731 o_errcode = PyInt_FromLong(posix_errno);
732 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)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000942 return PyObject_Unicode(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),
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000948 PyInt_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,
956 PyInt_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 *
Walter Dörwald612344f2007-05-04 19:28:21 +00001048get_bytes(PyObject *attr, const char *name)
1049{
1050 if (!attr) {
1051 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1052 return NULL;
1053 }
1054
1055 if (!PyBytes_Check(attr)) {
1056 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{
Walter Dörwald612344f2007-05-04 19:28:21 +00001112 return get_bytes(((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;
1144 PyObject *obj = get_bytes(((PyUnicodeErrorObject *)exc)->object, "object");
1145 if (!obj)
1146 return -1;
1147 size = PyBytes_GET_SIZE(obj);
1148 *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;
1212 PyObject *obj = get_bytes(((PyUnicodeErrorObject *)exc)->object, "object");
1213 if (!obj)
1214 return -1;
1215 size = PyBytes_GET_SIZE(obj);
1216 *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
1302UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1303 PyTypeObject *objecttype)
1304{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001305 Py_CLEAR(self->encoding);
1306 Py_CLEAR(self->object);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001307 Py_CLEAR(self->reason);
1308
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001309 if (!PyArg_ParseTuple(args, "O!O!nnO!",
Walter Dörwaldd2034312007-05-18 16:29:38 +00001310 &PyUnicode_Type, &self->encoding,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001311 objecttype, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001312 &self->start,
1313 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001314 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001315 self->encoding = self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001316 return -1;
1317 }
1318
1319 Py_INCREF(self->encoding);
1320 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001321 Py_INCREF(self->reason);
1322
1323 return 0;
1324}
1325
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001326static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001327UnicodeError_clear(PyUnicodeErrorObject *self)
1328{
1329 Py_CLEAR(self->encoding);
1330 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001331 Py_CLEAR(self->reason);
1332 return BaseException_clear((PyBaseExceptionObject *)self);
1333}
1334
1335static void
1336UnicodeError_dealloc(PyUnicodeErrorObject *self)
1337{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001338 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001339 UnicodeError_clear(self);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001340 Py_Type(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001341}
1342
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001343static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001344UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1345{
1346 Py_VISIT(self->encoding);
1347 Py_VISIT(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001348 Py_VISIT(self->reason);
1349 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1350}
1351
1352static PyMemberDef UnicodeError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001353 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1354 PyDoc_STR("exception encoding")},
1355 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1356 PyDoc_STR("exception object")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001357 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001358 PyDoc_STR("exception start")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001359 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001360 PyDoc_STR("exception end")},
1361 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1362 PyDoc_STR("exception reason")},
1363 {NULL} /* Sentinel */
1364};
1365
1366
1367/*
1368 * UnicodeEncodeError extends UnicodeError
1369 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001370
1371static int
1372UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1373{
1374 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1375 return -1;
1376 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1377 kwds, &PyUnicode_Type);
1378}
1379
1380static PyObject *
1381UnicodeEncodeError_str(PyObject *self)
1382{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001383 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001384
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001385 if (uself->end==uself->start+1) {
1386 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001387 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001388 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001389 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001390 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001391 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001392 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001393 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001394 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001395 fmt,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001396 ((PyUnicodeErrorObject *)self)->encoding,
Walter Dörwald787b03b2007-06-05 13:29:29 +00001397 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001398 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001399 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001400 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001401 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001402 return PyUnicode_FromFormat(
1403 "'%U' codec can't encode characters in position %zd-%zd: %U",
1404 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001405 uself->start,
1406 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001407 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001408 );
1409}
1410
1411static PyTypeObject _PyExc_UnicodeEncodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001412 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001413 "UnicodeEncodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001414 sizeof(PyUnicodeErrorObject), 0,
1415 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1416 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1417 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001418 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1419 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001420 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001421 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001422};
1423PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1424
1425PyObject *
1426PyUnicodeEncodeError_Create(
1427 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1428 Py_ssize_t start, Py_ssize_t end, const char *reason)
1429{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001430 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "Uu#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001431 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001432}
1433
1434
1435/*
1436 * UnicodeDecodeError extends UnicodeError
1437 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001438
1439static int
1440UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1441{
1442 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1443 return -1;
1444 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
Walter Dörwald612344f2007-05-04 19:28:21 +00001445 kwds, &PyBytes_Type);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001446}
1447
1448static PyObject *
1449UnicodeDecodeError_str(PyObject *self)
1450{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001451 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001452
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001453 if (uself->end==uself->start+1) {
1454 int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001455 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001456 "'%U' codec can't decode byte 0x%02x in position %zd: %U",
Walter Dörwaldd2034312007-05-18 16:29:38 +00001457 ((PyUnicodeErrorObject *)self)->encoding,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001458 byte,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001459 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001460 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001461 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001462 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001463 return PyUnicode_FromFormat(
1464 "'%U' codec can't decode bytes in position %zd-%zd: %U",
1465 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001466 uself->start,
1467 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001468 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001469 );
1470}
1471
1472static PyTypeObject _PyExc_UnicodeDecodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001473 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001474 "UnicodeDecodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001475 sizeof(PyUnicodeErrorObject), 0,
1476 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1477 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1478 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001479 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1480 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001481 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001482 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001483};
1484PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1485
1486PyObject *
1487PyUnicodeDecodeError_Create(
1488 const char *encoding, const char *object, Py_ssize_t length,
1489 Py_ssize_t start, Py_ssize_t end, const char *reason)
1490{
1491 assert(length < INT_MAX);
1492 assert(start < INT_MAX);
1493 assert(end < INT_MAX);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001494 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "Uy#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001495 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001496}
1497
1498
1499/*
1500 * UnicodeTranslateError extends UnicodeError
1501 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001502
1503static int
1504UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1505 PyObject *kwds)
1506{
1507 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1508 return -1;
1509
1510 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001511 Py_CLEAR(self->reason);
1512
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001513 if (!PyArg_ParseTuple(args, "O!nnO!",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001514 &PyUnicode_Type, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001515 &self->start,
1516 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001517 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001518 self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001519 return -1;
1520 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001521
Thomas Wouters477c8d52006-05-27 19:21:47 +00001522 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001523 Py_INCREF(self->reason);
1524
1525 return 0;
1526}
1527
1528
1529static PyObject *
1530UnicodeTranslateError_str(PyObject *self)
1531{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001532 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001533
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001534 if (uself->end==uself->start+1) {
1535 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001536 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001537 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001538 fmt = "can't translate character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001539 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001540 fmt = "can't translate character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001541 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001542 fmt = "can't translate character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001543 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001544 fmt,
1545 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001546 uself->start,
1547 uself->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001548 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001549 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001550 return PyUnicode_FromFormat(
1551 "can't translate characters in position %zd-%zd: %U",
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001552 uself->start,
1553 uself->end-1,
1554 uself->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001555 );
1556}
1557
1558static PyTypeObject _PyExc_UnicodeTranslateError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001559 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001560 "UnicodeTranslateError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001561 sizeof(PyUnicodeErrorObject), 0,
1562 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1563 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1564 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001565 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001566 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1567 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001568 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001569};
1570PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1571
1572PyObject *
1573PyUnicodeTranslateError_Create(
1574 const Py_UNICODE *object, Py_ssize_t length,
1575 Py_ssize_t start, Py_ssize_t end, const char *reason)
1576{
1577 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001578 object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001579}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001580
1581
1582/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001583 * AssertionError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001584 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001585SimpleExtendsException(PyExc_Exception, AssertionError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001586 "Assertion failed.");
1587
1588
1589/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001590 * ArithmeticError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001591 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001592SimpleExtendsException(PyExc_Exception, ArithmeticError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001593 "Base class for arithmetic errors.");
1594
1595
1596/*
1597 * FloatingPointError extends ArithmeticError
1598 */
1599SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
1600 "Floating point operation failed.");
1601
1602
1603/*
1604 * OverflowError extends ArithmeticError
1605 */
1606SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
1607 "Result too large to be represented.");
1608
1609
1610/*
1611 * ZeroDivisionError extends ArithmeticError
1612 */
1613SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
1614 "Second argument to a division or modulo operation was zero.");
1615
1616
1617/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001618 * SystemError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001619 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001620SimpleExtendsException(PyExc_Exception, SystemError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001621 "Internal error in the Python interpreter.\n"
1622 "\n"
1623 "Please report this to the Python maintainer, along with the traceback,\n"
1624 "the Python version, and the hardware/OS platform and version.");
1625
1626
1627/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001628 * ReferenceError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001629 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001630SimpleExtendsException(PyExc_Exception, ReferenceError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001631 "Weak ref proxy used after referent went away.");
1632
1633
1634/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001635 * MemoryError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001636 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001637SimpleExtendsException(PyExc_Exception, MemoryError, "Out of memory.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001638
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001639/*
1640 * BufferError extends Exception
1641 */
1642SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error.");
1643
Thomas Wouters477c8d52006-05-27 19:21:47 +00001644
1645/* Warning category docstrings */
1646
1647/*
1648 * Warning extends Exception
1649 */
1650SimpleExtendsException(PyExc_Exception, Warning,
1651 "Base class for warning categories.");
1652
1653
1654/*
1655 * UserWarning extends Warning
1656 */
1657SimpleExtendsException(PyExc_Warning, UserWarning,
1658 "Base class for warnings generated by user code.");
1659
1660
1661/*
1662 * DeprecationWarning extends Warning
1663 */
1664SimpleExtendsException(PyExc_Warning, DeprecationWarning,
1665 "Base class for warnings about deprecated features.");
1666
1667
1668/*
1669 * PendingDeprecationWarning extends Warning
1670 */
1671SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1672 "Base class for warnings about features which will be deprecated\n"
1673 "in the future.");
1674
1675
1676/*
1677 * SyntaxWarning extends Warning
1678 */
1679SimpleExtendsException(PyExc_Warning, SyntaxWarning,
1680 "Base class for warnings about dubious syntax.");
1681
1682
1683/*
1684 * RuntimeWarning extends Warning
1685 */
1686SimpleExtendsException(PyExc_Warning, RuntimeWarning,
1687 "Base class for warnings about dubious runtime behavior.");
1688
1689
1690/*
1691 * FutureWarning extends Warning
1692 */
1693SimpleExtendsException(PyExc_Warning, FutureWarning,
1694 "Base class for warnings about constructs that will change semantically\n"
1695 "in the future.");
1696
1697
1698/*
1699 * ImportWarning extends Warning
1700 */
1701SimpleExtendsException(PyExc_Warning, ImportWarning,
1702 "Base class for warnings about probable mistakes in module imports");
1703
1704
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001705/*
1706 * UnicodeWarning extends Warning
1707 */
1708SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1709 "Base class for warnings about Unicode related problems, mostly\n"
1710 "related to conversion problems.");
1711
1712
Thomas Wouters477c8d52006-05-27 19:21:47 +00001713/* Pre-computed MemoryError instance. Best to create this as early as
1714 * possible and not wait until a MemoryError is actually raised!
1715 */
1716PyObject *PyExc_MemoryErrorInst=NULL;
1717
Thomas Wouters89d996e2007-09-08 17:39:28 +00001718/* Pre-computed RuntimeError instance for when recursion depth is reached.
1719 Meant to be used when normalizing the exception for exceeding the recursion
1720 depth will cause its own infinite recursion.
1721*/
1722PyObject *PyExc_RecursionErrorInst = NULL;
1723
Thomas Wouters477c8d52006-05-27 19:21:47 +00001724#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1725 Py_FatalError("exceptions bootstrapping error.");
1726
1727#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001728 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1729 Py_FatalError("Module dictionary insertion problem.");
1730
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001731#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1732/* crt variable checking in VisualStudio .NET 2005 */
1733#include <crtdbg.h>
1734
1735static int prevCrtReportMode;
1736static _invalid_parameter_handler prevCrtHandler;
1737
1738/* Invalid parameter handler. Sets a ValueError exception */
1739static void
1740InvalidParameterHandler(
1741 const wchar_t * expression,
1742 const wchar_t * function,
1743 const wchar_t * file,
1744 unsigned int line,
1745 uintptr_t pReserved)
1746{
1747 /* Do nothing, allow execution to continue. Usually this
1748 * means that the CRT will set errno to EINVAL
1749 */
1750}
1751#endif
1752
1753
Thomas Wouters477c8d52006-05-27 19:21:47 +00001754PyMODINIT_FUNC
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001755_PyExc_Init(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001756{
Neal Norwitz2633c692007-02-26 22:22:47 +00001757 PyObject *bltinmod, *bdict;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001758
1759 PRE_INIT(BaseException)
1760 PRE_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001761 PRE_INIT(TypeError)
1762 PRE_INIT(StopIteration)
1763 PRE_INIT(GeneratorExit)
1764 PRE_INIT(SystemExit)
1765 PRE_INIT(KeyboardInterrupt)
1766 PRE_INIT(ImportError)
1767 PRE_INIT(EnvironmentError)
1768 PRE_INIT(IOError)
1769 PRE_INIT(OSError)
1770#ifdef MS_WINDOWS
1771 PRE_INIT(WindowsError)
1772#endif
1773#ifdef __VMS
1774 PRE_INIT(VMSError)
1775#endif
1776 PRE_INIT(EOFError)
1777 PRE_INIT(RuntimeError)
1778 PRE_INIT(NotImplementedError)
1779 PRE_INIT(NameError)
1780 PRE_INIT(UnboundLocalError)
1781 PRE_INIT(AttributeError)
1782 PRE_INIT(SyntaxError)
1783 PRE_INIT(IndentationError)
1784 PRE_INIT(TabError)
1785 PRE_INIT(LookupError)
1786 PRE_INIT(IndexError)
1787 PRE_INIT(KeyError)
1788 PRE_INIT(ValueError)
1789 PRE_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001790 PRE_INIT(UnicodeEncodeError)
1791 PRE_INIT(UnicodeDecodeError)
1792 PRE_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001793 PRE_INIT(AssertionError)
1794 PRE_INIT(ArithmeticError)
1795 PRE_INIT(FloatingPointError)
1796 PRE_INIT(OverflowError)
1797 PRE_INIT(ZeroDivisionError)
1798 PRE_INIT(SystemError)
1799 PRE_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001800 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001801 PRE_INIT(MemoryError)
1802 PRE_INIT(Warning)
1803 PRE_INIT(UserWarning)
1804 PRE_INIT(DeprecationWarning)
1805 PRE_INIT(PendingDeprecationWarning)
1806 PRE_INIT(SyntaxWarning)
1807 PRE_INIT(RuntimeWarning)
1808 PRE_INIT(FutureWarning)
1809 PRE_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001810 PRE_INIT(UnicodeWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001811
Thomas Wouters477c8d52006-05-27 19:21:47 +00001812 bltinmod = PyImport_ImportModule("__builtin__");
1813 if (bltinmod == NULL)
1814 Py_FatalError("exceptions bootstrapping error.");
1815 bdict = PyModule_GetDict(bltinmod);
1816 if (bdict == NULL)
1817 Py_FatalError("exceptions bootstrapping error.");
1818
1819 POST_INIT(BaseException)
1820 POST_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001821 POST_INIT(TypeError)
1822 POST_INIT(StopIteration)
1823 POST_INIT(GeneratorExit)
1824 POST_INIT(SystemExit)
1825 POST_INIT(KeyboardInterrupt)
1826 POST_INIT(ImportError)
1827 POST_INIT(EnvironmentError)
1828 POST_INIT(IOError)
1829 POST_INIT(OSError)
1830#ifdef MS_WINDOWS
1831 POST_INIT(WindowsError)
1832#endif
1833#ifdef __VMS
1834 POST_INIT(VMSError)
1835#endif
1836 POST_INIT(EOFError)
1837 POST_INIT(RuntimeError)
1838 POST_INIT(NotImplementedError)
1839 POST_INIT(NameError)
1840 POST_INIT(UnboundLocalError)
1841 POST_INIT(AttributeError)
1842 POST_INIT(SyntaxError)
1843 POST_INIT(IndentationError)
1844 POST_INIT(TabError)
1845 POST_INIT(LookupError)
1846 POST_INIT(IndexError)
1847 POST_INIT(KeyError)
1848 POST_INIT(ValueError)
1849 POST_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001850 POST_INIT(UnicodeEncodeError)
1851 POST_INIT(UnicodeDecodeError)
1852 POST_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001853 POST_INIT(AssertionError)
1854 POST_INIT(ArithmeticError)
1855 POST_INIT(FloatingPointError)
1856 POST_INIT(OverflowError)
1857 POST_INIT(ZeroDivisionError)
1858 POST_INIT(SystemError)
1859 POST_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001860 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001861 POST_INIT(MemoryError)
1862 POST_INIT(Warning)
1863 POST_INIT(UserWarning)
1864 POST_INIT(DeprecationWarning)
1865 POST_INIT(PendingDeprecationWarning)
1866 POST_INIT(SyntaxWarning)
1867 POST_INIT(RuntimeWarning)
1868 POST_INIT(FutureWarning)
1869 POST_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001870 POST_INIT(UnicodeWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001871
1872 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
1873 if (!PyExc_MemoryErrorInst)
1874 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1875
Thomas Wouters89d996e2007-09-08 17:39:28 +00001876 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
1877 if (!PyExc_RecursionErrorInst)
1878 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
1879 "recursion errors");
1880 else {
1881 PyBaseExceptionObject *err_inst =
1882 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
1883 PyObject *args_tuple;
1884 PyObject *exc_message;
1885 exc_message = PyString_FromString("maximum recursion depth exceeded");
1886 if (!exc_message)
1887 Py_FatalError("cannot allocate argument for RuntimeError "
1888 "pre-allocation");
1889 args_tuple = PyTuple_Pack(1, exc_message);
1890 if (!args_tuple)
1891 Py_FatalError("cannot allocate tuple for RuntimeError "
1892 "pre-allocation");
1893 Py_DECREF(exc_message);
1894 if (BaseException_init(err_inst, args_tuple, NULL))
1895 Py_FatalError("init of pre-allocated RuntimeError failed");
1896 Py_DECREF(args_tuple);
1897 }
1898
Thomas Wouters477c8d52006-05-27 19:21:47 +00001899 Py_DECREF(bltinmod);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001900
1901#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1902 /* Set CRT argument error handler */
1903 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
1904 /* turn off assertions in debug mode */
1905 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
1906#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001907}
1908
1909void
1910_PyExc_Fini(void)
1911{
1912 Py_XDECREF(PyExc_MemoryErrorInst);
1913 PyExc_MemoryErrorInst = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001914#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1915 /* reset CRT error handling */
1916 _set_invalid_parameter_handler(prevCrtHandler);
1917 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
1918#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001919}