blob: 9daa12a4e97163cf622c568a8630503cb30733c3 [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);
Benjamin Peterson90b13582012-02-03 19:22:31 -0500216 if (!seq)
217 return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000218 Py_CLEAR(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000219 self->args = seq;
220 return 0;
221}
222
Collin Winter828f04a2007-08-31 00:04:24 +0000223static PyObject *
224BaseException_get_tb(PyBaseExceptionObject *self)
225{
226 if (self->traceback == NULL) {
227 Py_INCREF(Py_None);
228 return Py_None;
229 }
230 Py_INCREF(self->traceback);
231 return self->traceback;
232}
233
234static int
235BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb)
236{
237 if (tb == NULL) {
238 PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted");
239 return -1;
240 }
241 else if (!(tb == Py_None || PyTraceBack_Check(tb))) {
242 PyErr_SetString(PyExc_TypeError,
243 "__traceback__ must be a traceback or None");
244 return -1;
245 }
246
247 Py_XINCREF(tb);
248 Py_XDECREF(self->traceback);
249 self->traceback = tb;
250 return 0;
251}
252
Georg Brandlab6f2f62009-03-31 04:16:10 +0000253static PyObject *
254BaseException_get_context(PyObject *self) {
255 PyObject *res = PyException_GetContext(self);
Benjamin Peterson90b13582012-02-03 19:22:31 -0500256 if (res)
257 return res; /* new reference already returned above */
Georg Brandlab6f2f62009-03-31 04:16:10 +0000258 Py_RETURN_NONE;
259}
260
261static int
262BaseException_set_context(PyObject *self, PyObject *arg) {
263 if (arg == NULL) {
264 PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted");
265 return -1;
266 } else if (arg == Py_None) {
267 arg = NULL;
268 } else if (!PyExceptionInstance_Check(arg)) {
269 PyErr_SetString(PyExc_TypeError, "exception context must be None "
270 "or derive from BaseException");
271 return -1;
272 } else {
273 /* PyException_SetContext steals this reference */
274 Py_INCREF(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 }
Georg Brandlab6f2f62009-03-31 04:16:10 +0000276 PyException_SetContext(self, arg);
277 return 0;
278}
279
280static PyObject *
281BaseException_get_cause(PyObject *self) {
282 PyObject *res = PyException_GetCause(self);
Benjamin Peterson90b13582012-02-03 19:22:31 -0500283 if (res)
284 return res; /* new reference already returned above */
Georg Brandlab6f2f62009-03-31 04:16:10 +0000285 Py_RETURN_NONE;
286}
287
288static int
289BaseException_set_cause(PyObject *self, PyObject *arg) {
290 if (arg == NULL) {
291 PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted");
292 return -1;
293 } else if (arg == Py_None) {
294 arg = NULL;
295 } else if (!PyExceptionInstance_Check(arg)) {
296 PyErr_SetString(PyExc_TypeError, "exception cause must be None "
297 "or derive from BaseException");
298 return -1;
299 } else {
300 /* PyException_SetCause steals this reference */
301 Py_INCREF(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 }
Georg Brandlab6f2f62009-03-31 04:16:10 +0000303 PyException_SetCause(self, arg);
304 return 0;
305}
306
Guido van Rossum360e4b82007-05-14 22:51:27 +0000307
Thomas Wouters477c8d52006-05-27 19:21:47 +0000308static PyGetSetDef BaseException_getset[] = {
309 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
310 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Collin Winter828f04a2007-08-31 00:04:24 +0000311 {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb},
Georg Brandlab6f2f62009-03-31 04:16:10 +0000312 {"__context__", (getter)BaseException_get_context,
313 (setter)BaseException_set_context, PyDoc_STR("exception context")},
314 {"__cause__", (getter)BaseException_get_cause,
315 (setter)BaseException_set_cause, PyDoc_STR("exception cause")},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000316 {NULL},
317};
318
319
Collin Winter828f04a2007-08-31 00:04:24 +0000320PyObject *
321PyException_GetTraceback(PyObject *self) {
322 PyBaseExceptionObject *base_self = (PyBaseExceptionObject *)self;
323 Py_XINCREF(base_self->traceback);
324 return base_self->traceback;
325}
326
327
328int
329PyException_SetTraceback(PyObject *self, PyObject *tb) {
330 return BaseException_set_tb((PyBaseExceptionObject *)self, tb);
331}
332
333PyObject *
334PyException_GetCause(PyObject *self) {
335 PyObject *cause = ((PyBaseExceptionObject *)self)->cause;
336 Py_XINCREF(cause);
337 return cause;
338}
339
340/* Steals a reference to cause */
341void
342PyException_SetCause(PyObject *self, PyObject *cause) {
343 PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause;
344 ((PyBaseExceptionObject *)self)->cause = cause;
345 Py_XDECREF(old_cause);
346}
347
348PyObject *
349PyException_GetContext(PyObject *self) {
350 PyObject *context = ((PyBaseExceptionObject *)self)->context;
351 Py_XINCREF(context);
352 return context;
353}
354
355/* Steals a reference to context */
356void
357PyException_SetContext(PyObject *self, PyObject *context) {
358 PyObject *old_context = ((PyBaseExceptionObject *)self)->context;
359 ((PyBaseExceptionObject *)self)->context = context;
360 Py_XDECREF(old_context);
361}
362
363
Thomas Wouters477c8d52006-05-27 19:21:47 +0000364static PyTypeObject _PyExc_BaseException = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000365 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +0000366 "BaseException", /*tp_name*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000367 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
368 0, /*tp_itemsize*/
369 (destructor)BaseException_dealloc, /*tp_dealloc*/
370 0, /*tp_print*/
371 0, /*tp_getattr*/
372 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000373 0, /* tp_reserved; */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000374 (reprfunc)BaseException_repr, /*tp_repr*/
375 0, /*tp_as_number*/
Brett Cannonba7bf492007-02-27 00:15:55 +0000376 0, /*tp_as_sequence*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000377 0, /*tp_as_mapping*/
378 0, /*tp_hash */
379 0, /*tp_call*/
380 (reprfunc)BaseException_str, /*tp_str*/
381 PyObject_GenericGetAttr, /*tp_getattro*/
382 PyObject_GenericSetAttr, /*tp_setattro*/
383 0, /*tp_as_buffer*/
Thomas Wouters27d517b2007-02-25 20:39:11 +0000384 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000386 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
387 (traverseproc)BaseException_traverse, /* tp_traverse */
388 (inquiry)BaseException_clear, /* tp_clear */
389 0, /* tp_richcompare */
390 0, /* tp_weaklistoffset */
391 0, /* tp_iter */
392 0, /* tp_iternext */
393 BaseException_methods, /* tp_methods */
Georg Brandlab6f2f62009-03-31 04:16:10 +0000394 0, /* tp_members */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000395 BaseException_getset, /* tp_getset */
396 0, /* tp_base */
397 0, /* tp_dict */
398 0, /* tp_descr_get */
399 0, /* tp_descr_set */
400 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
401 (initproc)BaseException_init, /* tp_init */
402 0, /* tp_alloc */
403 BaseException_new, /* tp_new */
404};
405/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
406from the previous implmentation and also allowing Python objects to be used
407in the API */
408PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
409
410/* note these macros omit the last semicolon so the macro invocation may
411 * include it and not look strange.
412 */
413#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
414static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000415 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000416 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000417 sizeof(PyBaseExceptionObject), \
418 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
419 0, 0, 0, 0, 0, 0, 0, \
420 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
421 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
422 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
423 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
424 (initproc)BaseException_init, 0, BaseException_new,\
425}; \
426PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
427
428#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
429static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000430 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000431 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000432 sizeof(Py ## EXCSTORE ## Object), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000433 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000434 0, 0, 0, 0, 0, \
435 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000436 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
437 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000438 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000439 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000440}; \
441PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
442
443#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
444static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000445 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000446 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000447 sizeof(Py ## EXCSTORE ## Object), 0, \
448 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
449 (reprfunc)EXCSTR, 0, 0, 0, \
450 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
451 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
452 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
453 EXCMEMBERS, 0, &_ ## EXCBASE, \
454 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000455 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000456}; \
457PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
458
459
460/*
461 * Exception extends BaseException
462 */
463SimpleExtendsException(PyExc_BaseException, Exception,
464 "Common base class for all non-exit exceptions.");
465
466
467/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000468 * TypeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000469 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000470SimpleExtendsException(PyExc_Exception, TypeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000471 "Inappropriate argument type.");
472
473
474/*
475 * StopIteration extends Exception
476 */
477SimpleExtendsException(PyExc_Exception, StopIteration,
Georg Brandla18af4e2007-04-21 15:47:16 +0000478 "Signal the end from iterator.__next__().");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000479
480
481/*
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000482 * GeneratorExit extends BaseException
Thomas Wouters477c8d52006-05-27 19:21:47 +0000483 */
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000484SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000485 "Request that a generator exit.");
486
487
488/*
489 * SystemExit extends BaseException
490 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000491
492static int
493SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
494{
495 Py_ssize_t size = PyTuple_GET_SIZE(args);
496
497 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
498 return -1;
499
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000500 if (size == 0)
501 return 0;
502 Py_CLEAR(self->code);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000503 if (size == 1)
504 self->code = PyTuple_GET_ITEM(args, 0);
505 else if (size > 1)
506 self->code = args;
507 Py_INCREF(self->code);
508 return 0;
509}
510
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000511static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000512SystemExit_clear(PySystemExitObject *self)
513{
514 Py_CLEAR(self->code);
515 return BaseException_clear((PyBaseExceptionObject *)self);
516}
517
518static void
519SystemExit_dealloc(PySystemExitObject *self)
520{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000521 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000522 SystemExit_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000523 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000524}
525
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000526static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000527SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
528{
529 Py_VISIT(self->code);
530 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
531}
532
533static PyMemberDef SystemExit_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000534 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
535 PyDoc_STR("exception code")},
536 {NULL} /* Sentinel */
537};
538
539ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
540 SystemExit_dealloc, 0, SystemExit_members, 0,
541 "Request to exit from the interpreter.");
542
543/*
544 * KeyboardInterrupt extends BaseException
545 */
546SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
547 "Program interrupted by user.");
548
549
550/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000551 * ImportError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000552 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000553SimpleExtendsException(PyExc_Exception, ImportError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000554 "Import can't find module, or can't find name in module.");
555
556
557/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000558 * EnvironmentError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000559 */
560
Thomas Wouters477c8d52006-05-27 19:21:47 +0000561/* Where a function has a single filename, such as open() or some
562 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
563 * called, giving a third argument which is the filename. But, so
564 * that old code using in-place unpacking doesn't break, e.g.:
565 *
566 * except IOError, (errno, strerror):
567 *
568 * we hack args so that it only contains two items. This also
569 * means we need our own __str__() which prints out the filename
570 * when it was supplied.
571 */
572static int
573EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
574 PyObject *kwds)
575{
576 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
577 PyObject *subslice = NULL;
578
579 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
580 return -1;
581
Thomas Wouters89f507f2006-12-13 04:49:30 +0000582 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000583 return 0;
584 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000585
586 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000587 &myerrno, &strerror, &filename)) {
588 return -1;
589 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000590 Py_CLEAR(self->myerrno); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000591 self->myerrno = myerrno;
592 Py_INCREF(self->myerrno);
593
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000594 Py_CLEAR(self->strerror); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000595 self->strerror = strerror;
596 Py_INCREF(self->strerror);
597
598 /* self->filename will remain Py_None otherwise */
599 if (filename != NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000600 Py_CLEAR(self->filename); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000601 self->filename = filename;
602 Py_INCREF(self->filename);
603
604 subslice = PyTuple_GetSlice(args, 0, 2);
605 if (!subslice)
606 return -1;
607
608 Py_DECREF(self->args); /* replacing args */
609 self->args = subslice;
610 }
611 return 0;
612}
613
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000614static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000615EnvironmentError_clear(PyEnvironmentErrorObject *self)
616{
617 Py_CLEAR(self->myerrno);
618 Py_CLEAR(self->strerror);
619 Py_CLEAR(self->filename);
620 return BaseException_clear((PyBaseExceptionObject *)self);
621}
622
623static void
624EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
625{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000626 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000627 EnvironmentError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000628 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000629}
630
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000631static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000632EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
633 void *arg)
634{
635 Py_VISIT(self->myerrno);
636 Py_VISIT(self->strerror);
637 Py_VISIT(self->filename);
638 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
639}
640
641static PyObject *
642EnvironmentError_str(PyEnvironmentErrorObject *self)
643{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000644 if (self->filename)
645 return PyUnicode_FromFormat("[Errno %S] %S: %R",
646 self->myerrno ? self->myerrno: Py_None,
647 self->strerror ? self->strerror: Py_None,
648 self->filename);
649 else if (self->myerrno && self->strerror)
650 return PyUnicode_FromFormat("[Errno %S] %S",
651 self->myerrno ? self->myerrno: Py_None,
652 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000654 return BaseException_str((PyBaseExceptionObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000655}
656
657static PyMemberDef EnvironmentError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
659 PyDoc_STR("exception errno")},
660 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
661 PyDoc_STR("exception strerror")},
662 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
663 PyDoc_STR("exception filename")},
664 {NULL} /* Sentinel */
665};
666
667
668static PyObject *
669EnvironmentError_reduce(PyEnvironmentErrorObject *self)
670{
671 PyObject *args = self->args;
672 PyObject *res = NULL, *tmp;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000673
Thomas Wouters477c8d52006-05-27 19:21:47 +0000674 /* self->args is only the first two real arguments if there was a
675 * file name given to EnvironmentError. */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000676 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677 args = PyTuple_New(3);
Benjamin Peterson90b13582012-02-03 19:22:31 -0500678 if (!args)
679 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000680
681 tmp = PyTuple_GET_ITEM(self->args, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000682 Py_INCREF(tmp);
683 PyTuple_SET_ITEM(args, 0, tmp);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000684
685 tmp = PyTuple_GET_ITEM(self->args, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000686 Py_INCREF(tmp);
687 PyTuple_SET_ITEM(args, 1, tmp);
688
689 Py_INCREF(self->filename);
690 PyTuple_SET_ITEM(args, 2, self->filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000691 } else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000692 Py_INCREF(args);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000693
694 if (self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000695 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000696 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000697 res = PyTuple_Pack(2, Py_TYPE(self), args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000698 Py_DECREF(args);
699 return res;
700}
701
702
703static PyMethodDef EnvironmentError_methods[] = {
704 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
705 {NULL}
706};
707
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000708ComplexExtendsException(PyExc_Exception, EnvironmentError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000709 EnvironmentError, EnvironmentError_dealloc,
710 EnvironmentError_methods, EnvironmentError_members,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000711 EnvironmentError_str,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000712 "Base class for I/O related errors.");
713
714
715/*
716 * IOError extends EnvironmentError
717 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000718MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000719 EnvironmentError, "I/O operation failed.");
720
721
722/*
723 * OSError extends EnvironmentError
724 */
725MiddlingExtendsException(PyExc_EnvironmentError, OSError,
726 EnvironmentError, "OS system call failed.");
727
728
729/*
730 * WindowsError extends OSError
731 */
732#ifdef MS_WINDOWS
733#include "errmap.h"
734
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000735static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000736WindowsError_clear(PyWindowsErrorObject *self)
737{
738 Py_CLEAR(self->myerrno);
739 Py_CLEAR(self->strerror);
740 Py_CLEAR(self->filename);
741 Py_CLEAR(self->winerror);
742 return BaseException_clear((PyBaseExceptionObject *)self);
743}
744
745static void
746WindowsError_dealloc(PyWindowsErrorObject *self)
747{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000748 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000749 WindowsError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000750 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000751}
752
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000753static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000754WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
755{
756 Py_VISIT(self->myerrno);
757 Py_VISIT(self->strerror);
758 Py_VISIT(self->filename);
759 Py_VISIT(self->winerror);
760 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
761}
762
Thomas Wouters477c8d52006-05-27 19:21:47 +0000763static int
764WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
765{
766 PyObject *o_errcode = NULL;
767 long errcode;
768 long posix_errno;
769
770 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
771 == -1)
772 return -1;
773
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000774 if (self->myerrno == NULL)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000775 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000776
777 /* Set errno to the POSIX errno, and winerror to the Win32
778 error code. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000779 errcode = PyLong_AsLong(self->myerrno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000780 if (errcode == -1 && PyErr_Occurred())
781 return -1;
782 posix_errno = winerror_to_errno(errcode);
783
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000784 Py_CLEAR(self->winerror);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000785 self->winerror = self->myerrno;
786
Christian Heimes217cfd12007-12-02 14:31:20 +0000787 o_errcode = PyLong_FromLong(posix_errno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000788 if (!o_errcode)
789 return -1;
790
791 self->myerrno = o_errcode;
792
793 return 0;
794}
795
796
797static PyObject *
798WindowsError_str(PyWindowsErrorObject *self)
799{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000800 if (self->filename)
801 return PyUnicode_FromFormat("[Error %S] %S: %R",
802 self->winerror ? self->winerror: Py_None,
803 self->strerror ? self->strerror: Py_None,
804 self->filename);
805 else if (self->winerror && self->strerror)
806 return PyUnicode_FromFormat("[Error %S] %S",
807 self->winerror ? self->winerror: Py_None,
808 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000809 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000810 return EnvironmentError_str((PyEnvironmentErrorObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000811}
812
813static PyMemberDef WindowsError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000814 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
815 PyDoc_STR("POSIX exception code")},
816 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
817 PyDoc_STR("exception strerror")},
818 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
819 PyDoc_STR("exception filename")},
820 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
821 PyDoc_STR("Win32 exception code")},
822 {NULL} /* Sentinel */
823};
824
825ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
826 WindowsError_dealloc, 0, WindowsError_members,
827 WindowsError_str, "MS-Windows OS system call failed.");
828
829#endif /* MS_WINDOWS */
830
831
832/*
833 * VMSError extends OSError (I think)
834 */
835#ifdef __VMS
836MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
837 "OpenVMS OS system call failed.");
838#endif
839
840
841/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000842 * EOFError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000843 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000844SimpleExtendsException(PyExc_Exception, EOFError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000845 "Read beyond end of file.");
846
847
848/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000849 * RuntimeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000850 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000851SimpleExtendsException(PyExc_Exception, RuntimeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000852 "Unspecified run-time error.");
853
854
855/*
856 * NotImplementedError extends RuntimeError
857 */
858SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
859 "Method or function hasn't been implemented yet.");
860
861/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000862 * NameError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000863 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000864SimpleExtendsException(PyExc_Exception, NameError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000865 "Name not found globally.");
866
867/*
868 * UnboundLocalError extends NameError
869 */
870SimpleExtendsException(PyExc_NameError, UnboundLocalError,
871 "Local name referenced but not bound to a value.");
872
873/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000874 * AttributeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000875 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000876SimpleExtendsException(PyExc_Exception, AttributeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000877 "Attribute not found.");
878
879
880/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000881 * SyntaxError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000882 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000883
884static int
885SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
886{
887 PyObject *info = NULL;
888 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
889
890 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
891 return -1;
892
893 if (lenargs >= 1) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000894 Py_CLEAR(self->msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000895 self->msg = PyTuple_GET_ITEM(args, 0);
896 Py_INCREF(self->msg);
897 }
898 if (lenargs == 2) {
899 info = PyTuple_GET_ITEM(args, 1);
900 info = PySequence_Tuple(info);
Benjamin Peterson90b13582012-02-03 19:22:31 -0500901 if (!info)
902 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000903
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000904 if (PyTuple_GET_SIZE(info) != 4) {
905 /* not a very good error message, but it's what Python 2.4 gives */
906 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
907 Py_DECREF(info);
908 return -1;
909 }
910
911 Py_CLEAR(self->filename);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000912 self->filename = PyTuple_GET_ITEM(info, 0);
913 Py_INCREF(self->filename);
914
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000915 Py_CLEAR(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000916 self->lineno = PyTuple_GET_ITEM(info, 1);
917 Py_INCREF(self->lineno);
918
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000919 Py_CLEAR(self->offset);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920 self->offset = PyTuple_GET_ITEM(info, 2);
921 Py_INCREF(self->offset);
922
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000923 Py_CLEAR(self->text);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000924 self->text = PyTuple_GET_ITEM(info, 3);
925 Py_INCREF(self->text);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000926
927 Py_DECREF(info);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928 }
929 return 0;
930}
931
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000932static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000933SyntaxError_clear(PySyntaxErrorObject *self)
934{
935 Py_CLEAR(self->msg);
936 Py_CLEAR(self->filename);
937 Py_CLEAR(self->lineno);
938 Py_CLEAR(self->offset);
939 Py_CLEAR(self->text);
940 Py_CLEAR(self->print_file_and_line);
941 return BaseException_clear((PyBaseExceptionObject *)self);
942}
943
944static void
945SyntaxError_dealloc(PySyntaxErrorObject *self)
946{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000947 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000948 SyntaxError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000949 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000950}
951
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000952static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
954{
955 Py_VISIT(self->msg);
956 Py_VISIT(self->filename);
957 Py_VISIT(self->lineno);
958 Py_VISIT(self->offset);
959 Py_VISIT(self->text);
960 Py_VISIT(self->print_file_and_line);
961 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
962}
963
964/* This is called "my_basename" instead of just "basename" to avoid name
965 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
966 defined, and Python does define that. */
Victor Stinner6237daf2010-04-28 17:26:19 +0000967static PyObject*
968my_basename(PyObject *name)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000969{
Victor Stinner6237daf2010-04-28 17:26:19 +0000970 Py_UNICODE *unicode;
971 Py_ssize_t i, size, offset;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000972
Victor Stinner6237daf2010-04-28 17:26:19 +0000973 unicode = PyUnicode_AS_UNICODE(name);
974 size = PyUnicode_GET_SIZE(name);
975 offset = 0;
976 for(i=0; i < size; i++) {
977 if (unicode[i] == SEP)
978 offset = i + 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000979 }
Victor Stinner6237daf2010-04-28 17:26:19 +0000980 if (offset != 0) {
981 return PyUnicode_FromUnicode(
982 PyUnicode_AS_UNICODE(name) + offset,
983 size - offset);
984 } else {
985 Py_INCREF(name);
986 return name;
987 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000988}
989
990
991static PyObject *
992SyntaxError_str(PySyntaxErrorObject *self)
993{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000994 int have_lineno = 0;
Victor Stinner6237daf2010-04-28 17:26:19 +0000995 PyObject *filename;
996 PyObject *result;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000997 /* Below, we always ignore overflow errors, just printing -1.
998 Still, we cannot allow an OverflowError to be raised, so
999 we need to call PyLong_AsLongAndOverflow. */
1000 int overflow;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001
1002 /* XXX -- do all the additional formatting with filename and
1003 lineno here */
1004
Neal Norwitzed2b7392007-08-26 04:51:10 +00001005 if (self->filename && PyUnicode_Check(self->filename)) {
Victor Stinner6237daf2010-04-28 17:26:19 +00001006 filename = my_basename(self->filename);
1007 if (filename == NULL)
1008 return NULL;
1009 } else {
1010 filename = NULL;
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001011 }
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001012 have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001013
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001014 if (!filename && !have_lineno)
Thomas Heller519a0422007-11-15 20:48:54 +00001015 return PyObject_Str(self->msg ? self->msg : Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001016
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001017 if (filename && have_lineno)
Victor Stinner6237daf2010-04-28 17:26:19 +00001018 result = PyUnicode_FromFormat("%S (%U, line %ld)",
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001019 self->msg ? self->msg : Py_None,
Victor Stinner6237daf2010-04-28 17:26:19 +00001020 filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001022 else if (filename)
Victor Stinner6237daf2010-04-28 17:26:19 +00001023 result = PyUnicode_FromFormat("%S (%U)",
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001024 self->msg ? self->msg : Py_None,
Victor Stinner6237daf2010-04-28 17:26:19 +00001025 filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001026 else /* only have_lineno */
Victor Stinner6237daf2010-04-28 17:26:19 +00001027 result = PyUnicode_FromFormat("%S (line %ld)",
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001028 self->msg ? self->msg : Py_None,
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001029 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Victor Stinner6237daf2010-04-28 17:26:19 +00001030 Py_XDECREF(filename);
1031 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032}
1033
1034static PyMemberDef SyntaxError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001035 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1036 PyDoc_STR("exception msg")},
1037 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1038 PyDoc_STR("exception filename")},
1039 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1040 PyDoc_STR("exception lineno")},
1041 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1042 PyDoc_STR("exception offset")},
1043 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1044 PyDoc_STR("exception text")},
1045 {"print_file_and_line", T_OBJECT,
1046 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1047 PyDoc_STR("exception print_file_and_line")},
1048 {NULL} /* Sentinel */
1049};
1050
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001051ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001052 SyntaxError_dealloc, 0, SyntaxError_members,
1053 SyntaxError_str, "Invalid syntax.");
1054
1055
1056/*
1057 * IndentationError extends SyntaxError
1058 */
1059MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
1060 "Improper indentation.");
1061
1062
1063/*
1064 * TabError extends IndentationError
1065 */
1066MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
1067 "Improper mixture of spaces and tabs.");
1068
1069
1070/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001071 * LookupError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001072 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001073SimpleExtendsException(PyExc_Exception, LookupError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001074 "Base class for lookup errors.");
1075
1076
1077/*
1078 * IndexError extends LookupError
1079 */
1080SimpleExtendsException(PyExc_LookupError, IndexError,
1081 "Sequence index out of range.");
1082
1083
1084/*
1085 * KeyError extends LookupError
1086 */
1087static PyObject *
1088KeyError_str(PyBaseExceptionObject *self)
1089{
1090 /* If args is a tuple of exactly one item, apply repr to args[0].
1091 This is done so that e.g. the exception raised by {}[''] prints
1092 KeyError: ''
1093 rather than the confusing
1094 KeyError
1095 alone. The downside is that if KeyError is raised with an explanatory
1096 string, that string will be displayed in quotes. Too bad.
1097 If args is anything else, use the default BaseException__str__().
1098 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001099 if (PyTuple_GET_SIZE(self->args) == 1) {
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001100 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001101 }
1102 return BaseException_str(self);
1103}
1104
1105ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
1106 0, 0, 0, KeyError_str, "Mapping key not found.");
1107
1108
1109/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001110 * ValueError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001111 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001112SimpleExtendsException(PyExc_Exception, ValueError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001113 "Inappropriate argument value (of correct type).");
1114
1115/*
1116 * UnicodeError extends ValueError
1117 */
1118
1119SimpleExtendsException(PyExc_ValueError, UnicodeError,
1120 "Unicode related error.");
1121
Thomas Wouters477c8d52006-05-27 19:21:47 +00001122static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001123get_string(PyObject *attr, const char *name)
Walter Dörwald612344f2007-05-04 19:28:21 +00001124{
1125 if (!attr) {
1126 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1127 return NULL;
1128 }
1129
Christian Heimes72b710a2008-05-26 13:28:38 +00001130 if (!PyBytes_Check(attr)) {
Walter Dörwald612344f2007-05-04 19:28:21 +00001131 PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
1132 return NULL;
1133 }
1134 Py_INCREF(attr);
1135 return attr;
1136}
1137
1138static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001139get_unicode(PyObject *attr, const char *name)
1140{
1141 if (!attr) {
1142 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1143 return NULL;
1144 }
1145
1146 if (!PyUnicode_Check(attr)) {
1147 PyErr_Format(PyExc_TypeError,
1148 "%.200s attribute must be unicode", name);
1149 return NULL;
1150 }
1151 Py_INCREF(attr);
1152 return attr;
1153}
1154
Walter Dörwaldd2034312007-05-18 16:29:38 +00001155static int
1156set_unicodefromstring(PyObject **attr, const char *value)
1157{
1158 PyObject *obj = PyUnicode_FromString(value);
1159 if (!obj)
1160 return -1;
1161 Py_CLEAR(*attr);
1162 *attr = obj;
1163 return 0;
1164}
1165
Thomas Wouters477c8d52006-05-27 19:21:47 +00001166PyObject *
1167PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1168{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001169 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001170}
1171
1172PyObject *
1173PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1174{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001175 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001176}
1177
1178PyObject *
1179PyUnicodeEncodeError_GetObject(PyObject *exc)
1180{
1181 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1182}
1183
1184PyObject *
1185PyUnicodeDecodeError_GetObject(PyObject *exc)
1186{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001187 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001188}
1189
1190PyObject *
1191PyUnicodeTranslateError_GetObject(PyObject *exc)
1192{
1193 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1194}
1195
1196int
1197PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1198{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001199 Py_ssize_t size;
1200 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1201 "object");
1202 if (!obj)
1203 return -1;
1204 *start = ((PyUnicodeErrorObject *)exc)->start;
1205 size = PyUnicode_GET_SIZE(obj);
1206 if (*start<0)
1207 *start = 0; /*XXX check for values <0*/
1208 if (*start>=size)
1209 *start = size-1;
1210 Py_DECREF(obj);
1211 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001212}
1213
1214
1215int
1216PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1217{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001218 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001219 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001220 if (!obj)
1221 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001222 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001223 *start = ((PyUnicodeErrorObject *)exc)->start;
1224 if (*start<0)
1225 *start = 0;
1226 if (*start>=size)
1227 *start = size-1;
1228 Py_DECREF(obj);
1229 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001230}
1231
1232
1233int
1234PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1235{
1236 return PyUnicodeEncodeError_GetStart(exc, start);
1237}
1238
1239
1240int
1241PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1242{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001243 ((PyUnicodeErrorObject *)exc)->start = start;
1244 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001245}
1246
1247
1248int
1249PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1250{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001251 ((PyUnicodeErrorObject *)exc)->start = start;
1252 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001253}
1254
1255
1256int
1257PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1258{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001259 ((PyUnicodeErrorObject *)exc)->start = start;
1260 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001261}
1262
1263
1264int
1265PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1266{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001267 Py_ssize_t size;
1268 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1269 "object");
1270 if (!obj)
1271 return -1;
1272 *end = ((PyUnicodeErrorObject *)exc)->end;
1273 size = PyUnicode_GET_SIZE(obj);
1274 if (*end<1)
1275 *end = 1;
1276 if (*end>size)
1277 *end = size;
1278 Py_DECREF(obj);
1279 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001280}
1281
1282
1283int
1284PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1285{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001286 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001287 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001288 if (!obj)
1289 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001290 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001291 *end = ((PyUnicodeErrorObject *)exc)->end;
1292 if (*end<1)
1293 *end = 1;
1294 if (*end>size)
1295 *end = size;
1296 Py_DECREF(obj);
1297 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001298}
1299
1300
1301int
1302PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1303{
1304 return PyUnicodeEncodeError_GetEnd(exc, start);
1305}
1306
1307
1308int
1309PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1310{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001311 ((PyUnicodeErrorObject *)exc)->end = end;
1312 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001313}
1314
1315
1316int
1317PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1318{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001319 ((PyUnicodeErrorObject *)exc)->end = end;
1320 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001321}
1322
1323
1324int
1325PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1326{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001327 ((PyUnicodeErrorObject *)exc)->end = end;
1328 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001329}
1330
1331PyObject *
1332PyUnicodeEncodeError_GetReason(PyObject *exc)
1333{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001334 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001335}
1336
1337
1338PyObject *
1339PyUnicodeDecodeError_GetReason(PyObject *exc)
1340{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001341 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001342}
1343
1344
1345PyObject *
1346PyUnicodeTranslateError_GetReason(PyObject *exc)
1347{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001348 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001349}
1350
1351
1352int
1353PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1354{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001355 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1356 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001357}
1358
1359
1360int
1361PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1362{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001363 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1364 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001365}
1366
1367
1368int
1369PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1370{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001371 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1372 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001373}
1374
1375
Thomas Wouters477c8d52006-05-27 19:21:47 +00001376static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001377UnicodeError_clear(PyUnicodeErrorObject *self)
1378{
1379 Py_CLEAR(self->encoding);
1380 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001381 Py_CLEAR(self->reason);
1382 return BaseException_clear((PyBaseExceptionObject *)self);
1383}
1384
1385static void
1386UnicodeError_dealloc(PyUnicodeErrorObject *self)
1387{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001388 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001389 UnicodeError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +00001390 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001391}
1392
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001393static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001394UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1395{
1396 Py_VISIT(self->encoding);
1397 Py_VISIT(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001398 Py_VISIT(self->reason);
1399 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1400}
1401
1402static PyMemberDef UnicodeError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001403 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1404 PyDoc_STR("exception encoding")},
1405 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1406 PyDoc_STR("exception object")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001407 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001408 PyDoc_STR("exception start")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001409 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001410 PyDoc_STR("exception end")},
1411 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1412 PyDoc_STR("exception reason")},
1413 {NULL} /* Sentinel */
1414};
1415
1416
1417/*
1418 * UnicodeEncodeError extends UnicodeError
1419 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001420
1421static int
1422UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1423{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001424 PyUnicodeErrorObject *err;
1425
Thomas Wouters477c8d52006-05-27 19:21:47 +00001426 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1427 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001428
1429 err = (PyUnicodeErrorObject *)self;
1430
1431 Py_CLEAR(err->encoding);
1432 Py_CLEAR(err->object);
1433 Py_CLEAR(err->reason);
1434
1435 if (!PyArg_ParseTuple(args, "O!O!nnO!",
1436 &PyUnicode_Type, &err->encoding,
1437 &PyUnicode_Type, &err->object,
1438 &err->start,
1439 &err->end,
1440 &PyUnicode_Type, &err->reason)) {
1441 err->encoding = err->object = err->reason = NULL;
1442 return -1;
1443 }
1444
1445 Py_INCREF(err->encoding);
1446 Py_INCREF(err->object);
1447 Py_INCREF(err->reason);
1448
1449 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001450}
1451
1452static PyObject *
1453UnicodeEncodeError_str(PyObject *self)
1454{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001455 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith0facd772010-02-24 15:42:29 +00001456 PyObject *result = NULL;
1457 PyObject *reason_str = NULL;
1458 PyObject *encoding_str = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001459
Eric Smith0facd772010-02-24 15:42:29 +00001460 /* Get reason and encoding as strings, which they might not be if
1461 they've been modified after we were contructed. */
1462 reason_str = PyObject_Str(uself->reason);
1463 if (reason_str == NULL)
1464 goto done;
1465 encoding_str = PyObject_Str(uself->encoding);
1466 if (encoding_str == NULL)
1467 goto done;
1468
1469 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001470 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001471 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001472 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001473 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001474 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001475 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001476 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001477 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
Eric Smith0facd772010-02-24 15:42:29 +00001478 result = PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001479 fmt,
Eric Smith0facd772010-02-24 15:42:29 +00001480 encoding_str,
Walter Dörwald787b03b2007-06-05 13:29:29 +00001481 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001482 uself->start,
Eric Smith0facd772010-02-24 15:42:29 +00001483 reason_str);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001484 }
Eric Smith0facd772010-02-24 15:42:29 +00001485 else {
1486 result = PyUnicode_FromFormat(
1487 "'%U' codec can't encode characters in position %zd-%zd: %U",
1488 encoding_str,
1489 uself->start,
1490 uself->end-1,
1491 reason_str);
1492 }
1493done:
1494 Py_XDECREF(reason_str);
1495 Py_XDECREF(encoding_str);
1496 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001497}
1498
1499static PyTypeObject _PyExc_UnicodeEncodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001500 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001501 "UnicodeEncodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001502 sizeof(PyUnicodeErrorObject), 0,
1503 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1504 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1505 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001506 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1507 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001508 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001509 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001510};
1511PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1512
1513PyObject *
1514PyUnicodeEncodeError_Create(
1515 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1516 Py_ssize_t start, Py_ssize_t end, const char *reason)
1517{
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001518 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001519 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001520}
1521
1522
1523/*
1524 * UnicodeDecodeError extends UnicodeError
1525 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001526
1527static int
1528UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1529{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001530 PyUnicodeErrorObject *ude;
1531 const char *data;
1532 Py_ssize_t size;
1533
Thomas Wouters477c8d52006-05-27 19:21:47 +00001534 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1535 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001536
1537 ude = (PyUnicodeErrorObject *)self;
1538
1539 Py_CLEAR(ude->encoding);
1540 Py_CLEAR(ude->object);
1541 Py_CLEAR(ude->reason);
1542
1543 if (!PyArg_ParseTuple(args, "O!OnnO!",
1544 &PyUnicode_Type, &ude->encoding,
1545 &ude->object,
1546 &ude->start,
1547 &ude->end,
1548 &PyUnicode_Type, &ude->reason)) {
1549 ude->encoding = ude->object = ude->reason = NULL;
1550 return -1;
1551 }
1552
Christian Heimes72b710a2008-05-26 13:28:38 +00001553 if (!PyBytes_Check(ude->object)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00001554 if (PyObject_AsReadBuffer(ude->object, (const void **)&data, &size)) {
1555 ude->encoding = ude->object = ude->reason = NULL;
1556 return -1;
1557 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001558 ude->object = PyBytes_FromStringAndSize(data, size);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001559 }
1560 else {
1561 Py_INCREF(ude->object);
1562 }
1563
1564 Py_INCREF(ude->encoding);
1565 Py_INCREF(ude->reason);
1566
1567 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001568}
1569
1570static PyObject *
1571UnicodeDecodeError_str(PyObject *self)
1572{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001573 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith0facd772010-02-24 15:42:29 +00001574 PyObject *result = NULL;
1575 PyObject *reason_str = NULL;
1576 PyObject *encoding_str = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001577
Eric Smith0facd772010-02-24 15:42:29 +00001578 /* Get reason and encoding as strings, which they might not be if
1579 they've been modified after we were contructed. */
1580 reason_str = PyObject_Str(uself->reason);
1581 if (reason_str == NULL)
1582 goto done;
1583 encoding_str = PyObject_Str(uself->encoding);
1584 if (encoding_str == NULL)
1585 goto done;
1586
1587 if (uself->start < PyBytes_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Christian Heimes72b710a2008-05-26 13:28:38 +00001588 int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
Eric Smith0facd772010-02-24 15:42:29 +00001589 result = PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001590 "'%U' codec can't decode byte 0x%02x in position %zd: %U",
Eric Smith0facd772010-02-24 15:42:29 +00001591 encoding_str,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001592 byte,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001593 uself->start,
Eric Smith0facd772010-02-24 15:42:29 +00001594 reason_str);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001595 }
Eric Smith0facd772010-02-24 15:42:29 +00001596 else {
1597 result = PyUnicode_FromFormat(
1598 "'%U' codec can't decode bytes in position %zd-%zd: %U",
1599 encoding_str,
1600 uself->start,
1601 uself->end-1,
1602 reason_str
1603 );
1604 }
1605done:
1606 Py_XDECREF(reason_str);
1607 Py_XDECREF(encoding_str);
1608 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001609}
1610
1611static PyTypeObject _PyExc_UnicodeDecodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001612 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001613 "UnicodeDecodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001614 sizeof(PyUnicodeErrorObject), 0,
1615 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1616 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1617 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001618 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1619 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001620 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001621 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001622};
1623PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1624
1625PyObject *
1626PyUnicodeDecodeError_Create(
1627 const char *encoding, const char *object, Py_ssize_t length,
1628 Py_ssize_t start, Py_ssize_t end, const char *reason)
1629{
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001630 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001631 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001632}
1633
1634
1635/*
1636 * UnicodeTranslateError extends UnicodeError
1637 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001638
1639static int
1640UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1641 PyObject *kwds)
1642{
1643 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1644 return -1;
1645
1646 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001647 Py_CLEAR(self->reason);
1648
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001649 if (!PyArg_ParseTuple(args, "O!nnO!",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001650 &PyUnicode_Type, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001651 &self->start,
1652 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001653 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001654 self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001655 return -1;
1656 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001657
Thomas Wouters477c8d52006-05-27 19:21:47 +00001658 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001659 Py_INCREF(self->reason);
1660
1661 return 0;
1662}
1663
1664
1665static PyObject *
1666UnicodeTranslateError_str(PyObject *self)
1667{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001668 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith0facd772010-02-24 15:42:29 +00001669 PyObject *result = NULL;
1670 PyObject *reason_str = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001671
Eric Smith0facd772010-02-24 15:42:29 +00001672 /* Get reason as a string, which it might not be if it's been
1673 modified after we were contructed. */
1674 reason_str = PyObject_Str(uself->reason);
1675 if (reason_str == NULL)
1676 goto done;
1677
1678 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001679 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001680 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001681 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001682 fmt = "can't translate character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001683 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001684 fmt = "can't translate character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001685 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001686 fmt = "can't translate character '\\U%08x' in position %zd: %U";
Benjamin Petersonc5f4e1e2010-02-25 01:22:28 +00001687 result = PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001688 fmt,
1689 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001690 uself->start,
Eric Smith0facd772010-02-24 15:42:29 +00001691 reason_str
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001692 );
Eric Smith0facd772010-02-24 15:42:29 +00001693 } else {
1694 result = PyUnicode_FromFormat(
1695 "can't translate characters in position %zd-%zd: %U",
1696 uself->start,
1697 uself->end-1,
1698 reason_str
1699 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001700 }
Eric Smith0facd772010-02-24 15:42:29 +00001701done:
1702 Py_XDECREF(reason_str);
1703 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001704}
1705
1706static PyTypeObject _PyExc_UnicodeTranslateError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001707 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001708 "UnicodeTranslateError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001709 sizeof(PyUnicodeErrorObject), 0,
1710 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1711 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1712 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001713 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001714 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1715 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001716 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001717};
1718PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1719
1720PyObject *
1721PyUnicodeTranslateError_Create(
1722 const Py_UNICODE *object, Py_ssize_t length,
1723 Py_ssize_t start, Py_ssize_t end, const char *reason)
1724{
1725 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001726 object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001727}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001728
1729
1730/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001731 * AssertionError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001732 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001733SimpleExtendsException(PyExc_Exception, AssertionError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001734 "Assertion failed.");
1735
1736
1737/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001738 * ArithmeticError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001739 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001740SimpleExtendsException(PyExc_Exception, ArithmeticError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001741 "Base class for arithmetic errors.");
1742
1743
1744/*
1745 * FloatingPointError extends ArithmeticError
1746 */
1747SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
1748 "Floating point operation failed.");
1749
1750
1751/*
1752 * OverflowError extends ArithmeticError
1753 */
1754SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
1755 "Result too large to be represented.");
1756
1757
1758/*
1759 * ZeroDivisionError extends ArithmeticError
1760 */
1761SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
1762 "Second argument to a division or modulo operation was zero.");
1763
1764
1765/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001766 * SystemError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001767 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001768SimpleExtendsException(PyExc_Exception, SystemError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001769 "Internal error in the Python interpreter.\n"
1770 "\n"
1771 "Please report this to the Python maintainer, along with the traceback,\n"
1772 "the Python version, and the hardware/OS platform and version.");
1773
1774
1775/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001776 * ReferenceError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001777 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001778SimpleExtendsException(PyExc_Exception, ReferenceError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001779 "Weak ref proxy used after referent went away.");
1780
1781
1782/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001783 * MemoryError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001784 */
Antoine Pitrou07e20ef2010-10-28 22:56:58 +00001785
1786#define MEMERRORS_SAVE 16
1787static PyBaseExceptionObject *memerrors_freelist = NULL;
1788static int memerrors_numfree = 0;
1789
1790static PyObject *
1791MemoryError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1792{
1793 PyBaseExceptionObject *self;
1794
1795 if (type != (PyTypeObject *) PyExc_MemoryError)
1796 return BaseException_new(type, args, kwds);
1797 if (memerrors_freelist == NULL)
1798 return BaseException_new(type, args, kwds);
1799 /* Fetch object from freelist and revive it */
1800 self = memerrors_freelist;
1801 self->args = PyTuple_New(0);
1802 /* This shouldn't happen since the empty tuple is persistent */
1803 if (self->args == NULL)
1804 return NULL;
1805 memerrors_freelist = (PyBaseExceptionObject *) self->dict;
1806 memerrors_numfree--;
1807 self->dict = NULL;
1808 _Py_NewReference((PyObject *)self);
1809 _PyObject_GC_TRACK(self);
1810 return (PyObject *)self;
1811}
1812
1813static void
1814MemoryError_dealloc(PyBaseExceptionObject *self)
1815{
1816 _PyObject_GC_UNTRACK(self);
1817 BaseException_clear(self);
1818 if (memerrors_numfree >= MEMERRORS_SAVE)
1819 Py_TYPE(self)->tp_free((PyObject *)self);
1820 else {
1821 self->dict = (PyObject *) memerrors_freelist;
1822 memerrors_freelist = self;
1823 memerrors_numfree++;
1824 }
1825}
1826
1827static void
1828preallocate_memerrors(void)
1829{
1830 /* We create enough MemoryErrors and then decref them, which will fill
1831 up the freelist. */
1832 int i;
1833 PyObject *errors[MEMERRORS_SAVE];
1834 for (i = 0; i < MEMERRORS_SAVE; i++) {
1835 errors[i] = MemoryError_new((PyTypeObject *) PyExc_MemoryError,
1836 NULL, NULL);
1837 if (!errors[i])
1838 Py_FatalError("Could not preallocate MemoryError object");
1839 }
1840 for (i = 0; i < MEMERRORS_SAVE; i++) {
1841 Py_DECREF(errors[i]);
1842 }
1843}
1844
1845static void
1846free_preallocated_memerrors(void)
1847{
1848 while (memerrors_freelist != NULL) {
1849 PyObject *self = (PyObject *) memerrors_freelist;
1850 memerrors_freelist = (PyBaseExceptionObject *) memerrors_freelist->dict;
1851 Py_TYPE(self)->tp_free((PyObject *)self);
1852 }
1853}
1854
1855
1856static PyTypeObject _PyExc_MemoryError = {
1857 PyVarObject_HEAD_INIT(NULL, 0)
1858 "MemoryError",
1859 sizeof(PyBaseExceptionObject),
1860 0, (destructor)MemoryError_dealloc, 0, 0, 0, 0, 0, 0, 0,
1861 0, 0, 0, 0, 0, 0, 0,
1862 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1863 PyDoc_STR("Out of memory."), (traverseproc)BaseException_traverse,
1864 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_PyExc_Exception,
1865 0, 0, 0, offsetof(PyBaseExceptionObject, dict),
1866 (initproc)BaseException_init, 0, MemoryError_new
1867};
1868PyObject *PyExc_MemoryError = (PyObject *) &_PyExc_MemoryError;
1869
Thomas Wouters477c8d52006-05-27 19:21:47 +00001870
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001871/*
1872 * BufferError extends Exception
1873 */
1874SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error.");
1875
Thomas Wouters477c8d52006-05-27 19:21:47 +00001876
1877/* Warning category docstrings */
1878
1879/*
1880 * Warning extends Exception
1881 */
1882SimpleExtendsException(PyExc_Exception, Warning,
1883 "Base class for warning categories.");
1884
1885
1886/*
1887 * UserWarning extends Warning
1888 */
1889SimpleExtendsException(PyExc_Warning, UserWarning,
1890 "Base class for warnings generated by user code.");
1891
1892
1893/*
1894 * DeprecationWarning extends Warning
1895 */
1896SimpleExtendsException(PyExc_Warning, DeprecationWarning,
1897 "Base class for warnings about deprecated features.");
1898
1899
1900/*
1901 * PendingDeprecationWarning extends Warning
1902 */
1903SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1904 "Base class for warnings about features which will be deprecated\n"
1905 "in the future.");
1906
1907
1908/*
1909 * SyntaxWarning extends Warning
1910 */
1911SimpleExtendsException(PyExc_Warning, SyntaxWarning,
1912 "Base class for warnings about dubious syntax.");
1913
1914
1915/*
1916 * RuntimeWarning extends Warning
1917 */
1918SimpleExtendsException(PyExc_Warning, RuntimeWarning,
1919 "Base class for warnings about dubious runtime behavior.");
1920
1921
1922/*
1923 * FutureWarning extends Warning
1924 */
1925SimpleExtendsException(PyExc_Warning, FutureWarning,
1926 "Base class for warnings about constructs that will change semantically\n"
1927 "in the future.");
1928
1929
1930/*
1931 * ImportWarning extends Warning
1932 */
1933SimpleExtendsException(PyExc_Warning, ImportWarning,
1934 "Base class for warnings about probable mistakes in module imports");
1935
1936
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001937/*
1938 * UnicodeWarning extends Warning
1939 */
1940SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1941 "Base class for warnings about Unicode related problems, mostly\n"
1942 "related to conversion problems.");
1943
Georg Brandl08be72d2010-10-24 15:11:22 +00001944
Guido van Rossum98297ee2007-11-06 21:34:58 +00001945/*
1946 * BytesWarning extends Warning
1947 */
1948SimpleExtendsException(PyExc_Warning, BytesWarning,
1949 "Base class for warnings about bytes and buffer related problems, mostly\n"
1950 "related to conversion from str or comparing to str.");
1951
1952
Georg Brandl08be72d2010-10-24 15:11:22 +00001953/*
1954 * ResourceWarning extends Warning
1955 */
1956SimpleExtendsException(PyExc_Warning, ResourceWarning,
1957 "Base class for warnings about resource usage.");
1958
1959
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001960
Thomas Wouters89d996e2007-09-08 17:39:28 +00001961/* Pre-computed RuntimeError instance for when recursion depth is reached.
1962 Meant to be used when normalizing the exception for exceeding the recursion
1963 depth will cause its own infinite recursion.
1964*/
1965PyObject *PyExc_RecursionErrorInst = NULL;
1966
Antoine Pitrou55f217f2012-01-18 21:23:13 +01001967#define PRE_INIT(TYPE) \
1968 if (!(_PyExc_ ## TYPE.tp_flags & Py_TPFLAGS_READY)) { \
1969 if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1970 Py_FatalError("exceptions bootstrapping error."); \
1971 Py_INCREF(PyExc_ ## TYPE); \
1972 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001973
Antoine Pitrou55f217f2012-01-18 21:23:13 +01001974#define POST_INIT(TYPE) \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001975 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1976 Py_FatalError("Module dictionary insertion problem.");
1977
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001978
Martin v. Löwis1a214512008-06-11 05:26:20 +00001979void
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001980_PyExc_Init(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001981{
Neal Norwitz2633c692007-02-26 22:22:47 +00001982 PyObject *bltinmod, *bdict;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001983
1984 PRE_INIT(BaseException)
1985 PRE_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001986 PRE_INIT(TypeError)
1987 PRE_INIT(StopIteration)
1988 PRE_INIT(GeneratorExit)
1989 PRE_INIT(SystemExit)
1990 PRE_INIT(KeyboardInterrupt)
1991 PRE_INIT(ImportError)
1992 PRE_INIT(EnvironmentError)
1993 PRE_INIT(IOError)
1994 PRE_INIT(OSError)
1995#ifdef MS_WINDOWS
1996 PRE_INIT(WindowsError)
1997#endif
1998#ifdef __VMS
1999 PRE_INIT(VMSError)
2000#endif
2001 PRE_INIT(EOFError)
2002 PRE_INIT(RuntimeError)
2003 PRE_INIT(NotImplementedError)
2004 PRE_INIT(NameError)
2005 PRE_INIT(UnboundLocalError)
2006 PRE_INIT(AttributeError)
2007 PRE_INIT(SyntaxError)
2008 PRE_INIT(IndentationError)
2009 PRE_INIT(TabError)
2010 PRE_INIT(LookupError)
2011 PRE_INIT(IndexError)
2012 PRE_INIT(KeyError)
2013 PRE_INIT(ValueError)
2014 PRE_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002015 PRE_INIT(UnicodeEncodeError)
2016 PRE_INIT(UnicodeDecodeError)
2017 PRE_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002018 PRE_INIT(AssertionError)
2019 PRE_INIT(ArithmeticError)
2020 PRE_INIT(FloatingPointError)
2021 PRE_INIT(OverflowError)
2022 PRE_INIT(ZeroDivisionError)
2023 PRE_INIT(SystemError)
2024 PRE_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00002025 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002026 PRE_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00002027 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002028 PRE_INIT(Warning)
2029 PRE_INIT(UserWarning)
2030 PRE_INIT(DeprecationWarning)
2031 PRE_INIT(PendingDeprecationWarning)
2032 PRE_INIT(SyntaxWarning)
2033 PRE_INIT(RuntimeWarning)
2034 PRE_INIT(FutureWarning)
2035 PRE_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002036 PRE_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00002037 PRE_INIT(BytesWarning)
Georg Brandl08be72d2010-10-24 15:11:22 +00002038 PRE_INIT(ResourceWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002039
Georg Brandl1a3284e2007-12-02 09:40:06 +00002040 bltinmod = PyImport_ImportModule("builtins");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002041 if (bltinmod == NULL)
2042 Py_FatalError("exceptions bootstrapping error.");
2043 bdict = PyModule_GetDict(bltinmod);
2044 if (bdict == NULL)
2045 Py_FatalError("exceptions bootstrapping error.");
2046
2047 POST_INIT(BaseException)
2048 POST_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002049 POST_INIT(TypeError)
2050 POST_INIT(StopIteration)
2051 POST_INIT(GeneratorExit)
2052 POST_INIT(SystemExit)
2053 POST_INIT(KeyboardInterrupt)
2054 POST_INIT(ImportError)
2055 POST_INIT(EnvironmentError)
2056 POST_INIT(IOError)
2057 POST_INIT(OSError)
2058#ifdef MS_WINDOWS
2059 POST_INIT(WindowsError)
2060#endif
2061#ifdef __VMS
2062 POST_INIT(VMSError)
2063#endif
2064 POST_INIT(EOFError)
2065 POST_INIT(RuntimeError)
2066 POST_INIT(NotImplementedError)
2067 POST_INIT(NameError)
2068 POST_INIT(UnboundLocalError)
2069 POST_INIT(AttributeError)
2070 POST_INIT(SyntaxError)
2071 POST_INIT(IndentationError)
2072 POST_INIT(TabError)
2073 POST_INIT(LookupError)
2074 POST_INIT(IndexError)
2075 POST_INIT(KeyError)
2076 POST_INIT(ValueError)
2077 POST_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002078 POST_INIT(UnicodeEncodeError)
2079 POST_INIT(UnicodeDecodeError)
2080 POST_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002081 POST_INIT(AssertionError)
2082 POST_INIT(ArithmeticError)
2083 POST_INIT(FloatingPointError)
2084 POST_INIT(OverflowError)
2085 POST_INIT(ZeroDivisionError)
2086 POST_INIT(SystemError)
2087 POST_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00002088 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002089 POST_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00002090 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002091 POST_INIT(Warning)
2092 POST_INIT(UserWarning)
2093 POST_INIT(DeprecationWarning)
2094 POST_INIT(PendingDeprecationWarning)
2095 POST_INIT(SyntaxWarning)
2096 POST_INIT(RuntimeWarning)
2097 POST_INIT(FutureWarning)
2098 POST_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002099 POST_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00002100 POST_INIT(BytesWarning)
Georg Brandl08be72d2010-10-24 15:11:22 +00002101 POST_INIT(ResourceWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002102
Antoine Pitrou07e20ef2010-10-28 22:56:58 +00002103 preallocate_memerrors();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002104
Antoine Pitrou1c7ade52012-01-18 16:13:31 +01002105 if (!PyExc_RecursionErrorInst) {
2106 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2107 if (!PyExc_RecursionErrorInst)
2108 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2109 "recursion errors");
2110 else {
2111 PyBaseExceptionObject *err_inst =
2112 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2113 PyObject *args_tuple;
2114 PyObject *exc_message;
2115 exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
2116 if (!exc_message)
2117 Py_FatalError("cannot allocate argument for RuntimeError "
2118 "pre-allocation");
2119 args_tuple = PyTuple_Pack(1, exc_message);
2120 if (!args_tuple)
2121 Py_FatalError("cannot allocate tuple for RuntimeError "
2122 "pre-allocation");
2123 Py_DECREF(exc_message);
2124 if (BaseException_init(err_inst, args_tuple, NULL))
2125 Py_FatalError("init of pre-allocated RuntimeError failed");
2126 Py_DECREF(args_tuple);
2127 }
Thomas Wouters89d996e2007-09-08 17:39:28 +00002128 }
Benjamin Petersonefe7c9d2012-02-10 08:46:54 -05002129 Py_DECREF(bltinmod);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002130}
2131
2132void
2133_PyExc_Fini(void)
2134{
Benjamin Peterson78565b22009-06-28 19:19:51 +00002135 Py_CLEAR(PyExc_RecursionErrorInst);
Antoine Pitrou07e20ef2010-10-28 22:56:58 +00002136 free_preallocated_memerrors();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002137}