blob: cbc41e8ba2393e46e73f106b50768f23b41153d8 [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 Winter828f04a2007-08-31 00:04:24 +000031 self->traceback = 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);
Thomas Wouters477c8d52006-05-27 19:21:47 +000061 return 0;
62}
63
64static void
65BaseException_dealloc(PyBaseExceptionObject *self)
66{
Thomas Wouters89f507f2006-12-13 04:49:30 +000067 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000068 BaseException_clear(self);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000069 Py_Type(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000070}
71
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000072static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000073BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
74{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000075 Py_VISIT(self->dict);
Thomas Wouters477c8d52006-05-27 19:21:47 +000076 Py_VISIT(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000077 Py_VISIT(self->traceback);
Thomas Wouters477c8d52006-05-27 19:21:47 +000078 return 0;
79}
80
81static PyObject *
82BaseException_str(PyBaseExceptionObject *self)
83{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000084 switch (PyTuple_GET_SIZE(self->args)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000085 case 0:
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +000086 return PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +000087 case 1:
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +000088 return PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +000089 default:
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +000090 return PyObject_Unicode(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +000091 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000092}
93
94static PyObject *
95BaseException_repr(PyBaseExceptionObject *self)
96{
Thomas Wouters477c8d52006-05-27 19:21:47 +000097 char *name;
98 char *dot;
99
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000100 name = (char *)Py_Type(self)->tp_name;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000101 dot = strrchr(name, '.');
102 if (dot != NULL) name = dot+1;
103
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000104 return PyUnicode_FromFormat("%s%R", name, self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000105}
106
107/* Pickling support */
108static PyObject *
109BaseException_reduce(PyBaseExceptionObject *self)
110{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000111 if (self->args && self->dict)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000112 return PyTuple_Pack(3, Py_Type(self), self->args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000113 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000114 return PyTuple_Pack(2, Py_Type(self), self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000115}
116
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000117/*
118 * Needed for backward compatibility, since exceptions used to store
119 * all their attributes in the __dict__. Code is taken from cPickle's
120 * load_build function.
121 */
122static PyObject *
123BaseException_setstate(PyObject *self, PyObject *state)
124{
125 PyObject *d_key, *d_value;
126 Py_ssize_t i = 0;
127
128 if (state != Py_None) {
129 if (!PyDict_Check(state)) {
130 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
131 return NULL;
132 }
133 while (PyDict_Next(state, &i, &d_key, &d_value)) {
134 if (PyObject_SetAttr(self, d_key, d_value) < 0)
135 return NULL;
136 }
137 }
138 Py_RETURN_NONE;
139}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000140
Collin Winter828f04a2007-08-31 00:04:24 +0000141static PyObject *
142BaseException_with_traceback(PyObject *self, PyObject *tb) {
143 if (PyException_SetTraceback(self, tb))
144 return NULL;
145
146 Py_INCREF(self);
147 return self;
148}
149
Thomas Wouters477c8d52006-05-27 19:21:47 +0000150
151static PyMethodDef BaseException_methods[] = {
152 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000153 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Collin Winter828f04a2007-08-31 00:04:24 +0000154 {"with_traceback", (PyCFunction)BaseException_with_traceback, METH_O },
Thomas Wouters477c8d52006-05-27 19:21:47 +0000155 {NULL, NULL, 0, NULL},
156};
157
158
Thomas Wouters477c8d52006-05-27 19:21:47 +0000159static PyObject *
160BaseException_get_dict(PyBaseExceptionObject *self)
161{
162 if (self->dict == NULL) {
163 self->dict = PyDict_New();
164 if (!self->dict)
165 return NULL;
166 }
167 Py_INCREF(self->dict);
168 return self->dict;
169}
170
171static int
172BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
173{
174 if (val == NULL) {
175 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
176 return -1;
177 }
178 if (!PyDict_Check(val)) {
179 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
180 return -1;
181 }
182 Py_CLEAR(self->dict);
183 Py_INCREF(val);
184 self->dict = val;
185 return 0;
186}
187
188static PyObject *
189BaseException_get_args(PyBaseExceptionObject *self)
190{
191 if (self->args == NULL) {
192 Py_INCREF(Py_None);
193 return Py_None;
194 }
195 Py_INCREF(self->args);
196 return self->args;
197}
198
199static int
200BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
201{
202 PyObject *seq;
203 if (val == NULL) {
204 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
205 return -1;
206 }
207 seq = PySequence_Tuple(val);
208 if (!seq) return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000209 Py_CLEAR(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000210 self->args = seq;
211 return 0;
212}
213
Collin Winter828f04a2007-08-31 00:04:24 +0000214static PyObject *
215BaseException_get_tb(PyBaseExceptionObject *self)
216{
217 if (self->traceback == NULL) {
218 Py_INCREF(Py_None);
219 return Py_None;
220 }
221 Py_INCREF(self->traceback);
222 return self->traceback;
223}
224
225static int
226BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb)
227{
228 if (tb == NULL) {
229 PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted");
230 return -1;
231 }
232 else if (!(tb == Py_None || PyTraceBack_Check(tb))) {
233 PyErr_SetString(PyExc_TypeError,
234 "__traceback__ must be a traceback or None");
235 return -1;
236 }
237
238 Py_XINCREF(tb);
239 Py_XDECREF(self->traceback);
240 self->traceback = tb;
241 return 0;
242}
243
Guido van Rossum360e4b82007-05-14 22:51:27 +0000244
Thomas Wouters477c8d52006-05-27 19:21:47 +0000245static PyGetSetDef BaseException_getset[] = {
246 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
247 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Collin Winter828f04a2007-08-31 00:04:24 +0000248 {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000249 {NULL},
250};
251
252
Collin Winter828f04a2007-08-31 00:04:24 +0000253PyObject *
254PyException_GetTraceback(PyObject *self) {
255 PyBaseExceptionObject *base_self = (PyBaseExceptionObject *)self;
256 Py_XINCREF(base_self->traceback);
257 return base_self->traceback;
258}
259
260
261int
262PyException_SetTraceback(PyObject *self, PyObject *tb) {
263 return BaseException_set_tb((PyBaseExceptionObject *)self, tb);
264}
265
266PyObject *
267PyException_GetCause(PyObject *self) {
268 PyObject *cause = ((PyBaseExceptionObject *)self)->cause;
269 Py_XINCREF(cause);
270 return cause;
271}
272
273/* Steals a reference to cause */
274void
275PyException_SetCause(PyObject *self, PyObject *cause) {
276 PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause;
277 ((PyBaseExceptionObject *)self)->cause = cause;
278 Py_XDECREF(old_cause);
279}
280
281PyObject *
282PyException_GetContext(PyObject *self) {
283 PyObject *context = ((PyBaseExceptionObject *)self)->context;
284 Py_XINCREF(context);
285 return context;
286}
287
288/* Steals a reference to context */
289void
290PyException_SetContext(PyObject *self, PyObject *context) {
291 PyObject *old_context = ((PyBaseExceptionObject *)self)->context;
292 ((PyBaseExceptionObject *)self)->context = context;
293 Py_XDECREF(old_context);
294}
295
296
297static PyMemberDef BaseException_members[] = {
298 {"__context__", T_OBJECT, offsetof(PyBaseExceptionObject, context), 0,
299 PyDoc_STR("exception context")},
300 {"__cause__", T_OBJECT, offsetof(PyBaseExceptionObject, cause), 0,
301 PyDoc_STR("exception cause")},
302 {NULL} /* Sentinel */
303};
304
Thomas Wouters477c8d52006-05-27 19:21:47 +0000305static PyTypeObject _PyExc_BaseException = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000306 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +0000307 "BaseException", /*tp_name*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000308 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
309 0, /*tp_itemsize*/
310 (destructor)BaseException_dealloc, /*tp_dealloc*/
311 0, /*tp_print*/
312 0, /*tp_getattr*/
313 0, /*tp_setattr*/
314 0, /* tp_compare; */
315 (reprfunc)BaseException_repr, /*tp_repr*/
316 0, /*tp_as_number*/
Brett Cannonba7bf492007-02-27 00:15:55 +0000317 0, /*tp_as_sequence*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000318 0, /*tp_as_mapping*/
319 0, /*tp_hash */
320 0, /*tp_call*/
321 (reprfunc)BaseException_str, /*tp_str*/
322 PyObject_GenericGetAttr, /*tp_getattro*/
323 PyObject_GenericSetAttr, /*tp_setattro*/
324 0, /*tp_as_buffer*/
Thomas Wouters27d517b2007-02-25 20:39:11 +0000325 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
326 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000327 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
328 (traverseproc)BaseException_traverse, /* tp_traverse */
329 (inquiry)BaseException_clear, /* tp_clear */
330 0, /* tp_richcompare */
331 0, /* tp_weaklistoffset */
332 0, /* tp_iter */
333 0, /* tp_iternext */
334 BaseException_methods, /* tp_methods */
Collin Winter828f04a2007-08-31 00:04:24 +0000335 BaseException_members, /* tp_members */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000336 BaseException_getset, /* tp_getset */
337 0, /* tp_base */
338 0, /* tp_dict */
339 0, /* tp_descr_get */
340 0, /* tp_descr_set */
341 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
342 (initproc)BaseException_init, /* tp_init */
343 0, /* tp_alloc */
344 BaseException_new, /* tp_new */
345};
346/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
347from the previous implmentation and also allowing Python objects to be used
348in the API */
349PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
350
351/* note these macros omit the last semicolon so the macro invocation may
352 * include it and not look strange.
353 */
354#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
355static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000356 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000357 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000358 sizeof(PyBaseExceptionObject), \
359 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
360 0, 0, 0, 0, 0, 0, 0, \
361 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
362 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
363 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
364 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
365 (initproc)BaseException_init, 0, BaseException_new,\
366}; \
367PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
368
369#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
370static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000371 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000372 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000373 sizeof(Py ## EXCSTORE ## Object), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000374 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000375 0, 0, 0, 0, 0, \
376 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000377 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
378 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000379 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000380 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000381}; \
382PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
383
384#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
385static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000386 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000387 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000388 sizeof(Py ## EXCSTORE ## Object), 0, \
389 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
390 (reprfunc)EXCSTR, 0, 0, 0, \
391 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
392 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
393 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
394 EXCMEMBERS, 0, &_ ## EXCBASE, \
395 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000396 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000397}; \
398PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
399
400
401/*
402 * Exception extends BaseException
403 */
404SimpleExtendsException(PyExc_BaseException, Exception,
405 "Common base class for all non-exit exceptions.");
406
407
408/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000409 * TypeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000410 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000411SimpleExtendsException(PyExc_Exception, TypeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000412 "Inappropriate argument type.");
413
414
415/*
416 * StopIteration extends Exception
417 */
418SimpleExtendsException(PyExc_Exception, StopIteration,
Georg Brandla18af4e2007-04-21 15:47:16 +0000419 "Signal the end from iterator.__next__().");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000420
421
422/*
423 * GeneratorExit extends Exception
424 */
425SimpleExtendsException(PyExc_Exception, GeneratorExit,
426 "Request that a generator exit.");
427
428
429/*
430 * SystemExit extends BaseException
431 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000432
433static int
434SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
435{
436 Py_ssize_t size = PyTuple_GET_SIZE(args);
437
438 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
439 return -1;
440
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000441 if (size == 0)
442 return 0;
443 Py_CLEAR(self->code);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000444 if (size == 1)
445 self->code = PyTuple_GET_ITEM(args, 0);
446 else if (size > 1)
447 self->code = args;
448 Py_INCREF(self->code);
449 return 0;
450}
451
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000452static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000453SystemExit_clear(PySystemExitObject *self)
454{
455 Py_CLEAR(self->code);
456 return BaseException_clear((PyBaseExceptionObject *)self);
457}
458
459static void
460SystemExit_dealloc(PySystemExitObject *self)
461{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000462 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000463 SystemExit_clear(self);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000464 Py_Type(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000465}
466
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000467static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000468SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
469{
470 Py_VISIT(self->code);
471 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
472}
473
474static PyMemberDef SystemExit_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000475 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
476 PyDoc_STR("exception code")},
477 {NULL} /* Sentinel */
478};
479
480ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
481 SystemExit_dealloc, 0, SystemExit_members, 0,
482 "Request to exit from the interpreter.");
483
484/*
485 * KeyboardInterrupt extends BaseException
486 */
487SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
488 "Program interrupted by user.");
489
490
491/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000492 * ImportError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000493 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000494SimpleExtendsException(PyExc_Exception, ImportError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000495 "Import can't find module, or can't find name in module.");
496
497
498/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000499 * EnvironmentError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000500 */
501
Thomas Wouters477c8d52006-05-27 19:21:47 +0000502/* Where a function has a single filename, such as open() or some
503 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
504 * called, giving a third argument which is the filename. But, so
505 * that old code using in-place unpacking doesn't break, e.g.:
506 *
507 * except IOError, (errno, strerror):
508 *
509 * we hack args so that it only contains two items. This also
510 * means we need our own __str__() which prints out the filename
511 * when it was supplied.
512 */
513static int
514EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
515 PyObject *kwds)
516{
517 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
518 PyObject *subslice = NULL;
519
520 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
521 return -1;
522
Thomas Wouters89f507f2006-12-13 04:49:30 +0000523 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000524 return 0;
525 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000526
527 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000528 &myerrno, &strerror, &filename)) {
529 return -1;
530 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000531 Py_CLEAR(self->myerrno); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000532 self->myerrno = myerrno;
533 Py_INCREF(self->myerrno);
534
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000535 Py_CLEAR(self->strerror); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000536 self->strerror = strerror;
537 Py_INCREF(self->strerror);
538
539 /* self->filename will remain Py_None otherwise */
540 if (filename != NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000541 Py_CLEAR(self->filename); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000542 self->filename = filename;
543 Py_INCREF(self->filename);
544
545 subslice = PyTuple_GetSlice(args, 0, 2);
546 if (!subslice)
547 return -1;
548
549 Py_DECREF(self->args); /* replacing args */
550 self->args = subslice;
551 }
552 return 0;
553}
554
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000555static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000556EnvironmentError_clear(PyEnvironmentErrorObject *self)
557{
558 Py_CLEAR(self->myerrno);
559 Py_CLEAR(self->strerror);
560 Py_CLEAR(self->filename);
561 return BaseException_clear((PyBaseExceptionObject *)self);
562}
563
564static void
565EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
566{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000567 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000568 EnvironmentError_clear(self);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000569 Py_Type(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000570}
571
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000572static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000573EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
574 void *arg)
575{
576 Py_VISIT(self->myerrno);
577 Py_VISIT(self->strerror);
578 Py_VISIT(self->filename);
579 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
580}
581
582static PyObject *
583EnvironmentError_str(PyEnvironmentErrorObject *self)
584{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000585 if (self->filename)
586 return PyUnicode_FromFormat("[Errno %S] %S: %R",
587 self->myerrno ? self->myerrno: Py_None,
588 self->strerror ? self->strerror: Py_None,
589 self->filename);
590 else if (self->myerrno && self->strerror)
591 return PyUnicode_FromFormat("[Errno %S] %S",
592 self->myerrno ? self->myerrno: Py_None,
593 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000594 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000595 return BaseException_str((PyBaseExceptionObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000596}
597
598static PyMemberDef EnvironmentError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000599 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
600 PyDoc_STR("exception errno")},
601 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
602 PyDoc_STR("exception strerror")},
603 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
604 PyDoc_STR("exception filename")},
605 {NULL} /* Sentinel */
606};
607
608
609static PyObject *
610EnvironmentError_reduce(PyEnvironmentErrorObject *self)
611{
612 PyObject *args = self->args;
613 PyObject *res = NULL, *tmp;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000614
Thomas Wouters477c8d52006-05-27 19:21:47 +0000615 /* self->args is only the first two real arguments if there was a
616 * file name given to EnvironmentError. */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000617 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000618 args = PyTuple_New(3);
619 if (!args) return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000620
621 tmp = PyTuple_GET_ITEM(self->args, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000622 Py_INCREF(tmp);
623 PyTuple_SET_ITEM(args, 0, tmp);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000624
625 tmp = PyTuple_GET_ITEM(self->args, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000626 Py_INCREF(tmp);
627 PyTuple_SET_ITEM(args, 1, tmp);
628
629 Py_INCREF(self->filename);
630 PyTuple_SET_ITEM(args, 2, self->filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000631 } else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000632 Py_INCREF(args);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000633
634 if (self->dict)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000635 res = PyTuple_Pack(3, Py_Type(self), args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000636 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000637 res = PyTuple_Pack(2, Py_Type(self), args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000638 Py_DECREF(args);
639 return res;
640}
641
642
643static PyMethodDef EnvironmentError_methods[] = {
644 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
645 {NULL}
646};
647
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000648ComplexExtendsException(PyExc_Exception, EnvironmentError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000649 EnvironmentError, EnvironmentError_dealloc,
650 EnvironmentError_methods, EnvironmentError_members,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000651 EnvironmentError_str,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000652 "Base class for I/O related errors.");
653
654
655/*
656 * IOError extends EnvironmentError
657 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000658MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000659 EnvironmentError, "I/O operation failed.");
660
661
662/*
663 * OSError extends EnvironmentError
664 */
665MiddlingExtendsException(PyExc_EnvironmentError, OSError,
666 EnvironmentError, "OS system call failed.");
667
668
669/*
670 * WindowsError extends OSError
671 */
672#ifdef MS_WINDOWS
673#include "errmap.h"
674
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000675static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000676WindowsError_clear(PyWindowsErrorObject *self)
677{
678 Py_CLEAR(self->myerrno);
679 Py_CLEAR(self->strerror);
680 Py_CLEAR(self->filename);
681 Py_CLEAR(self->winerror);
682 return BaseException_clear((PyBaseExceptionObject *)self);
683}
684
685static void
686WindowsError_dealloc(PyWindowsErrorObject *self)
687{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000688 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000689 WindowsError_clear(self);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000690 Py_Type(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000691}
692
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000693static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
695{
696 Py_VISIT(self->myerrno);
697 Py_VISIT(self->strerror);
698 Py_VISIT(self->filename);
699 Py_VISIT(self->winerror);
700 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
701}
702
Thomas Wouters477c8d52006-05-27 19:21:47 +0000703static int
704WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
705{
706 PyObject *o_errcode = NULL;
707 long errcode;
708 long posix_errno;
709
710 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
711 == -1)
712 return -1;
713
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000714 if (self->myerrno == NULL)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000715 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000716
717 /* Set errno to the POSIX errno, and winerror to the Win32
718 error code. */
719 errcode = PyInt_AsLong(self->myerrno);
720 if (errcode == -1 && PyErr_Occurred())
721 return -1;
722 posix_errno = winerror_to_errno(errcode);
723
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000724 Py_CLEAR(self->winerror);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000725 self->winerror = self->myerrno;
726
727 o_errcode = PyInt_FromLong(posix_errno);
728 if (!o_errcode)
729 return -1;
730
731 self->myerrno = o_errcode;
732
733 return 0;
734}
735
736
737static PyObject *
738WindowsError_str(PyWindowsErrorObject *self)
739{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000740 if (self->filename)
741 return PyUnicode_FromFormat("[Error %S] %S: %R",
742 self->winerror ? self->winerror: Py_None,
743 self->strerror ? self->strerror: Py_None,
744 self->filename);
745 else if (self->winerror && self->strerror)
746 return PyUnicode_FromFormat("[Error %S] %S",
747 self->winerror ? self->winerror: Py_None,
748 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000749 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000750 return EnvironmentError_str((PyEnvironmentErrorObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000751}
752
753static PyMemberDef WindowsError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000754 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
755 PyDoc_STR("POSIX exception code")},
756 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
757 PyDoc_STR("exception strerror")},
758 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
759 PyDoc_STR("exception filename")},
760 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
761 PyDoc_STR("Win32 exception code")},
762 {NULL} /* Sentinel */
763};
764
765ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
766 WindowsError_dealloc, 0, WindowsError_members,
767 WindowsError_str, "MS-Windows OS system call failed.");
768
769#endif /* MS_WINDOWS */
770
771
772/*
773 * VMSError extends OSError (I think)
774 */
775#ifdef __VMS
776MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
777 "OpenVMS OS system call failed.");
778#endif
779
780
781/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000782 * EOFError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000783 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000784SimpleExtendsException(PyExc_Exception, EOFError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000785 "Read beyond end of file.");
786
787
788/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000789 * RuntimeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000790 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000791SimpleExtendsException(PyExc_Exception, RuntimeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000792 "Unspecified run-time error.");
793
794
795/*
796 * NotImplementedError extends RuntimeError
797 */
798SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
799 "Method or function hasn't been implemented yet.");
800
801/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000802 * NameError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000803 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000804SimpleExtendsException(PyExc_Exception, NameError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000805 "Name not found globally.");
806
807/*
808 * UnboundLocalError extends NameError
809 */
810SimpleExtendsException(PyExc_NameError, UnboundLocalError,
811 "Local name referenced but not bound to a value.");
812
813/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000814 * AttributeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000815 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000816SimpleExtendsException(PyExc_Exception, AttributeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000817 "Attribute not found.");
818
819
820/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000821 * SyntaxError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000822 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000823
824static int
825SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
826{
827 PyObject *info = NULL;
828 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
829
830 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
831 return -1;
832
833 if (lenargs >= 1) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000834 Py_CLEAR(self->msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000835 self->msg = PyTuple_GET_ITEM(args, 0);
836 Py_INCREF(self->msg);
837 }
838 if (lenargs == 2) {
839 info = PyTuple_GET_ITEM(args, 1);
840 info = PySequence_Tuple(info);
841 if (!info) return -1;
842
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000843 if (PyTuple_GET_SIZE(info) != 4) {
844 /* not a very good error message, but it's what Python 2.4 gives */
845 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
846 Py_DECREF(info);
847 return -1;
848 }
849
850 Py_CLEAR(self->filename);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000851 self->filename = PyTuple_GET_ITEM(info, 0);
852 Py_INCREF(self->filename);
853
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000854 Py_CLEAR(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000855 self->lineno = PyTuple_GET_ITEM(info, 1);
856 Py_INCREF(self->lineno);
857
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000858 Py_CLEAR(self->offset);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859 self->offset = PyTuple_GET_ITEM(info, 2);
860 Py_INCREF(self->offset);
861
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000862 Py_CLEAR(self->text);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000863 self->text = PyTuple_GET_ITEM(info, 3);
864 Py_INCREF(self->text);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000865
866 Py_DECREF(info);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000867 }
868 return 0;
869}
870
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000871static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000872SyntaxError_clear(PySyntaxErrorObject *self)
873{
874 Py_CLEAR(self->msg);
875 Py_CLEAR(self->filename);
876 Py_CLEAR(self->lineno);
877 Py_CLEAR(self->offset);
878 Py_CLEAR(self->text);
879 Py_CLEAR(self->print_file_and_line);
880 return BaseException_clear((PyBaseExceptionObject *)self);
881}
882
883static void
884SyntaxError_dealloc(PySyntaxErrorObject *self)
885{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000886 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000887 SyntaxError_clear(self);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000888 Py_Type(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000889}
890
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000891static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
893{
894 Py_VISIT(self->msg);
895 Py_VISIT(self->filename);
896 Py_VISIT(self->lineno);
897 Py_VISIT(self->offset);
898 Py_VISIT(self->text);
899 Py_VISIT(self->print_file_and_line);
900 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
901}
902
903/* This is called "my_basename" instead of just "basename" to avoid name
904 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
905 defined, and Python does define that. */
906static char *
907my_basename(char *name)
908{
909 char *cp = name;
910 char *result = name;
911
912 if (name == NULL)
913 return "???";
914 while (*cp != '\0') {
915 if (*cp == SEP)
916 result = cp + 1;
917 ++cp;
918 }
919 return result;
920}
921
922
923static PyObject *
924SyntaxError_str(PySyntaxErrorObject *self)
925{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000926 int have_lineno = 0;
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000927 char *filename = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928
929 /* XXX -- do all the additional formatting with filename and
930 lineno here */
931
Neal Norwitzed2b7392007-08-26 04:51:10 +0000932 if (self->filename && PyUnicode_Check(self->filename)) {
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000933 filename = PyUnicode_AsString(self->filename);
934 }
Guido van Rossumddefaf32007-01-14 03:31:43 +0000935 have_lineno = (self->lineno != NULL) && PyInt_CheckExact(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000936
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000937 if (!filename && !have_lineno)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000938 return PyObject_Unicode(self->msg ? self->msg : Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000939
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000940 if (filename && have_lineno)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000941 return PyUnicode_FromFormat("%S (%s, line %ld)",
942 self->msg ? self->msg : Py_None,
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000943 my_basename(filename),
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000944 PyInt_AsLong(self->lineno));
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000945 else if (filename)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000946 return PyUnicode_FromFormat("%S (%s)",
947 self->msg ? self->msg : Py_None,
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000948 my_basename(filename));
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000949 else /* only have_lineno */
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000950 return PyUnicode_FromFormat("%S (line %ld)",
951 self->msg ? self->msg : Py_None,
952 PyInt_AsLong(self->lineno));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953}
954
955static PyMemberDef SyntaxError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
957 PyDoc_STR("exception msg")},
958 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
959 PyDoc_STR("exception filename")},
960 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
961 PyDoc_STR("exception lineno")},
962 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
963 PyDoc_STR("exception offset")},
964 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
965 PyDoc_STR("exception text")},
966 {"print_file_and_line", T_OBJECT,
967 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
968 PyDoc_STR("exception print_file_and_line")},
969 {NULL} /* Sentinel */
970};
971
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000972ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000973 SyntaxError_dealloc, 0, SyntaxError_members,
974 SyntaxError_str, "Invalid syntax.");
975
976
977/*
978 * IndentationError extends SyntaxError
979 */
980MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
981 "Improper indentation.");
982
983
984/*
985 * TabError extends IndentationError
986 */
987MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
988 "Improper mixture of spaces and tabs.");
989
990
991/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000992 * LookupError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000993 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000994SimpleExtendsException(PyExc_Exception, LookupError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000995 "Base class for lookup errors.");
996
997
998/*
999 * IndexError extends LookupError
1000 */
1001SimpleExtendsException(PyExc_LookupError, IndexError,
1002 "Sequence index out of range.");
1003
1004
1005/*
1006 * KeyError extends LookupError
1007 */
1008static PyObject *
1009KeyError_str(PyBaseExceptionObject *self)
1010{
1011 /* If args is a tuple of exactly one item, apply repr to args[0].
1012 This is done so that e.g. the exception raised by {}[''] prints
1013 KeyError: ''
1014 rather than the confusing
1015 KeyError
1016 alone. The downside is that if KeyError is raised with an explanatory
1017 string, that string will be displayed in quotes. Too bad.
1018 If args is anything else, use the default BaseException__str__().
1019 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001020 if (PyTuple_GET_SIZE(self->args) == 1) {
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001021 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001022 }
1023 return BaseException_str(self);
1024}
1025
1026ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
1027 0, 0, 0, KeyError_str, "Mapping key not found.");
1028
1029
1030/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001031 * ValueError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001033SimpleExtendsException(PyExc_Exception, ValueError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001034 "Inappropriate argument value (of correct type).");
1035
1036/*
1037 * UnicodeError extends ValueError
1038 */
1039
1040SimpleExtendsException(PyExc_ValueError, UnicodeError,
1041 "Unicode related error.");
1042
Thomas Wouters477c8d52006-05-27 19:21:47 +00001043static PyObject *
Walter Dörwald612344f2007-05-04 19:28:21 +00001044get_bytes(PyObject *attr, const char *name)
1045{
1046 if (!attr) {
1047 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1048 return NULL;
1049 }
1050
1051 if (!PyBytes_Check(attr)) {
1052 PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
1053 return NULL;
1054 }
1055 Py_INCREF(attr);
1056 return attr;
1057}
1058
1059static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001060get_unicode(PyObject *attr, const char *name)
1061{
1062 if (!attr) {
1063 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1064 return NULL;
1065 }
1066
1067 if (!PyUnicode_Check(attr)) {
1068 PyErr_Format(PyExc_TypeError,
1069 "%.200s attribute must be unicode", name);
1070 return NULL;
1071 }
1072 Py_INCREF(attr);
1073 return attr;
1074}
1075
Walter Dörwaldd2034312007-05-18 16:29:38 +00001076static int
1077set_unicodefromstring(PyObject **attr, const char *value)
1078{
1079 PyObject *obj = PyUnicode_FromString(value);
1080 if (!obj)
1081 return -1;
1082 Py_CLEAR(*attr);
1083 *attr = obj;
1084 return 0;
1085}
1086
Thomas Wouters477c8d52006-05-27 19:21:47 +00001087PyObject *
1088PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1089{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001090 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001091}
1092
1093PyObject *
1094PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1095{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001096 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001097}
1098
1099PyObject *
1100PyUnicodeEncodeError_GetObject(PyObject *exc)
1101{
1102 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1103}
1104
1105PyObject *
1106PyUnicodeDecodeError_GetObject(PyObject *exc)
1107{
Walter Dörwald612344f2007-05-04 19:28:21 +00001108 return get_bytes(((PyUnicodeErrorObject *)exc)->object, "object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001109}
1110
1111PyObject *
1112PyUnicodeTranslateError_GetObject(PyObject *exc)
1113{
1114 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1115}
1116
1117int
1118PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1119{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001120 Py_ssize_t size;
1121 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1122 "object");
1123 if (!obj)
1124 return -1;
1125 *start = ((PyUnicodeErrorObject *)exc)->start;
1126 size = PyUnicode_GET_SIZE(obj);
1127 if (*start<0)
1128 *start = 0; /*XXX check for values <0*/
1129 if (*start>=size)
1130 *start = size-1;
1131 Py_DECREF(obj);
1132 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001133}
1134
1135
1136int
1137PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1138{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001139 Py_ssize_t size;
1140 PyObject *obj = get_bytes(((PyUnicodeErrorObject *)exc)->object, "object");
1141 if (!obj)
1142 return -1;
1143 size = PyBytes_GET_SIZE(obj);
1144 *start = ((PyUnicodeErrorObject *)exc)->start;
1145 if (*start<0)
1146 *start = 0;
1147 if (*start>=size)
1148 *start = size-1;
1149 Py_DECREF(obj);
1150 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001151}
1152
1153
1154int
1155PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1156{
1157 return PyUnicodeEncodeError_GetStart(exc, start);
1158}
1159
1160
1161int
1162PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1163{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001164 ((PyUnicodeErrorObject *)exc)->start = start;
1165 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001166}
1167
1168
1169int
1170PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1171{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001172 ((PyUnicodeErrorObject *)exc)->start = start;
1173 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001174}
1175
1176
1177int
1178PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1179{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001180 ((PyUnicodeErrorObject *)exc)->start = start;
1181 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001182}
1183
1184
1185int
1186PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1187{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001188 Py_ssize_t size;
1189 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1190 "object");
1191 if (!obj)
1192 return -1;
1193 *end = ((PyUnicodeErrorObject *)exc)->end;
1194 size = PyUnicode_GET_SIZE(obj);
1195 if (*end<1)
1196 *end = 1;
1197 if (*end>size)
1198 *end = size;
1199 Py_DECREF(obj);
1200 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001201}
1202
1203
1204int
1205PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1206{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001207 Py_ssize_t size;
1208 PyObject *obj = get_bytes(((PyUnicodeErrorObject *)exc)->object, "object");
1209 if (!obj)
1210 return -1;
1211 size = PyBytes_GET_SIZE(obj);
1212 *end = ((PyUnicodeErrorObject *)exc)->end;
1213 if (*end<1)
1214 *end = 1;
1215 if (*end>size)
1216 *end = size;
1217 Py_DECREF(obj);
1218 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001219}
1220
1221
1222int
1223PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1224{
1225 return PyUnicodeEncodeError_GetEnd(exc, start);
1226}
1227
1228
1229int
1230PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1231{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001232 ((PyUnicodeErrorObject *)exc)->end = end;
1233 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001234}
1235
1236
1237int
1238PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1239{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001240 ((PyUnicodeErrorObject *)exc)->end = end;
1241 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001242}
1243
1244
1245int
1246PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1247{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001248 ((PyUnicodeErrorObject *)exc)->end = end;
1249 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001250}
1251
1252PyObject *
1253PyUnicodeEncodeError_GetReason(PyObject *exc)
1254{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001255 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001256}
1257
1258
1259PyObject *
1260PyUnicodeDecodeError_GetReason(PyObject *exc)
1261{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001262 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001263}
1264
1265
1266PyObject *
1267PyUnicodeTranslateError_GetReason(PyObject *exc)
1268{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001269 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001270}
1271
1272
1273int
1274PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1275{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001276 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1277 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001278}
1279
1280
1281int
1282PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1283{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001284 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1285 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001286}
1287
1288
1289int
1290PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1291{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001292 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1293 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001294}
1295
1296
Thomas Wouters477c8d52006-05-27 19:21:47 +00001297static int
1298UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1299 PyTypeObject *objecttype)
1300{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001301 Py_CLEAR(self->encoding);
1302 Py_CLEAR(self->object);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001303 Py_CLEAR(self->reason);
1304
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001305 if (!PyArg_ParseTuple(args, "O!O!nnO!",
Walter Dörwaldd2034312007-05-18 16:29:38 +00001306 &PyUnicode_Type, &self->encoding,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001307 objecttype, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001308 &self->start,
1309 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001310 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001311 self->encoding = self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001312 return -1;
1313 }
1314
1315 Py_INCREF(self->encoding);
1316 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001317 Py_INCREF(self->reason);
1318
1319 return 0;
1320}
1321
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001322static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001323UnicodeError_clear(PyUnicodeErrorObject *self)
1324{
1325 Py_CLEAR(self->encoding);
1326 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001327 Py_CLEAR(self->reason);
1328 return BaseException_clear((PyBaseExceptionObject *)self);
1329}
1330
1331static void
1332UnicodeError_dealloc(PyUnicodeErrorObject *self)
1333{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001334 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001335 UnicodeError_clear(self);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001336 Py_Type(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001337}
1338
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001339static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001340UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1341{
1342 Py_VISIT(self->encoding);
1343 Py_VISIT(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001344 Py_VISIT(self->reason);
1345 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1346}
1347
1348static PyMemberDef UnicodeError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001349 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1350 PyDoc_STR("exception encoding")},
1351 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1352 PyDoc_STR("exception object")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001353 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001354 PyDoc_STR("exception start")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001355 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001356 PyDoc_STR("exception end")},
1357 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1358 PyDoc_STR("exception reason")},
1359 {NULL} /* Sentinel */
1360};
1361
1362
1363/*
1364 * UnicodeEncodeError extends UnicodeError
1365 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001366
1367static int
1368UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1369{
1370 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1371 return -1;
1372 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1373 kwds, &PyUnicode_Type);
1374}
1375
1376static PyObject *
1377UnicodeEncodeError_str(PyObject *self)
1378{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001379 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001380
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001381 if (uself->end==uself->start+1) {
1382 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001383 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001384 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001385 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001386 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001387 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001388 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001389 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001390 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001391 fmt,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001392 ((PyUnicodeErrorObject *)self)->encoding,
Walter Dörwald787b03b2007-06-05 13:29:29 +00001393 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001394 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001395 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001396 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001397 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001398 return PyUnicode_FromFormat(
1399 "'%U' codec can't encode characters in position %zd-%zd: %U",
1400 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001401 uself->start,
1402 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001403 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001404 );
1405}
1406
1407static PyTypeObject _PyExc_UnicodeEncodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001408 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001409 "UnicodeEncodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001410 sizeof(PyUnicodeErrorObject), 0,
1411 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1412 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1413 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001414 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1415 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001416 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001417 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001418};
1419PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1420
1421PyObject *
1422PyUnicodeEncodeError_Create(
1423 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1424 Py_ssize_t start, Py_ssize_t end, const char *reason)
1425{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001426 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "Uu#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001427 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001428}
1429
1430
1431/*
1432 * UnicodeDecodeError extends UnicodeError
1433 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001434
1435static int
1436UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1437{
1438 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1439 return -1;
1440 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
Walter Dörwald612344f2007-05-04 19:28:21 +00001441 kwds, &PyBytes_Type);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001442}
1443
1444static PyObject *
1445UnicodeDecodeError_str(PyObject *self)
1446{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001447 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001448
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001449 if (uself->end==uself->start+1) {
1450 int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001451 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001452 "'%U' codec can't decode byte 0x%02x in position %zd: %U",
Walter Dörwaldd2034312007-05-18 16:29:38 +00001453 ((PyUnicodeErrorObject *)self)->encoding,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001454 byte,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001455 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001456 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001457 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001458 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001459 return PyUnicode_FromFormat(
1460 "'%U' codec can't decode bytes in position %zd-%zd: %U",
1461 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001462 uself->start,
1463 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001464 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001465 );
1466}
1467
1468static PyTypeObject _PyExc_UnicodeDecodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001469 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001470 "UnicodeDecodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001471 sizeof(PyUnicodeErrorObject), 0,
1472 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1473 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1474 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001475 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1476 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001477 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001478 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001479};
1480PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1481
1482PyObject *
1483PyUnicodeDecodeError_Create(
1484 const char *encoding, const char *object, Py_ssize_t length,
1485 Py_ssize_t start, Py_ssize_t end, const char *reason)
1486{
1487 assert(length < INT_MAX);
1488 assert(start < INT_MAX);
1489 assert(end < INT_MAX);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001490 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "Uy#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001491 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001492}
1493
1494
1495/*
1496 * UnicodeTranslateError extends UnicodeError
1497 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001498
1499static int
1500UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1501 PyObject *kwds)
1502{
1503 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1504 return -1;
1505
1506 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001507 Py_CLEAR(self->reason);
1508
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001509 if (!PyArg_ParseTuple(args, "O!nnO!",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001510 &PyUnicode_Type, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001511 &self->start,
1512 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001513 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001514 self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001515 return -1;
1516 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001517
Thomas Wouters477c8d52006-05-27 19:21:47 +00001518 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001519 Py_INCREF(self->reason);
1520
1521 return 0;
1522}
1523
1524
1525static PyObject *
1526UnicodeTranslateError_str(PyObject *self)
1527{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001528 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001529
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001530 if (uself->end==uself->start+1) {
1531 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001532 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001533 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001534 fmt = "can't translate character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001535 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001536 fmt = "can't translate character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001537 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001538 fmt = "can't translate character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001539 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001540 fmt,
1541 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001542 uself->start,
1543 uself->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001544 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001545 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001546 return PyUnicode_FromFormat(
1547 "can't translate characters in position %zd-%zd: %U",
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001548 uself->start,
1549 uself->end-1,
1550 uself->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001551 );
1552}
1553
1554static PyTypeObject _PyExc_UnicodeTranslateError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001555 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001556 "UnicodeTranslateError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001557 sizeof(PyUnicodeErrorObject), 0,
1558 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1559 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1560 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001561 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001562 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1563 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001564 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001565};
1566PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1567
1568PyObject *
1569PyUnicodeTranslateError_Create(
1570 const Py_UNICODE *object, Py_ssize_t length,
1571 Py_ssize_t start, Py_ssize_t end, const char *reason)
1572{
1573 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001574 object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001575}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001576
1577
1578/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001579 * AssertionError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001580 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001581SimpleExtendsException(PyExc_Exception, AssertionError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001582 "Assertion failed.");
1583
1584
1585/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001586 * ArithmeticError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001587 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001588SimpleExtendsException(PyExc_Exception, ArithmeticError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001589 "Base class for arithmetic errors.");
1590
1591
1592/*
1593 * FloatingPointError extends ArithmeticError
1594 */
1595SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
1596 "Floating point operation failed.");
1597
1598
1599/*
1600 * OverflowError extends ArithmeticError
1601 */
1602SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
1603 "Result too large to be represented.");
1604
1605
1606/*
1607 * ZeroDivisionError extends ArithmeticError
1608 */
1609SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
1610 "Second argument to a division or modulo operation was zero.");
1611
1612
1613/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001614 * SystemError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001615 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001616SimpleExtendsException(PyExc_Exception, SystemError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001617 "Internal error in the Python interpreter.\n"
1618 "\n"
1619 "Please report this to the Python maintainer, along with the traceback,\n"
1620 "the Python version, and the hardware/OS platform and version.");
1621
1622
1623/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001624 * ReferenceError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001625 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001626SimpleExtendsException(PyExc_Exception, ReferenceError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001627 "Weak ref proxy used after referent went away.");
1628
1629
1630/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001631 * MemoryError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001632 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001633SimpleExtendsException(PyExc_Exception, MemoryError, "Out of memory.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001634
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001635/*
1636 * BufferError extends Exception
1637 */
1638SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error.");
1639
Thomas Wouters477c8d52006-05-27 19:21:47 +00001640
1641/* Warning category docstrings */
1642
1643/*
1644 * Warning extends Exception
1645 */
1646SimpleExtendsException(PyExc_Exception, Warning,
1647 "Base class for warning categories.");
1648
1649
1650/*
1651 * UserWarning extends Warning
1652 */
1653SimpleExtendsException(PyExc_Warning, UserWarning,
1654 "Base class for warnings generated by user code.");
1655
1656
1657/*
1658 * DeprecationWarning extends Warning
1659 */
1660SimpleExtendsException(PyExc_Warning, DeprecationWarning,
1661 "Base class for warnings about deprecated features.");
1662
1663
1664/*
1665 * PendingDeprecationWarning extends Warning
1666 */
1667SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1668 "Base class for warnings about features which will be deprecated\n"
1669 "in the future.");
1670
1671
1672/*
1673 * SyntaxWarning extends Warning
1674 */
1675SimpleExtendsException(PyExc_Warning, SyntaxWarning,
1676 "Base class for warnings about dubious syntax.");
1677
1678
1679/*
1680 * RuntimeWarning extends Warning
1681 */
1682SimpleExtendsException(PyExc_Warning, RuntimeWarning,
1683 "Base class for warnings about dubious runtime behavior.");
1684
1685
1686/*
1687 * FutureWarning extends Warning
1688 */
1689SimpleExtendsException(PyExc_Warning, FutureWarning,
1690 "Base class for warnings about constructs that will change semantically\n"
1691 "in the future.");
1692
1693
1694/*
1695 * ImportWarning extends Warning
1696 */
1697SimpleExtendsException(PyExc_Warning, ImportWarning,
1698 "Base class for warnings about probable mistakes in module imports");
1699
1700
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001701/*
1702 * UnicodeWarning extends Warning
1703 */
1704SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1705 "Base class for warnings about Unicode related problems, mostly\n"
1706 "related to conversion problems.");
1707
1708
Thomas Wouters477c8d52006-05-27 19:21:47 +00001709/* Pre-computed MemoryError instance. Best to create this as early as
1710 * possible and not wait until a MemoryError is actually raised!
1711 */
1712PyObject *PyExc_MemoryErrorInst=NULL;
1713
Thomas Wouters477c8d52006-05-27 19:21:47 +00001714#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1715 Py_FatalError("exceptions bootstrapping error.");
1716
1717#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001718 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1719 Py_FatalError("Module dictionary insertion problem.");
1720
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001721#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1722/* crt variable checking in VisualStudio .NET 2005 */
1723#include <crtdbg.h>
1724
1725static int prevCrtReportMode;
1726static _invalid_parameter_handler prevCrtHandler;
1727
1728/* Invalid parameter handler. Sets a ValueError exception */
1729static void
1730InvalidParameterHandler(
1731 const wchar_t * expression,
1732 const wchar_t * function,
1733 const wchar_t * file,
1734 unsigned int line,
1735 uintptr_t pReserved)
1736{
1737 /* Do nothing, allow execution to continue. Usually this
1738 * means that the CRT will set errno to EINVAL
1739 */
1740}
1741#endif
1742
1743
Thomas Wouters477c8d52006-05-27 19:21:47 +00001744PyMODINIT_FUNC
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001745_PyExc_Init(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001746{
Neal Norwitz2633c692007-02-26 22:22:47 +00001747 PyObject *bltinmod, *bdict;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001748
1749 PRE_INIT(BaseException)
1750 PRE_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001751 PRE_INIT(TypeError)
1752 PRE_INIT(StopIteration)
1753 PRE_INIT(GeneratorExit)
1754 PRE_INIT(SystemExit)
1755 PRE_INIT(KeyboardInterrupt)
1756 PRE_INIT(ImportError)
1757 PRE_INIT(EnvironmentError)
1758 PRE_INIT(IOError)
1759 PRE_INIT(OSError)
1760#ifdef MS_WINDOWS
1761 PRE_INIT(WindowsError)
1762#endif
1763#ifdef __VMS
1764 PRE_INIT(VMSError)
1765#endif
1766 PRE_INIT(EOFError)
1767 PRE_INIT(RuntimeError)
1768 PRE_INIT(NotImplementedError)
1769 PRE_INIT(NameError)
1770 PRE_INIT(UnboundLocalError)
1771 PRE_INIT(AttributeError)
1772 PRE_INIT(SyntaxError)
1773 PRE_INIT(IndentationError)
1774 PRE_INIT(TabError)
1775 PRE_INIT(LookupError)
1776 PRE_INIT(IndexError)
1777 PRE_INIT(KeyError)
1778 PRE_INIT(ValueError)
1779 PRE_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001780 PRE_INIT(UnicodeEncodeError)
1781 PRE_INIT(UnicodeDecodeError)
1782 PRE_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001783 PRE_INIT(AssertionError)
1784 PRE_INIT(ArithmeticError)
1785 PRE_INIT(FloatingPointError)
1786 PRE_INIT(OverflowError)
1787 PRE_INIT(ZeroDivisionError)
1788 PRE_INIT(SystemError)
1789 PRE_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001790 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001791 PRE_INIT(MemoryError)
1792 PRE_INIT(Warning)
1793 PRE_INIT(UserWarning)
1794 PRE_INIT(DeprecationWarning)
1795 PRE_INIT(PendingDeprecationWarning)
1796 PRE_INIT(SyntaxWarning)
1797 PRE_INIT(RuntimeWarning)
1798 PRE_INIT(FutureWarning)
1799 PRE_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001800 PRE_INIT(UnicodeWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001801
Thomas Wouters477c8d52006-05-27 19:21:47 +00001802 bltinmod = PyImport_ImportModule("__builtin__");
1803 if (bltinmod == NULL)
1804 Py_FatalError("exceptions bootstrapping error.");
1805 bdict = PyModule_GetDict(bltinmod);
1806 if (bdict == NULL)
1807 Py_FatalError("exceptions bootstrapping error.");
1808
1809 POST_INIT(BaseException)
1810 POST_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001811 POST_INIT(TypeError)
1812 POST_INIT(StopIteration)
1813 POST_INIT(GeneratorExit)
1814 POST_INIT(SystemExit)
1815 POST_INIT(KeyboardInterrupt)
1816 POST_INIT(ImportError)
1817 POST_INIT(EnvironmentError)
1818 POST_INIT(IOError)
1819 POST_INIT(OSError)
1820#ifdef MS_WINDOWS
1821 POST_INIT(WindowsError)
1822#endif
1823#ifdef __VMS
1824 POST_INIT(VMSError)
1825#endif
1826 POST_INIT(EOFError)
1827 POST_INIT(RuntimeError)
1828 POST_INIT(NotImplementedError)
1829 POST_INIT(NameError)
1830 POST_INIT(UnboundLocalError)
1831 POST_INIT(AttributeError)
1832 POST_INIT(SyntaxError)
1833 POST_INIT(IndentationError)
1834 POST_INIT(TabError)
1835 POST_INIT(LookupError)
1836 POST_INIT(IndexError)
1837 POST_INIT(KeyError)
1838 POST_INIT(ValueError)
1839 POST_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001840 POST_INIT(UnicodeEncodeError)
1841 POST_INIT(UnicodeDecodeError)
1842 POST_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001843 POST_INIT(AssertionError)
1844 POST_INIT(ArithmeticError)
1845 POST_INIT(FloatingPointError)
1846 POST_INIT(OverflowError)
1847 POST_INIT(ZeroDivisionError)
1848 POST_INIT(SystemError)
1849 POST_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001850 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001851 POST_INIT(MemoryError)
1852 POST_INIT(Warning)
1853 POST_INIT(UserWarning)
1854 POST_INIT(DeprecationWarning)
1855 POST_INIT(PendingDeprecationWarning)
1856 POST_INIT(SyntaxWarning)
1857 POST_INIT(RuntimeWarning)
1858 POST_INIT(FutureWarning)
1859 POST_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001860 POST_INIT(UnicodeWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001861
1862 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
1863 if (!PyExc_MemoryErrorInst)
1864 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1865
1866 Py_DECREF(bltinmod);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001867
1868#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1869 /* Set CRT argument error handler */
1870 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
1871 /* turn off assertions in debug mode */
1872 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
1873#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001874}
1875
1876void
1877_PyExc_Fini(void)
1878{
1879 Py_XDECREF(PyExc_MemoryErrorInst);
1880 PyExc_MemoryErrorInst = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001881#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1882 /* reset CRT error handling */
1883 _set_invalid_parameter_handler(prevCrtHandler);
1884 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
1885#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001886}