blob: 9258ace50a10158d083eec7000a774ec42113a28 [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
Georg Brandlab6f2f62009-03-31 04:16:10 +0000253static PyObject *
254BaseException_get_context(PyObject *self) {
255 PyObject *res = PyException_GetContext(self);
256 if (res) return res; /* new reference already returned above */
257 Py_RETURN_NONE;
258}
259
260static int
261BaseException_set_context(PyObject *self, PyObject *arg) {
262 if (arg == NULL) {
263 PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted");
264 return -1;
265 } else if (arg == Py_None) {
266 arg = NULL;
267 } else if (!PyExceptionInstance_Check(arg)) {
268 PyErr_SetString(PyExc_TypeError, "exception context must be None "
269 "or derive from BaseException");
270 return -1;
271 } else {
272 /* PyException_SetContext steals this reference */
273 Py_INCREF(arg);
274 }
275 PyException_SetContext(self, arg);
276 return 0;
277}
278
279static PyObject *
280BaseException_get_cause(PyObject *self) {
281 PyObject *res = PyException_GetCause(self);
282 if (res) return res; /* new reference already returned above */
283 Py_RETURN_NONE;
284}
285
286static int
287BaseException_set_cause(PyObject *self, PyObject *arg) {
288 if (arg == NULL) {
289 PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted");
290 return -1;
291 } else if (arg == Py_None) {
292 arg = NULL;
293 } else if (!PyExceptionInstance_Check(arg)) {
294 PyErr_SetString(PyExc_TypeError, "exception cause must be None "
295 "or derive from BaseException");
296 return -1;
297 } else {
298 /* PyException_SetCause steals this reference */
299 Py_INCREF(arg);
300 }
301 PyException_SetCause(self, arg);
302 return 0;
303}
304
Guido van Rossum360e4b82007-05-14 22:51:27 +0000305
Thomas Wouters477c8d52006-05-27 19:21:47 +0000306static PyGetSetDef BaseException_getset[] = {
307 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
308 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Collin Winter828f04a2007-08-31 00:04:24 +0000309 {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb},
Georg Brandlab6f2f62009-03-31 04:16:10 +0000310 {"__context__", (getter)BaseException_get_context,
311 (setter)BaseException_set_context, PyDoc_STR("exception context")},
312 {"__cause__", (getter)BaseException_get_cause,
313 (setter)BaseException_set_cause, PyDoc_STR("exception cause")},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000314 {NULL},
315};
316
317
Collin Winter828f04a2007-08-31 00:04:24 +0000318PyObject *
319PyException_GetTraceback(PyObject *self) {
320 PyBaseExceptionObject *base_self = (PyBaseExceptionObject *)self;
321 Py_XINCREF(base_self->traceback);
322 return base_self->traceback;
323}
324
325
326int
327PyException_SetTraceback(PyObject *self, PyObject *tb) {
328 return BaseException_set_tb((PyBaseExceptionObject *)self, tb);
329}
330
331PyObject *
332PyException_GetCause(PyObject *self) {
333 PyObject *cause = ((PyBaseExceptionObject *)self)->cause;
334 Py_XINCREF(cause);
335 return cause;
336}
337
338/* Steals a reference to cause */
339void
340PyException_SetCause(PyObject *self, PyObject *cause) {
341 PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause;
342 ((PyBaseExceptionObject *)self)->cause = cause;
343 Py_XDECREF(old_cause);
344}
345
346PyObject *
347PyException_GetContext(PyObject *self) {
348 PyObject *context = ((PyBaseExceptionObject *)self)->context;
349 Py_XINCREF(context);
350 return context;
351}
352
353/* Steals a reference to context */
354void
355PyException_SetContext(PyObject *self, PyObject *context) {
356 PyObject *old_context = ((PyBaseExceptionObject *)self)->context;
357 ((PyBaseExceptionObject *)self)->context = context;
358 Py_XDECREF(old_context);
359}
360
361
Thomas Wouters477c8d52006-05-27 19:21:47 +0000362static PyTypeObject _PyExc_BaseException = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000363 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +0000364 "BaseException", /*tp_name*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000365 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
366 0, /*tp_itemsize*/
367 (destructor)BaseException_dealloc, /*tp_dealloc*/
368 0, /*tp_print*/
369 0, /*tp_getattr*/
370 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000371 0, /* tp_reserved; */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000372 (reprfunc)BaseException_repr, /*tp_repr*/
373 0, /*tp_as_number*/
Brett Cannonba7bf492007-02-27 00:15:55 +0000374 0, /*tp_as_sequence*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000375 0, /*tp_as_mapping*/
376 0, /*tp_hash */
377 0, /*tp_call*/
378 (reprfunc)BaseException_str, /*tp_str*/
379 PyObject_GenericGetAttr, /*tp_getattro*/
380 PyObject_GenericSetAttr, /*tp_setattro*/
381 0, /*tp_as_buffer*/
Thomas Wouters27d517b2007-02-25 20:39:11 +0000382 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
383 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000384 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
385 (traverseproc)BaseException_traverse, /* tp_traverse */
386 (inquiry)BaseException_clear, /* tp_clear */
387 0, /* tp_richcompare */
388 0, /* tp_weaklistoffset */
389 0, /* tp_iter */
390 0, /* tp_iternext */
391 BaseException_methods, /* tp_methods */
Georg Brandlab6f2f62009-03-31 04:16:10 +0000392 0, /* tp_members */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000393 BaseException_getset, /* tp_getset */
394 0, /* tp_base */
395 0, /* tp_dict */
396 0, /* tp_descr_get */
397 0, /* tp_descr_set */
398 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
399 (initproc)BaseException_init, /* tp_init */
400 0, /* tp_alloc */
401 BaseException_new, /* tp_new */
402};
403/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
404from the previous implmentation and also allowing Python objects to be used
405in the API */
406PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
407
408/* note these macros omit the last semicolon so the macro invocation may
409 * include it and not look strange.
410 */
411#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
412static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000413 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000414 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000415 sizeof(PyBaseExceptionObject), \
416 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
417 0, 0, 0, 0, 0, 0, 0, \
418 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
419 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
420 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
421 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
422 (initproc)BaseException_init, 0, BaseException_new,\
423}; \
424PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
425
426#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
427static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000428 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000429 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000430 sizeof(Py ## EXCSTORE ## Object), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000431 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000432 0, 0, 0, 0, 0, \
433 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000434 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
435 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000436 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000437 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000438}; \
439PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
440
441#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
442static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000443 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000444 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000445 sizeof(Py ## EXCSTORE ## Object), 0, \
446 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
447 (reprfunc)EXCSTR, 0, 0, 0, \
448 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
449 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
450 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
451 EXCMEMBERS, 0, &_ ## EXCBASE, \
452 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000453 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000454}; \
455PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
456
457
458/*
459 * Exception extends BaseException
460 */
461SimpleExtendsException(PyExc_BaseException, Exception,
462 "Common base class for all non-exit exceptions.");
463
464
465/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000466 * TypeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000467 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000468SimpleExtendsException(PyExc_Exception, TypeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000469 "Inappropriate argument type.");
470
471
472/*
473 * StopIteration extends Exception
474 */
475SimpleExtendsException(PyExc_Exception, StopIteration,
Georg Brandla18af4e2007-04-21 15:47:16 +0000476 "Signal the end from iterator.__next__().");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000477
478
479/*
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000480 * GeneratorExit extends BaseException
Thomas Wouters477c8d52006-05-27 19:21:47 +0000481 */
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000482SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000483 "Request that a generator exit.");
484
485
486/*
487 * SystemExit extends BaseException
488 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000489
490static int
491SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
492{
493 Py_ssize_t size = PyTuple_GET_SIZE(args);
494
495 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
496 return -1;
497
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000498 if (size == 0)
499 return 0;
500 Py_CLEAR(self->code);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000501 if (size == 1)
502 self->code = PyTuple_GET_ITEM(args, 0);
503 else if (size > 1)
504 self->code = args;
505 Py_INCREF(self->code);
506 return 0;
507}
508
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000509static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000510SystemExit_clear(PySystemExitObject *self)
511{
512 Py_CLEAR(self->code);
513 return BaseException_clear((PyBaseExceptionObject *)self);
514}
515
516static void
517SystemExit_dealloc(PySystemExitObject *self)
518{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000519 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000520 SystemExit_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000521 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000522}
523
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000524static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000525SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
526{
527 Py_VISIT(self->code);
528 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
529}
530
531static PyMemberDef SystemExit_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000532 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
533 PyDoc_STR("exception code")},
534 {NULL} /* Sentinel */
535};
536
537ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
538 SystemExit_dealloc, 0, SystemExit_members, 0,
539 "Request to exit from the interpreter.");
540
541/*
542 * KeyboardInterrupt extends BaseException
543 */
544SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
545 "Program interrupted by user.");
546
547
548/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000549 * ImportError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000550 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000551SimpleExtendsException(PyExc_Exception, ImportError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000552 "Import can't find module, or can't find name in module.");
553
554
555/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000556 * EnvironmentError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000557 */
558
Thomas Wouters477c8d52006-05-27 19:21:47 +0000559/* Where a function has a single filename, such as open() or some
560 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
561 * called, giving a third argument which is the filename. But, so
562 * that old code using in-place unpacking doesn't break, e.g.:
563 *
564 * except IOError, (errno, strerror):
565 *
566 * we hack args so that it only contains two items. This also
567 * means we need our own __str__() which prints out the filename
568 * when it was supplied.
569 */
570static int
571EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
572 PyObject *kwds)
573{
574 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
575 PyObject *subslice = NULL;
576
577 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
578 return -1;
579
Thomas Wouters89f507f2006-12-13 04:49:30 +0000580 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000581 return 0;
582 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000583
584 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000585 &myerrno, &strerror, &filename)) {
586 return -1;
587 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000588 Py_CLEAR(self->myerrno); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000589 self->myerrno = myerrno;
590 Py_INCREF(self->myerrno);
591
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000592 Py_CLEAR(self->strerror); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000593 self->strerror = strerror;
594 Py_INCREF(self->strerror);
595
596 /* self->filename will remain Py_None otherwise */
597 if (filename != NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000598 Py_CLEAR(self->filename); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000599 self->filename = filename;
600 Py_INCREF(self->filename);
601
602 subslice = PyTuple_GetSlice(args, 0, 2);
603 if (!subslice)
604 return -1;
605
606 Py_DECREF(self->args); /* replacing args */
607 self->args = subslice;
608 }
609 return 0;
610}
611
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000612static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000613EnvironmentError_clear(PyEnvironmentErrorObject *self)
614{
615 Py_CLEAR(self->myerrno);
616 Py_CLEAR(self->strerror);
617 Py_CLEAR(self->filename);
618 return BaseException_clear((PyBaseExceptionObject *)self);
619}
620
621static void
622EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
623{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000624 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000625 EnvironmentError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000626 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000627}
628
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000629static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000630EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
631 void *arg)
632{
633 Py_VISIT(self->myerrno);
634 Py_VISIT(self->strerror);
635 Py_VISIT(self->filename);
636 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
637}
638
639static PyObject *
640EnvironmentError_str(PyEnvironmentErrorObject *self)
641{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000642 if (self->filename)
643 return PyUnicode_FromFormat("[Errno %S] %S: %R",
644 self->myerrno ? self->myerrno: Py_None,
645 self->strerror ? self->strerror: Py_None,
646 self->filename);
647 else if (self->myerrno && self->strerror)
648 return PyUnicode_FromFormat("[Errno %S] %S",
649 self->myerrno ? self->myerrno: Py_None,
650 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000651 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000652 return BaseException_str((PyBaseExceptionObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653}
654
655static PyMemberDef EnvironmentError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000656 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
657 PyDoc_STR("exception errno")},
658 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
659 PyDoc_STR("exception strerror")},
660 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
661 PyDoc_STR("exception filename")},
662 {NULL} /* Sentinel */
663};
664
665
666static PyObject *
667EnvironmentError_reduce(PyEnvironmentErrorObject *self)
668{
669 PyObject *args = self->args;
670 PyObject *res = NULL, *tmp;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000671
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672 /* self->args is only the first two real arguments if there was a
673 * file name given to EnvironmentError. */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000674 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000675 args = PyTuple_New(3);
676 if (!args) return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000677
678 tmp = PyTuple_GET_ITEM(self->args, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000679 Py_INCREF(tmp);
680 PyTuple_SET_ITEM(args, 0, tmp);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000681
682 tmp = PyTuple_GET_ITEM(self->args, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000683 Py_INCREF(tmp);
684 PyTuple_SET_ITEM(args, 1, tmp);
685
686 Py_INCREF(self->filename);
687 PyTuple_SET_ITEM(args, 2, self->filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000688 } else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000689 Py_INCREF(args);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000690
691 if (self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000692 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000693 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000694 res = PyTuple_Pack(2, Py_TYPE(self), args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000695 Py_DECREF(args);
696 return res;
697}
698
699
700static PyMethodDef EnvironmentError_methods[] = {
701 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
702 {NULL}
703};
704
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000705ComplexExtendsException(PyExc_Exception, EnvironmentError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000706 EnvironmentError, EnvironmentError_dealloc,
707 EnvironmentError_methods, EnvironmentError_members,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000708 EnvironmentError_str,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000709 "Base class for I/O related errors.");
710
711
712/*
713 * IOError extends EnvironmentError
714 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000715MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000716 EnvironmentError, "I/O operation failed.");
717
718
719/*
720 * OSError extends EnvironmentError
721 */
722MiddlingExtendsException(PyExc_EnvironmentError, OSError,
723 EnvironmentError, "OS system call failed.");
724
725
726/*
727 * WindowsError extends OSError
728 */
729#ifdef MS_WINDOWS
730#include "errmap.h"
731
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000732static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000733WindowsError_clear(PyWindowsErrorObject *self)
734{
735 Py_CLEAR(self->myerrno);
736 Py_CLEAR(self->strerror);
737 Py_CLEAR(self->filename);
738 Py_CLEAR(self->winerror);
739 return BaseException_clear((PyBaseExceptionObject *)self);
740}
741
742static void
743WindowsError_dealloc(PyWindowsErrorObject *self)
744{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000745 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000746 WindowsError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000747 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000748}
749
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000750static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000751WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
752{
753 Py_VISIT(self->myerrno);
754 Py_VISIT(self->strerror);
755 Py_VISIT(self->filename);
756 Py_VISIT(self->winerror);
757 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
758}
759
Thomas Wouters477c8d52006-05-27 19:21:47 +0000760static int
761WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
762{
763 PyObject *o_errcode = NULL;
764 long errcode;
765 long posix_errno;
766
767 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
768 == -1)
769 return -1;
770
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000771 if (self->myerrno == NULL)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000772 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000773
774 /* Set errno to the POSIX errno, and winerror to the Win32
775 error code. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000776 errcode = PyLong_AsLong(self->myerrno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777 if (errcode == -1 && PyErr_Occurred())
778 return -1;
779 posix_errno = winerror_to_errno(errcode);
780
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000781 Py_CLEAR(self->winerror);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000782 self->winerror = self->myerrno;
783
Christian Heimes217cfd12007-12-02 14:31:20 +0000784 o_errcode = PyLong_FromLong(posix_errno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000785 if (!o_errcode)
786 return -1;
787
788 self->myerrno = o_errcode;
789
790 return 0;
791}
792
793
794static PyObject *
795WindowsError_str(PyWindowsErrorObject *self)
796{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000797 if (self->filename)
798 return PyUnicode_FromFormat("[Error %S] %S: %R",
799 self->winerror ? self->winerror: Py_None,
800 self->strerror ? self->strerror: Py_None,
801 self->filename);
802 else if (self->winerror && self->strerror)
803 return PyUnicode_FromFormat("[Error %S] %S",
804 self->winerror ? self->winerror: Py_None,
805 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000806 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000807 return EnvironmentError_str((PyEnvironmentErrorObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000808}
809
810static PyMemberDef WindowsError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000811 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
812 PyDoc_STR("POSIX exception code")},
813 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
814 PyDoc_STR("exception strerror")},
815 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
816 PyDoc_STR("exception filename")},
817 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
818 PyDoc_STR("Win32 exception code")},
819 {NULL} /* Sentinel */
820};
821
822ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
823 WindowsError_dealloc, 0, WindowsError_members,
824 WindowsError_str, "MS-Windows OS system call failed.");
825
826#endif /* MS_WINDOWS */
827
828
829/*
830 * VMSError extends OSError (I think)
831 */
832#ifdef __VMS
833MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
834 "OpenVMS OS system call failed.");
835#endif
836
837
838/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000839 * EOFError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000840 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000841SimpleExtendsException(PyExc_Exception, EOFError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000842 "Read beyond end of file.");
843
844
845/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000846 * RuntimeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000847 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000848SimpleExtendsException(PyExc_Exception, RuntimeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000849 "Unspecified run-time error.");
850
851
852/*
853 * NotImplementedError extends RuntimeError
854 */
855SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
856 "Method or function hasn't been implemented yet.");
857
858/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000859 * NameError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000860 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000861SimpleExtendsException(PyExc_Exception, NameError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000862 "Name not found globally.");
863
864/*
865 * UnboundLocalError extends NameError
866 */
867SimpleExtendsException(PyExc_NameError, UnboundLocalError,
868 "Local name referenced but not bound to a value.");
869
870/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000871 * AttributeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000872 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000873SimpleExtendsException(PyExc_Exception, AttributeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000874 "Attribute not found.");
875
876
877/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000878 * SyntaxError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000879 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000880
881static int
882SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
883{
884 PyObject *info = NULL;
885 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
886
887 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
888 return -1;
889
890 if (lenargs >= 1) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000891 Py_CLEAR(self->msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892 self->msg = PyTuple_GET_ITEM(args, 0);
893 Py_INCREF(self->msg);
894 }
895 if (lenargs == 2) {
896 info = PyTuple_GET_ITEM(args, 1);
897 info = PySequence_Tuple(info);
898 if (!info) return -1;
899
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000900 if (PyTuple_GET_SIZE(info) != 4) {
901 /* not a very good error message, but it's what Python 2.4 gives */
902 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
903 Py_DECREF(info);
904 return -1;
905 }
906
907 Py_CLEAR(self->filename);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000908 self->filename = PyTuple_GET_ITEM(info, 0);
909 Py_INCREF(self->filename);
910
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000911 Py_CLEAR(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000912 self->lineno = PyTuple_GET_ITEM(info, 1);
913 Py_INCREF(self->lineno);
914
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000915 Py_CLEAR(self->offset);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000916 self->offset = PyTuple_GET_ITEM(info, 2);
917 Py_INCREF(self->offset);
918
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000919 Py_CLEAR(self->text);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920 self->text = PyTuple_GET_ITEM(info, 3);
921 Py_INCREF(self->text);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000922
923 Py_DECREF(info);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000924 }
925 return 0;
926}
927
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000928static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000929SyntaxError_clear(PySyntaxErrorObject *self)
930{
931 Py_CLEAR(self->msg);
932 Py_CLEAR(self->filename);
933 Py_CLEAR(self->lineno);
934 Py_CLEAR(self->offset);
935 Py_CLEAR(self->text);
936 Py_CLEAR(self->print_file_and_line);
937 return BaseException_clear((PyBaseExceptionObject *)self);
938}
939
940static void
941SyntaxError_dealloc(PySyntaxErrorObject *self)
942{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000943 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000944 SyntaxError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000945 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000946}
947
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000948static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000949SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
950{
951 Py_VISIT(self->msg);
952 Py_VISIT(self->filename);
953 Py_VISIT(self->lineno);
954 Py_VISIT(self->offset);
955 Py_VISIT(self->text);
956 Py_VISIT(self->print_file_and_line);
957 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
958}
959
960/* This is called "my_basename" instead of just "basename" to avoid name
961 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
962 defined, and Python does define that. */
963static char *
964my_basename(char *name)
965{
966 char *cp = name;
967 char *result = name;
968
969 if (name == NULL)
970 return "???";
971 while (*cp != '\0') {
972 if (*cp == SEP)
973 result = cp + 1;
974 ++cp;
975 }
976 return result;
977}
978
979
980static PyObject *
981SyntaxError_str(PySyntaxErrorObject *self)
982{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000983 int have_lineno = 0;
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000984 char *filename = 0;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000985 /* Below, we always ignore overflow errors, just printing -1.
986 Still, we cannot allow an OverflowError to be raised, so
987 we need to call PyLong_AsLongAndOverflow. */
988 int overflow;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000989
990 /* XXX -- do all the additional formatting with filename and
991 lineno here */
992
Neal Norwitzed2b7392007-08-26 04:51:10 +0000993 if (self->filename && PyUnicode_Check(self->filename)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000994 filename = _PyUnicode_AsString(self->filename);
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000995 }
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000996 have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000997
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000998 if (!filename && !have_lineno)
Thomas Heller519a0422007-11-15 20:48:54 +0000999 return PyObject_Str(self->msg ? self->msg : Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001000
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001001 if (filename && have_lineno)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001002 return PyUnicode_FromFormat("%S (%s, line %ld)",
1003 self->msg ? self->msg : Py_None,
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001004 my_basename(filename),
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001005 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001006 else if (filename)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001007 return PyUnicode_FromFormat("%S (%s)",
1008 self->msg ? self->msg : Py_None,
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001009 my_basename(filename));
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001010 else /* only have_lineno */
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001011 return PyUnicode_FromFormat("%S (line %ld)",
1012 self->msg ? self->msg : Py_None,
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001013 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001014}
1015
1016static PyMemberDef SyntaxError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001017 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1018 PyDoc_STR("exception msg")},
1019 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1020 PyDoc_STR("exception filename")},
1021 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1022 PyDoc_STR("exception lineno")},
1023 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1024 PyDoc_STR("exception offset")},
1025 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1026 PyDoc_STR("exception text")},
1027 {"print_file_and_line", T_OBJECT,
1028 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1029 PyDoc_STR("exception print_file_and_line")},
1030 {NULL} /* Sentinel */
1031};
1032
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001033ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001034 SyntaxError_dealloc, 0, SyntaxError_members,
1035 SyntaxError_str, "Invalid syntax.");
1036
1037
1038/*
1039 * IndentationError extends SyntaxError
1040 */
1041MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
1042 "Improper indentation.");
1043
1044
1045/*
1046 * TabError extends IndentationError
1047 */
1048MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
1049 "Improper mixture of spaces and tabs.");
1050
1051
1052/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001053 * LookupError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001054 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001055SimpleExtendsException(PyExc_Exception, LookupError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001056 "Base class for lookup errors.");
1057
1058
1059/*
1060 * IndexError extends LookupError
1061 */
1062SimpleExtendsException(PyExc_LookupError, IndexError,
1063 "Sequence index out of range.");
1064
1065
1066/*
1067 * KeyError extends LookupError
1068 */
1069static PyObject *
1070KeyError_str(PyBaseExceptionObject *self)
1071{
1072 /* If args is a tuple of exactly one item, apply repr to args[0].
1073 This is done so that e.g. the exception raised by {}[''] prints
1074 KeyError: ''
1075 rather than the confusing
1076 KeyError
1077 alone. The downside is that if KeyError is raised with an explanatory
1078 string, that string will be displayed in quotes. Too bad.
1079 If args is anything else, use the default BaseException__str__().
1080 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001081 if (PyTuple_GET_SIZE(self->args) == 1) {
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001082 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001083 }
1084 return BaseException_str(self);
1085}
1086
1087ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
1088 0, 0, 0, KeyError_str, "Mapping key not found.");
1089
1090
1091/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001092 * ValueError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001093 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001094SimpleExtendsException(PyExc_Exception, ValueError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001095 "Inappropriate argument value (of correct type).");
1096
1097/*
1098 * UnicodeError extends ValueError
1099 */
1100
1101SimpleExtendsException(PyExc_ValueError, UnicodeError,
1102 "Unicode related error.");
1103
Thomas Wouters477c8d52006-05-27 19:21:47 +00001104static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001105get_string(PyObject *attr, const char *name)
Walter Dörwald612344f2007-05-04 19:28:21 +00001106{
1107 if (!attr) {
1108 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1109 return NULL;
1110 }
1111
Christian Heimes72b710a2008-05-26 13:28:38 +00001112 if (!PyBytes_Check(attr)) {
Walter Dörwald612344f2007-05-04 19:28:21 +00001113 PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
1114 return NULL;
1115 }
1116 Py_INCREF(attr);
1117 return attr;
1118}
1119
1120static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001121get_unicode(PyObject *attr, const char *name)
1122{
1123 if (!attr) {
1124 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1125 return NULL;
1126 }
1127
1128 if (!PyUnicode_Check(attr)) {
1129 PyErr_Format(PyExc_TypeError,
1130 "%.200s attribute must be unicode", name);
1131 return NULL;
1132 }
1133 Py_INCREF(attr);
1134 return attr;
1135}
1136
Walter Dörwaldd2034312007-05-18 16:29:38 +00001137static int
1138set_unicodefromstring(PyObject **attr, const char *value)
1139{
1140 PyObject *obj = PyUnicode_FromString(value);
1141 if (!obj)
1142 return -1;
1143 Py_CLEAR(*attr);
1144 *attr = obj;
1145 return 0;
1146}
1147
Thomas Wouters477c8d52006-05-27 19:21:47 +00001148PyObject *
1149PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1150{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001151 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001152}
1153
1154PyObject *
1155PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1156{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001157 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001158}
1159
1160PyObject *
1161PyUnicodeEncodeError_GetObject(PyObject *exc)
1162{
1163 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1164}
1165
1166PyObject *
1167PyUnicodeDecodeError_GetObject(PyObject *exc)
1168{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001169 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001170}
1171
1172PyObject *
1173PyUnicodeTranslateError_GetObject(PyObject *exc)
1174{
1175 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1176}
1177
1178int
1179PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1180{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001181 Py_ssize_t size;
1182 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1183 "object");
1184 if (!obj)
1185 return -1;
1186 *start = ((PyUnicodeErrorObject *)exc)->start;
1187 size = PyUnicode_GET_SIZE(obj);
1188 if (*start<0)
1189 *start = 0; /*XXX check for values <0*/
1190 if (*start>=size)
1191 *start = size-1;
1192 Py_DECREF(obj);
1193 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001194}
1195
1196
1197int
1198PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1199{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001200 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001201 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001202 if (!obj)
1203 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001204 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001205 *start = ((PyUnicodeErrorObject *)exc)->start;
1206 if (*start<0)
1207 *start = 0;
1208 if (*start>=size)
1209 *start = size-1;
1210 Py_DECREF(obj);
1211 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001212}
1213
1214
1215int
1216PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1217{
1218 return PyUnicodeEncodeError_GetStart(exc, start);
1219}
1220
1221
1222int
1223PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1224{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001225 ((PyUnicodeErrorObject *)exc)->start = start;
1226 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001227}
1228
1229
1230int
1231PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1232{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001233 ((PyUnicodeErrorObject *)exc)->start = start;
1234 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001235}
1236
1237
1238int
1239PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1240{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001241 ((PyUnicodeErrorObject *)exc)->start = start;
1242 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001243}
1244
1245
1246int
1247PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1248{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001249 Py_ssize_t size;
1250 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1251 "object");
1252 if (!obj)
1253 return -1;
1254 *end = ((PyUnicodeErrorObject *)exc)->end;
1255 size = PyUnicode_GET_SIZE(obj);
1256 if (*end<1)
1257 *end = 1;
1258 if (*end>size)
1259 *end = size;
1260 Py_DECREF(obj);
1261 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001262}
1263
1264
1265int
1266PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1267{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001268 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001269 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001270 if (!obj)
1271 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001272 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001273 *end = ((PyUnicodeErrorObject *)exc)->end;
1274 if (*end<1)
1275 *end = 1;
1276 if (*end>size)
1277 *end = size;
1278 Py_DECREF(obj);
1279 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001280}
1281
1282
1283int
1284PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1285{
1286 return PyUnicodeEncodeError_GetEnd(exc, start);
1287}
1288
1289
1290int
1291PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1292{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001293 ((PyUnicodeErrorObject *)exc)->end = end;
1294 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001295}
1296
1297
1298int
1299PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1300{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001301 ((PyUnicodeErrorObject *)exc)->end = end;
1302 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001303}
1304
1305
1306int
1307PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1308{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001309 ((PyUnicodeErrorObject *)exc)->end = end;
1310 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001311}
1312
1313PyObject *
1314PyUnicodeEncodeError_GetReason(PyObject *exc)
1315{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001316 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001317}
1318
1319
1320PyObject *
1321PyUnicodeDecodeError_GetReason(PyObject *exc)
1322{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001323 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001324}
1325
1326
1327PyObject *
1328PyUnicodeTranslateError_GetReason(PyObject *exc)
1329{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001330 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001331}
1332
1333
1334int
1335PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1336{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001337 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1338 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001339}
1340
1341
1342int
1343PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1344{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001345 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1346 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001347}
1348
1349
1350int
1351PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1352{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001353 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1354 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001355}
1356
1357
Thomas Wouters477c8d52006-05-27 19:21:47 +00001358static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001359UnicodeError_clear(PyUnicodeErrorObject *self)
1360{
1361 Py_CLEAR(self->encoding);
1362 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001363 Py_CLEAR(self->reason);
1364 return BaseException_clear((PyBaseExceptionObject *)self);
1365}
1366
1367static void
1368UnicodeError_dealloc(PyUnicodeErrorObject *self)
1369{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001370 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001371 UnicodeError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +00001372 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001373}
1374
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001375static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001376UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1377{
1378 Py_VISIT(self->encoding);
1379 Py_VISIT(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001380 Py_VISIT(self->reason);
1381 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1382}
1383
1384static PyMemberDef UnicodeError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001385 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1386 PyDoc_STR("exception encoding")},
1387 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1388 PyDoc_STR("exception object")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001389 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001390 PyDoc_STR("exception start")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001391 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001392 PyDoc_STR("exception end")},
1393 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1394 PyDoc_STR("exception reason")},
1395 {NULL} /* Sentinel */
1396};
1397
1398
1399/*
1400 * UnicodeEncodeError extends UnicodeError
1401 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001402
1403static int
1404UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1405{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001406 PyUnicodeErrorObject *err;
1407
Thomas Wouters477c8d52006-05-27 19:21:47 +00001408 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1409 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001410
1411 err = (PyUnicodeErrorObject *)self;
1412
1413 Py_CLEAR(err->encoding);
1414 Py_CLEAR(err->object);
1415 Py_CLEAR(err->reason);
1416
1417 if (!PyArg_ParseTuple(args, "O!O!nnO!",
1418 &PyUnicode_Type, &err->encoding,
1419 &PyUnicode_Type, &err->object,
1420 &err->start,
1421 &err->end,
1422 &PyUnicode_Type, &err->reason)) {
1423 err->encoding = err->object = err->reason = NULL;
1424 return -1;
1425 }
1426
1427 Py_INCREF(err->encoding);
1428 Py_INCREF(err->object);
1429 Py_INCREF(err->reason);
1430
1431 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001432}
1433
1434static PyObject *
1435UnicodeEncodeError_str(PyObject *self)
1436{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001437 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001438
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001439 if (uself->end==uself->start+1) {
1440 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001441 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001442 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001443 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001444 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001445 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001446 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001447 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001448 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001449 fmt,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001450 ((PyUnicodeErrorObject *)self)->encoding,
Walter Dörwald787b03b2007-06-05 13:29:29 +00001451 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001452 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001453 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001454 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001455 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001456 return PyUnicode_FromFormat(
1457 "'%U' codec can't encode characters in position %zd-%zd: %U",
1458 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001459 uself->start,
1460 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001461 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001462 );
1463}
1464
1465static PyTypeObject _PyExc_UnicodeEncodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001466 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001467 "UnicodeEncodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001468 sizeof(PyUnicodeErrorObject), 0,
1469 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1470 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1471 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001472 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1473 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001474 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001475 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001476};
1477PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1478
1479PyObject *
1480PyUnicodeEncodeError_Create(
1481 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1482 Py_ssize_t start, Py_ssize_t end, const char *reason)
1483{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001484 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "Uu#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001485 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001486}
1487
1488
1489/*
1490 * UnicodeDecodeError extends UnicodeError
1491 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001492
1493static int
1494UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1495{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001496 PyUnicodeErrorObject *ude;
1497 const char *data;
1498 Py_ssize_t size;
1499
Thomas Wouters477c8d52006-05-27 19:21:47 +00001500 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1501 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001502
1503 ude = (PyUnicodeErrorObject *)self;
1504
1505 Py_CLEAR(ude->encoding);
1506 Py_CLEAR(ude->object);
1507 Py_CLEAR(ude->reason);
1508
1509 if (!PyArg_ParseTuple(args, "O!OnnO!",
1510 &PyUnicode_Type, &ude->encoding,
1511 &ude->object,
1512 &ude->start,
1513 &ude->end,
1514 &PyUnicode_Type, &ude->reason)) {
1515 ude->encoding = ude->object = ude->reason = NULL;
1516 return -1;
1517 }
1518
Christian Heimes72b710a2008-05-26 13:28:38 +00001519 if (!PyBytes_Check(ude->object)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00001520 if (PyObject_AsReadBuffer(ude->object, (const void **)&data, &size)) {
1521 ude->encoding = ude->object = ude->reason = NULL;
1522 return -1;
1523 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001524 ude->object = PyBytes_FromStringAndSize(data, size);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001525 }
1526 else {
1527 Py_INCREF(ude->object);
1528 }
1529
1530 Py_INCREF(ude->encoding);
1531 Py_INCREF(ude->reason);
1532
1533 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001534}
1535
1536static PyObject *
1537UnicodeDecodeError_str(PyObject *self)
1538{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001539 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001540
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001541 if (uself->end==uself->start+1) {
Christian Heimes72b710a2008-05-26 13:28:38 +00001542 int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001543 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001544 "'%U' codec can't decode byte 0x%02x in position %zd: %U",
Walter Dörwaldd2034312007-05-18 16:29:38 +00001545 ((PyUnicodeErrorObject *)self)->encoding,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001546 byte,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001547 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001548 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001549 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001550 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001551 return PyUnicode_FromFormat(
1552 "'%U' codec can't decode bytes in position %zd-%zd: %U",
1553 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001554 uself->start,
1555 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001556 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001557 );
1558}
1559
1560static PyTypeObject _PyExc_UnicodeDecodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001561 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001562 "UnicodeDecodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001563 sizeof(PyUnicodeErrorObject), 0,
1564 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1565 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1566 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001567 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1568 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001569 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001570 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001571};
1572PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1573
1574PyObject *
1575PyUnicodeDecodeError_Create(
1576 const char *encoding, const char *object, Py_ssize_t length,
1577 Py_ssize_t start, Py_ssize_t end, const char *reason)
1578{
1579 assert(length < INT_MAX);
1580 assert(start < INT_MAX);
1581 assert(end < INT_MAX);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001582 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "Uy#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001583 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001584}
1585
1586
1587/*
1588 * UnicodeTranslateError extends UnicodeError
1589 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001590
1591static int
1592UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1593 PyObject *kwds)
1594{
1595 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1596 return -1;
1597
1598 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001599 Py_CLEAR(self->reason);
1600
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001601 if (!PyArg_ParseTuple(args, "O!nnO!",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001602 &PyUnicode_Type, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001603 &self->start,
1604 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001605 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001606 self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001607 return -1;
1608 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001609
Thomas Wouters477c8d52006-05-27 19:21:47 +00001610 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001611 Py_INCREF(self->reason);
1612
1613 return 0;
1614}
1615
1616
1617static PyObject *
1618UnicodeTranslateError_str(PyObject *self)
1619{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001620 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001621
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001622 if (uself->end==uself->start+1) {
1623 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001624 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001625 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001626 fmt = "can't translate character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001627 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001628 fmt = "can't translate character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001629 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001630 fmt = "can't translate character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001631 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001632 fmt,
1633 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001634 uself->start,
1635 uself->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001636 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001637 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001638 return PyUnicode_FromFormat(
1639 "can't translate characters in position %zd-%zd: %U",
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001640 uself->start,
1641 uself->end-1,
1642 uself->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001643 );
1644}
1645
1646static PyTypeObject _PyExc_UnicodeTranslateError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001647 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001648 "UnicodeTranslateError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001649 sizeof(PyUnicodeErrorObject), 0,
1650 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1651 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1652 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001653 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001654 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1655 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001656 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001657};
1658PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1659
1660PyObject *
1661PyUnicodeTranslateError_Create(
1662 const Py_UNICODE *object, Py_ssize_t length,
1663 Py_ssize_t start, Py_ssize_t end, const char *reason)
1664{
1665 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001666 object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001667}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001668
1669
1670/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001671 * AssertionError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001672 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001673SimpleExtendsException(PyExc_Exception, AssertionError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001674 "Assertion failed.");
1675
1676
1677/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001678 * ArithmeticError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001679 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001680SimpleExtendsException(PyExc_Exception, ArithmeticError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001681 "Base class for arithmetic errors.");
1682
1683
1684/*
1685 * FloatingPointError extends ArithmeticError
1686 */
1687SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
1688 "Floating point operation failed.");
1689
1690
1691/*
1692 * OverflowError extends ArithmeticError
1693 */
1694SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
1695 "Result too large to be represented.");
1696
1697
1698/*
1699 * ZeroDivisionError extends ArithmeticError
1700 */
1701SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
1702 "Second argument to a division or modulo operation was zero.");
1703
1704
1705/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001706 * SystemError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001707 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001708SimpleExtendsException(PyExc_Exception, SystemError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001709 "Internal error in the Python interpreter.\n"
1710 "\n"
1711 "Please report this to the Python maintainer, along with the traceback,\n"
1712 "the Python version, and the hardware/OS platform and version.");
1713
1714
1715/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001716 * ReferenceError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001717 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001718SimpleExtendsException(PyExc_Exception, ReferenceError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001719 "Weak ref proxy used after referent went away.");
1720
1721
1722/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001723 * MemoryError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001724 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001725SimpleExtendsException(PyExc_Exception, MemoryError, "Out of memory.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001726
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001727/*
1728 * BufferError extends Exception
1729 */
1730SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error.");
1731
Thomas Wouters477c8d52006-05-27 19:21:47 +00001732
1733/* Warning category docstrings */
1734
1735/*
1736 * Warning extends Exception
1737 */
1738SimpleExtendsException(PyExc_Exception, Warning,
1739 "Base class for warning categories.");
1740
1741
1742/*
1743 * UserWarning extends Warning
1744 */
1745SimpleExtendsException(PyExc_Warning, UserWarning,
1746 "Base class for warnings generated by user code.");
1747
1748
1749/*
1750 * DeprecationWarning extends Warning
1751 */
1752SimpleExtendsException(PyExc_Warning, DeprecationWarning,
1753 "Base class for warnings about deprecated features.");
1754
1755
1756/*
1757 * PendingDeprecationWarning extends Warning
1758 */
1759SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1760 "Base class for warnings about features which will be deprecated\n"
1761 "in the future.");
1762
1763
1764/*
1765 * SyntaxWarning extends Warning
1766 */
1767SimpleExtendsException(PyExc_Warning, SyntaxWarning,
1768 "Base class for warnings about dubious syntax.");
1769
1770
1771/*
1772 * RuntimeWarning extends Warning
1773 */
1774SimpleExtendsException(PyExc_Warning, RuntimeWarning,
1775 "Base class for warnings about dubious runtime behavior.");
1776
1777
1778/*
1779 * FutureWarning extends Warning
1780 */
1781SimpleExtendsException(PyExc_Warning, FutureWarning,
1782 "Base class for warnings about constructs that will change semantically\n"
1783 "in the future.");
1784
1785
1786/*
1787 * ImportWarning extends Warning
1788 */
1789SimpleExtendsException(PyExc_Warning, ImportWarning,
1790 "Base class for warnings about probable mistakes in module imports");
1791
1792
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001793/*
1794 * UnicodeWarning extends Warning
1795 */
1796SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1797 "Base class for warnings about Unicode related problems, mostly\n"
1798 "related to conversion problems.");
1799
Guido van Rossum98297ee2007-11-06 21:34:58 +00001800/*
1801 * BytesWarning extends Warning
1802 */
1803SimpleExtendsException(PyExc_Warning, BytesWarning,
1804 "Base class for warnings about bytes and buffer related problems, mostly\n"
1805 "related to conversion from str or comparing to str.");
1806
1807
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001808
Thomas Wouters477c8d52006-05-27 19:21:47 +00001809/* Pre-computed MemoryError instance. Best to create this as early as
1810 * possible and not wait until a MemoryError is actually raised!
1811 */
1812PyObject *PyExc_MemoryErrorInst=NULL;
1813
Thomas Wouters89d996e2007-09-08 17:39:28 +00001814/* Pre-computed RuntimeError instance for when recursion depth is reached.
1815 Meant to be used when normalizing the exception for exceeding the recursion
1816 depth will cause its own infinite recursion.
1817*/
1818PyObject *PyExc_RecursionErrorInst = NULL;
1819
Thomas Wouters477c8d52006-05-27 19:21:47 +00001820#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1821 Py_FatalError("exceptions bootstrapping error.");
1822
1823#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001824 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1825 Py_FatalError("Module dictionary insertion problem.");
1826
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001827
Martin v. Löwis1a214512008-06-11 05:26:20 +00001828void
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001829_PyExc_Init(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001830{
Neal Norwitz2633c692007-02-26 22:22:47 +00001831 PyObject *bltinmod, *bdict;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001832
1833 PRE_INIT(BaseException)
1834 PRE_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001835 PRE_INIT(TypeError)
1836 PRE_INIT(StopIteration)
1837 PRE_INIT(GeneratorExit)
1838 PRE_INIT(SystemExit)
1839 PRE_INIT(KeyboardInterrupt)
1840 PRE_INIT(ImportError)
1841 PRE_INIT(EnvironmentError)
1842 PRE_INIT(IOError)
1843 PRE_INIT(OSError)
1844#ifdef MS_WINDOWS
1845 PRE_INIT(WindowsError)
1846#endif
1847#ifdef __VMS
1848 PRE_INIT(VMSError)
1849#endif
1850 PRE_INIT(EOFError)
1851 PRE_INIT(RuntimeError)
1852 PRE_INIT(NotImplementedError)
1853 PRE_INIT(NameError)
1854 PRE_INIT(UnboundLocalError)
1855 PRE_INIT(AttributeError)
1856 PRE_INIT(SyntaxError)
1857 PRE_INIT(IndentationError)
1858 PRE_INIT(TabError)
1859 PRE_INIT(LookupError)
1860 PRE_INIT(IndexError)
1861 PRE_INIT(KeyError)
1862 PRE_INIT(ValueError)
1863 PRE_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001864 PRE_INIT(UnicodeEncodeError)
1865 PRE_INIT(UnicodeDecodeError)
1866 PRE_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001867 PRE_INIT(AssertionError)
1868 PRE_INIT(ArithmeticError)
1869 PRE_INIT(FloatingPointError)
1870 PRE_INIT(OverflowError)
1871 PRE_INIT(ZeroDivisionError)
1872 PRE_INIT(SystemError)
1873 PRE_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001874 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001875 PRE_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00001876 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001877 PRE_INIT(Warning)
1878 PRE_INIT(UserWarning)
1879 PRE_INIT(DeprecationWarning)
1880 PRE_INIT(PendingDeprecationWarning)
1881 PRE_INIT(SyntaxWarning)
1882 PRE_INIT(RuntimeWarning)
1883 PRE_INIT(FutureWarning)
1884 PRE_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001885 PRE_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001886 PRE_INIT(BytesWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001887
Georg Brandl1a3284e2007-12-02 09:40:06 +00001888 bltinmod = PyImport_ImportModule("builtins");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001889 if (bltinmod == NULL)
1890 Py_FatalError("exceptions bootstrapping error.");
1891 bdict = PyModule_GetDict(bltinmod);
1892 if (bdict == NULL)
1893 Py_FatalError("exceptions bootstrapping error.");
1894
1895 POST_INIT(BaseException)
1896 POST_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001897 POST_INIT(TypeError)
1898 POST_INIT(StopIteration)
1899 POST_INIT(GeneratorExit)
1900 POST_INIT(SystemExit)
1901 POST_INIT(KeyboardInterrupt)
1902 POST_INIT(ImportError)
1903 POST_INIT(EnvironmentError)
1904 POST_INIT(IOError)
1905 POST_INIT(OSError)
1906#ifdef MS_WINDOWS
1907 POST_INIT(WindowsError)
1908#endif
1909#ifdef __VMS
1910 POST_INIT(VMSError)
1911#endif
1912 POST_INIT(EOFError)
1913 POST_INIT(RuntimeError)
1914 POST_INIT(NotImplementedError)
1915 POST_INIT(NameError)
1916 POST_INIT(UnboundLocalError)
1917 POST_INIT(AttributeError)
1918 POST_INIT(SyntaxError)
1919 POST_INIT(IndentationError)
1920 POST_INIT(TabError)
1921 POST_INIT(LookupError)
1922 POST_INIT(IndexError)
1923 POST_INIT(KeyError)
1924 POST_INIT(ValueError)
1925 POST_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001926 POST_INIT(UnicodeEncodeError)
1927 POST_INIT(UnicodeDecodeError)
1928 POST_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001929 POST_INIT(AssertionError)
1930 POST_INIT(ArithmeticError)
1931 POST_INIT(FloatingPointError)
1932 POST_INIT(OverflowError)
1933 POST_INIT(ZeroDivisionError)
1934 POST_INIT(SystemError)
1935 POST_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001936 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001937 POST_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00001938 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001939 POST_INIT(Warning)
1940 POST_INIT(UserWarning)
1941 POST_INIT(DeprecationWarning)
1942 POST_INIT(PendingDeprecationWarning)
1943 POST_INIT(SyntaxWarning)
1944 POST_INIT(RuntimeWarning)
1945 POST_INIT(FutureWarning)
1946 POST_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001947 POST_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001948 POST_INIT(BytesWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001949
1950 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
1951 if (!PyExc_MemoryErrorInst)
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001952 Py_FatalError("Cannot pre-allocate MemoryError instance");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001953
Thomas Wouters89d996e2007-09-08 17:39:28 +00001954 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
1955 if (!PyExc_RecursionErrorInst)
1956 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
1957 "recursion errors");
1958 else {
1959 PyBaseExceptionObject *err_inst =
1960 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
1961 PyObject *args_tuple;
1962 PyObject *exc_message;
Neal Norwitzbed67842007-10-27 04:00:45 +00001963 exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
Thomas Wouters89d996e2007-09-08 17:39:28 +00001964 if (!exc_message)
1965 Py_FatalError("cannot allocate argument for RuntimeError "
1966 "pre-allocation");
1967 args_tuple = PyTuple_Pack(1, exc_message);
1968 if (!args_tuple)
1969 Py_FatalError("cannot allocate tuple for RuntimeError "
1970 "pre-allocation");
1971 Py_DECREF(exc_message);
1972 if (BaseException_init(err_inst, args_tuple, NULL))
1973 Py_FatalError("init of pre-allocated RuntimeError failed");
1974 Py_DECREF(args_tuple);
1975 }
1976
Thomas Wouters477c8d52006-05-27 19:21:47 +00001977 Py_DECREF(bltinmod);
1978}
1979
1980void
1981_PyExc_Fini(void)
1982{
1983 Py_XDECREF(PyExc_MemoryErrorInst);
1984 PyExc_MemoryErrorInst = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001985}