blob: 02a55c12542cf1d796057df547b236a3969c3e76 [file] [log] [blame]
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001/*
2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
3 *
4 * Thanks go to Tim Peters and Michael Hudson for debugging.
5 */
6
Thomas Wouters477c8d52006-05-27 19:21:47 +00007#define PY_SSIZE_T_CLEAN
8#include <Python.h>
9#include "structmember.h"
10#include "osdefs.h"
11
12#define MAKE_IT_NONE(x) (x) = Py_None; Py_INCREF(Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +000013
14/* NOTE: If the exception class hierarchy changes, don't forget to update
15 * Lib/test/exception_hierarchy.txt
16 */
17
Thomas Wouters477c8d52006-05-27 19:21:47 +000018/*
19 * BaseException
20 */
21static PyObject *
22BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
23{
24 PyBaseExceptionObject *self;
25
26 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +000027 if (!self)
28 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000029 /* the dict is created on the fly in PyObject_GenericSetAttr */
Guido van Rossumebe3e162007-05-17 18:20:34 +000030 self->dict = NULL;
Collin Winter1966f1c2007-09-01 20:26:44 +000031 self->traceback = self->cause = self->context = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000032
33 self->args = PyTuple_New(0);
34 if (!self->args) {
35 Py_DECREF(self);
36 return NULL;
37 }
38
Thomas Wouters477c8d52006-05-27 19:21:47 +000039 return (PyObject *)self;
40}
41
42static int
43BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
44{
Christian Heimes90aa7642007-12-19 02:45:37 +000045 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000046 return -1;
47
Thomas Wouters477c8d52006-05-27 19:21:47 +000048 Py_DECREF(self->args);
49 self->args = args;
50 Py_INCREF(self->args);
51
Thomas Wouters477c8d52006-05-27 19:21:47 +000052 return 0;
53}
54
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000055static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000056BaseException_clear(PyBaseExceptionObject *self)
57{
58 Py_CLEAR(self->dict);
59 Py_CLEAR(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000060 Py_CLEAR(self->traceback);
Collin Winter1966f1c2007-09-01 20:26:44 +000061 Py_CLEAR(self->cause);
62 Py_CLEAR(self->context);
Thomas Wouters477c8d52006-05-27 19:21:47 +000063 return 0;
64}
65
66static void
67BaseException_dealloc(PyBaseExceptionObject *self)
68{
Thomas Wouters89f507f2006-12-13 04:49:30 +000069 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000070 BaseException_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +000071 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000072}
73
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000074static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000075BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
76{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000077 Py_VISIT(self->dict);
Thomas Wouters477c8d52006-05-27 19:21:47 +000078 Py_VISIT(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000079 Py_VISIT(self->traceback);
Collin Winter1966f1c2007-09-01 20:26:44 +000080 Py_VISIT(self->cause);
81 Py_VISIT(self->context);
Thomas Wouters477c8d52006-05-27 19:21:47 +000082 return 0;
83}
84
85static PyObject *
86BaseException_str(PyBaseExceptionObject *self)
87{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000088 switch (PyTuple_GET_SIZE(self->args)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000089 case 0:
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +000090 return PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +000091 case 1:
Thomas Heller519a0422007-11-15 20:48:54 +000092 return PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +000093 default:
Thomas Heller519a0422007-11-15 20:48:54 +000094 return PyObject_Str(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +000095 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000096}
97
98static PyObject *
99BaseException_repr(PyBaseExceptionObject *self)
100{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000101 char *name;
102 char *dot;
103
Christian Heimes90aa7642007-12-19 02:45:37 +0000104 name = (char *)Py_TYPE(self)->tp_name;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000105 dot = strrchr(name, '.');
106 if (dot != NULL) name = dot+1;
107
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000108 return PyUnicode_FromFormat("%s%R", name, self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000109}
110
111/* Pickling support */
112static PyObject *
113BaseException_reduce(PyBaseExceptionObject *self)
114{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000115 if (self->args && self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000116 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000117 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000118 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000119}
120
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000121/*
122 * Needed for backward compatibility, since exceptions used to store
123 * all their attributes in the __dict__. Code is taken from cPickle's
124 * load_build function.
125 */
126static PyObject *
127BaseException_setstate(PyObject *self, PyObject *state)
128{
129 PyObject *d_key, *d_value;
130 Py_ssize_t i = 0;
131
132 if (state != Py_None) {
133 if (!PyDict_Check(state)) {
134 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
135 return NULL;
136 }
137 while (PyDict_Next(state, &i, &d_key, &d_value)) {
138 if (PyObject_SetAttr(self, d_key, d_value) < 0)
139 return NULL;
140 }
141 }
142 Py_RETURN_NONE;
143}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000144
Collin Winter828f04a2007-08-31 00:04:24 +0000145static PyObject *
146BaseException_with_traceback(PyObject *self, PyObject *tb) {
147 if (PyException_SetTraceback(self, tb))
148 return NULL;
149
150 Py_INCREF(self);
151 return self;
152}
153
Georg Brandl76941002008-05-05 21:38:47 +0000154PyDoc_STRVAR(with_traceback_doc,
155"Exception.with_traceback(tb) --\n\
156 set self.__traceback__ to tb and return self.");
157
Thomas Wouters477c8d52006-05-27 19:21:47 +0000158
159static PyMethodDef BaseException_methods[] = {
160 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000161 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Georg Brandl76941002008-05-05 21:38:47 +0000162 {"with_traceback", (PyCFunction)BaseException_with_traceback, METH_O,
163 with_traceback_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000164 {NULL, NULL, 0, NULL},
165};
166
167
Thomas Wouters477c8d52006-05-27 19:21:47 +0000168static PyObject *
169BaseException_get_dict(PyBaseExceptionObject *self)
170{
171 if (self->dict == NULL) {
172 self->dict = PyDict_New();
173 if (!self->dict)
174 return NULL;
175 }
176 Py_INCREF(self->dict);
177 return self->dict;
178}
179
180static int
181BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
182{
183 if (val == NULL) {
184 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
185 return -1;
186 }
187 if (!PyDict_Check(val)) {
188 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
189 return -1;
190 }
191 Py_CLEAR(self->dict);
192 Py_INCREF(val);
193 self->dict = val;
194 return 0;
195}
196
197static PyObject *
198BaseException_get_args(PyBaseExceptionObject *self)
199{
200 if (self->args == NULL) {
201 Py_INCREF(Py_None);
202 return Py_None;
203 }
204 Py_INCREF(self->args);
205 return self->args;
206}
207
208static int
209BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
210{
211 PyObject *seq;
212 if (val == NULL) {
213 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
214 return -1;
215 }
216 seq = PySequence_Tuple(val);
217 if (!seq) return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000218 Py_CLEAR(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000219 self->args = seq;
220 return 0;
221}
222
Collin Winter828f04a2007-08-31 00:04:24 +0000223static PyObject *
224BaseException_get_tb(PyBaseExceptionObject *self)
225{
226 if (self->traceback == NULL) {
227 Py_INCREF(Py_None);
228 return Py_None;
229 }
230 Py_INCREF(self->traceback);
231 return self->traceback;
232}
233
234static int
235BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb)
236{
237 if (tb == NULL) {
238 PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted");
239 return -1;
240 }
241 else if (!(tb == Py_None || PyTraceBack_Check(tb))) {
242 PyErr_SetString(PyExc_TypeError,
243 "__traceback__ must be a traceback or None");
244 return -1;
245 }
246
247 Py_XINCREF(tb);
248 Py_XDECREF(self->traceback);
249 self->traceback = tb;
250 return 0;
251}
252
Guido van Rossum360e4b82007-05-14 22:51:27 +0000253
Thomas Wouters477c8d52006-05-27 19:21:47 +0000254static PyGetSetDef BaseException_getset[] = {
255 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
256 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Collin Winter828f04a2007-08-31 00:04:24 +0000257 {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000258 {NULL},
259};
260
261
Collin Winter828f04a2007-08-31 00:04:24 +0000262PyObject *
263PyException_GetTraceback(PyObject *self) {
264 PyBaseExceptionObject *base_self = (PyBaseExceptionObject *)self;
265 Py_XINCREF(base_self->traceback);
266 return base_self->traceback;
267}
268
269
270int
271PyException_SetTraceback(PyObject *self, PyObject *tb) {
272 return BaseException_set_tb((PyBaseExceptionObject *)self, tb);
273}
274
275PyObject *
276PyException_GetCause(PyObject *self) {
277 PyObject *cause = ((PyBaseExceptionObject *)self)->cause;
278 Py_XINCREF(cause);
279 return cause;
280}
281
282/* Steals a reference to cause */
283void
284PyException_SetCause(PyObject *self, PyObject *cause) {
285 PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause;
286 ((PyBaseExceptionObject *)self)->cause = cause;
287 Py_XDECREF(old_cause);
288}
289
290PyObject *
291PyException_GetContext(PyObject *self) {
292 PyObject *context = ((PyBaseExceptionObject *)self)->context;
293 Py_XINCREF(context);
294 return context;
295}
296
297/* Steals a reference to context */
298void
299PyException_SetContext(PyObject *self, PyObject *context) {
300 PyObject *old_context = ((PyBaseExceptionObject *)self)->context;
301 ((PyBaseExceptionObject *)self)->context = context;
302 Py_XDECREF(old_context);
303}
304
305
306static PyMemberDef BaseException_members[] = {
307 {"__context__", T_OBJECT, offsetof(PyBaseExceptionObject, context), 0,
308 PyDoc_STR("exception context")},
309 {"__cause__", T_OBJECT, offsetof(PyBaseExceptionObject, cause), 0,
310 PyDoc_STR("exception cause")},
311 {NULL} /* Sentinel */
312};
313
Thomas Wouters477c8d52006-05-27 19:21:47 +0000314static PyTypeObject _PyExc_BaseException = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000315 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +0000316 "BaseException", /*tp_name*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000317 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
318 0, /*tp_itemsize*/
319 (destructor)BaseException_dealloc, /*tp_dealloc*/
320 0, /*tp_print*/
321 0, /*tp_getattr*/
322 0, /*tp_setattr*/
323 0, /* tp_compare; */
324 (reprfunc)BaseException_repr, /*tp_repr*/
325 0, /*tp_as_number*/
Brett Cannonba7bf492007-02-27 00:15:55 +0000326 0, /*tp_as_sequence*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000327 0, /*tp_as_mapping*/
328 0, /*tp_hash */
329 0, /*tp_call*/
330 (reprfunc)BaseException_str, /*tp_str*/
331 PyObject_GenericGetAttr, /*tp_getattro*/
332 PyObject_GenericSetAttr, /*tp_setattro*/
333 0, /*tp_as_buffer*/
Thomas Wouters27d517b2007-02-25 20:39:11 +0000334 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
335 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000336 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
337 (traverseproc)BaseException_traverse, /* tp_traverse */
338 (inquiry)BaseException_clear, /* tp_clear */
339 0, /* tp_richcompare */
340 0, /* tp_weaklistoffset */
341 0, /* tp_iter */
342 0, /* tp_iternext */
343 BaseException_methods, /* tp_methods */
Collin Winter828f04a2007-08-31 00:04:24 +0000344 BaseException_members, /* tp_members */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000345 BaseException_getset, /* tp_getset */
346 0, /* tp_base */
347 0, /* tp_dict */
348 0, /* tp_descr_get */
349 0, /* tp_descr_set */
350 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
351 (initproc)BaseException_init, /* tp_init */
352 0, /* tp_alloc */
353 BaseException_new, /* tp_new */
354};
355/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
356from the previous implmentation and also allowing Python objects to be used
357in the API */
358PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
359
360/* note these macros omit the last semicolon so the macro invocation may
361 * include it and not look strange.
362 */
363#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
364static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000365 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000366 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000367 sizeof(PyBaseExceptionObject), \
368 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
369 0, 0, 0, 0, 0, 0, 0, \
370 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
371 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
372 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
373 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
374 (initproc)BaseException_init, 0, BaseException_new,\
375}; \
376PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
377
378#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
379static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000380 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000381 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000382 sizeof(Py ## EXCSTORE ## Object), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000383 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000384 0, 0, 0, 0, 0, \
385 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000386 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
387 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000388 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000389 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000390}; \
391PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
392
393#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
394static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000395 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000396 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000397 sizeof(Py ## EXCSTORE ## Object), 0, \
398 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
399 (reprfunc)EXCSTR, 0, 0, 0, \
400 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
401 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
402 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
403 EXCMEMBERS, 0, &_ ## EXCBASE, \
404 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000405 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000406}; \
407PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
408
409
410/*
411 * Exception extends BaseException
412 */
413SimpleExtendsException(PyExc_BaseException, Exception,
414 "Common base class for all non-exit exceptions.");
415
416
417/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000418 * TypeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000419 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000420SimpleExtendsException(PyExc_Exception, TypeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000421 "Inappropriate argument type.");
422
423
424/*
425 * StopIteration extends Exception
426 */
427SimpleExtendsException(PyExc_Exception, StopIteration,
Georg Brandla18af4e2007-04-21 15:47:16 +0000428 "Signal the end from iterator.__next__().");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000429
430
431/*
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000432 * GeneratorExit extends BaseException
Thomas Wouters477c8d52006-05-27 19:21:47 +0000433 */
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000434SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000435 "Request that a generator exit.");
436
437
438/*
439 * SystemExit extends BaseException
440 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000441
442static int
443SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
444{
445 Py_ssize_t size = PyTuple_GET_SIZE(args);
446
447 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
448 return -1;
449
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000450 if (size == 0)
451 return 0;
452 Py_CLEAR(self->code);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000453 if (size == 1)
454 self->code = PyTuple_GET_ITEM(args, 0);
455 else if (size > 1)
456 self->code = args;
457 Py_INCREF(self->code);
458 return 0;
459}
460
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000461static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000462SystemExit_clear(PySystemExitObject *self)
463{
464 Py_CLEAR(self->code);
465 return BaseException_clear((PyBaseExceptionObject *)self);
466}
467
468static void
469SystemExit_dealloc(PySystemExitObject *self)
470{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000471 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000472 SystemExit_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000473 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000474}
475
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000476static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000477SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
478{
479 Py_VISIT(self->code);
480 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
481}
482
483static PyMemberDef SystemExit_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000484 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
485 PyDoc_STR("exception code")},
486 {NULL} /* Sentinel */
487};
488
489ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
490 SystemExit_dealloc, 0, SystemExit_members, 0,
491 "Request to exit from the interpreter.");
492
493/*
494 * KeyboardInterrupt extends BaseException
495 */
496SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
497 "Program interrupted by user.");
498
499
500/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000501 * ImportError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000502 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000503SimpleExtendsException(PyExc_Exception, ImportError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000504 "Import can't find module, or can't find name in module.");
505
506
507/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000508 * EnvironmentError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000509 */
510
Thomas Wouters477c8d52006-05-27 19:21:47 +0000511/* Where a function has a single filename, such as open() or some
512 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
513 * called, giving a third argument which is the filename. But, so
514 * that old code using in-place unpacking doesn't break, e.g.:
515 *
516 * except IOError, (errno, strerror):
517 *
518 * we hack args so that it only contains two items. This also
519 * means we need our own __str__() which prints out the filename
520 * when it was supplied.
521 */
522static int
523EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
524 PyObject *kwds)
525{
526 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
527 PyObject *subslice = NULL;
528
529 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
530 return -1;
531
Thomas Wouters89f507f2006-12-13 04:49:30 +0000532 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000533 return 0;
534 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000535
536 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000537 &myerrno, &strerror, &filename)) {
538 return -1;
539 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000540 Py_CLEAR(self->myerrno); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000541 self->myerrno = myerrno;
542 Py_INCREF(self->myerrno);
543
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000544 Py_CLEAR(self->strerror); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000545 self->strerror = strerror;
546 Py_INCREF(self->strerror);
547
548 /* self->filename will remain Py_None otherwise */
549 if (filename != NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000550 Py_CLEAR(self->filename); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000551 self->filename = filename;
552 Py_INCREF(self->filename);
553
554 subslice = PyTuple_GetSlice(args, 0, 2);
555 if (!subslice)
556 return -1;
557
558 Py_DECREF(self->args); /* replacing args */
559 self->args = subslice;
560 }
561 return 0;
562}
563
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000564static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000565EnvironmentError_clear(PyEnvironmentErrorObject *self)
566{
567 Py_CLEAR(self->myerrno);
568 Py_CLEAR(self->strerror);
569 Py_CLEAR(self->filename);
570 return BaseException_clear((PyBaseExceptionObject *)self);
571}
572
573static void
574EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
575{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000576 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000577 EnvironmentError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000578 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000579}
580
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000581static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000582EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
583 void *arg)
584{
585 Py_VISIT(self->myerrno);
586 Py_VISIT(self->strerror);
587 Py_VISIT(self->filename);
588 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
589}
590
591static PyObject *
592EnvironmentError_str(PyEnvironmentErrorObject *self)
593{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000594 if (self->filename)
595 return PyUnicode_FromFormat("[Errno %S] %S: %R",
596 self->myerrno ? self->myerrno: Py_None,
597 self->strerror ? self->strerror: Py_None,
598 self->filename);
599 else if (self->myerrno && self->strerror)
600 return PyUnicode_FromFormat("[Errno %S] %S",
601 self->myerrno ? self->myerrno: Py_None,
602 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000603 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000604 return BaseException_str((PyBaseExceptionObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000605}
606
607static PyMemberDef EnvironmentError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000608 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
609 PyDoc_STR("exception errno")},
610 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
611 PyDoc_STR("exception strerror")},
612 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
613 PyDoc_STR("exception filename")},
614 {NULL} /* Sentinel */
615};
616
617
618static PyObject *
619EnvironmentError_reduce(PyEnvironmentErrorObject *self)
620{
621 PyObject *args = self->args;
622 PyObject *res = NULL, *tmp;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000623
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624 /* self->args is only the first two real arguments if there was a
625 * file name given to EnvironmentError. */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000626 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000627 args = PyTuple_New(3);
628 if (!args) return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000629
630 tmp = PyTuple_GET_ITEM(self->args, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000631 Py_INCREF(tmp);
632 PyTuple_SET_ITEM(args, 0, tmp);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000633
634 tmp = PyTuple_GET_ITEM(self->args, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000635 Py_INCREF(tmp);
636 PyTuple_SET_ITEM(args, 1, tmp);
637
638 Py_INCREF(self->filename);
639 PyTuple_SET_ITEM(args, 2, self->filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000640 } else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000641 Py_INCREF(args);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000642
643 if (self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000644 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000645 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000646 res = PyTuple_Pack(2, Py_TYPE(self), args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000647 Py_DECREF(args);
648 return res;
649}
650
651
652static PyMethodDef EnvironmentError_methods[] = {
653 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
654 {NULL}
655};
656
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000657ComplexExtendsException(PyExc_Exception, EnvironmentError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 EnvironmentError, EnvironmentError_dealloc,
659 EnvironmentError_methods, EnvironmentError_members,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000660 EnvironmentError_str,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000661 "Base class for I/O related errors.");
662
663
664/*
665 * IOError extends EnvironmentError
666 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000667MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000668 EnvironmentError, "I/O operation failed.");
669
670
671/*
672 * OSError extends EnvironmentError
673 */
674MiddlingExtendsException(PyExc_EnvironmentError, OSError,
675 EnvironmentError, "OS system call failed.");
676
677
678/*
679 * WindowsError extends OSError
680 */
681#ifdef MS_WINDOWS
682#include "errmap.h"
683
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000684static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000685WindowsError_clear(PyWindowsErrorObject *self)
686{
687 Py_CLEAR(self->myerrno);
688 Py_CLEAR(self->strerror);
689 Py_CLEAR(self->filename);
690 Py_CLEAR(self->winerror);
691 return BaseException_clear((PyBaseExceptionObject *)self);
692}
693
694static void
695WindowsError_dealloc(PyWindowsErrorObject *self)
696{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000697 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000698 WindowsError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000699 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700}
701
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000702static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000703WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
704{
705 Py_VISIT(self->myerrno);
706 Py_VISIT(self->strerror);
707 Py_VISIT(self->filename);
708 Py_VISIT(self->winerror);
709 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
710}
711
Thomas Wouters477c8d52006-05-27 19:21:47 +0000712static int
713WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
714{
715 PyObject *o_errcode = NULL;
716 long errcode;
717 long posix_errno;
718
719 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
720 == -1)
721 return -1;
722
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000723 if (self->myerrno == NULL)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000724 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000725
726 /* Set errno to the POSIX errno, and winerror to the Win32
727 error code. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000728 errcode = PyLong_AsLong(self->myerrno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000729 if (errcode == -1 && PyErr_Occurred())
730 return -1;
731 posix_errno = winerror_to_errno(errcode);
732
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000733 Py_CLEAR(self->winerror);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000734 self->winerror = self->myerrno;
735
Christian Heimes217cfd12007-12-02 14:31:20 +0000736 o_errcode = PyLong_FromLong(posix_errno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000737 if (!o_errcode)
738 return -1;
739
740 self->myerrno = o_errcode;
741
742 return 0;
743}
744
745
746static PyObject *
747WindowsError_str(PyWindowsErrorObject *self)
748{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000749 if (self->filename)
750 return PyUnicode_FromFormat("[Error %S] %S: %R",
751 self->winerror ? self->winerror: Py_None,
752 self->strerror ? self->strerror: Py_None,
753 self->filename);
754 else if (self->winerror && self->strerror)
755 return PyUnicode_FromFormat("[Error %S] %S",
756 self->winerror ? self->winerror: Py_None,
757 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000758 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000759 return EnvironmentError_str((PyEnvironmentErrorObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000760}
761
762static PyMemberDef WindowsError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000763 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
764 PyDoc_STR("POSIX exception code")},
765 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
766 PyDoc_STR("exception strerror")},
767 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
768 PyDoc_STR("exception filename")},
769 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
770 PyDoc_STR("Win32 exception code")},
771 {NULL} /* Sentinel */
772};
773
774ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
775 WindowsError_dealloc, 0, WindowsError_members,
776 WindowsError_str, "MS-Windows OS system call failed.");
777
778#endif /* MS_WINDOWS */
779
780
781/*
782 * VMSError extends OSError (I think)
783 */
784#ifdef __VMS
785MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
786 "OpenVMS OS system call failed.");
787#endif
788
789
790/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000791 * EOFError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000792 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000793SimpleExtendsException(PyExc_Exception, EOFError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000794 "Read beyond end of file.");
795
796
797/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000798 * RuntimeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000799 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000800SimpleExtendsException(PyExc_Exception, RuntimeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000801 "Unspecified run-time error.");
802
803
804/*
805 * NotImplementedError extends RuntimeError
806 */
807SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
808 "Method or function hasn't been implemented yet.");
809
810/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000811 * NameError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000812 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000813SimpleExtendsException(PyExc_Exception, NameError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000814 "Name not found globally.");
815
816/*
817 * UnboundLocalError extends NameError
818 */
819SimpleExtendsException(PyExc_NameError, UnboundLocalError,
820 "Local name referenced but not bound to a value.");
821
822/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000823 * AttributeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000824 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000825SimpleExtendsException(PyExc_Exception, AttributeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000826 "Attribute not found.");
827
828
829/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000830 * SyntaxError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000831 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000832
833static int
834SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
835{
836 PyObject *info = NULL;
837 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
838
839 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
840 return -1;
841
842 if (lenargs >= 1) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000843 Py_CLEAR(self->msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000844 self->msg = PyTuple_GET_ITEM(args, 0);
845 Py_INCREF(self->msg);
846 }
847 if (lenargs == 2) {
848 info = PyTuple_GET_ITEM(args, 1);
849 info = PySequence_Tuple(info);
850 if (!info) return -1;
851
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000852 if (PyTuple_GET_SIZE(info) != 4) {
853 /* not a very good error message, but it's what Python 2.4 gives */
854 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
855 Py_DECREF(info);
856 return -1;
857 }
858
859 Py_CLEAR(self->filename);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000860 self->filename = PyTuple_GET_ITEM(info, 0);
861 Py_INCREF(self->filename);
862
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000863 Py_CLEAR(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000864 self->lineno = PyTuple_GET_ITEM(info, 1);
865 Py_INCREF(self->lineno);
866
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000867 Py_CLEAR(self->offset);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000868 self->offset = PyTuple_GET_ITEM(info, 2);
869 Py_INCREF(self->offset);
870
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000871 Py_CLEAR(self->text);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000872 self->text = PyTuple_GET_ITEM(info, 3);
873 Py_INCREF(self->text);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000874
875 Py_DECREF(info);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000876 }
877 return 0;
878}
879
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000880static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000881SyntaxError_clear(PySyntaxErrorObject *self)
882{
883 Py_CLEAR(self->msg);
884 Py_CLEAR(self->filename);
885 Py_CLEAR(self->lineno);
886 Py_CLEAR(self->offset);
887 Py_CLEAR(self->text);
888 Py_CLEAR(self->print_file_and_line);
889 return BaseException_clear((PyBaseExceptionObject *)self);
890}
891
892static void
893SyntaxError_dealloc(PySyntaxErrorObject *self)
894{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000895 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000896 SyntaxError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000897 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000898}
899
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000900static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000901SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
902{
903 Py_VISIT(self->msg);
904 Py_VISIT(self->filename);
905 Py_VISIT(self->lineno);
906 Py_VISIT(self->offset);
907 Py_VISIT(self->text);
908 Py_VISIT(self->print_file_and_line);
909 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
910}
911
912/* This is called "my_basename" instead of just "basename" to avoid name
913 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
914 defined, and Python does define that. */
915static char *
916my_basename(char *name)
917{
918 char *cp = name;
919 char *result = name;
920
921 if (name == NULL)
922 return "???";
923 while (*cp != '\0') {
924 if (*cp == SEP)
925 result = cp + 1;
926 ++cp;
927 }
928 return result;
929}
930
931
932static PyObject *
933SyntaxError_str(PySyntaxErrorObject *self)
934{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000935 int have_lineno = 0;
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000936 char *filename = 0;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000937 /* Below, we always ignore overflow errors, just printing -1.
938 Still, we cannot allow an OverflowError to be raised, so
939 we need to call PyLong_AsLongAndOverflow. */
940 int overflow;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000941
942 /* XXX -- do all the additional formatting with filename and
943 lineno here */
944
Neal Norwitzed2b7392007-08-26 04:51:10 +0000945 if (self->filename && PyUnicode_Check(self->filename)) {
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000946 filename = PyUnicode_AsString(self->filename);
947 }
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000948 have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000949
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000950 if (!filename && !have_lineno)
Thomas Heller519a0422007-11-15 20:48:54 +0000951 return PyObject_Str(self->msg ? self->msg : Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000952
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000953 if (filename && have_lineno)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000954 return PyUnicode_FromFormat("%S (%s, line %ld)",
955 self->msg ? self->msg : Py_None,
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000956 my_basename(filename),
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000957 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000958 else if (filename)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000959 return PyUnicode_FromFormat("%S (%s)",
960 self->msg ? self->msg : Py_None,
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000961 my_basename(filename));
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000962 else /* only have_lineno */
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000963 return PyUnicode_FromFormat("%S (line %ld)",
964 self->msg ? self->msg : Py_None,
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000965 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000966}
967
968static PyMemberDef SyntaxError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000969 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
970 PyDoc_STR("exception msg")},
971 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
972 PyDoc_STR("exception filename")},
973 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
974 PyDoc_STR("exception lineno")},
975 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
976 PyDoc_STR("exception offset")},
977 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
978 PyDoc_STR("exception text")},
979 {"print_file_and_line", T_OBJECT,
980 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
981 PyDoc_STR("exception print_file_and_line")},
982 {NULL} /* Sentinel */
983};
984
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000985ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000986 SyntaxError_dealloc, 0, SyntaxError_members,
987 SyntaxError_str, "Invalid syntax.");
988
989
990/*
991 * IndentationError extends SyntaxError
992 */
993MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
994 "Improper indentation.");
995
996
997/*
998 * TabError extends IndentationError
999 */
1000MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
1001 "Improper mixture of spaces and tabs.");
1002
1003
1004/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001005 * LookupError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001006 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001007SimpleExtendsException(PyExc_Exception, LookupError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008 "Base class for lookup errors.");
1009
1010
1011/*
1012 * IndexError extends LookupError
1013 */
1014SimpleExtendsException(PyExc_LookupError, IndexError,
1015 "Sequence index out of range.");
1016
1017
1018/*
1019 * KeyError extends LookupError
1020 */
1021static PyObject *
1022KeyError_str(PyBaseExceptionObject *self)
1023{
1024 /* If args is a tuple of exactly one item, apply repr to args[0].
1025 This is done so that e.g. the exception raised by {}[''] prints
1026 KeyError: ''
1027 rather than the confusing
1028 KeyError
1029 alone. The downside is that if KeyError is raised with an explanatory
1030 string, that string will be displayed in quotes. Too bad.
1031 If args is anything else, use the default BaseException__str__().
1032 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001033 if (PyTuple_GET_SIZE(self->args) == 1) {
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001034 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001035 }
1036 return BaseException_str(self);
1037}
1038
1039ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
1040 0, 0, 0, KeyError_str, "Mapping key not found.");
1041
1042
1043/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001044 * ValueError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001045 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001046SimpleExtendsException(PyExc_Exception, ValueError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001047 "Inappropriate argument value (of correct type).");
1048
1049/*
1050 * UnicodeError extends ValueError
1051 */
1052
1053SimpleExtendsException(PyExc_ValueError, UnicodeError,
1054 "Unicode related error.");
1055
Thomas Wouters477c8d52006-05-27 19:21:47 +00001056static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001057get_string(PyObject *attr, const char *name)
Walter Dörwald612344f2007-05-04 19:28:21 +00001058{
1059 if (!attr) {
1060 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1061 return NULL;
1062 }
1063
Christian Heimes72b710a2008-05-26 13:28:38 +00001064 if (!PyBytes_Check(attr)) {
Walter Dörwald612344f2007-05-04 19:28:21 +00001065 PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
1066 return NULL;
1067 }
1068 Py_INCREF(attr);
1069 return attr;
1070}
1071
1072static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001073get_unicode(PyObject *attr, const char *name)
1074{
1075 if (!attr) {
1076 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1077 return NULL;
1078 }
1079
1080 if (!PyUnicode_Check(attr)) {
1081 PyErr_Format(PyExc_TypeError,
1082 "%.200s attribute must be unicode", name);
1083 return NULL;
1084 }
1085 Py_INCREF(attr);
1086 return attr;
1087}
1088
Walter Dörwaldd2034312007-05-18 16:29:38 +00001089static int
1090set_unicodefromstring(PyObject **attr, const char *value)
1091{
1092 PyObject *obj = PyUnicode_FromString(value);
1093 if (!obj)
1094 return -1;
1095 Py_CLEAR(*attr);
1096 *attr = obj;
1097 return 0;
1098}
1099
Thomas Wouters477c8d52006-05-27 19:21:47 +00001100PyObject *
1101PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1102{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001103 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001104}
1105
1106PyObject *
1107PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1108{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001109 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001110}
1111
1112PyObject *
1113PyUnicodeEncodeError_GetObject(PyObject *exc)
1114{
1115 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1116}
1117
1118PyObject *
1119PyUnicodeDecodeError_GetObject(PyObject *exc)
1120{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001121 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001122}
1123
1124PyObject *
1125PyUnicodeTranslateError_GetObject(PyObject *exc)
1126{
1127 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1128}
1129
1130int
1131PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1132{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001133 Py_ssize_t size;
1134 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1135 "object");
1136 if (!obj)
1137 return -1;
1138 *start = ((PyUnicodeErrorObject *)exc)->start;
1139 size = PyUnicode_GET_SIZE(obj);
1140 if (*start<0)
1141 *start = 0; /*XXX check for values <0*/
1142 if (*start>=size)
1143 *start = size-1;
1144 Py_DECREF(obj);
1145 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001146}
1147
1148
1149int
1150PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1151{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001152 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001153 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001154 if (!obj)
1155 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001156 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001157 *start = ((PyUnicodeErrorObject *)exc)->start;
1158 if (*start<0)
1159 *start = 0;
1160 if (*start>=size)
1161 *start = size-1;
1162 Py_DECREF(obj);
1163 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001164}
1165
1166
1167int
1168PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1169{
1170 return PyUnicodeEncodeError_GetStart(exc, start);
1171}
1172
1173
1174int
1175PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1176{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001177 ((PyUnicodeErrorObject *)exc)->start = start;
1178 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001179}
1180
1181
1182int
1183PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1184{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001185 ((PyUnicodeErrorObject *)exc)->start = start;
1186 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001187}
1188
1189
1190int
1191PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1192{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001193 ((PyUnicodeErrorObject *)exc)->start = start;
1194 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001195}
1196
1197
1198int
1199PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1200{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001201 Py_ssize_t size;
1202 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1203 "object");
1204 if (!obj)
1205 return -1;
1206 *end = ((PyUnicodeErrorObject *)exc)->end;
1207 size = PyUnicode_GET_SIZE(obj);
1208 if (*end<1)
1209 *end = 1;
1210 if (*end>size)
1211 *end = size;
1212 Py_DECREF(obj);
1213 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001214}
1215
1216
1217int
1218PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1219{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001220 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001221 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001222 if (!obj)
1223 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001224 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001225 *end = ((PyUnicodeErrorObject *)exc)->end;
1226 if (*end<1)
1227 *end = 1;
1228 if (*end>size)
1229 *end = size;
1230 Py_DECREF(obj);
1231 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001232}
1233
1234
1235int
1236PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1237{
1238 return PyUnicodeEncodeError_GetEnd(exc, start);
1239}
1240
1241
1242int
1243PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1244{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001245 ((PyUnicodeErrorObject *)exc)->end = end;
1246 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001247}
1248
1249
1250int
1251PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1252{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001253 ((PyUnicodeErrorObject *)exc)->end = end;
1254 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001255}
1256
1257
1258int
1259PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1260{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001261 ((PyUnicodeErrorObject *)exc)->end = end;
1262 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001263}
1264
1265PyObject *
1266PyUnicodeEncodeError_GetReason(PyObject *exc)
1267{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001268 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001269}
1270
1271
1272PyObject *
1273PyUnicodeDecodeError_GetReason(PyObject *exc)
1274{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001275 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001276}
1277
1278
1279PyObject *
1280PyUnicodeTranslateError_GetReason(PyObject *exc)
1281{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001282 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001283}
1284
1285
1286int
1287PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1288{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001289 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1290 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001291}
1292
1293
1294int
1295PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1296{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001297 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1298 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001299}
1300
1301
1302int
1303PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1304{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001305 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1306 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001307}
1308
1309
Thomas Wouters477c8d52006-05-27 19:21:47 +00001310static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001311UnicodeError_clear(PyUnicodeErrorObject *self)
1312{
1313 Py_CLEAR(self->encoding);
1314 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001315 Py_CLEAR(self->reason);
1316 return BaseException_clear((PyBaseExceptionObject *)self);
1317}
1318
1319static void
1320UnicodeError_dealloc(PyUnicodeErrorObject *self)
1321{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001322 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001323 UnicodeError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +00001324 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001325}
1326
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001327static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001328UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1329{
1330 Py_VISIT(self->encoding);
1331 Py_VISIT(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001332 Py_VISIT(self->reason);
1333 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1334}
1335
1336static PyMemberDef UnicodeError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001337 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1338 PyDoc_STR("exception encoding")},
1339 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1340 PyDoc_STR("exception object")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001341 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001342 PyDoc_STR("exception start")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001343 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001344 PyDoc_STR("exception end")},
1345 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1346 PyDoc_STR("exception reason")},
1347 {NULL} /* Sentinel */
1348};
1349
1350
1351/*
1352 * UnicodeEncodeError extends UnicodeError
1353 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001354
1355static int
1356UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1357{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001358 PyUnicodeErrorObject *err;
1359
Thomas Wouters477c8d52006-05-27 19:21:47 +00001360 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1361 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001362
1363 err = (PyUnicodeErrorObject *)self;
1364
1365 Py_CLEAR(err->encoding);
1366 Py_CLEAR(err->object);
1367 Py_CLEAR(err->reason);
1368
1369 if (!PyArg_ParseTuple(args, "O!O!nnO!",
1370 &PyUnicode_Type, &err->encoding,
1371 &PyUnicode_Type, &err->object,
1372 &err->start,
1373 &err->end,
1374 &PyUnicode_Type, &err->reason)) {
1375 err->encoding = err->object = err->reason = NULL;
1376 return -1;
1377 }
1378
1379 Py_INCREF(err->encoding);
1380 Py_INCREF(err->object);
1381 Py_INCREF(err->reason);
1382
1383 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001384}
1385
1386static PyObject *
1387UnicodeEncodeError_str(PyObject *self)
1388{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001389 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001390
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001391 if (uself->end==uself->start+1) {
1392 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001393 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001394 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001395 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001396 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001397 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001398 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001399 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001400 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001401 fmt,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001402 ((PyUnicodeErrorObject *)self)->encoding,
Walter Dörwald787b03b2007-06-05 13:29:29 +00001403 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001404 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001405 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001406 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001407 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001408 return PyUnicode_FromFormat(
1409 "'%U' codec can't encode characters in position %zd-%zd: %U",
1410 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001411 uself->start,
1412 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001413 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001414 );
1415}
1416
1417static PyTypeObject _PyExc_UnicodeEncodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001418 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001419 "UnicodeEncodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001420 sizeof(PyUnicodeErrorObject), 0,
1421 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1422 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1423 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001424 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1425 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001426 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001427 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001428};
1429PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1430
1431PyObject *
1432PyUnicodeEncodeError_Create(
1433 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1434 Py_ssize_t start, Py_ssize_t end, const char *reason)
1435{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001436 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "Uu#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001437 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001438}
1439
1440
1441/*
1442 * UnicodeDecodeError extends UnicodeError
1443 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001444
1445static int
1446UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1447{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001448 PyUnicodeErrorObject *ude;
1449 const char *data;
1450 Py_ssize_t size;
1451
Thomas Wouters477c8d52006-05-27 19:21:47 +00001452 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1453 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001454
1455 ude = (PyUnicodeErrorObject *)self;
1456
1457 Py_CLEAR(ude->encoding);
1458 Py_CLEAR(ude->object);
1459 Py_CLEAR(ude->reason);
1460
1461 if (!PyArg_ParseTuple(args, "O!OnnO!",
1462 &PyUnicode_Type, &ude->encoding,
1463 &ude->object,
1464 &ude->start,
1465 &ude->end,
1466 &PyUnicode_Type, &ude->reason)) {
1467 ude->encoding = ude->object = ude->reason = NULL;
1468 return -1;
1469 }
1470
Christian Heimes72b710a2008-05-26 13:28:38 +00001471 if (!PyBytes_Check(ude->object)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00001472 if (PyObject_AsReadBuffer(ude->object, (const void **)&data, &size)) {
1473 ude->encoding = ude->object = ude->reason = NULL;
1474 return -1;
1475 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001476 ude->object = PyBytes_FromStringAndSize(data, size);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001477 }
1478 else {
1479 Py_INCREF(ude->object);
1480 }
1481
1482 Py_INCREF(ude->encoding);
1483 Py_INCREF(ude->reason);
1484
1485 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001486}
1487
1488static PyObject *
1489UnicodeDecodeError_str(PyObject *self)
1490{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001491 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001492
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001493 if (uself->end==uself->start+1) {
Christian Heimes72b710a2008-05-26 13:28:38 +00001494 int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001495 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001496 "'%U' codec can't decode byte 0x%02x in position %zd: %U",
Walter Dörwaldd2034312007-05-18 16:29:38 +00001497 ((PyUnicodeErrorObject *)self)->encoding,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001498 byte,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001499 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001500 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001501 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001502 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001503 return PyUnicode_FromFormat(
1504 "'%U' codec can't decode bytes in position %zd-%zd: %U",
1505 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001506 uself->start,
1507 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001508 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001509 );
1510}
1511
1512static PyTypeObject _PyExc_UnicodeDecodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001513 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001514 "UnicodeDecodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001515 sizeof(PyUnicodeErrorObject), 0,
1516 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1517 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1518 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001519 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1520 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001521 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001522 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001523};
1524PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1525
1526PyObject *
1527PyUnicodeDecodeError_Create(
1528 const char *encoding, const char *object, Py_ssize_t length,
1529 Py_ssize_t start, Py_ssize_t end, const char *reason)
1530{
1531 assert(length < INT_MAX);
1532 assert(start < INT_MAX);
1533 assert(end < INT_MAX);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001534 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "Uy#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001535 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001536}
1537
1538
1539/*
1540 * UnicodeTranslateError extends UnicodeError
1541 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001542
1543static int
1544UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1545 PyObject *kwds)
1546{
1547 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1548 return -1;
1549
1550 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001551 Py_CLEAR(self->reason);
1552
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001553 if (!PyArg_ParseTuple(args, "O!nnO!",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001554 &PyUnicode_Type, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001555 &self->start,
1556 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001557 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001558 self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001559 return -1;
1560 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001561
Thomas Wouters477c8d52006-05-27 19:21:47 +00001562 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001563 Py_INCREF(self->reason);
1564
1565 return 0;
1566}
1567
1568
1569static PyObject *
1570UnicodeTranslateError_str(PyObject *self)
1571{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001572 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001573
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001574 if (uself->end==uself->start+1) {
1575 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001576 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001577 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001578 fmt = "can't translate character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001579 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001580 fmt = "can't translate character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001581 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001582 fmt = "can't translate character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001583 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001584 fmt,
1585 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001586 uself->start,
1587 uself->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001588 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001589 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001590 return PyUnicode_FromFormat(
1591 "can't translate characters in position %zd-%zd: %U",
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001592 uself->start,
1593 uself->end-1,
1594 uself->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001595 );
1596}
1597
1598static PyTypeObject _PyExc_UnicodeTranslateError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001599 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001600 "UnicodeTranslateError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001601 sizeof(PyUnicodeErrorObject), 0,
1602 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1603 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1604 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001605 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001606 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1607 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001608 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001609};
1610PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1611
1612PyObject *
1613PyUnicodeTranslateError_Create(
1614 const Py_UNICODE *object, Py_ssize_t length,
1615 Py_ssize_t start, Py_ssize_t end, const char *reason)
1616{
1617 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001618 object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001619}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001620
1621
1622/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001623 * AssertionError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001624 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001625SimpleExtendsException(PyExc_Exception, AssertionError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001626 "Assertion failed.");
1627
1628
1629/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001630 * ArithmeticError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001631 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001632SimpleExtendsException(PyExc_Exception, ArithmeticError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001633 "Base class for arithmetic errors.");
1634
1635
1636/*
1637 * FloatingPointError extends ArithmeticError
1638 */
1639SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
1640 "Floating point operation failed.");
1641
1642
1643/*
1644 * OverflowError extends ArithmeticError
1645 */
1646SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
1647 "Result too large to be represented.");
1648
1649
1650/*
1651 * ZeroDivisionError extends ArithmeticError
1652 */
1653SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
1654 "Second argument to a division or modulo operation was zero.");
1655
1656
1657/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001658 * SystemError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001659 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001660SimpleExtendsException(PyExc_Exception, SystemError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001661 "Internal error in the Python interpreter.\n"
1662 "\n"
1663 "Please report this to the Python maintainer, along with the traceback,\n"
1664 "the Python version, and the hardware/OS platform and version.");
1665
1666
1667/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001668 * ReferenceError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001669 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001670SimpleExtendsException(PyExc_Exception, ReferenceError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001671 "Weak ref proxy used after referent went away.");
1672
1673
1674/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001675 * MemoryError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001676 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001677SimpleExtendsException(PyExc_Exception, MemoryError, "Out of memory.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001678
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001679/*
1680 * BufferError extends Exception
1681 */
1682SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error.");
1683
Thomas Wouters477c8d52006-05-27 19:21:47 +00001684
1685/* Warning category docstrings */
1686
1687/*
1688 * Warning extends Exception
1689 */
1690SimpleExtendsException(PyExc_Exception, Warning,
1691 "Base class for warning categories.");
1692
1693
1694/*
1695 * UserWarning extends Warning
1696 */
1697SimpleExtendsException(PyExc_Warning, UserWarning,
1698 "Base class for warnings generated by user code.");
1699
1700
1701/*
1702 * DeprecationWarning extends Warning
1703 */
1704SimpleExtendsException(PyExc_Warning, DeprecationWarning,
1705 "Base class for warnings about deprecated features.");
1706
1707
1708/*
1709 * PendingDeprecationWarning extends Warning
1710 */
1711SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1712 "Base class for warnings about features which will be deprecated\n"
1713 "in the future.");
1714
1715
1716/*
1717 * SyntaxWarning extends Warning
1718 */
1719SimpleExtendsException(PyExc_Warning, SyntaxWarning,
1720 "Base class for warnings about dubious syntax.");
1721
1722
1723/*
1724 * RuntimeWarning extends Warning
1725 */
1726SimpleExtendsException(PyExc_Warning, RuntimeWarning,
1727 "Base class for warnings about dubious runtime behavior.");
1728
1729
1730/*
1731 * FutureWarning extends Warning
1732 */
1733SimpleExtendsException(PyExc_Warning, FutureWarning,
1734 "Base class for warnings about constructs that will change semantically\n"
1735 "in the future.");
1736
1737
1738/*
1739 * ImportWarning extends Warning
1740 */
1741SimpleExtendsException(PyExc_Warning, ImportWarning,
1742 "Base class for warnings about probable mistakes in module imports");
1743
1744
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001745/*
1746 * UnicodeWarning extends Warning
1747 */
1748SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1749 "Base class for warnings about Unicode related problems, mostly\n"
1750 "related to conversion problems.");
1751
Guido van Rossum98297ee2007-11-06 21:34:58 +00001752/*
1753 * BytesWarning extends Warning
1754 */
1755SimpleExtendsException(PyExc_Warning, BytesWarning,
1756 "Base class for warnings about bytes and buffer related problems, mostly\n"
1757 "related to conversion from str or comparing to str.");
1758
1759
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001760
Thomas Wouters477c8d52006-05-27 19:21:47 +00001761/* Pre-computed MemoryError instance. Best to create this as early as
1762 * possible and not wait until a MemoryError is actually raised!
1763 */
1764PyObject *PyExc_MemoryErrorInst=NULL;
1765
Thomas Wouters89d996e2007-09-08 17:39:28 +00001766/* Pre-computed RuntimeError instance for when recursion depth is reached.
1767 Meant to be used when normalizing the exception for exceeding the recursion
1768 depth will cause its own infinite recursion.
1769*/
1770PyObject *PyExc_RecursionErrorInst = NULL;
1771
Thomas Wouters477c8d52006-05-27 19:21:47 +00001772#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1773 Py_FatalError("exceptions bootstrapping error.");
1774
1775#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001776 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1777 Py_FatalError("Module dictionary insertion problem.");
1778
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001779#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1780/* crt variable checking in VisualStudio .NET 2005 */
1781#include <crtdbg.h>
1782
1783static int prevCrtReportMode;
1784static _invalid_parameter_handler prevCrtHandler;
1785
1786/* Invalid parameter handler. Sets a ValueError exception */
1787static void
1788InvalidParameterHandler(
1789 const wchar_t * expression,
1790 const wchar_t * function,
1791 const wchar_t * file,
1792 unsigned int line,
1793 uintptr_t pReserved)
1794{
1795 /* Do nothing, allow execution to continue. Usually this
1796 * means that the CRT will set errno to EINVAL
1797 */
1798}
1799#endif
1800
1801
Martin v. Löwis1a214512008-06-11 05:26:20 +00001802void
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001803_PyExc_Init(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001804{
Neal Norwitz2633c692007-02-26 22:22:47 +00001805 PyObject *bltinmod, *bdict;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001806
1807 PRE_INIT(BaseException)
1808 PRE_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001809 PRE_INIT(TypeError)
1810 PRE_INIT(StopIteration)
1811 PRE_INIT(GeneratorExit)
1812 PRE_INIT(SystemExit)
1813 PRE_INIT(KeyboardInterrupt)
1814 PRE_INIT(ImportError)
1815 PRE_INIT(EnvironmentError)
1816 PRE_INIT(IOError)
1817 PRE_INIT(OSError)
1818#ifdef MS_WINDOWS
1819 PRE_INIT(WindowsError)
1820#endif
1821#ifdef __VMS
1822 PRE_INIT(VMSError)
1823#endif
1824 PRE_INIT(EOFError)
1825 PRE_INIT(RuntimeError)
1826 PRE_INIT(NotImplementedError)
1827 PRE_INIT(NameError)
1828 PRE_INIT(UnboundLocalError)
1829 PRE_INIT(AttributeError)
1830 PRE_INIT(SyntaxError)
1831 PRE_INIT(IndentationError)
1832 PRE_INIT(TabError)
1833 PRE_INIT(LookupError)
1834 PRE_INIT(IndexError)
1835 PRE_INIT(KeyError)
1836 PRE_INIT(ValueError)
1837 PRE_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001838 PRE_INIT(UnicodeEncodeError)
1839 PRE_INIT(UnicodeDecodeError)
1840 PRE_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001841 PRE_INIT(AssertionError)
1842 PRE_INIT(ArithmeticError)
1843 PRE_INIT(FloatingPointError)
1844 PRE_INIT(OverflowError)
1845 PRE_INIT(ZeroDivisionError)
1846 PRE_INIT(SystemError)
1847 PRE_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001848 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001849 PRE_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00001850 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001851 PRE_INIT(Warning)
1852 PRE_INIT(UserWarning)
1853 PRE_INIT(DeprecationWarning)
1854 PRE_INIT(PendingDeprecationWarning)
1855 PRE_INIT(SyntaxWarning)
1856 PRE_INIT(RuntimeWarning)
1857 PRE_INIT(FutureWarning)
1858 PRE_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001859 PRE_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001860 PRE_INIT(BytesWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001861
Georg Brandl1a3284e2007-12-02 09:40:06 +00001862 bltinmod = PyImport_ImportModule("builtins");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001863 if (bltinmod == NULL)
1864 Py_FatalError("exceptions bootstrapping error.");
1865 bdict = PyModule_GetDict(bltinmod);
1866 if (bdict == NULL)
1867 Py_FatalError("exceptions bootstrapping error.");
1868
1869 POST_INIT(BaseException)
1870 POST_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001871 POST_INIT(TypeError)
1872 POST_INIT(StopIteration)
1873 POST_INIT(GeneratorExit)
1874 POST_INIT(SystemExit)
1875 POST_INIT(KeyboardInterrupt)
1876 POST_INIT(ImportError)
1877 POST_INIT(EnvironmentError)
1878 POST_INIT(IOError)
1879 POST_INIT(OSError)
1880#ifdef MS_WINDOWS
1881 POST_INIT(WindowsError)
1882#endif
1883#ifdef __VMS
1884 POST_INIT(VMSError)
1885#endif
1886 POST_INIT(EOFError)
1887 POST_INIT(RuntimeError)
1888 POST_INIT(NotImplementedError)
1889 POST_INIT(NameError)
1890 POST_INIT(UnboundLocalError)
1891 POST_INIT(AttributeError)
1892 POST_INIT(SyntaxError)
1893 POST_INIT(IndentationError)
1894 POST_INIT(TabError)
1895 POST_INIT(LookupError)
1896 POST_INIT(IndexError)
1897 POST_INIT(KeyError)
1898 POST_INIT(ValueError)
1899 POST_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001900 POST_INIT(UnicodeEncodeError)
1901 POST_INIT(UnicodeDecodeError)
1902 POST_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001903 POST_INIT(AssertionError)
1904 POST_INIT(ArithmeticError)
1905 POST_INIT(FloatingPointError)
1906 POST_INIT(OverflowError)
1907 POST_INIT(ZeroDivisionError)
1908 POST_INIT(SystemError)
1909 POST_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001910 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001911 POST_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00001912 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001913 POST_INIT(Warning)
1914 POST_INIT(UserWarning)
1915 POST_INIT(DeprecationWarning)
1916 POST_INIT(PendingDeprecationWarning)
1917 POST_INIT(SyntaxWarning)
1918 POST_INIT(RuntimeWarning)
1919 POST_INIT(FutureWarning)
1920 POST_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001921 POST_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001922 POST_INIT(BytesWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001923
1924 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
1925 if (!PyExc_MemoryErrorInst)
1926 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1927
Thomas Wouters89d996e2007-09-08 17:39:28 +00001928 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
1929 if (!PyExc_RecursionErrorInst)
1930 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
1931 "recursion errors");
1932 else {
1933 PyBaseExceptionObject *err_inst =
1934 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
1935 PyObject *args_tuple;
1936 PyObject *exc_message;
Neal Norwitzbed67842007-10-27 04:00:45 +00001937 exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
Thomas Wouters89d996e2007-09-08 17:39:28 +00001938 if (!exc_message)
1939 Py_FatalError("cannot allocate argument for RuntimeError "
1940 "pre-allocation");
1941 args_tuple = PyTuple_Pack(1, exc_message);
1942 if (!args_tuple)
1943 Py_FatalError("cannot allocate tuple for RuntimeError "
1944 "pre-allocation");
1945 Py_DECREF(exc_message);
1946 if (BaseException_init(err_inst, args_tuple, NULL))
1947 Py_FatalError("init of pre-allocated RuntimeError failed");
1948 Py_DECREF(args_tuple);
1949 }
1950
Thomas Wouters477c8d52006-05-27 19:21:47 +00001951 Py_DECREF(bltinmod);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001952
1953#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1954 /* Set CRT argument error handler */
1955 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
1956 /* turn off assertions in debug mode */
1957 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
1958#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001959}
1960
1961void
1962_PyExc_Fini(void)
1963{
1964 Py_XDECREF(PyExc_MemoryErrorInst);
1965 PyExc_MemoryErrorInst = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001966#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
1967 /* reset CRT error handling */
1968 _set_invalid_parameter_handler(prevCrtHandler);
1969 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
1970#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001971}