blob: a4e90fc6396492517a253e1571ae0a8157e14ca8 [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
R David Murray1cb0cb22013-02-27 08:57:09 -050032 if (args) {
33 self->args = args;
34 Py_INCREF(args);
35 return (PyObject *)self;
36 }
37
Thomas Wouters477c8d52006-05-27 19:21:47 +000038 self->args = PyTuple_New(0);
39 if (!self->args) {
40 Py_DECREF(self);
41 return NULL;
42 }
43
Thomas Wouters477c8d52006-05-27 19:21:47 +000044 return (PyObject *)self;
45}
46
47static int
48BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
49{
R David Murray1cb0cb22013-02-27 08:57:09 -050050 PyObject *tmp;
51
Christian Heimes90aa7642007-12-19 02:45:37 +000052 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000053 return -1;
54
R David Murray1cb0cb22013-02-27 08:57:09 -050055 tmp = self->args;
Thomas Wouters477c8d52006-05-27 19:21:47 +000056 self->args = args;
57 Py_INCREF(self->args);
R David Murray1cb0cb22013-02-27 08:57:09 -050058 Py_XDECREF(tmp);
Thomas Wouters477c8d52006-05-27 19:21:47 +000059
Thomas Wouters477c8d52006-05-27 19:21:47 +000060 return 0;
61}
62
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000063static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000064BaseException_clear(PyBaseExceptionObject *self)
65{
66 Py_CLEAR(self->dict);
67 Py_CLEAR(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000068 Py_CLEAR(self->traceback);
Collin Winter1966f1c2007-09-01 20:26:44 +000069 Py_CLEAR(self->cause);
70 Py_CLEAR(self->context);
Thomas Wouters477c8d52006-05-27 19:21:47 +000071 return 0;
72}
73
74static void
75BaseException_dealloc(PyBaseExceptionObject *self)
76{
Thomas Wouters89f507f2006-12-13 04:49:30 +000077 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000078 BaseException_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +000079 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000080}
81
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000082static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000083BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
84{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000085 Py_VISIT(self->dict);
Thomas Wouters477c8d52006-05-27 19:21:47 +000086 Py_VISIT(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000087 Py_VISIT(self->traceback);
Collin Winter1966f1c2007-09-01 20:26:44 +000088 Py_VISIT(self->cause);
89 Py_VISIT(self->context);
Thomas Wouters477c8d52006-05-27 19:21:47 +000090 return 0;
91}
92
93static PyObject *
94BaseException_str(PyBaseExceptionObject *self)
95{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000096 switch (PyTuple_GET_SIZE(self->args)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000097 case 0:
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +000098 return PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +000099 case 1:
Thomas Heller519a0422007-11-15 20:48:54 +0000100 return PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000101 default:
Thomas Heller519a0422007-11-15 20:48:54 +0000102 return PyObject_Str(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000103 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000104}
105
106static PyObject *
107BaseException_repr(PyBaseExceptionObject *self)
108{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000109 char *name;
110 char *dot;
111
Christian Heimes90aa7642007-12-19 02:45:37 +0000112 name = (char *)Py_TYPE(self)->tp_name;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000113 dot = strrchr(name, '.');
114 if (dot != NULL) name = dot+1;
115
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000116 return PyUnicode_FromFormat("%s%R", name, self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117}
118
119/* Pickling support */
120static PyObject *
121BaseException_reduce(PyBaseExceptionObject *self)
122{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000123 if (self->args && self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000124 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000125 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000126 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000127}
128
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000129/*
130 * Needed for backward compatibility, since exceptions used to store
131 * all their attributes in the __dict__. Code is taken from cPickle's
132 * load_build function.
133 */
134static PyObject *
135BaseException_setstate(PyObject *self, PyObject *state)
136{
137 PyObject *d_key, *d_value;
138 Py_ssize_t i = 0;
139
140 if (state != Py_None) {
141 if (!PyDict_Check(state)) {
142 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
143 return NULL;
144 }
145 while (PyDict_Next(state, &i, &d_key, &d_value)) {
146 if (PyObject_SetAttr(self, d_key, d_value) < 0)
147 return NULL;
148 }
149 }
150 Py_RETURN_NONE;
151}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000152
Collin Winter828f04a2007-08-31 00:04:24 +0000153static PyObject *
154BaseException_with_traceback(PyObject *self, PyObject *tb) {
155 if (PyException_SetTraceback(self, tb))
156 return NULL;
157
158 Py_INCREF(self);
159 return self;
160}
161
Georg Brandl76941002008-05-05 21:38:47 +0000162PyDoc_STRVAR(with_traceback_doc,
163"Exception.with_traceback(tb) --\n\
164 set self.__traceback__ to tb and return self.");
165
Thomas Wouters477c8d52006-05-27 19:21:47 +0000166
167static PyMethodDef BaseException_methods[] = {
168 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000169 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Georg Brandl76941002008-05-05 21:38:47 +0000170 {"with_traceback", (PyCFunction)BaseException_with_traceback, METH_O,
171 with_traceback_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000172 {NULL, NULL, 0, NULL},
173};
174
175
Thomas Wouters477c8d52006-05-27 19:21:47 +0000176static PyObject *
177BaseException_get_dict(PyBaseExceptionObject *self)
178{
179 if (self->dict == NULL) {
180 self->dict = PyDict_New();
181 if (!self->dict)
182 return NULL;
183 }
184 Py_INCREF(self->dict);
185 return self->dict;
186}
187
188static int
189BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
190{
191 if (val == NULL) {
192 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
193 return -1;
194 }
195 if (!PyDict_Check(val)) {
196 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
197 return -1;
198 }
199 Py_CLEAR(self->dict);
200 Py_INCREF(val);
201 self->dict = val;
202 return 0;
203}
204
205static PyObject *
206BaseException_get_args(PyBaseExceptionObject *self)
207{
208 if (self->args == NULL) {
209 Py_INCREF(Py_None);
210 return Py_None;
211 }
212 Py_INCREF(self->args);
213 return self->args;
214}
215
216static int
217BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
218{
219 PyObject *seq;
220 if (val == NULL) {
221 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
222 return -1;
223 }
224 seq = PySequence_Tuple(val);
Benjamin Peterson90b13582012-02-03 19:22:31 -0500225 if (!seq)
226 return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000227 Py_CLEAR(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000228 self->args = seq;
229 return 0;
230}
231
Collin Winter828f04a2007-08-31 00:04:24 +0000232static PyObject *
233BaseException_get_tb(PyBaseExceptionObject *self)
234{
235 if (self->traceback == NULL) {
236 Py_INCREF(Py_None);
237 return Py_None;
238 }
239 Py_INCREF(self->traceback);
240 return self->traceback;
241}
242
243static int
244BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb)
245{
246 if (tb == NULL) {
247 PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted");
248 return -1;
249 }
250 else if (!(tb == Py_None || PyTraceBack_Check(tb))) {
251 PyErr_SetString(PyExc_TypeError,
252 "__traceback__ must be a traceback or None");
253 return -1;
254 }
255
256 Py_XINCREF(tb);
257 Py_XDECREF(self->traceback);
258 self->traceback = tb;
259 return 0;
260}
261
Georg Brandlab6f2f62009-03-31 04:16:10 +0000262static PyObject *
263BaseException_get_context(PyObject *self) {
264 PyObject *res = PyException_GetContext(self);
Benjamin Peterson90b13582012-02-03 19:22:31 -0500265 if (res)
266 return res; /* new reference already returned above */
Georg Brandlab6f2f62009-03-31 04:16:10 +0000267 Py_RETURN_NONE;
268}
269
270static int
271BaseException_set_context(PyObject *self, PyObject *arg) {
272 if (arg == NULL) {
273 PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted");
274 return -1;
275 } else if (arg == Py_None) {
276 arg = NULL;
277 } else if (!PyExceptionInstance_Check(arg)) {
278 PyErr_SetString(PyExc_TypeError, "exception context must be None "
279 "or derive from BaseException");
280 return -1;
281 } else {
282 /* PyException_SetContext steals this reference */
283 Py_INCREF(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 }
Georg Brandlab6f2f62009-03-31 04:16:10 +0000285 PyException_SetContext(self, arg);
286 return 0;
287}
288
289static PyObject *
290BaseException_get_cause(PyObject *self) {
291 PyObject *res = PyException_GetCause(self);
Benjamin Peterson90b13582012-02-03 19:22:31 -0500292 if (res)
293 return res; /* new reference already returned above */
Georg Brandlab6f2f62009-03-31 04:16:10 +0000294 Py_RETURN_NONE;
295}
296
297static int
298BaseException_set_cause(PyObject *self, PyObject *arg) {
299 if (arg == NULL) {
300 PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted");
301 return -1;
302 } else if (arg == Py_None) {
303 arg = NULL;
304 } else if (!PyExceptionInstance_Check(arg)) {
305 PyErr_SetString(PyExc_TypeError, "exception cause must be None "
306 "or derive from BaseException");
307 return -1;
308 } else {
309 /* PyException_SetCause steals this reference */
310 Py_INCREF(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 }
Georg Brandlab6f2f62009-03-31 04:16:10 +0000312 PyException_SetCause(self, arg);
313 return 0;
314}
315
Guido van Rossum360e4b82007-05-14 22:51:27 +0000316
Thomas Wouters477c8d52006-05-27 19:21:47 +0000317static PyGetSetDef BaseException_getset[] = {
318 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
319 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Collin Winter828f04a2007-08-31 00:04:24 +0000320 {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb},
Georg Brandlab6f2f62009-03-31 04:16:10 +0000321 {"__context__", (getter)BaseException_get_context,
322 (setter)BaseException_set_context, PyDoc_STR("exception context")},
323 {"__cause__", (getter)BaseException_get_cause,
324 (setter)BaseException_set_cause, PyDoc_STR("exception cause")},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000325 {NULL},
326};
327
328
Collin Winter828f04a2007-08-31 00:04:24 +0000329PyObject *
330PyException_GetTraceback(PyObject *self) {
331 PyBaseExceptionObject *base_self = (PyBaseExceptionObject *)self;
332 Py_XINCREF(base_self->traceback);
333 return base_self->traceback;
334}
335
336
337int
338PyException_SetTraceback(PyObject *self, PyObject *tb) {
339 return BaseException_set_tb((PyBaseExceptionObject *)self, tb);
340}
341
342PyObject *
343PyException_GetCause(PyObject *self) {
344 PyObject *cause = ((PyBaseExceptionObject *)self)->cause;
345 Py_XINCREF(cause);
346 return cause;
347}
348
349/* Steals a reference to cause */
350void
351PyException_SetCause(PyObject *self, PyObject *cause) {
352 PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause;
353 ((PyBaseExceptionObject *)self)->cause = cause;
354 Py_XDECREF(old_cause);
355}
356
357PyObject *
358PyException_GetContext(PyObject *self) {
359 PyObject *context = ((PyBaseExceptionObject *)self)->context;
360 Py_XINCREF(context);
361 return context;
362}
363
364/* Steals a reference to context */
365void
366PyException_SetContext(PyObject *self, PyObject *context) {
367 PyObject *old_context = ((PyBaseExceptionObject *)self)->context;
368 ((PyBaseExceptionObject *)self)->context = context;
369 Py_XDECREF(old_context);
370}
371
372
Thomas Wouters477c8d52006-05-27 19:21:47 +0000373static PyTypeObject _PyExc_BaseException = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000374 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +0000375 "BaseException", /*tp_name*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000376 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
377 0, /*tp_itemsize*/
378 (destructor)BaseException_dealloc, /*tp_dealloc*/
379 0, /*tp_print*/
380 0, /*tp_getattr*/
381 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000382 0, /* tp_reserved; */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000383 (reprfunc)BaseException_repr, /*tp_repr*/
384 0, /*tp_as_number*/
Brett Cannonba7bf492007-02-27 00:15:55 +0000385 0, /*tp_as_sequence*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000386 0, /*tp_as_mapping*/
387 0, /*tp_hash */
388 0, /*tp_call*/
389 (reprfunc)BaseException_str, /*tp_str*/
390 PyObject_GenericGetAttr, /*tp_getattro*/
391 PyObject_GenericSetAttr, /*tp_setattro*/
392 0, /*tp_as_buffer*/
Thomas Wouters27d517b2007-02-25 20:39:11 +0000393 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000395 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
396 (traverseproc)BaseException_traverse, /* tp_traverse */
397 (inquiry)BaseException_clear, /* tp_clear */
398 0, /* tp_richcompare */
399 0, /* tp_weaklistoffset */
400 0, /* tp_iter */
401 0, /* tp_iternext */
402 BaseException_methods, /* tp_methods */
Georg Brandlab6f2f62009-03-31 04:16:10 +0000403 0, /* tp_members */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000404 BaseException_getset, /* tp_getset */
405 0, /* tp_base */
406 0, /* tp_dict */
407 0, /* tp_descr_get */
408 0, /* tp_descr_set */
409 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
410 (initproc)BaseException_init, /* tp_init */
411 0, /* tp_alloc */
412 BaseException_new, /* tp_new */
413};
414/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
415from the previous implmentation and also allowing Python objects to be used
416in the API */
417PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
418
419/* note these macros omit the last semicolon so the macro invocation may
420 * include it and not look strange.
421 */
422#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
423static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000424 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000425 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000426 sizeof(PyBaseExceptionObject), \
427 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
428 0, 0, 0, 0, 0, 0, 0, \
429 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
430 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
431 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
432 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
433 (initproc)BaseException_init, 0, BaseException_new,\
434}; \
435PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
436
437#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
438static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000439 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000440 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000441 sizeof(Py ## EXCSTORE ## Object), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000442 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000443 0, 0, 0, 0, 0, \
444 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000445 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
446 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000447 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000448 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000449}; \
450PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
451
452#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
453static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000454 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000455 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000456 sizeof(Py ## EXCSTORE ## Object), 0, \
457 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
458 (reprfunc)EXCSTR, 0, 0, 0, \
459 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
460 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
461 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
462 EXCMEMBERS, 0, &_ ## EXCBASE, \
463 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000464 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000465}; \
466PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
467
468
469/*
470 * Exception extends BaseException
471 */
472SimpleExtendsException(PyExc_BaseException, Exception,
473 "Common base class for all non-exit exceptions.");
474
475
476/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000477 * TypeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000478 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000479SimpleExtendsException(PyExc_Exception, TypeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000480 "Inappropriate argument type.");
481
482
483/*
484 * StopIteration extends Exception
485 */
486SimpleExtendsException(PyExc_Exception, StopIteration,
Georg Brandla18af4e2007-04-21 15:47:16 +0000487 "Signal the end from iterator.__next__().");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000488
489
490/*
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000491 * GeneratorExit extends BaseException
Thomas Wouters477c8d52006-05-27 19:21:47 +0000492 */
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000493SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000494 "Request that a generator exit.");
495
496
497/*
498 * SystemExit extends BaseException
499 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000500
501static int
502SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
503{
504 Py_ssize_t size = PyTuple_GET_SIZE(args);
505
506 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
507 return -1;
508
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000509 if (size == 0)
510 return 0;
511 Py_CLEAR(self->code);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000512 if (size == 1)
513 self->code = PyTuple_GET_ITEM(args, 0);
514 else if (size > 1)
515 self->code = args;
516 Py_INCREF(self->code);
517 return 0;
518}
519
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000520static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000521SystemExit_clear(PySystemExitObject *self)
522{
523 Py_CLEAR(self->code);
524 return BaseException_clear((PyBaseExceptionObject *)self);
525}
526
527static void
528SystemExit_dealloc(PySystemExitObject *self)
529{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000530 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000531 SystemExit_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000532 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000533}
534
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000535static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000536SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
537{
538 Py_VISIT(self->code);
539 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
540}
541
542static PyMemberDef SystemExit_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000543 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
544 PyDoc_STR("exception code")},
545 {NULL} /* Sentinel */
546};
547
548ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
549 SystemExit_dealloc, 0, SystemExit_members, 0,
550 "Request to exit from the interpreter.");
551
552/*
553 * KeyboardInterrupt extends BaseException
554 */
555SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
556 "Program interrupted by user.");
557
558
559/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000560 * ImportError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000561 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000562SimpleExtendsException(PyExc_Exception, ImportError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000563 "Import can't find module, or can't find name in module.");
564
565
566/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000567 * EnvironmentError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000568 */
569
Thomas Wouters477c8d52006-05-27 19:21:47 +0000570/* Where a function has a single filename, such as open() or some
571 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
572 * called, giving a third argument which is the filename. But, so
573 * that old code using in-place unpacking doesn't break, e.g.:
574 *
575 * except IOError, (errno, strerror):
576 *
577 * we hack args so that it only contains two items. This also
578 * means we need our own __str__() which prints out the filename
579 * when it was supplied.
580 */
581static int
582EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
583 PyObject *kwds)
584{
585 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
586 PyObject *subslice = NULL;
587
588 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
589 return -1;
590
Thomas Wouters89f507f2006-12-13 04:49:30 +0000591 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000592 return 0;
593 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000594
595 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000596 &myerrno, &strerror, &filename)) {
597 return -1;
598 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000599 Py_CLEAR(self->myerrno); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000600 self->myerrno = myerrno;
601 Py_INCREF(self->myerrno);
602
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000603 Py_CLEAR(self->strerror); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604 self->strerror = strerror;
605 Py_INCREF(self->strerror);
606
607 /* self->filename will remain Py_None otherwise */
608 if (filename != NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000609 Py_CLEAR(self->filename); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000610 self->filename = filename;
611 Py_INCREF(self->filename);
612
613 subslice = PyTuple_GetSlice(args, 0, 2);
614 if (!subslice)
615 return -1;
616
617 Py_DECREF(self->args); /* replacing args */
618 self->args = subslice;
619 }
620 return 0;
621}
622
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000623static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624EnvironmentError_clear(PyEnvironmentErrorObject *self)
625{
626 Py_CLEAR(self->myerrno);
627 Py_CLEAR(self->strerror);
628 Py_CLEAR(self->filename);
629 return BaseException_clear((PyBaseExceptionObject *)self);
630}
631
632static void
633EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
634{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000635 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000636 EnvironmentError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000637 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000638}
639
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000640static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000641EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
642 void *arg)
643{
644 Py_VISIT(self->myerrno);
645 Py_VISIT(self->strerror);
646 Py_VISIT(self->filename);
647 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
648}
649
650static PyObject *
651EnvironmentError_str(PyEnvironmentErrorObject *self)
652{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000653 if (self->filename)
654 return PyUnicode_FromFormat("[Errno %S] %S: %R",
655 self->myerrno ? self->myerrno: Py_None,
656 self->strerror ? self->strerror: Py_None,
657 self->filename);
658 else if (self->myerrno && self->strerror)
659 return PyUnicode_FromFormat("[Errno %S] %S",
660 self->myerrno ? self->myerrno: Py_None,
661 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000662 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000663 return BaseException_str((PyBaseExceptionObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000664}
665
666static PyMemberDef EnvironmentError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000667 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
668 PyDoc_STR("exception errno")},
669 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
670 PyDoc_STR("exception strerror")},
671 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
672 PyDoc_STR("exception filename")},
673 {NULL} /* Sentinel */
674};
675
676
677static PyObject *
678EnvironmentError_reduce(PyEnvironmentErrorObject *self)
679{
680 PyObject *args = self->args;
681 PyObject *res = NULL, *tmp;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000682
Thomas Wouters477c8d52006-05-27 19:21:47 +0000683 /* self->args is only the first two real arguments if there was a
684 * file name given to EnvironmentError. */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000685 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000686 args = PyTuple_New(3);
Benjamin Peterson90b13582012-02-03 19:22:31 -0500687 if (!args)
688 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000689
690 tmp = PyTuple_GET_ITEM(self->args, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000691 Py_INCREF(tmp);
692 PyTuple_SET_ITEM(args, 0, tmp);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000693
694 tmp = PyTuple_GET_ITEM(self->args, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000695 Py_INCREF(tmp);
696 PyTuple_SET_ITEM(args, 1, tmp);
697
698 Py_INCREF(self->filename);
699 PyTuple_SET_ITEM(args, 2, self->filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000700 } else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000701 Py_INCREF(args);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000702
703 if (self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000704 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000705 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000706 res = PyTuple_Pack(2, Py_TYPE(self), args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000707 Py_DECREF(args);
708 return res;
709}
710
711
712static PyMethodDef EnvironmentError_methods[] = {
713 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
714 {NULL}
715};
716
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000717ComplexExtendsException(PyExc_Exception, EnvironmentError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000718 EnvironmentError, EnvironmentError_dealloc,
719 EnvironmentError_methods, EnvironmentError_members,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000720 EnvironmentError_str,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000721 "Base class for I/O related errors.");
722
723
724/*
725 * IOError extends EnvironmentError
726 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000727MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000728 EnvironmentError, "I/O operation failed.");
729
730
731/*
732 * OSError extends EnvironmentError
733 */
734MiddlingExtendsException(PyExc_EnvironmentError, OSError,
735 EnvironmentError, "OS system call failed.");
736
737
738/*
739 * WindowsError extends OSError
740 */
741#ifdef MS_WINDOWS
742#include "errmap.h"
743
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000744static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000745WindowsError_clear(PyWindowsErrorObject *self)
746{
747 Py_CLEAR(self->myerrno);
748 Py_CLEAR(self->strerror);
749 Py_CLEAR(self->filename);
750 Py_CLEAR(self->winerror);
751 return BaseException_clear((PyBaseExceptionObject *)self);
752}
753
754static void
755WindowsError_dealloc(PyWindowsErrorObject *self)
756{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000757 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000758 WindowsError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000759 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000760}
761
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000762static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000763WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
764{
765 Py_VISIT(self->myerrno);
766 Py_VISIT(self->strerror);
767 Py_VISIT(self->filename);
768 Py_VISIT(self->winerror);
769 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
770}
771
Thomas Wouters477c8d52006-05-27 19:21:47 +0000772static int
773WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
774{
775 PyObject *o_errcode = NULL;
776 long errcode;
777 long posix_errno;
778
779 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
780 == -1)
781 return -1;
782
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000783 if (self->myerrno == NULL)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000784 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000785
786 /* Set errno to the POSIX errno, and winerror to the Win32
787 error code. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000788 errcode = PyLong_AsLong(self->myerrno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000789 if (errcode == -1 && PyErr_Occurred())
790 return -1;
791 posix_errno = winerror_to_errno(errcode);
792
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000793 Py_CLEAR(self->winerror);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000794 self->winerror = self->myerrno;
795
Christian Heimes217cfd12007-12-02 14:31:20 +0000796 o_errcode = PyLong_FromLong(posix_errno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000797 if (!o_errcode)
798 return -1;
799
800 self->myerrno = o_errcode;
801
802 return 0;
803}
804
805
806static PyObject *
807WindowsError_str(PyWindowsErrorObject *self)
808{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000809 if (self->filename)
810 return PyUnicode_FromFormat("[Error %S] %S: %R",
811 self->winerror ? self->winerror: Py_None,
812 self->strerror ? self->strerror: Py_None,
813 self->filename);
814 else if (self->winerror && self->strerror)
815 return PyUnicode_FromFormat("[Error %S] %S",
816 self->winerror ? self->winerror: Py_None,
817 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000818 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000819 return EnvironmentError_str((PyEnvironmentErrorObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000820}
821
822static PyMemberDef WindowsError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000823 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
824 PyDoc_STR("POSIX exception code")},
825 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
826 PyDoc_STR("exception strerror")},
827 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
828 PyDoc_STR("exception filename")},
829 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
830 PyDoc_STR("Win32 exception code")},
831 {NULL} /* Sentinel */
832};
833
834ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
835 WindowsError_dealloc, 0, WindowsError_members,
836 WindowsError_str, "MS-Windows OS system call failed.");
837
838#endif /* MS_WINDOWS */
839
840
841/*
842 * VMSError extends OSError (I think)
843 */
844#ifdef __VMS
845MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
846 "OpenVMS OS system call failed.");
847#endif
848
849
850/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000851 * EOFError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000852 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000853SimpleExtendsException(PyExc_Exception, EOFError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000854 "Read beyond end of file.");
855
856
857/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000858 * RuntimeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000860SimpleExtendsException(PyExc_Exception, RuntimeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000861 "Unspecified run-time error.");
862
863
864/*
865 * NotImplementedError extends RuntimeError
866 */
867SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
868 "Method or function hasn't been implemented yet.");
869
870/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000871 * NameError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000872 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000873SimpleExtendsException(PyExc_Exception, NameError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000874 "Name not found globally.");
875
876/*
877 * UnboundLocalError extends NameError
878 */
879SimpleExtendsException(PyExc_NameError, UnboundLocalError,
880 "Local name referenced but not bound to a value.");
881
882/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000883 * AttributeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000884 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000885SimpleExtendsException(PyExc_Exception, AttributeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000886 "Attribute not found.");
887
888
889/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000890 * SyntaxError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000891 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892
893static int
894SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
895{
896 PyObject *info = NULL;
897 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
898
899 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
900 return -1;
901
902 if (lenargs >= 1) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000903 Py_CLEAR(self->msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000904 self->msg = PyTuple_GET_ITEM(args, 0);
905 Py_INCREF(self->msg);
906 }
907 if (lenargs == 2) {
908 info = PyTuple_GET_ITEM(args, 1);
909 info = PySequence_Tuple(info);
Benjamin Peterson90b13582012-02-03 19:22:31 -0500910 if (!info)
911 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000912
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000913 if (PyTuple_GET_SIZE(info) != 4) {
914 /* not a very good error message, but it's what Python 2.4 gives */
915 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
916 Py_DECREF(info);
917 return -1;
918 }
919
920 Py_CLEAR(self->filename);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000921 self->filename = PyTuple_GET_ITEM(info, 0);
922 Py_INCREF(self->filename);
923
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000924 Py_CLEAR(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000925 self->lineno = PyTuple_GET_ITEM(info, 1);
926 Py_INCREF(self->lineno);
927
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000928 Py_CLEAR(self->offset);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000929 self->offset = PyTuple_GET_ITEM(info, 2);
930 Py_INCREF(self->offset);
931
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000932 Py_CLEAR(self->text);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000933 self->text = PyTuple_GET_ITEM(info, 3);
934 Py_INCREF(self->text);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000935
936 Py_DECREF(info);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000937 }
938 return 0;
939}
940
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000941static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000942SyntaxError_clear(PySyntaxErrorObject *self)
943{
944 Py_CLEAR(self->msg);
945 Py_CLEAR(self->filename);
946 Py_CLEAR(self->lineno);
947 Py_CLEAR(self->offset);
948 Py_CLEAR(self->text);
949 Py_CLEAR(self->print_file_and_line);
950 return BaseException_clear((PyBaseExceptionObject *)self);
951}
952
953static void
954SyntaxError_dealloc(PySyntaxErrorObject *self)
955{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000956 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957 SyntaxError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000958 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000959}
960
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000961static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000962SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
963{
964 Py_VISIT(self->msg);
965 Py_VISIT(self->filename);
966 Py_VISIT(self->lineno);
967 Py_VISIT(self->offset);
968 Py_VISIT(self->text);
969 Py_VISIT(self->print_file_and_line);
970 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
971}
972
973/* This is called "my_basename" instead of just "basename" to avoid name
974 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
975 defined, and Python does define that. */
Victor Stinner6237daf2010-04-28 17:26:19 +0000976static PyObject*
977my_basename(PyObject *name)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000978{
Victor Stinner6237daf2010-04-28 17:26:19 +0000979 Py_UNICODE *unicode;
980 Py_ssize_t i, size, offset;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000981
Victor Stinner6237daf2010-04-28 17:26:19 +0000982 unicode = PyUnicode_AS_UNICODE(name);
983 size = PyUnicode_GET_SIZE(name);
984 offset = 0;
985 for(i=0; i < size; i++) {
986 if (unicode[i] == SEP)
987 offset = i + 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000988 }
Victor Stinner6237daf2010-04-28 17:26:19 +0000989 if (offset != 0) {
990 return PyUnicode_FromUnicode(
991 PyUnicode_AS_UNICODE(name) + offset,
992 size - offset);
993 } else {
994 Py_INCREF(name);
995 return name;
996 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000997}
998
999
1000static PyObject *
1001SyntaxError_str(PySyntaxErrorObject *self)
1002{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001003 int have_lineno = 0;
Victor Stinner6237daf2010-04-28 17:26:19 +00001004 PyObject *filename;
1005 PyObject *result;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001006 /* Below, we always ignore overflow errors, just printing -1.
1007 Still, we cannot allow an OverflowError to be raised, so
1008 we need to call PyLong_AsLongAndOverflow. */
1009 int overflow;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001010
1011 /* XXX -- do all the additional formatting with filename and
1012 lineno here */
1013
Neal Norwitzed2b7392007-08-26 04:51:10 +00001014 if (self->filename && PyUnicode_Check(self->filename)) {
Victor Stinner6237daf2010-04-28 17:26:19 +00001015 filename = my_basename(self->filename);
1016 if (filename == NULL)
1017 return NULL;
1018 } else {
1019 filename = NULL;
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001020 }
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001021 have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001022
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001023 if (!filename && !have_lineno)
Thomas Heller519a0422007-11-15 20:48:54 +00001024 return PyObject_Str(self->msg ? self->msg : Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001025
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001026 if (filename && have_lineno)
Victor Stinner6237daf2010-04-28 17:26:19 +00001027 result = PyUnicode_FromFormat("%S (%U, line %ld)",
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001028 self->msg ? self->msg : Py_None,
Victor Stinner6237daf2010-04-28 17:26:19 +00001029 filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001031 else if (filename)
Victor Stinner6237daf2010-04-28 17:26:19 +00001032 result = PyUnicode_FromFormat("%S (%U)",
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001033 self->msg ? self->msg : Py_None,
Victor Stinner6237daf2010-04-28 17:26:19 +00001034 filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001035 else /* only have_lineno */
Victor Stinner6237daf2010-04-28 17:26:19 +00001036 result = PyUnicode_FromFormat("%S (line %ld)",
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001037 self->msg ? self->msg : Py_None,
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001038 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Victor Stinner6237daf2010-04-28 17:26:19 +00001039 Py_XDECREF(filename);
1040 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001041}
1042
1043static PyMemberDef SyntaxError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001044 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1045 PyDoc_STR("exception msg")},
1046 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1047 PyDoc_STR("exception filename")},
1048 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1049 PyDoc_STR("exception lineno")},
1050 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1051 PyDoc_STR("exception offset")},
1052 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1053 PyDoc_STR("exception text")},
1054 {"print_file_and_line", T_OBJECT,
1055 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1056 PyDoc_STR("exception print_file_and_line")},
1057 {NULL} /* Sentinel */
1058};
1059
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001060ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001061 SyntaxError_dealloc, 0, SyntaxError_members,
1062 SyntaxError_str, "Invalid syntax.");
1063
1064
1065/*
1066 * IndentationError extends SyntaxError
1067 */
1068MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
1069 "Improper indentation.");
1070
1071
1072/*
1073 * TabError extends IndentationError
1074 */
1075MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
1076 "Improper mixture of spaces and tabs.");
1077
1078
1079/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001080 * LookupError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001081 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001082SimpleExtendsException(PyExc_Exception, LookupError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001083 "Base class for lookup errors.");
1084
1085
1086/*
1087 * IndexError extends LookupError
1088 */
1089SimpleExtendsException(PyExc_LookupError, IndexError,
1090 "Sequence index out of range.");
1091
1092
1093/*
1094 * KeyError extends LookupError
1095 */
1096static PyObject *
1097KeyError_str(PyBaseExceptionObject *self)
1098{
1099 /* If args is a tuple of exactly one item, apply repr to args[0].
1100 This is done so that e.g. the exception raised by {}[''] prints
1101 KeyError: ''
1102 rather than the confusing
1103 KeyError
1104 alone. The downside is that if KeyError is raised with an explanatory
1105 string, that string will be displayed in quotes. Too bad.
1106 If args is anything else, use the default BaseException__str__().
1107 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001108 if (PyTuple_GET_SIZE(self->args) == 1) {
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001109 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001110 }
1111 return BaseException_str(self);
1112}
1113
1114ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
1115 0, 0, 0, KeyError_str, "Mapping key not found.");
1116
1117
1118/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001119 * ValueError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001120 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001121SimpleExtendsException(PyExc_Exception, ValueError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001122 "Inappropriate argument value (of correct type).");
1123
1124/*
1125 * UnicodeError extends ValueError
1126 */
1127
1128SimpleExtendsException(PyExc_ValueError, UnicodeError,
1129 "Unicode related error.");
1130
Thomas Wouters477c8d52006-05-27 19:21:47 +00001131static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001132get_string(PyObject *attr, const char *name)
Walter Dörwald612344f2007-05-04 19:28:21 +00001133{
1134 if (!attr) {
1135 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1136 return NULL;
1137 }
1138
Christian Heimes72b710a2008-05-26 13:28:38 +00001139 if (!PyBytes_Check(attr)) {
Walter Dörwald612344f2007-05-04 19:28:21 +00001140 PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
1141 return NULL;
1142 }
1143 Py_INCREF(attr);
1144 return attr;
1145}
1146
1147static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001148get_unicode(PyObject *attr, const char *name)
1149{
1150 if (!attr) {
1151 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1152 return NULL;
1153 }
1154
1155 if (!PyUnicode_Check(attr)) {
1156 PyErr_Format(PyExc_TypeError,
1157 "%.200s attribute must be unicode", name);
1158 return NULL;
1159 }
1160 Py_INCREF(attr);
1161 return attr;
1162}
1163
Walter Dörwaldd2034312007-05-18 16:29:38 +00001164static int
1165set_unicodefromstring(PyObject **attr, const char *value)
1166{
1167 PyObject *obj = PyUnicode_FromString(value);
1168 if (!obj)
1169 return -1;
1170 Py_CLEAR(*attr);
1171 *attr = obj;
1172 return 0;
1173}
1174
Thomas Wouters477c8d52006-05-27 19:21:47 +00001175PyObject *
1176PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1177{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001178 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001179}
1180
1181PyObject *
1182PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1183{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001184 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001185}
1186
1187PyObject *
1188PyUnicodeEncodeError_GetObject(PyObject *exc)
1189{
1190 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1191}
1192
1193PyObject *
1194PyUnicodeDecodeError_GetObject(PyObject *exc)
1195{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001196 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001197}
1198
1199PyObject *
1200PyUnicodeTranslateError_GetObject(PyObject *exc)
1201{
1202 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1203}
1204
1205int
1206PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1207{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001208 Py_ssize_t size;
1209 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1210 "object");
1211 if (!obj)
1212 return -1;
1213 *start = ((PyUnicodeErrorObject *)exc)->start;
1214 size = PyUnicode_GET_SIZE(obj);
1215 if (*start<0)
1216 *start = 0; /*XXX check for values <0*/
1217 if (*start>=size)
1218 *start = size-1;
1219 Py_DECREF(obj);
1220 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001221}
1222
1223
1224int
1225PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1226{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001227 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001228 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001229 if (!obj)
1230 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001231 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001232 *start = ((PyUnicodeErrorObject *)exc)->start;
1233 if (*start<0)
1234 *start = 0;
1235 if (*start>=size)
1236 *start = size-1;
1237 Py_DECREF(obj);
1238 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001239}
1240
1241
1242int
1243PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1244{
1245 return PyUnicodeEncodeError_GetStart(exc, start);
1246}
1247
1248
1249int
1250PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1251{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001252 ((PyUnicodeErrorObject *)exc)->start = start;
1253 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001254}
1255
1256
1257int
1258PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1259{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001260 ((PyUnicodeErrorObject *)exc)->start = start;
1261 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001262}
1263
1264
1265int
1266PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1267{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001268 ((PyUnicodeErrorObject *)exc)->start = start;
1269 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001270}
1271
1272
1273int
1274PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1275{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001276 Py_ssize_t size;
1277 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1278 "object");
1279 if (!obj)
1280 return -1;
1281 *end = ((PyUnicodeErrorObject *)exc)->end;
1282 size = PyUnicode_GET_SIZE(obj);
1283 if (*end<1)
1284 *end = 1;
1285 if (*end>size)
1286 *end = size;
1287 Py_DECREF(obj);
1288 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001289}
1290
1291
1292int
1293PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1294{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001295 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001296 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001297 if (!obj)
1298 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001299 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001300 *end = ((PyUnicodeErrorObject *)exc)->end;
1301 if (*end<1)
1302 *end = 1;
1303 if (*end>size)
1304 *end = size;
1305 Py_DECREF(obj);
1306 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001307}
1308
1309
1310int
1311PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1312{
1313 return PyUnicodeEncodeError_GetEnd(exc, start);
1314}
1315
1316
1317int
1318PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1319{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001320 ((PyUnicodeErrorObject *)exc)->end = end;
1321 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001322}
1323
1324
1325int
1326PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1327{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001328 ((PyUnicodeErrorObject *)exc)->end = end;
1329 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001330}
1331
1332
1333int
1334PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1335{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001336 ((PyUnicodeErrorObject *)exc)->end = end;
1337 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001338}
1339
1340PyObject *
1341PyUnicodeEncodeError_GetReason(PyObject *exc)
1342{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001343 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001344}
1345
1346
1347PyObject *
1348PyUnicodeDecodeError_GetReason(PyObject *exc)
1349{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001350 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001351}
1352
1353
1354PyObject *
1355PyUnicodeTranslateError_GetReason(PyObject *exc)
1356{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001357 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001358}
1359
1360
1361int
1362PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1363{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001364 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1365 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001366}
1367
1368
1369int
1370PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1371{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001372 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1373 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001374}
1375
1376
1377int
1378PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1379{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001380 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1381 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001382}
1383
1384
Thomas Wouters477c8d52006-05-27 19:21:47 +00001385static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001386UnicodeError_clear(PyUnicodeErrorObject *self)
1387{
1388 Py_CLEAR(self->encoding);
1389 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001390 Py_CLEAR(self->reason);
1391 return BaseException_clear((PyBaseExceptionObject *)self);
1392}
1393
1394static void
1395UnicodeError_dealloc(PyUnicodeErrorObject *self)
1396{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001397 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001398 UnicodeError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +00001399 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001400}
1401
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001402static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001403UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1404{
1405 Py_VISIT(self->encoding);
1406 Py_VISIT(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001407 Py_VISIT(self->reason);
1408 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1409}
1410
1411static PyMemberDef UnicodeError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001412 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1413 PyDoc_STR("exception encoding")},
1414 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1415 PyDoc_STR("exception object")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001416 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001417 PyDoc_STR("exception start")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001418 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001419 PyDoc_STR("exception end")},
1420 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1421 PyDoc_STR("exception reason")},
1422 {NULL} /* Sentinel */
1423};
1424
1425
1426/*
1427 * UnicodeEncodeError extends UnicodeError
1428 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001429
1430static int
1431UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1432{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001433 PyUnicodeErrorObject *err;
1434
Thomas Wouters477c8d52006-05-27 19:21:47 +00001435 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1436 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001437
1438 err = (PyUnicodeErrorObject *)self;
1439
1440 Py_CLEAR(err->encoding);
1441 Py_CLEAR(err->object);
1442 Py_CLEAR(err->reason);
1443
1444 if (!PyArg_ParseTuple(args, "O!O!nnO!",
1445 &PyUnicode_Type, &err->encoding,
1446 &PyUnicode_Type, &err->object,
1447 &err->start,
1448 &err->end,
1449 &PyUnicode_Type, &err->reason)) {
1450 err->encoding = err->object = err->reason = NULL;
1451 return -1;
1452 }
1453
1454 Py_INCREF(err->encoding);
1455 Py_INCREF(err->object);
1456 Py_INCREF(err->reason);
1457
1458 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001459}
1460
1461static PyObject *
1462UnicodeEncodeError_str(PyObject *self)
1463{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001464 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith0facd772010-02-24 15:42:29 +00001465 PyObject *result = NULL;
1466 PyObject *reason_str = NULL;
1467 PyObject *encoding_str = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001468
Eric Smith0facd772010-02-24 15:42:29 +00001469 /* Get reason and encoding as strings, which they might not be if
1470 they've been modified after we were contructed. */
1471 reason_str = PyObject_Str(uself->reason);
1472 if (reason_str == NULL)
1473 goto done;
1474 encoding_str = PyObject_Str(uself->encoding);
1475 if (encoding_str == NULL)
1476 goto done;
1477
1478 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001479 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001480 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001481 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001482 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001483 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001484 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001485 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001486 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
Eric Smith0facd772010-02-24 15:42:29 +00001487 result = PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001488 fmt,
Eric Smith0facd772010-02-24 15:42:29 +00001489 encoding_str,
Walter Dörwald787b03b2007-06-05 13:29:29 +00001490 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001491 uself->start,
Eric Smith0facd772010-02-24 15:42:29 +00001492 reason_str);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001493 }
Eric Smith0facd772010-02-24 15:42:29 +00001494 else {
1495 result = PyUnicode_FromFormat(
1496 "'%U' codec can't encode characters in position %zd-%zd: %U",
1497 encoding_str,
1498 uself->start,
1499 uself->end-1,
1500 reason_str);
1501 }
1502done:
1503 Py_XDECREF(reason_str);
1504 Py_XDECREF(encoding_str);
1505 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001506}
1507
1508static PyTypeObject _PyExc_UnicodeEncodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001509 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001510 "UnicodeEncodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001511 sizeof(PyUnicodeErrorObject), 0,
1512 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1513 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1514 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001515 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1516 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001517 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001518 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001519};
1520PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1521
1522PyObject *
1523PyUnicodeEncodeError_Create(
1524 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1525 Py_ssize_t start, Py_ssize_t end, const char *reason)
1526{
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001527 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001528 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001529}
1530
1531
1532/*
1533 * UnicodeDecodeError extends UnicodeError
1534 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001535
1536static int
1537UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1538{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001539 PyUnicodeErrorObject *ude;
1540 const char *data;
1541 Py_ssize_t size;
1542
Thomas Wouters477c8d52006-05-27 19:21:47 +00001543 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1544 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001545
1546 ude = (PyUnicodeErrorObject *)self;
1547
1548 Py_CLEAR(ude->encoding);
1549 Py_CLEAR(ude->object);
1550 Py_CLEAR(ude->reason);
1551
1552 if (!PyArg_ParseTuple(args, "O!OnnO!",
1553 &PyUnicode_Type, &ude->encoding,
1554 &ude->object,
1555 &ude->start,
1556 &ude->end,
1557 &PyUnicode_Type, &ude->reason)) {
1558 ude->encoding = ude->object = ude->reason = NULL;
1559 return -1;
1560 }
1561
Christian Heimes72b710a2008-05-26 13:28:38 +00001562 if (!PyBytes_Check(ude->object)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00001563 if (PyObject_AsReadBuffer(ude->object, (const void **)&data, &size)) {
1564 ude->encoding = ude->object = ude->reason = NULL;
1565 return -1;
1566 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001567 ude->object = PyBytes_FromStringAndSize(data, size);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001568 }
1569 else {
1570 Py_INCREF(ude->object);
1571 }
1572
1573 Py_INCREF(ude->encoding);
1574 Py_INCREF(ude->reason);
1575
1576 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001577}
1578
1579static PyObject *
1580UnicodeDecodeError_str(PyObject *self)
1581{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001582 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith0facd772010-02-24 15:42:29 +00001583 PyObject *result = NULL;
1584 PyObject *reason_str = NULL;
1585 PyObject *encoding_str = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001586
Eric Smith0facd772010-02-24 15:42:29 +00001587 /* Get reason and encoding as strings, which they might not be if
1588 they've been modified after we were contructed. */
1589 reason_str = PyObject_Str(uself->reason);
1590 if (reason_str == NULL)
1591 goto done;
1592 encoding_str = PyObject_Str(uself->encoding);
1593 if (encoding_str == NULL)
1594 goto done;
1595
1596 if (uself->start < PyBytes_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Christian Heimes72b710a2008-05-26 13:28:38 +00001597 int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
Eric Smith0facd772010-02-24 15:42:29 +00001598 result = PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001599 "'%U' codec can't decode byte 0x%02x in position %zd: %U",
Eric Smith0facd772010-02-24 15:42:29 +00001600 encoding_str,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001601 byte,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001602 uself->start,
Eric Smith0facd772010-02-24 15:42:29 +00001603 reason_str);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001604 }
Eric Smith0facd772010-02-24 15:42:29 +00001605 else {
1606 result = PyUnicode_FromFormat(
1607 "'%U' codec can't decode bytes in position %zd-%zd: %U",
1608 encoding_str,
1609 uself->start,
1610 uself->end-1,
1611 reason_str
1612 );
1613 }
1614done:
1615 Py_XDECREF(reason_str);
1616 Py_XDECREF(encoding_str);
1617 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618}
1619
1620static PyTypeObject _PyExc_UnicodeDecodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001621 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001622 "UnicodeDecodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001623 sizeof(PyUnicodeErrorObject), 0,
1624 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1625 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1626 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001627 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1628 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001629 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001630 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001631};
1632PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1633
1634PyObject *
1635PyUnicodeDecodeError_Create(
1636 const char *encoding, const char *object, Py_ssize_t length,
1637 Py_ssize_t start, Py_ssize_t end, const char *reason)
1638{
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001639 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001640 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001641}
1642
1643
1644/*
1645 * UnicodeTranslateError extends UnicodeError
1646 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001647
1648static int
1649UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1650 PyObject *kwds)
1651{
1652 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1653 return -1;
1654
1655 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001656 Py_CLEAR(self->reason);
1657
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001658 if (!PyArg_ParseTuple(args, "O!nnO!",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001659 &PyUnicode_Type, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001660 &self->start,
1661 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001662 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001663 self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001664 return -1;
1665 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001666
Thomas Wouters477c8d52006-05-27 19:21:47 +00001667 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001668 Py_INCREF(self->reason);
1669
1670 return 0;
1671}
1672
1673
1674static PyObject *
1675UnicodeTranslateError_str(PyObject *self)
1676{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001677 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith0facd772010-02-24 15:42:29 +00001678 PyObject *result = NULL;
1679 PyObject *reason_str = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001680
Eric Smith0facd772010-02-24 15:42:29 +00001681 /* Get reason as a string, which it might not be if it's been
1682 modified after we were contructed. */
1683 reason_str = PyObject_Str(uself->reason);
1684 if (reason_str == NULL)
1685 goto done;
1686
1687 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001688 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001689 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001690 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001691 fmt = "can't translate character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001692 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001693 fmt = "can't translate character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001694 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001695 fmt = "can't translate character '\\U%08x' in position %zd: %U";
Benjamin Petersonc5f4e1e2010-02-25 01:22:28 +00001696 result = PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001697 fmt,
1698 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001699 uself->start,
Eric Smith0facd772010-02-24 15:42:29 +00001700 reason_str
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001701 );
Eric Smith0facd772010-02-24 15:42:29 +00001702 } else {
1703 result = PyUnicode_FromFormat(
1704 "can't translate characters in position %zd-%zd: %U",
1705 uself->start,
1706 uself->end-1,
1707 reason_str
1708 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001709 }
Eric Smith0facd772010-02-24 15:42:29 +00001710done:
1711 Py_XDECREF(reason_str);
1712 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001713}
1714
1715static PyTypeObject _PyExc_UnicodeTranslateError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001716 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001717 "UnicodeTranslateError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001718 sizeof(PyUnicodeErrorObject), 0,
1719 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1720 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1721 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001722 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001723 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1724 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001725 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001726};
1727PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1728
1729PyObject *
1730PyUnicodeTranslateError_Create(
1731 const Py_UNICODE *object, Py_ssize_t length,
1732 Py_ssize_t start, Py_ssize_t end, const char *reason)
1733{
1734 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001735 object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001736}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001737
1738
1739/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001740 * AssertionError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001741 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001742SimpleExtendsException(PyExc_Exception, AssertionError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001743 "Assertion failed.");
1744
1745
1746/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001747 * ArithmeticError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001748 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001749SimpleExtendsException(PyExc_Exception, ArithmeticError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001750 "Base class for arithmetic errors.");
1751
1752
1753/*
1754 * FloatingPointError extends ArithmeticError
1755 */
1756SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
1757 "Floating point operation failed.");
1758
1759
1760/*
1761 * OverflowError extends ArithmeticError
1762 */
1763SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
1764 "Result too large to be represented.");
1765
1766
1767/*
1768 * ZeroDivisionError extends ArithmeticError
1769 */
1770SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
1771 "Second argument to a division or modulo operation was zero.");
1772
1773
1774/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001775 * SystemError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001776 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001777SimpleExtendsException(PyExc_Exception, SystemError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001778 "Internal error in the Python interpreter.\n"
1779 "\n"
1780 "Please report this to the Python maintainer, along with the traceback,\n"
1781 "the Python version, and the hardware/OS platform and version.");
1782
1783
1784/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001785 * ReferenceError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001786 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001787SimpleExtendsException(PyExc_Exception, ReferenceError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001788 "Weak ref proxy used after referent went away.");
1789
1790
1791/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001792 * MemoryError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001793 */
Antoine Pitrou07e20ef2010-10-28 22:56:58 +00001794
1795#define MEMERRORS_SAVE 16
1796static PyBaseExceptionObject *memerrors_freelist = NULL;
1797static int memerrors_numfree = 0;
1798
1799static PyObject *
1800MemoryError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1801{
1802 PyBaseExceptionObject *self;
1803
1804 if (type != (PyTypeObject *) PyExc_MemoryError)
1805 return BaseException_new(type, args, kwds);
1806 if (memerrors_freelist == NULL)
1807 return BaseException_new(type, args, kwds);
1808 /* Fetch object from freelist and revive it */
1809 self = memerrors_freelist;
1810 self->args = PyTuple_New(0);
1811 /* This shouldn't happen since the empty tuple is persistent */
1812 if (self->args == NULL)
1813 return NULL;
1814 memerrors_freelist = (PyBaseExceptionObject *) self->dict;
1815 memerrors_numfree--;
1816 self->dict = NULL;
1817 _Py_NewReference((PyObject *)self);
1818 _PyObject_GC_TRACK(self);
1819 return (PyObject *)self;
1820}
1821
1822static void
1823MemoryError_dealloc(PyBaseExceptionObject *self)
1824{
1825 _PyObject_GC_UNTRACK(self);
1826 BaseException_clear(self);
1827 if (memerrors_numfree >= MEMERRORS_SAVE)
1828 Py_TYPE(self)->tp_free((PyObject *)self);
1829 else {
1830 self->dict = (PyObject *) memerrors_freelist;
1831 memerrors_freelist = self;
1832 memerrors_numfree++;
1833 }
1834}
1835
1836static void
1837preallocate_memerrors(void)
1838{
1839 /* We create enough MemoryErrors and then decref them, which will fill
1840 up the freelist. */
1841 int i;
1842 PyObject *errors[MEMERRORS_SAVE];
1843 for (i = 0; i < MEMERRORS_SAVE; i++) {
1844 errors[i] = MemoryError_new((PyTypeObject *) PyExc_MemoryError,
1845 NULL, NULL);
1846 if (!errors[i])
1847 Py_FatalError("Could not preallocate MemoryError object");
1848 }
1849 for (i = 0; i < MEMERRORS_SAVE; i++) {
1850 Py_DECREF(errors[i]);
1851 }
1852}
1853
1854static void
1855free_preallocated_memerrors(void)
1856{
1857 while (memerrors_freelist != NULL) {
1858 PyObject *self = (PyObject *) memerrors_freelist;
1859 memerrors_freelist = (PyBaseExceptionObject *) memerrors_freelist->dict;
1860 Py_TYPE(self)->tp_free((PyObject *)self);
1861 }
1862}
1863
1864
1865static PyTypeObject _PyExc_MemoryError = {
1866 PyVarObject_HEAD_INIT(NULL, 0)
1867 "MemoryError",
1868 sizeof(PyBaseExceptionObject),
1869 0, (destructor)MemoryError_dealloc, 0, 0, 0, 0, 0, 0, 0,
1870 0, 0, 0, 0, 0, 0, 0,
1871 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1872 PyDoc_STR("Out of memory."), (traverseproc)BaseException_traverse,
1873 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_PyExc_Exception,
1874 0, 0, 0, offsetof(PyBaseExceptionObject, dict),
1875 (initproc)BaseException_init, 0, MemoryError_new
1876};
1877PyObject *PyExc_MemoryError = (PyObject *) &_PyExc_MemoryError;
1878
Thomas Wouters477c8d52006-05-27 19:21:47 +00001879
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001880/*
1881 * BufferError extends Exception
1882 */
1883SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error.");
1884
Thomas Wouters477c8d52006-05-27 19:21:47 +00001885
1886/* Warning category docstrings */
1887
1888/*
1889 * Warning extends Exception
1890 */
1891SimpleExtendsException(PyExc_Exception, Warning,
1892 "Base class for warning categories.");
1893
1894
1895/*
1896 * UserWarning extends Warning
1897 */
1898SimpleExtendsException(PyExc_Warning, UserWarning,
1899 "Base class for warnings generated by user code.");
1900
1901
1902/*
1903 * DeprecationWarning extends Warning
1904 */
1905SimpleExtendsException(PyExc_Warning, DeprecationWarning,
1906 "Base class for warnings about deprecated features.");
1907
1908
1909/*
1910 * PendingDeprecationWarning extends Warning
1911 */
1912SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1913 "Base class for warnings about features which will be deprecated\n"
1914 "in the future.");
1915
1916
1917/*
1918 * SyntaxWarning extends Warning
1919 */
1920SimpleExtendsException(PyExc_Warning, SyntaxWarning,
1921 "Base class for warnings about dubious syntax.");
1922
1923
1924/*
1925 * RuntimeWarning extends Warning
1926 */
1927SimpleExtendsException(PyExc_Warning, RuntimeWarning,
1928 "Base class for warnings about dubious runtime behavior.");
1929
1930
1931/*
1932 * FutureWarning extends Warning
1933 */
1934SimpleExtendsException(PyExc_Warning, FutureWarning,
1935 "Base class for warnings about constructs that will change semantically\n"
1936 "in the future.");
1937
1938
1939/*
1940 * ImportWarning extends Warning
1941 */
1942SimpleExtendsException(PyExc_Warning, ImportWarning,
1943 "Base class for warnings about probable mistakes in module imports");
1944
1945
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001946/*
1947 * UnicodeWarning extends Warning
1948 */
1949SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1950 "Base class for warnings about Unicode related problems, mostly\n"
1951 "related to conversion problems.");
1952
Georg Brandl08be72d2010-10-24 15:11:22 +00001953
Guido van Rossum98297ee2007-11-06 21:34:58 +00001954/*
1955 * BytesWarning extends Warning
1956 */
1957SimpleExtendsException(PyExc_Warning, BytesWarning,
1958 "Base class for warnings about bytes and buffer related problems, mostly\n"
1959 "related to conversion from str or comparing to str.");
1960
1961
Georg Brandl08be72d2010-10-24 15:11:22 +00001962/*
1963 * ResourceWarning extends Warning
1964 */
1965SimpleExtendsException(PyExc_Warning, ResourceWarning,
1966 "Base class for warnings about resource usage.");
1967
1968
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001969
Thomas Wouters89d996e2007-09-08 17:39:28 +00001970/* Pre-computed RuntimeError instance for when recursion depth is reached.
1971 Meant to be used when normalizing the exception for exceeding the recursion
1972 depth will cause its own infinite recursion.
1973*/
1974PyObject *PyExc_RecursionErrorInst = NULL;
1975
Antoine Pitrou55f217f2012-01-18 21:23:13 +01001976#define PRE_INIT(TYPE) \
1977 if (!(_PyExc_ ## TYPE.tp_flags & Py_TPFLAGS_READY)) { \
1978 if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1979 Py_FatalError("exceptions bootstrapping error."); \
1980 Py_INCREF(PyExc_ ## TYPE); \
1981 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001982
Antoine Pitrou55f217f2012-01-18 21:23:13 +01001983#define POST_INIT(TYPE) \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001984 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1985 Py_FatalError("Module dictionary insertion problem.");
1986
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001987
Martin v. Löwis1a214512008-06-11 05:26:20 +00001988void
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001989_PyExc_Init(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001990{
Neal Norwitz2633c692007-02-26 22:22:47 +00001991 PyObject *bltinmod, *bdict;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001992
1993 PRE_INIT(BaseException)
1994 PRE_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001995 PRE_INIT(TypeError)
1996 PRE_INIT(StopIteration)
1997 PRE_INIT(GeneratorExit)
1998 PRE_INIT(SystemExit)
1999 PRE_INIT(KeyboardInterrupt)
2000 PRE_INIT(ImportError)
2001 PRE_INIT(EnvironmentError)
2002 PRE_INIT(IOError)
2003 PRE_INIT(OSError)
2004#ifdef MS_WINDOWS
2005 PRE_INIT(WindowsError)
2006#endif
2007#ifdef __VMS
2008 PRE_INIT(VMSError)
2009#endif
2010 PRE_INIT(EOFError)
2011 PRE_INIT(RuntimeError)
2012 PRE_INIT(NotImplementedError)
2013 PRE_INIT(NameError)
2014 PRE_INIT(UnboundLocalError)
2015 PRE_INIT(AttributeError)
2016 PRE_INIT(SyntaxError)
2017 PRE_INIT(IndentationError)
2018 PRE_INIT(TabError)
2019 PRE_INIT(LookupError)
2020 PRE_INIT(IndexError)
2021 PRE_INIT(KeyError)
2022 PRE_INIT(ValueError)
2023 PRE_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002024 PRE_INIT(UnicodeEncodeError)
2025 PRE_INIT(UnicodeDecodeError)
2026 PRE_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002027 PRE_INIT(AssertionError)
2028 PRE_INIT(ArithmeticError)
2029 PRE_INIT(FloatingPointError)
2030 PRE_INIT(OverflowError)
2031 PRE_INIT(ZeroDivisionError)
2032 PRE_INIT(SystemError)
2033 PRE_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00002034 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002035 PRE_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00002036 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002037 PRE_INIT(Warning)
2038 PRE_INIT(UserWarning)
2039 PRE_INIT(DeprecationWarning)
2040 PRE_INIT(PendingDeprecationWarning)
2041 PRE_INIT(SyntaxWarning)
2042 PRE_INIT(RuntimeWarning)
2043 PRE_INIT(FutureWarning)
2044 PRE_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002045 PRE_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00002046 PRE_INIT(BytesWarning)
Georg Brandl08be72d2010-10-24 15:11:22 +00002047 PRE_INIT(ResourceWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002048
Georg Brandl1a3284e2007-12-02 09:40:06 +00002049 bltinmod = PyImport_ImportModule("builtins");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002050 if (bltinmod == NULL)
2051 Py_FatalError("exceptions bootstrapping error.");
2052 bdict = PyModule_GetDict(bltinmod);
2053 if (bdict == NULL)
2054 Py_FatalError("exceptions bootstrapping error.");
2055
2056 POST_INIT(BaseException)
2057 POST_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002058 POST_INIT(TypeError)
2059 POST_INIT(StopIteration)
2060 POST_INIT(GeneratorExit)
2061 POST_INIT(SystemExit)
2062 POST_INIT(KeyboardInterrupt)
2063 POST_INIT(ImportError)
2064 POST_INIT(EnvironmentError)
2065 POST_INIT(IOError)
2066 POST_INIT(OSError)
2067#ifdef MS_WINDOWS
2068 POST_INIT(WindowsError)
2069#endif
2070#ifdef __VMS
2071 POST_INIT(VMSError)
2072#endif
2073 POST_INIT(EOFError)
2074 POST_INIT(RuntimeError)
2075 POST_INIT(NotImplementedError)
2076 POST_INIT(NameError)
2077 POST_INIT(UnboundLocalError)
2078 POST_INIT(AttributeError)
2079 POST_INIT(SyntaxError)
2080 POST_INIT(IndentationError)
2081 POST_INIT(TabError)
2082 POST_INIT(LookupError)
2083 POST_INIT(IndexError)
2084 POST_INIT(KeyError)
2085 POST_INIT(ValueError)
2086 POST_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002087 POST_INIT(UnicodeEncodeError)
2088 POST_INIT(UnicodeDecodeError)
2089 POST_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002090 POST_INIT(AssertionError)
2091 POST_INIT(ArithmeticError)
2092 POST_INIT(FloatingPointError)
2093 POST_INIT(OverflowError)
2094 POST_INIT(ZeroDivisionError)
2095 POST_INIT(SystemError)
2096 POST_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00002097 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002098 POST_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00002099 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002100 POST_INIT(Warning)
2101 POST_INIT(UserWarning)
2102 POST_INIT(DeprecationWarning)
2103 POST_INIT(PendingDeprecationWarning)
2104 POST_INIT(SyntaxWarning)
2105 POST_INIT(RuntimeWarning)
2106 POST_INIT(FutureWarning)
2107 POST_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002108 POST_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00002109 POST_INIT(BytesWarning)
Georg Brandl08be72d2010-10-24 15:11:22 +00002110 POST_INIT(ResourceWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002111
Antoine Pitrou07e20ef2010-10-28 22:56:58 +00002112 preallocate_memerrors();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002113
Antoine Pitrou1c7ade52012-01-18 16:13:31 +01002114 if (!PyExc_RecursionErrorInst) {
2115 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2116 if (!PyExc_RecursionErrorInst)
2117 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2118 "recursion errors");
2119 else {
2120 PyBaseExceptionObject *err_inst =
2121 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2122 PyObject *args_tuple;
2123 PyObject *exc_message;
2124 exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
2125 if (!exc_message)
2126 Py_FatalError("cannot allocate argument for RuntimeError "
2127 "pre-allocation");
2128 args_tuple = PyTuple_Pack(1, exc_message);
2129 if (!args_tuple)
2130 Py_FatalError("cannot allocate tuple for RuntimeError "
2131 "pre-allocation");
2132 Py_DECREF(exc_message);
2133 if (BaseException_init(err_inst, args_tuple, NULL))
2134 Py_FatalError("init of pre-allocated RuntimeError failed");
2135 Py_DECREF(args_tuple);
2136 }
Thomas Wouters89d996e2007-09-08 17:39:28 +00002137 }
Benjamin Petersonefe7c9d2012-02-10 08:46:54 -05002138 Py_DECREF(bltinmod);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002139}
2140
2141void
2142_PyExc_Fini(void)
2143{
Benjamin Peterson78565b22009-06-28 19:19:51 +00002144 Py_CLEAR(PyExc_RecursionErrorInst);
Antoine Pitrou07e20ef2010-10-28 22:56:58 +00002145 free_preallocated_memerrors();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002146}