blob: 0e28f0fb70c95867a30cf8d67a7e2ed2e534e010 [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
Thomas Wouters477c8d52006-05-27 19:21:47 +000012
13/* NOTE: If the exception class hierarchy changes, don't forget to update
14 * Lib/test/exception_hierarchy.txt
15 */
16
Thomas Wouters477c8d52006-05-27 19:21:47 +000017/*
18 * BaseException
19 */
20static PyObject *
21BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
22{
23 PyBaseExceptionObject *self;
24
25 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +000026 if (!self)
27 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000028 /* the dict is created on the fly in PyObject_GenericSetAttr */
Guido van Rossumebe3e162007-05-17 18:20:34 +000029 self->dict = NULL;
Collin Winter1966f1c2007-09-01 20:26:44 +000030 self->traceback = self->cause = self->context = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000031
32 self->args = PyTuple_New(0);
33 if (!self->args) {
34 Py_DECREF(self);
35 return NULL;
36 }
37
Thomas Wouters477c8d52006-05-27 19:21:47 +000038 return (PyObject *)self;
39}
40
41static int
42BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
43{
Christian Heimes90aa7642007-12-19 02:45:37 +000044 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000045 return -1;
46
Thomas Wouters477c8d52006-05-27 19:21:47 +000047 Py_DECREF(self->args);
48 self->args = args;
49 Py_INCREF(self->args);
50
Thomas Wouters477c8d52006-05-27 19:21:47 +000051 return 0;
52}
53
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000054static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000055BaseException_clear(PyBaseExceptionObject *self)
56{
57 Py_CLEAR(self->dict);
58 Py_CLEAR(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000059 Py_CLEAR(self->traceback);
Collin Winter1966f1c2007-09-01 20:26:44 +000060 Py_CLEAR(self->cause);
61 Py_CLEAR(self->context);
Thomas Wouters477c8d52006-05-27 19:21:47 +000062 return 0;
63}
64
65static void
66BaseException_dealloc(PyBaseExceptionObject *self)
67{
Thomas Wouters89f507f2006-12-13 04:49:30 +000068 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000069 BaseException_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +000070 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000071}
72
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000073static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000074BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
75{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000076 Py_VISIT(self->dict);
Thomas Wouters477c8d52006-05-27 19:21:47 +000077 Py_VISIT(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000078 Py_VISIT(self->traceback);
Collin Winter1966f1c2007-09-01 20:26:44 +000079 Py_VISIT(self->cause);
80 Py_VISIT(self->context);
Thomas Wouters477c8d52006-05-27 19:21:47 +000081 return 0;
82}
83
84static PyObject *
85BaseException_str(PyBaseExceptionObject *self)
86{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000087 switch (PyTuple_GET_SIZE(self->args)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000088 case 0:
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +000089 return PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +000090 case 1:
Thomas Heller519a0422007-11-15 20:48:54 +000091 return PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +000092 default:
Thomas Heller519a0422007-11-15 20:48:54 +000093 return PyObject_Str(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +000094 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000095}
96
97static PyObject *
98BaseException_repr(PyBaseExceptionObject *self)
99{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000100 char *name;
101 char *dot;
102
Christian Heimes90aa7642007-12-19 02:45:37 +0000103 name = (char *)Py_TYPE(self)->tp_name;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000104 dot = strrchr(name, '.');
105 if (dot != NULL) name = dot+1;
106
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000107 return PyUnicode_FromFormat("%s%R", name, self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108}
109
110/* Pickling support */
111static PyObject *
112BaseException_reduce(PyBaseExceptionObject *self)
113{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000114 if (self->args && self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000115 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000116 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000117 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000118}
119
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000120/*
121 * Needed for backward compatibility, since exceptions used to store
122 * all their attributes in the __dict__. Code is taken from cPickle's
123 * load_build function.
124 */
125static PyObject *
126BaseException_setstate(PyObject *self, PyObject *state)
127{
128 PyObject *d_key, *d_value;
129 Py_ssize_t i = 0;
130
131 if (state != Py_None) {
132 if (!PyDict_Check(state)) {
133 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
134 return NULL;
135 }
136 while (PyDict_Next(state, &i, &d_key, &d_value)) {
137 if (PyObject_SetAttr(self, d_key, d_value) < 0)
138 return NULL;
139 }
140 }
141 Py_RETURN_NONE;
142}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000143
Collin Winter828f04a2007-08-31 00:04:24 +0000144static PyObject *
145BaseException_with_traceback(PyObject *self, PyObject *tb) {
146 if (PyException_SetTraceback(self, tb))
147 return NULL;
148
149 Py_INCREF(self);
150 return self;
151}
152
Georg Brandl76941002008-05-05 21:38:47 +0000153PyDoc_STRVAR(with_traceback_doc,
154"Exception.with_traceback(tb) --\n\
155 set self.__traceback__ to tb and return self.");
156
Thomas Wouters477c8d52006-05-27 19:21:47 +0000157
158static PyMethodDef BaseException_methods[] = {
159 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000160 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Georg Brandl76941002008-05-05 21:38:47 +0000161 {"with_traceback", (PyCFunction)BaseException_with_traceback, METH_O,
162 with_traceback_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000163 {NULL, NULL, 0, NULL},
164};
165
166
Thomas Wouters477c8d52006-05-27 19:21:47 +0000167static PyObject *
168BaseException_get_dict(PyBaseExceptionObject *self)
169{
170 if (self->dict == NULL) {
171 self->dict = PyDict_New();
172 if (!self->dict)
173 return NULL;
174 }
175 Py_INCREF(self->dict);
176 return self->dict;
177}
178
179static int
180BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
181{
182 if (val == NULL) {
183 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
184 return -1;
185 }
186 if (!PyDict_Check(val)) {
187 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
188 return -1;
189 }
190 Py_CLEAR(self->dict);
191 Py_INCREF(val);
192 self->dict = val;
193 return 0;
194}
195
196static PyObject *
197BaseException_get_args(PyBaseExceptionObject *self)
198{
199 if (self->args == NULL) {
200 Py_INCREF(Py_None);
201 return Py_None;
202 }
203 Py_INCREF(self->args);
204 return self->args;
205}
206
207static int
208BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
209{
210 PyObject *seq;
211 if (val == NULL) {
212 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
213 return -1;
214 }
215 seq = PySequence_Tuple(val);
216 if (!seq) return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000217 Py_CLEAR(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000218 self->args = seq;
219 return 0;
220}
221
Collin Winter828f04a2007-08-31 00:04:24 +0000222static PyObject *
223BaseException_get_tb(PyBaseExceptionObject *self)
224{
225 if (self->traceback == NULL) {
226 Py_INCREF(Py_None);
227 return Py_None;
228 }
229 Py_INCREF(self->traceback);
230 return self->traceback;
231}
232
233static int
234BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb)
235{
236 if (tb == NULL) {
237 PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted");
238 return -1;
239 }
240 else if (!(tb == Py_None || PyTraceBack_Check(tb))) {
241 PyErr_SetString(PyExc_TypeError,
242 "__traceback__ must be a traceback or None");
243 return -1;
244 }
245
246 Py_XINCREF(tb);
247 Py_XDECREF(self->traceback);
248 self->traceback = tb;
249 return 0;
250}
251
Georg Brandlab6f2f62009-03-31 04:16:10 +0000252static PyObject *
253BaseException_get_context(PyObject *self) {
254 PyObject *res = PyException_GetContext(self);
255 if (res) return res; /* new reference already returned above */
256 Py_RETURN_NONE;
257}
258
259static int
260BaseException_set_context(PyObject *self, PyObject *arg) {
261 if (arg == NULL) {
262 PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted");
263 return -1;
264 } else if (arg == Py_None) {
265 arg = NULL;
266 } else if (!PyExceptionInstance_Check(arg)) {
267 PyErr_SetString(PyExc_TypeError, "exception context must be None "
268 "or derive from BaseException");
269 return -1;
270 } else {
271 /* PyException_SetContext steals this reference */
272 Py_INCREF(arg);
273 }
274 PyException_SetContext(self, arg);
275 return 0;
276}
277
278static PyObject *
279BaseException_get_cause(PyObject *self) {
280 PyObject *res = PyException_GetCause(self);
281 if (res) return res; /* new reference already returned above */
282 Py_RETURN_NONE;
283}
284
285static int
286BaseException_set_cause(PyObject *self, PyObject *arg) {
287 if (arg == NULL) {
288 PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted");
289 return -1;
290 } else if (arg == Py_None) {
291 arg = NULL;
292 } else if (!PyExceptionInstance_Check(arg)) {
293 PyErr_SetString(PyExc_TypeError, "exception cause must be None "
294 "or derive from BaseException");
295 return -1;
296 } else {
297 /* PyException_SetCause steals this reference */
298 Py_INCREF(arg);
299 }
300 PyException_SetCause(self, arg);
301 return 0;
302}
303
Guido van Rossum360e4b82007-05-14 22:51:27 +0000304
Thomas Wouters477c8d52006-05-27 19:21:47 +0000305static PyGetSetDef BaseException_getset[] = {
306 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
307 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Collin Winter828f04a2007-08-31 00:04:24 +0000308 {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb},
Georg Brandlab6f2f62009-03-31 04:16:10 +0000309 {"__context__", (getter)BaseException_get_context,
310 (setter)BaseException_set_context, PyDoc_STR("exception context")},
311 {"__cause__", (getter)BaseException_get_cause,
312 (setter)BaseException_set_cause, PyDoc_STR("exception cause")},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000313 {NULL},
314};
315
316
Collin Winter828f04a2007-08-31 00:04:24 +0000317PyObject *
318PyException_GetTraceback(PyObject *self) {
319 PyBaseExceptionObject *base_self = (PyBaseExceptionObject *)self;
320 Py_XINCREF(base_self->traceback);
321 return base_self->traceback;
322}
323
324
325int
326PyException_SetTraceback(PyObject *self, PyObject *tb) {
327 return BaseException_set_tb((PyBaseExceptionObject *)self, tb);
328}
329
330PyObject *
331PyException_GetCause(PyObject *self) {
332 PyObject *cause = ((PyBaseExceptionObject *)self)->cause;
333 Py_XINCREF(cause);
334 return cause;
335}
336
337/* Steals a reference to cause */
338void
339PyException_SetCause(PyObject *self, PyObject *cause) {
340 PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause;
341 ((PyBaseExceptionObject *)self)->cause = cause;
342 Py_XDECREF(old_cause);
343}
344
345PyObject *
346PyException_GetContext(PyObject *self) {
347 PyObject *context = ((PyBaseExceptionObject *)self)->context;
348 Py_XINCREF(context);
349 return context;
350}
351
352/* Steals a reference to context */
353void
354PyException_SetContext(PyObject *self, PyObject *context) {
355 PyObject *old_context = ((PyBaseExceptionObject *)self)->context;
356 ((PyBaseExceptionObject *)self)->context = context;
357 Py_XDECREF(old_context);
358}
359
360
Thomas Wouters477c8d52006-05-27 19:21:47 +0000361static PyTypeObject _PyExc_BaseException = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000362 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +0000363 "BaseException", /*tp_name*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000364 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
365 0, /*tp_itemsize*/
366 (destructor)BaseException_dealloc, /*tp_dealloc*/
367 0, /*tp_print*/
368 0, /*tp_getattr*/
369 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000370 0, /* tp_reserved; */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000371 (reprfunc)BaseException_repr, /*tp_repr*/
372 0, /*tp_as_number*/
Brett Cannonba7bf492007-02-27 00:15:55 +0000373 0, /*tp_as_sequence*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000374 0, /*tp_as_mapping*/
375 0, /*tp_hash */
376 0, /*tp_call*/
377 (reprfunc)BaseException_str, /*tp_str*/
378 PyObject_GenericGetAttr, /*tp_getattro*/
379 PyObject_GenericSetAttr, /*tp_setattro*/
380 0, /*tp_as_buffer*/
Thomas Wouters27d517b2007-02-25 20:39:11 +0000381 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
382 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000383 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
384 (traverseproc)BaseException_traverse, /* tp_traverse */
385 (inquiry)BaseException_clear, /* tp_clear */
386 0, /* tp_richcompare */
387 0, /* tp_weaklistoffset */
388 0, /* tp_iter */
389 0, /* tp_iternext */
390 BaseException_methods, /* tp_methods */
Georg Brandlab6f2f62009-03-31 04:16:10 +0000391 0, /* tp_members */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000392 BaseException_getset, /* tp_getset */
393 0, /* tp_base */
394 0, /* tp_dict */
395 0, /* tp_descr_get */
396 0, /* tp_descr_set */
397 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
398 (initproc)BaseException_init, /* tp_init */
399 0, /* tp_alloc */
400 BaseException_new, /* tp_new */
401};
402/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
403from the previous implmentation and also allowing Python objects to be used
404in the API */
405PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
406
407/* note these macros omit the last semicolon so the macro invocation may
408 * include it and not look strange.
409 */
410#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
411static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000412 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000413 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000414 sizeof(PyBaseExceptionObject), \
415 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
416 0, 0, 0, 0, 0, 0, 0, \
417 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
418 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
419 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
420 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
421 (initproc)BaseException_init, 0, BaseException_new,\
422}; \
423PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
424
425#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
426static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000427 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000428 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000429 sizeof(Py ## EXCSTORE ## Object), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000430 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000431 0, 0, 0, 0, 0, \
432 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000433 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
434 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000435 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000436 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000437}; \
438PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
439
440#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
441static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000442 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000443 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000444 sizeof(Py ## EXCSTORE ## Object), 0, \
445 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
446 (reprfunc)EXCSTR, 0, 0, 0, \
447 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
448 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
449 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
450 EXCMEMBERS, 0, &_ ## EXCBASE, \
451 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000452 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000453}; \
454PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
455
456
457/*
458 * Exception extends BaseException
459 */
460SimpleExtendsException(PyExc_BaseException, Exception,
461 "Common base class for all non-exit exceptions.");
462
463
464/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000465 * TypeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000466 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000467SimpleExtendsException(PyExc_Exception, TypeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000468 "Inappropriate argument type.");
469
470
471/*
472 * StopIteration extends Exception
473 */
474SimpleExtendsException(PyExc_Exception, StopIteration,
Georg Brandla18af4e2007-04-21 15:47:16 +0000475 "Signal the end from iterator.__next__().");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000476
477
478/*
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000479 * GeneratorExit extends BaseException
Thomas Wouters477c8d52006-05-27 19:21:47 +0000480 */
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000481SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000482 "Request that a generator exit.");
483
484
485/*
486 * SystemExit extends BaseException
487 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000488
489static int
490SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
491{
492 Py_ssize_t size = PyTuple_GET_SIZE(args);
493
494 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
495 return -1;
496
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000497 if (size == 0)
498 return 0;
499 Py_CLEAR(self->code);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000500 if (size == 1)
501 self->code = PyTuple_GET_ITEM(args, 0);
502 else if (size > 1)
503 self->code = args;
504 Py_INCREF(self->code);
505 return 0;
506}
507
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000508static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000509SystemExit_clear(PySystemExitObject *self)
510{
511 Py_CLEAR(self->code);
512 return BaseException_clear((PyBaseExceptionObject *)self);
513}
514
515static void
516SystemExit_dealloc(PySystemExitObject *self)
517{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000518 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000519 SystemExit_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000520 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000521}
522
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000523static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000524SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
525{
526 Py_VISIT(self->code);
527 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
528}
529
530static PyMemberDef SystemExit_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000531 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
532 PyDoc_STR("exception code")},
533 {NULL} /* Sentinel */
534};
535
536ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
537 SystemExit_dealloc, 0, SystemExit_members, 0,
538 "Request to exit from the interpreter.");
539
540/*
541 * KeyboardInterrupt extends BaseException
542 */
543SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
544 "Program interrupted by user.");
545
546
547/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000548 * ImportError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000549 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000550SimpleExtendsException(PyExc_Exception, ImportError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000551 "Import can't find module, or can't find name in module.");
552
553
554/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000555 * EnvironmentError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000556 */
557
Thomas Wouters477c8d52006-05-27 19:21:47 +0000558/* Where a function has a single filename, such as open() or some
559 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
560 * called, giving a third argument which is the filename. But, so
561 * that old code using in-place unpacking doesn't break, e.g.:
562 *
563 * except IOError, (errno, strerror):
564 *
565 * we hack args so that it only contains two items. This also
566 * means we need our own __str__() which prints out the filename
567 * when it was supplied.
568 */
569static int
570EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
571 PyObject *kwds)
572{
573 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
574 PyObject *subslice = NULL;
575
576 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
577 return -1;
578
Thomas Wouters89f507f2006-12-13 04:49:30 +0000579 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000580 return 0;
581 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000582
583 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000584 &myerrno, &strerror, &filename)) {
585 return -1;
586 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000587 Py_CLEAR(self->myerrno); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000588 self->myerrno = myerrno;
589 Py_INCREF(self->myerrno);
590
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000591 Py_CLEAR(self->strerror); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000592 self->strerror = strerror;
593 Py_INCREF(self->strerror);
594
595 /* self->filename will remain Py_None otherwise */
596 if (filename != NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000597 Py_CLEAR(self->filename); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000598 self->filename = filename;
599 Py_INCREF(self->filename);
600
601 subslice = PyTuple_GetSlice(args, 0, 2);
602 if (!subslice)
603 return -1;
604
605 Py_DECREF(self->args); /* replacing args */
606 self->args = subslice;
607 }
608 return 0;
609}
610
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000611static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000612EnvironmentError_clear(PyEnvironmentErrorObject *self)
613{
614 Py_CLEAR(self->myerrno);
615 Py_CLEAR(self->strerror);
616 Py_CLEAR(self->filename);
617 return BaseException_clear((PyBaseExceptionObject *)self);
618}
619
620static void
621EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
622{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000623 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624 EnvironmentError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000625 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000626}
627
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000628static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000629EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
630 void *arg)
631{
632 Py_VISIT(self->myerrno);
633 Py_VISIT(self->strerror);
634 Py_VISIT(self->filename);
635 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
636}
637
638static PyObject *
639EnvironmentError_str(PyEnvironmentErrorObject *self)
640{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000641 if (self->filename)
642 return PyUnicode_FromFormat("[Errno %S] %S: %R",
643 self->myerrno ? self->myerrno: Py_None,
644 self->strerror ? self->strerror: Py_None,
645 self->filename);
646 else if (self->myerrno && self->strerror)
647 return PyUnicode_FromFormat("[Errno %S] %S",
648 self->myerrno ? self->myerrno: Py_None,
649 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000650 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000651 return BaseException_str((PyBaseExceptionObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000652}
653
654static PyMemberDef EnvironmentError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000655 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
656 PyDoc_STR("exception errno")},
657 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
658 PyDoc_STR("exception strerror")},
659 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
660 PyDoc_STR("exception filename")},
661 {NULL} /* Sentinel */
662};
663
664
665static PyObject *
666EnvironmentError_reduce(PyEnvironmentErrorObject *self)
667{
668 PyObject *args = self->args;
669 PyObject *res = NULL, *tmp;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000670
Thomas Wouters477c8d52006-05-27 19:21:47 +0000671 /* self->args is only the first two real arguments if there was a
672 * file name given to EnvironmentError. */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000673 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000674 args = PyTuple_New(3);
675 if (!args) return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000676
677 tmp = PyTuple_GET_ITEM(self->args, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000678 Py_INCREF(tmp);
679 PyTuple_SET_ITEM(args, 0, tmp);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000680
681 tmp = PyTuple_GET_ITEM(self->args, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000682 Py_INCREF(tmp);
683 PyTuple_SET_ITEM(args, 1, tmp);
684
685 Py_INCREF(self->filename);
686 PyTuple_SET_ITEM(args, 2, self->filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000687 } else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000688 Py_INCREF(args);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000689
690 if (self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000691 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000692 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000693 res = PyTuple_Pack(2, Py_TYPE(self), args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694 Py_DECREF(args);
695 return res;
696}
697
698
699static PyMethodDef EnvironmentError_methods[] = {
700 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
701 {NULL}
702};
703
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000704ComplexExtendsException(PyExc_Exception, EnvironmentError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000705 EnvironmentError, EnvironmentError_dealloc,
706 EnvironmentError_methods, EnvironmentError_members,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000707 EnvironmentError_str,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000708 "Base class for I/O related errors.");
709
710
711/*
712 * IOError extends EnvironmentError
713 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000714MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000715 EnvironmentError, "I/O operation failed.");
716
717
718/*
719 * OSError extends EnvironmentError
720 */
721MiddlingExtendsException(PyExc_EnvironmentError, OSError,
722 EnvironmentError, "OS system call failed.");
723
724
725/*
726 * WindowsError extends OSError
727 */
728#ifdef MS_WINDOWS
729#include "errmap.h"
730
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000731static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000732WindowsError_clear(PyWindowsErrorObject *self)
733{
734 Py_CLEAR(self->myerrno);
735 Py_CLEAR(self->strerror);
736 Py_CLEAR(self->filename);
737 Py_CLEAR(self->winerror);
738 return BaseException_clear((PyBaseExceptionObject *)self);
739}
740
741static void
742WindowsError_dealloc(PyWindowsErrorObject *self)
743{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000744 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000745 WindowsError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000746 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000747}
748
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000749static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000750WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
751{
752 Py_VISIT(self->myerrno);
753 Py_VISIT(self->strerror);
754 Py_VISIT(self->filename);
755 Py_VISIT(self->winerror);
756 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
757}
758
Thomas Wouters477c8d52006-05-27 19:21:47 +0000759static int
760WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
761{
762 PyObject *o_errcode = NULL;
763 long errcode;
764 long posix_errno;
765
766 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
767 == -1)
768 return -1;
769
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000770 if (self->myerrno == NULL)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000771 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000772
773 /* Set errno to the POSIX errno, and winerror to the Win32
774 error code. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000775 errcode = PyLong_AsLong(self->myerrno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000776 if (errcode == -1 && PyErr_Occurred())
777 return -1;
778 posix_errno = winerror_to_errno(errcode);
779
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000780 Py_CLEAR(self->winerror);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000781 self->winerror = self->myerrno;
782
Christian Heimes217cfd12007-12-02 14:31:20 +0000783 o_errcode = PyLong_FromLong(posix_errno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000784 if (!o_errcode)
785 return -1;
786
787 self->myerrno = o_errcode;
788
789 return 0;
790}
791
792
793static PyObject *
794WindowsError_str(PyWindowsErrorObject *self)
795{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000796 if (self->filename)
797 return PyUnicode_FromFormat("[Error %S] %S: %R",
798 self->winerror ? self->winerror: Py_None,
799 self->strerror ? self->strerror: Py_None,
800 self->filename);
801 else if (self->winerror && self->strerror)
802 return PyUnicode_FromFormat("[Error %S] %S",
803 self->winerror ? self->winerror: Py_None,
804 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000805 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000806 return EnvironmentError_str((PyEnvironmentErrorObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000807}
808
809static PyMemberDef WindowsError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000810 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
811 PyDoc_STR("POSIX exception code")},
812 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
813 PyDoc_STR("exception strerror")},
814 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
815 PyDoc_STR("exception filename")},
816 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
817 PyDoc_STR("Win32 exception code")},
818 {NULL} /* Sentinel */
819};
820
821ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
822 WindowsError_dealloc, 0, WindowsError_members,
823 WindowsError_str, "MS-Windows OS system call failed.");
824
825#endif /* MS_WINDOWS */
826
827
828/*
829 * VMSError extends OSError (I think)
830 */
831#ifdef __VMS
832MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
833 "OpenVMS OS system call failed.");
834#endif
835
836
837/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000838 * EOFError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000839 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000840SimpleExtendsException(PyExc_Exception, EOFError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841 "Read beyond end of file.");
842
843
844/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000845 * RuntimeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000846 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000847SimpleExtendsException(PyExc_Exception, RuntimeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000848 "Unspecified run-time error.");
849
850
851/*
852 * NotImplementedError extends RuntimeError
853 */
854SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
855 "Method or function hasn't been implemented yet.");
856
857/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000858 * NameError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000860SimpleExtendsException(PyExc_Exception, NameError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000861 "Name not found globally.");
862
863/*
864 * UnboundLocalError extends NameError
865 */
866SimpleExtendsException(PyExc_NameError, UnboundLocalError,
867 "Local name referenced but not bound to a value.");
868
869/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000870 * AttributeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000871 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000872SimpleExtendsException(PyExc_Exception, AttributeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000873 "Attribute not found.");
874
875
876/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000877 * SyntaxError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000878 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000879
880static int
881SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
882{
883 PyObject *info = NULL;
884 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
885
886 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
887 return -1;
888
889 if (lenargs >= 1) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000890 Py_CLEAR(self->msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000891 self->msg = PyTuple_GET_ITEM(args, 0);
892 Py_INCREF(self->msg);
893 }
894 if (lenargs == 2) {
895 info = PyTuple_GET_ITEM(args, 1);
896 info = PySequence_Tuple(info);
897 if (!info) return -1;
898
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000899 if (PyTuple_GET_SIZE(info) != 4) {
900 /* not a very good error message, but it's what Python 2.4 gives */
901 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
902 Py_DECREF(info);
903 return -1;
904 }
905
906 Py_CLEAR(self->filename);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907 self->filename = PyTuple_GET_ITEM(info, 0);
908 Py_INCREF(self->filename);
909
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000910 Py_CLEAR(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000911 self->lineno = PyTuple_GET_ITEM(info, 1);
912 Py_INCREF(self->lineno);
913
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000914 Py_CLEAR(self->offset);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000915 self->offset = PyTuple_GET_ITEM(info, 2);
916 Py_INCREF(self->offset);
917
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000918 Py_CLEAR(self->text);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000919 self->text = PyTuple_GET_ITEM(info, 3);
920 Py_INCREF(self->text);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000921
922 Py_DECREF(info);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000923 }
924 return 0;
925}
926
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000927static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928SyntaxError_clear(PySyntaxErrorObject *self)
929{
930 Py_CLEAR(self->msg);
931 Py_CLEAR(self->filename);
932 Py_CLEAR(self->lineno);
933 Py_CLEAR(self->offset);
934 Py_CLEAR(self->text);
935 Py_CLEAR(self->print_file_and_line);
936 return BaseException_clear((PyBaseExceptionObject *)self);
937}
938
939static void
940SyntaxError_dealloc(PySyntaxErrorObject *self)
941{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000942 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000943 SyntaxError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000944 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000945}
946
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000947static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000948SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
949{
950 Py_VISIT(self->msg);
951 Py_VISIT(self->filename);
952 Py_VISIT(self->lineno);
953 Py_VISIT(self->offset);
954 Py_VISIT(self->text);
955 Py_VISIT(self->print_file_and_line);
956 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
957}
958
959/* This is called "my_basename" instead of just "basename" to avoid name
960 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
961 defined, and Python does define that. */
962static char *
963my_basename(char *name)
964{
965 char *cp = name;
966 char *result = name;
967
968 if (name == NULL)
969 return "???";
970 while (*cp != '\0') {
971 if (*cp == SEP)
972 result = cp + 1;
973 ++cp;
974 }
975 return result;
976}
977
978
979static PyObject *
980SyntaxError_str(PySyntaxErrorObject *self)
981{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000982 int have_lineno = 0;
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000983 char *filename = 0;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000984 /* Below, we always ignore overflow errors, just printing -1.
985 Still, we cannot allow an OverflowError to be raised, so
986 we need to call PyLong_AsLongAndOverflow. */
987 int overflow;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000988
989 /* XXX -- do all the additional formatting with filename and
990 lineno here */
991
Neal Norwitzed2b7392007-08-26 04:51:10 +0000992 if (self->filename && PyUnicode_Check(self->filename)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000993 filename = _PyUnicode_AsString(self->filename);
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000994 }
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000995 have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000996
Martin v. Löwis10a60b32007-07-18 02:28:27 +0000997 if (!filename && !have_lineno)
Thomas Heller519a0422007-11-15 20:48:54 +0000998 return PyObject_Str(self->msg ? self->msg : Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000999
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001000 if (filename && have_lineno)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001001 return PyUnicode_FromFormat("%S (%s, line %ld)",
1002 self->msg ? self->msg : Py_None,
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001003 my_basename(filename),
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001004 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001005 else if (filename)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001006 return PyUnicode_FromFormat("%S (%s)",
1007 self->msg ? self->msg : Py_None,
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001008 my_basename(filename));
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001009 else /* only have_lineno */
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001010 return PyUnicode_FromFormat("%S (line %ld)",
1011 self->msg ? self->msg : Py_None,
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001012 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001013}
1014
1015static PyMemberDef SyntaxError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001016 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1017 PyDoc_STR("exception msg")},
1018 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1019 PyDoc_STR("exception filename")},
1020 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1021 PyDoc_STR("exception lineno")},
1022 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1023 PyDoc_STR("exception offset")},
1024 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1025 PyDoc_STR("exception text")},
1026 {"print_file_and_line", T_OBJECT,
1027 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1028 PyDoc_STR("exception print_file_and_line")},
1029 {NULL} /* Sentinel */
1030};
1031
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001032ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001033 SyntaxError_dealloc, 0, SyntaxError_members,
1034 SyntaxError_str, "Invalid syntax.");
1035
1036
1037/*
1038 * IndentationError extends SyntaxError
1039 */
1040MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
1041 "Improper indentation.");
1042
1043
1044/*
1045 * TabError extends IndentationError
1046 */
1047MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
1048 "Improper mixture of spaces and tabs.");
1049
1050
1051/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001052 * LookupError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001053 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001054SimpleExtendsException(PyExc_Exception, LookupError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001055 "Base class for lookup errors.");
1056
1057
1058/*
1059 * IndexError extends LookupError
1060 */
1061SimpleExtendsException(PyExc_LookupError, IndexError,
1062 "Sequence index out of range.");
1063
1064
1065/*
1066 * KeyError extends LookupError
1067 */
1068static PyObject *
1069KeyError_str(PyBaseExceptionObject *self)
1070{
1071 /* If args is a tuple of exactly one item, apply repr to args[0].
1072 This is done so that e.g. the exception raised by {}[''] prints
1073 KeyError: ''
1074 rather than the confusing
1075 KeyError
1076 alone. The downside is that if KeyError is raised with an explanatory
1077 string, that string will be displayed in quotes. Too bad.
1078 If args is anything else, use the default BaseException__str__().
1079 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001080 if (PyTuple_GET_SIZE(self->args) == 1) {
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001081 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001082 }
1083 return BaseException_str(self);
1084}
1085
1086ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
1087 0, 0, 0, KeyError_str, "Mapping key not found.");
1088
1089
1090/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001091 * ValueError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001092 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001093SimpleExtendsException(PyExc_Exception, ValueError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001094 "Inappropriate argument value (of correct type).");
1095
1096/*
1097 * UnicodeError extends ValueError
1098 */
1099
1100SimpleExtendsException(PyExc_ValueError, UnicodeError,
1101 "Unicode related error.");
1102
Thomas Wouters477c8d52006-05-27 19:21:47 +00001103static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001104get_string(PyObject *attr, const char *name)
Walter Dörwald612344f2007-05-04 19:28:21 +00001105{
1106 if (!attr) {
1107 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1108 return NULL;
1109 }
1110
Christian Heimes72b710a2008-05-26 13:28:38 +00001111 if (!PyBytes_Check(attr)) {
Walter Dörwald612344f2007-05-04 19:28:21 +00001112 PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
1113 return NULL;
1114 }
1115 Py_INCREF(attr);
1116 return attr;
1117}
1118
1119static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001120get_unicode(PyObject *attr, const char *name)
1121{
1122 if (!attr) {
1123 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1124 return NULL;
1125 }
1126
1127 if (!PyUnicode_Check(attr)) {
1128 PyErr_Format(PyExc_TypeError,
1129 "%.200s attribute must be unicode", name);
1130 return NULL;
1131 }
1132 Py_INCREF(attr);
1133 return attr;
1134}
1135
Walter Dörwaldd2034312007-05-18 16:29:38 +00001136static int
1137set_unicodefromstring(PyObject **attr, const char *value)
1138{
1139 PyObject *obj = PyUnicode_FromString(value);
1140 if (!obj)
1141 return -1;
1142 Py_CLEAR(*attr);
1143 *attr = obj;
1144 return 0;
1145}
1146
Thomas Wouters477c8d52006-05-27 19:21:47 +00001147PyObject *
1148PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1149{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001150 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001151}
1152
1153PyObject *
1154PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1155{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001156 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001157}
1158
1159PyObject *
1160PyUnicodeEncodeError_GetObject(PyObject *exc)
1161{
1162 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1163}
1164
1165PyObject *
1166PyUnicodeDecodeError_GetObject(PyObject *exc)
1167{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001168 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001169}
1170
1171PyObject *
1172PyUnicodeTranslateError_GetObject(PyObject *exc)
1173{
1174 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1175}
1176
1177int
1178PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1179{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001180 Py_ssize_t size;
1181 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1182 "object");
1183 if (!obj)
1184 return -1;
1185 *start = ((PyUnicodeErrorObject *)exc)->start;
1186 size = PyUnicode_GET_SIZE(obj);
1187 if (*start<0)
1188 *start = 0; /*XXX check for values <0*/
1189 if (*start>=size)
1190 *start = size-1;
1191 Py_DECREF(obj);
1192 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001193}
1194
1195
1196int
1197PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1198{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001199 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001200 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001201 if (!obj)
1202 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001203 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001204 *start = ((PyUnicodeErrorObject *)exc)->start;
1205 if (*start<0)
1206 *start = 0;
1207 if (*start>=size)
1208 *start = size-1;
1209 Py_DECREF(obj);
1210 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001211}
1212
1213
1214int
1215PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1216{
1217 return PyUnicodeEncodeError_GetStart(exc, start);
1218}
1219
1220
1221int
1222PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1223{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001224 ((PyUnicodeErrorObject *)exc)->start = start;
1225 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001226}
1227
1228
1229int
1230PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1231{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001232 ((PyUnicodeErrorObject *)exc)->start = start;
1233 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001234}
1235
1236
1237int
1238PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1239{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001240 ((PyUnicodeErrorObject *)exc)->start = start;
1241 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001242}
1243
1244
1245int
1246PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1247{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001248 Py_ssize_t size;
1249 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1250 "object");
1251 if (!obj)
1252 return -1;
1253 *end = ((PyUnicodeErrorObject *)exc)->end;
1254 size = PyUnicode_GET_SIZE(obj);
1255 if (*end<1)
1256 *end = 1;
1257 if (*end>size)
1258 *end = size;
1259 Py_DECREF(obj);
1260 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001261}
1262
1263
1264int
1265PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1266{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001267 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001268 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001269 if (!obj)
1270 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001271 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001272 *end = ((PyUnicodeErrorObject *)exc)->end;
1273 if (*end<1)
1274 *end = 1;
1275 if (*end>size)
1276 *end = size;
1277 Py_DECREF(obj);
1278 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001279}
1280
1281
1282int
1283PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1284{
1285 return PyUnicodeEncodeError_GetEnd(exc, start);
1286}
1287
1288
1289int
1290PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1291{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001292 ((PyUnicodeErrorObject *)exc)->end = end;
1293 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001294}
1295
1296
1297int
1298PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1299{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001300 ((PyUnicodeErrorObject *)exc)->end = end;
1301 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001302}
1303
1304
1305int
1306PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1307{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001308 ((PyUnicodeErrorObject *)exc)->end = end;
1309 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001310}
1311
1312PyObject *
1313PyUnicodeEncodeError_GetReason(PyObject *exc)
1314{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001315 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001316}
1317
1318
1319PyObject *
1320PyUnicodeDecodeError_GetReason(PyObject *exc)
1321{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001322 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001323}
1324
1325
1326PyObject *
1327PyUnicodeTranslateError_GetReason(PyObject *exc)
1328{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001329 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001330}
1331
1332
1333int
1334PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1335{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001336 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1337 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001338}
1339
1340
1341int
1342PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1343{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001344 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1345 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001346}
1347
1348
1349int
1350PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1351{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001352 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1353 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001354}
1355
1356
Thomas Wouters477c8d52006-05-27 19:21:47 +00001357static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001358UnicodeError_clear(PyUnicodeErrorObject *self)
1359{
1360 Py_CLEAR(self->encoding);
1361 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001362 Py_CLEAR(self->reason);
1363 return BaseException_clear((PyBaseExceptionObject *)self);
1364}
1365
1366static void
1367UnicodeError_dealloc(PyUnicodeErrorObject *self)
1368{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001369 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001370 UnicodeError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +00001371 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001372}
1373
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001374static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001375UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1376{
1377 Py_VISIT(self->encoding);
1378 Py_VISIT(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001379 Py_VISIT(self->reason);
1380 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1381}
1382
1383static PyMemberDef UnicodeError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001384 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1385 PyDoc_STR("exception encoding")},
1386 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1387 PyDoc_STR("exception object")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001388 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001389 PyDoc_STR("exception start")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001390 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001391 PyDoc_STR("exception end")},
1392 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1393 PyDoc_STR("exception reason")},
1394 {NULL} /* Sentinel */
1395};
1396
1397
1398/*
1399 * UnicodeEncodeError extends UnicodeError
1400 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001401
1402static int
1403UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1404{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001405 PyUnicodeErrorObject *err;
1406
Thomas Wouters477c8d52006-05-27 19:21:47 +00001407 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1408 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001409
1410 err = (PyUnicodeErrorObject *)self;
1411
1412 Py_CLEAR(err->encoding);
1413 Py_CLEAR(err->object);
1414 Py_CLEAR(err->reason);
1415
1416 if (!PyArg_ParseTuple(args, "O!O!nnO!",
1417 &PyUnicode_Type, &err->encoding,
1418 &PyUnicode_Type, &err->object,
1419 &err->start,
1420 &err->end,
1421 &PyUnicode_Type, &err->reason)) {
1422 err->encoding = err->object = err->reason = NULL;
1423 return -1;
1424 }
1425
1426 Py_INCREF(err->encoding);
1427 Py_INCREF(err->object);
1428 Py_INCREF(err->reason);
1429
1430 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001431}
1432
1433static PyObject *
1434UnicodeEncodeError_str(PyObject *self)
1435{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001436 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001437
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001438 if (uself->end==uself->start+1) {
1439 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001440 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001441 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001442 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001443 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001444 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001445 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001446 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001447 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001448 fmt,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001449 ((PyUnicodeErrorObject *)self)->encoding,
Walter Dörwald787b03b2007-06-05 13:29:29 +00001450 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001451 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001452 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001453 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001454 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001455 return PyUnicode_FromFormat(
1456 "'%U' codec can't encode characters in position %zd-%zd: %U",
1457 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001458 uself->start,
1459 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001460 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001461 );
1462}
1463
1464static PyTypeObject _PyExc_UnicodeEncodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001465 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001466 "UnicodeEncodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001467 sizeof(PyUnicodeErrorObject), 0,
1468 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1469 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1470 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001471 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1472 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001473 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001474 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001475};
1476PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1477
1478PyObject *
1479PyUnicodeEncodeError_Create(
1480 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1481 Py_ssize_t start, Py_ssize_t end, const char *reason)
1482{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001483 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "Uu#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001484 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001485}
1486
1487
1488/*
1489 * UnicodeDecodeError extends UnicodeError
1490 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001491
1492static int
1493UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1494{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001495 PyUnicodeErrorObject *ude;
1496 const char *data;
1497 Py_ssize_t size;
1498
Thomas Wouters477c8d52006-05-27 19:21:47 +00001499 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1500 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001501
1502 ude = (PyUnicodeErrorObject *)self;
1503
1504 Py_CLEAR(ude->encoding);
1505 Py_CLEAR(ude->object);
1506 Py_CLEAR(ude->reason);
1507
1508 if (!PyArg_ParseTuple(args, "O!OnnO!",
1509 &PyUnicode_Type, &ude->encoding,
1510 &ude->object,
1511 &ude->start,
1512 &ude->end,
1513 &PyUnicode_Type, &ude->reason)) {
1514 ude->encoding = ude->object = ude->reason = NULL;
1515 return -1;
1516 }
1517
Christian Heimes72b710a2008-05-26 13:28:38 +00001518 if (!PyBytes_Check(ude->object)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00001519 if (PyObject_AsReadBuffer(ude->object, (const void **)&data, &size)) {
1520 ude->encoding = ude->object = ude->reason = NULL;
1521 return -1;
1522 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001523 ude->object = PyBytes_FromStringAndSize(data, size);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001524 }
1525 else {
1526 Py_INCREF(ude->object);
1527 }
1528
1529 Py_INCREF(ude->encoding);
1530 Py_INCREF(ude->reason);
1531
1532 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001533}
1534
1535static PyObject *
1536UnicodeDecodeError_str(PyObject *self)
1537{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001538 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001539
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001540 if (uself->end==uself->start+1) {
Christian Heimes72b710a2008-05-26 13:28:38 +00001541 int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001542 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001543 "'%U' codec can't decode byte 0x%02x in position %zd: %U",
Walter Dörwaldd2034312007-05-18 16:29:38 +00001544 ((PyUnicodeErrorObject *)self)->encoding,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001545 byte,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001546 uself->start,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001547 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001548 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001549 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001550 return PyUnicode_FromFormat(
1551 "'%U' codec can't decode bytes in position %zd-%zd: %U",
1552 ((PyUnicodeErrorObject *)self)->encoding,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001553 uself->start,
1554 uself->end-1,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001555 ((PyUnicodeErrorObject *)self)->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001556 );
1557}
1558
1559static PyTypeObject _PyExc_UnicodeDecodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001560 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001561 "UnicodeDecodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001562 sizeof(PyUnicodeErrorObject), 0,
1563 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1564 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1565 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001566 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1567 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001568 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001569 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001570};
1571PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1572
1573PyObject *
1574PyUnicodeDecodeError_Create(
1575 const char *encoding, const char *object, Py_ssize_t length,
1576 Py_ssize_t start, Py_ssize_t end, const char *reason)
1577{
1578 assert(length < INT_MAX);
1579 assert(start < INT_MAX);
1580 assert(end < INT_MAX);
Walter Dörwaldd2034312007-05-18 16:29:38 +00001581 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "Uy#nnU",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001582 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001583}
1584
1585
1586/*
1587 * UnicodeTranslateError extends UnicodeError
1588 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001589
1590static int
1591UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1592 PyObject *kwds)
1593{
1594 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1595 return -1;
1596
1597 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001598 Py_CLEAR(self->reason);
1599
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001600 if (!PyArg_ParseTuple(args, "O!nnO!",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001601 &PyUnicode_Type, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001602 &self->start,
1603 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001604 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001605 self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001606 return -1;
1607 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001608
Thomas Wouters477c8d52006-05-27 19:21:47 +00001609 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001610 Py_INCREF(self->reason);
1611
1612 return 0;
1613}
1614
1615
1616static PyObject *
1617UnicodeTranslateError_str(PyObject *self)
1618{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001619 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001620
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001621 if (uself->end==uself->start+1) {
1622 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001623 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001624 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001625 fmt = "can't translate character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001626 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001627 fmt = "can't translate character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001628 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001629 fmt = "can't translate character '\\U%08x' in position %zd: %U";
Walter Dörwaldd2034312007-05-18 16:29:38 +00001630 return PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001631 fmt,
1632 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001633 uself->start,
1634 uself->reason
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001635 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001636 }
Walter Dörwaldd2034312007-05-18 16:29:38 +00001637 return PyUnicode_FromFormat(
1638 "can't translate characters in position %zd-%zd: %U",
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001639 uself->start,
1640 uself->end-1,
1641 uself->reason
Thomas Wouters477c8d52006-05-27 19:21:47 +00001642 );
1643}
1644
1645static PyTypeObject _PyExc_UnicodeTranslateError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001646 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001647 "UnicodeTranslateError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001648 sizeof(PyUnicodeErrorObject), 0,
1649 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1650 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1651 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001652 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001653 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1654 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001655 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001656};
1657PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1658
1659PyObject *
1660PyUnicodeTranslateError_Create(
1661 const Py_UNICODE *object, Py_ssize_t length,
1662 Py_ssize_t start, Py_ssize_t end, const char *reason)
1663{
1664 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001665 object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001666}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001667
1668
1669/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001670 * AssertionError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001671 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001672SimpleExtendsException(PyExc_Exception, AssertionError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001673 "Assertion failed.");
1674
1675
1676/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001677 * ArithmeticError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001678 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001679SimpleExtendsException(PyExc_Exception, ArithmeticError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001680 "Base class for arithmetic errors.");
1681
1682
1683/*
1684 * FloatingPointError extends ArithmeticError
1685 */
1686SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
1687 "Floating point operation failed.");
1688
1689
1690/*
1691 * OverflowError extends ArithmeticError
1692 */
1693SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
1694 "Result too large to be represented.");
1695
1696
1697/*
1698 * ZeroDivisionError extends ArithmeticError
1699 */
1700SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
1701 "Second argument to a division or modulo operation was zero.");
1702
1703
1704/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001705 * SystemError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001706 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001707SimpleExtendsException(PyExc_Exception, SystemError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001708 "Internal error in the Python interpreter.\n"
1709 "\n"
1710 "Please report this to the Python maintainer, along with the traceback,\n"
1711 "the Python version, and the hardware/OS platform and version.");
1712
1713
1714/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001715 * ReferenceError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001716 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001717SimpleExtendsException(PyExc_Exception, ReferenceError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001718 "Weak ref proxy used after referent went away.");
1719
1720
1721/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001722 * MemoryError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001723 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001724SimpleExtendsException(PyExc_Exception, MemoryError, "Out of memory.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001725
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001726/*
1727 * BufferError extends Exception
1728 */
1729SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error.");
1730
Thomas Wouters477c8d52006-05-27 19:21:47 +00001731
1732/* Warning category docstrings */
1733
1734/*
1735 * Warning extends Exception
1736 */
1737SimpleExtendsException(PyExc_Exception, Warning,
1738 "Base class for warning categories.");
1739
1740
1741/*
1742 * UserWarning extends Warning
1743 */
1744SimpleExtendsException(PyExc_Warning, UserWarning,
1745 "Base class for warnings generated by user code.");
1746
1747
1748/*
1749 * DeprecationWarning extends Warning
1750 */
1751SimpleExtendsException(PyExc_Warning, DeprecationWarning,
1752 "Base class for warnings about deprecated features.");
1753
1754
1755/*
1756 * PendingDeprecationWarning extends Warning
1757 */
1758SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1759 "Base class for warnings about features which will be deprecated\n"
1760 "in the future.");
1761
1762
1763/*
1764 * SyntaxWarning extends Warning
1765 */
1766SimpleExtendsException(PyExc_Warning, SyntaxWarning,
1767 "Base class for warnings about dubious syntax.");
1768
1769
1770/*
1771 * RuntimeWarning extends Warning
1772 */
1773SimpleExtendsException(PyExc_Warning, RuntimeWarning,
1774 "Base class for warnings about dubious runtime behavior.");
1775
1776
1777/*
1778 * FutureWarning extends Warning
1779 */
1780SimpleExtendsException(PyExc_Warning, FutureWarning,
1781 "Base class for warnings about constructs that will change semantically\n"
1782 "in the future.");
1783
1784
1785/*
1786 * ImportWarning extends Warning
1787 */
1788SimpleExtendsException(PyExc_Warning, ImportWarning,
1789 "Base class for warnings about probable mistakes in module imports");
1790
1791
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001792/*
1793 * UnicodeWarning extends Warning
1794 */
1795SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1796 "Base class for warnings about Unicode related problems, mostly\n"
1797 "related to conversion problems.");
1798
Guido van Rossum98297ee2007-11-06 21:34:58 +00001799/*
1800 * BytesWarning extends Warning
1801 */
1802SimpleExtendsException(PyExc_Warning, BytesWarning,
1803 "Base class for warnings about bytes and buffer related problems, mostly\n"
1804 "related to conversion from str or comparing to str.");
1805
1806
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001807
Thomas Wouters477c8d52006-05-27 19:21:47 +00001808/* Pre-computed MemoryError instance. Best to create this as early as
1809 * possible and not wait until a MemoryError is actually raised!
1810 */
1811PyObject *PyExc_MemoryErrorInst=NULL;
1812
Thomas Wouters89d996e2007-09-08 17:39:28 +00001813/* Pre-computed RuntimeError instance for when recursion depth is reached.
1814 Meant to be used when normalizing the exception for exceeding the recursion
1815 depth will cause its own infinite recursion.
1816*/
1817PyObject *PyExc_RecursionErrorInst = NULL;
1818
Thomas Wouters477c8d52006-05-27 19:21:47 +00001819#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1820 Py_FatalError("exceptions bootstrapping error.");
1821
1822#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001823 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1824 Py_FatalError("Module dictionary insertion problem.");
1825
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001826
Martin v. Löwis1a214512008-06-11 05:26:20 +00001827void
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001828_PyExc_Init(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001829{
Neal Norwitz2633c692007-02-26 22:22:47 +00001830 PyObject *bltinmod, *bdict;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001831
1832 PRE_INIT(BaseException)
1833 PRE_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001834 PRE_INIT(TypeError)
1835 PRE_INIT(StopIteration)
1836 PRE_INIT(GeneratorExit)
1837 PRE_INIT(SystemExit)
1838 PRE_INIT(KeyboardInterrupt)
1839 PRE_INIT(ImportError)
1840 PRE_INIT(EnvironmentError)
1841 PRE_INIT(IOError)
1842 PRE_INIT(OSError)
1843#ifdef MS_WINDOWS
1844 PRE_INIT(WindowsError)
1845#endif
1846#ifdef __VMS
1847 PRE_INIT(VMSError)
1848#endif
1849 PRE_INIT(EOFError)
1850 PRE_INIT(RuntimeError)
1851 PRE_INIT(NotImplementedError)
1852 PRE_INIT(NameError)
1853 PRE_INIT(UnboundLocalError)
1854 PRE_INIT(AttributeError)
1855 PRE_INIT(SyntaxError)
1856 PRE_INIT(IndentationError)
1857 PRE_INIT(TabError)
1858 PRE_INIT(LookupError)
1859 PRE_INIT(IndexError)
1860 PRE_INIT(KeyError)
1861 PRE_INIT(ValueError)
1862 PRE_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001863 PRE_INIT(UnicodeEncodeError)
1864 PRE_INIT(UnicodeDecodeError)
1865 PRE_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001866 PRE_INIT(AssertionError)
1867 PRE_INIT(ArithmeticError)
1868 PRE_INIT(FloatingPointError)
1869 PRE_INIT(OverflowError)
1870 PRE_INIT(ZeroDivisionError)
1871 PRE_INIT(SystemError)
1872 PRE_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001873 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001874 PRE_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00001875 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001876 PRE_INIT(Warning)
1877 PRE_INIT(UserWarning)
1878 PRE_INIT(DeprecationWarning)
1879 PRE_INIT(PendingDeprecationWarning)
1880 PRE_INIT(SyntaxWarning)
1881 PRE_INIT(RuntimeWarning)
1882 PRE_INIT(FutureWarning)
1883 PRE_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001884 PRE_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001885 PRE_INIT(BytesWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001886
Georg Brandl1a3284e2007-12-02 09:40:06 +00001887 bltinmod = PyImport_ImportModule("builtins");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001888 if (bltinmod == NULL)
1889 Py_FatalError("exceptions bootstrapping error.");
1890 bdict = PyModule_GetDict(bltinmod);
1891 if (bdict == NULL)
1892 Py_FatalError("exceptions bootstrapping error.");
1893
1894 POST_INIT(BaseException)
1895 POST_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001896 POST_INIT(TypeError)
1897 POST_INIT(StopIteration)
1898 POST_INIT(GeneratorExit)
1899 POST_INIT(SystemExit)
1900 POST_INIT(KeyboardInterrupt)
1901 POST_INIT(ImportError)
1902 POST_INIT(EnvironmentError)
1903 POST_INIT(IOError)
1904 POST_INIT(OSError)
1905#ifdef MS_WINDOWS
1906 POST_INIT(WindowsError)
1907#endif
1908#ifdef __VMS
1909 POST_INIT(VMSError)
1910#endif
1911 POST_INIT(EOFError)
1912 POST_INIT(RuntimeError)
1913 POST_INIT(NotImplementedError)
1914 POST_INIT(NameError)
1915 POST_INIT(UnboundLocalError)
1916 POST_INIT(AttributeError)
1917 POST_INIT(SyntaxError)
1918 POST_INIT(IndentationError)
1919 POST_INIT(TabError)
1920 POST_INIT(LookupError)
1921 POST_INIT(IndexError)
1922 POST_INIT(KeyError)
1923 POST_INIT(ValueError)
1924 POST_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001925 POST_INIT(UnicodeEncodeError)
1926 POST_INIT(UnicodeDecodeError)
1927 POST_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001928 POST_INIT(AssertionError)
1929 POST_INIT(ArithmeticError)
1930 POST_INIT(FloatingPointError)
1931 POST_INIT(OverflowError)
1932 POST_INIT(ZeroDivisionError)
1933 POST_INIT(SystemError)
1934 POST_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00001935 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001936 POST_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00001937 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001938 POST_INIT(Warning)
1939 POST_INIT(UserWarning)
1940 POST_INIT(DeprecationWarning)
1941 POST_INIT(PendingDeprecationWarning)
1942 POST_INIT(SyntaxWarning)
1943 POST_INIT(RuntimeWarning)
1944 POST_INIT(FutureWarning)
1945 POST_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001946 POST_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001947 POST_INIT(BytesWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001948
1949 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
1950 if (!PyExc_MemoryErrorInst)
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001951 Py_FatalError("Cannot pre-allocate MemoryError instance");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001952
Thomas Wouters89d996e2007-09-08 17:39:28 +00001953 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
1954 if (!PyExc_RecursionErrorInst)
1955 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
1956 "recursion errors");
1957 else {
1958 PyBaseExceptionObject *err_inst =
1959 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
1960 PyObject *args_tuple;
1961 PyObject *exc_message;
Neal Norwitzbed67842007-10-27 04:00:45 +00001962 exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
Thomas Wouters89d996e2007-09-08 17:39:28 +00001963 if (!exc_message)
1964 Py_FatalError("cannot allocate argument for RuntimeError "
1965 "pre-allocation");
1966 args_tuple = PyTuple_Pack(1, exc_message);
1967 if (!args_tuple)
1968 Py_FatalError("cannot allocate tuple for RuntimeError "
1969 "pre-allocation");
1970 Py_DECREF(exc_message);
1971 if (BaseException_init(err_inst, args_tuple, NULL))
1972 Py_FatalError("init of pre-allocated RuntimeError failed");
1973 Py_DECREF(args_tuple);
1974 }
1975
Thomas Wouters477c8d52006-05-27 19:21:47 +00001976 Py_DECREF(bltinmod);
1977}
1978
1979void
1980_PyExc_Fini(void)
1981{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001982 Py_CLEAR(PyExc_MemoryErrorInst);
1983 Py_CLEAR(PyExc_RecursionErrorInst);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001984}