blob: fb7864f0b2d1c56f77ce84ad17673d22f6f5bd03 [file] [log] [blame]
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001/*
2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
3 *
4 * Thanks go to Tim Peters and Michael Hudson for debugging.
5 */
6
Thomas Wouters477c8d52006-05-27 19:21:47 +00007#define PY_SSIZE_T_CLEAN
8#include <Python.h>
9#include "structmember.h"
10#include "osdefs.h"
11
Thomas Wouters477c8d52006-05-27 19:21:47 +000012
13/* NOTE: If the exception class hierarchy changes, don't forget to update
14 * Lib/test/exception_hierarchy.txt
15 */
16
Thomas Wouters477c8d52006-05-27 19:21:47 +000017/*
18 * BaseException
19 */
20static PyObject *
21BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
22{
23 PyBaseExceptionObject *self;
24
25 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +000026 if (!self)
27 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000028 /* the dict is created on the fly in PyObject_GenericSetAttr */
Guido van Rossumebe3e162007-05-17 18:20:34 +000029 self->dict = NULL;
Collin Winter1966f1c2007-09-01 20:26:44 +000030 self->traceback = self->cause = self->context = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000031
32 self->args = PyTuple_New(0);
33 if (!self->args) {
34 Py_DECREF(self);
35 return NULL;
36 }
37
Thomas Wouters477c8d52006-05-27 19:21:47 +000038 return (PyObject *)self;
39}
40
41static int
42BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
43{
Christian Heimes90aa7642007-12-19 02:45:37 +000044 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000045 return -1;
46
Thomas Wouters477c8d52006-05-27 19:21:47 +000047 Py_DECREF(self->args);
48 self->args = args;
49 Py_INCREF(self->args);
50
Thomas Wouters477c8d52006-05-27 19:21:47 +000051 return 0;
52}
53
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000054static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000055BaseException_clear(PyBaseExceptionObject *self)
56{
57 Py_CLEAR(self->dict);
58 Py_CLEAR(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000059 Py_CLEAR(self->traceback);
Collin Winter1966f1c2007-09-01 20:26:44 +000060 Py_CLEAR(self->cause);
61 Py_CLEAR(self->context);
Thomas Wouters477c8d52006-05-27 19:21:47 +000062 return 0;
63}
64
65static void
66BaseException_dealloc(PyBaseExceptionObject *self)
67{
Thomas Wouters89f507f2006-12-13 04:49:30 +000068 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000069 BaseException_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +000070 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000071}
72
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000073static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000074BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
75{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000076 Py_VISIT(self->dict);
Thomas Wouters477c8d52006-05-27 19:21:47 +000077 Py_VISIT(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000078 Py_VISIT(self->traceback);
Collin Winter1966f1c2007-09-01 20:26:44 +000079 Py_VISIT(self->cause);
80 Py_VISIT(self->context);
Thomas Wouters477c8d52006-05-27 19:21:47 +000081 return 0;
82}
83
84static PyObject *
85BaseException_str(PyBaseExceptionObject *self)
86{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000087 switch (PyTuple_GET_SIZE(self->args)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +000088 case 0:
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +000089 return PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +000090 case 1:
Thomas Heller519a0422007-11-15 20:48:54 +000091 return PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +000092 default:
Thomas Heller519a0422007-11-15 20:48:54 +000093 return PyObject_Str(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +000094 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000095}
96
97static PyObject *
98BaseException_repr(PyBaseExceptionObject *self)
99{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000100 char *name;
101 char *dot;
102
Christian Heimes90aa7642007-12-19 02:45:37 +0000103 name = (char *)Py_TYPE(self)->tp_name;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000104 dot = strrchr(name, '.');
105 if (dot != NULL) name = dot+1;
106
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000107 return PyUnicode_FromFormat("%s%R", name, self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108}
109
110/* Pickling support */
111static PyObject *
112BaseException_reduce(PyBaseExceptionObject *self)
113{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000114 if (self->args && self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000115 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000116 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000117 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000118}
119
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000120/*
121 * Needed for backward compatibility, since exceptions used to store
122 * all their attributes in the __dict__. Code is taken from cPickle's
123 * load_build function.
124 */
125static PyObject *
126BaseException_setstate(PyObject *self, PyObject *state)
127{
128 PyObject *d_key, *d_value;
129 Py_ssize_t i = 0;
130
131 if (state != Py_None) {
132 if (!PyDict_Check(state)) {
133 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
134 return NULL;
135 }
136 while (PyDict_Next(state, &i, &d_key, &d_value)) {
137 if (PyObject_SetAttr(self, d_key, d_value) < 0)
138 return NULL;
139 }
140 }
141 Py_RETURN_NONE;
142}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000143
Collin Winter828f04a2007-08-31 00:04:24 +0000144static PyObject *
145BaseException_with_traceback(PyObject *self, PyObject *tb) {
146 if (PyException_SetTraceback(self, tb))
147 return NULL;
148
149 Py_INCREF(self);
150 return self;
151}
152
Georg Brandl76941002008-05-05 21:38:47 +0000153PyDoc_STRVAR(with_traceback_doc,
154"Exception.with_traceback(tb) --\n\
155 set self.__traceback__ to tb and return self.");
156
Thomas Wouters477c8d52006-05-27 19:21:47 +0000157
158static PyMethodDef BaseException_methods[] = {
159 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000160 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Georg Brandl76941002008-05-05 21:38:47 +0000161 {"with_traceback", (PyCFunction)BaseException_with_traceback, METH_O,
162 with_traceback_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000163 {NULL, NULL, 0, NULL},
164};
165
166
Thomas Wouters477c8d52006-05-27 19:21:47 +0000167static PyObject *
168BaseException_get_dict(PyBaseExceptionObject *self)
169{
170 if (self->dict == NULL) {
171 self->dict = PyDict_New();
172 if (!self->dict)
173 return NULL;
174 }
175 Py_INCREF(self->dict);
176 return self->dict;
177}
178
179static int
180BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
181{
182 if (val == NULL) {
183 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
184 return -1;
185 }
186 if (!PyDict_Check(val)) {
187 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
188 return -1;
189 }
190 Py_CLEAR(self->dict);
191 Py_INCREF(val);
192 self->dict = val;
193 return 0;
194}
195
196static PyObject *
197BaseException_get_args(PyBaseExceptionObject *self)
198{
199 if (self->args == NULL) {
200 Py_INCREF(Py_None);
201 return Py_None;
202 }
203 Py_INCREF(self->args);
204 return self->args;
205}
206
207static int
208BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
209{
210 PyObject *seq;
211 if (val == NULL) {
212 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
213 return -1;
214 }
215 seq = PySequence_Tuple(val);
216 if (!seq) return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000217 Py_CLEAR(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000218 self->args = seq;
219 return 0;
220}
221
Collin Winter828f04a2007-08-31 00:04:24 +0000222static PyObject *
223BaseException_get_tb(PyBaseExceptionObject *self)
224{
225 if (self->traceback == NULL) {
226 Py_INCREF(Py_None);
227 return Py_None;
228 }
229 Py_INCREF(self->traceback);
230 return self->traceback;
231}
232
233static int
234BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb)
235{
236 if (tb == NULL) {
237 PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted");
238 return -1;
239 }
240 else if (!(tb == Py_None || PyTraceBack_Check(tb))) {
241 PyErr_SetString(PyExc_TypeError,
242 "__traceback__ must be a traceback or None");
243 return -1;
244 }
245
246 Py_XINCREF(tb);
247 Py_XDECREF(self->traceback);
248 self->traceback = tb;
249 return 0;
250}
251
Georg Brandlab6f2f62009-03-31 04:16:10 +0000252static PyObject *
253BaseException_get_context(PyObject *self) {
254 PyObject *res = PyException_GetContext(self);
255 if (res) return res; /* new reference already returned above */
256 Py_RETURN_NONE;
257}
258
259static int
260BaseException_set_context(PyObject *self, PyObject *arg) {
261 if (arg == NULL) {
262 PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted");
263 return -1;
264 } else if (arg == Py_None) {
265 arg = NULL;
266 } else if (!PyExceptionInstance_Check(arg)) {
267 PyErr_SetString(PyExc_TypeError, "exception context must be None "
268 "or derive from BaseException");
269 return -1;
270 } else {
271 /* PyException_SetContext steals this reference */
272 Py_INCREF(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 }
Georg Brandlab6f2f62009-03-31 04:16:10 +0000274 PyException_SetContext(self, arg);
275 return 0;
276}
277
278static PyObject *
279BaseException_get_cause(PyObject *self) {
280 PyObject *res = PyException_GetCause(self);
281 if (res) return res; /* new reference already returned above */
282 Py_RETURN_NONE;
283}
284
285static int
286BaseException_set_cause(PyObject *self, PyObject *arg) {
287 if (arg == NULL) {
288 PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted");
289 return -1;
290 } else if (arg == Py_None) {
291 arg = NULL;
292 } else if (!PyExceptionInstance_Check(arg)) {
293 PyErr_SetString(PyExc_TypeError, "exception cause must be None "
294 "or derive from BaseException");
295 return -1;
296 } else {
297 /* PyException_SetCause steals this reference */
298 Py_INCREF(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 }
Georg Brandlab6f2f62009-03-31 04:16:10 +0000300 PyException_SetCause(self, arg);
301 return 0;
302}
303
Guido van Rossum360e4b82007-05-14 22:51:27 +0000304
Thomas Wouters477c8d52006-05-27 19:21:47 +0000305static PyGetSetDef BaseException_getset[] = {
306 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
307 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Collin Winter828f04a2007-08-31 00:04:24 +0000308 {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb},
Georg Brandlab6f2f62009-03-31 04:16:10 +0000309 {"__context__", (getter)BaseException_get_context,
310 (setter)BaseException_set_context, PyDoc_STR("exception context")},
311 {"__cause__", (getter)BaseException_get_cause,
312 (setter)BaseException_set_cause, PyDoc_STR("exception cause")},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000313 {NULL},
314};
315
316
Collin Winter828f04a2007-08-31 00:04:24 +0000317PyObject *
318PyException_GetTraceback(PyObject *self) {
319 PyBaseExceptionObject *base_self = (PyBaseExceptionObject *)self;
320 Py_XINCREF(base_self->traceback);
321 return base_self->traceback;
322}
323
324
325int
326PyException_SetTraceback(PyObject *self, PyObject *tb) {
327 return BaseException_set_tb((PyBaseExceptionObject *)self, tb);
328}
329
330PyObject *
331PyException_GetCause(PyObject *self) {
332 PyObject *cause = ((PyBaseExceptionObject *)self)->cause;
333 Py_XINCREF(cause);
334 return cause;
335}
336
337/* Steals a reference to cause */
338void
339PyException_SetCause(PyObject *self, PyObject *cause) {
340 PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause;
341 ((PyBaseExceptionObject *)self)->cause = cause;
342 Py_XDECREF(old_cause);
343}
344
345PyObject *
346PyException_GetContext(PyObject *self) {
347 PyObject *context = ((PyBaseExceptionObject *)self)->context;
348 Py_XINCREF(context);
349 return context;
350}
351
352/* Steals a reference to context */
353void
354PyException_SetContext(PyObject *self, PyObject *context) {
355 PyObject *old_context = ((PyBaseExceptionObject *)self)->context;
356 ((PyBaseExceptionObject *)self)->context = context;
357 Py_XDECREF(old_context);
358}
359
360
Thomas Wouters477c8d52006-05-27 19:21:47 +0000361static PyTypeObject _PyExc_BaseException = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000362 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +0000363 "BaseException", /*tp_name*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000364 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
365 0, /*tp_itemsize*/
366 (destructor)BaseException_dealloc, /*tp_dealloc*/
367 0, /*tp_print*/
368 0, /*tp_getattr*/
369 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000370 0, /* tp_reserved; */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000371 (reprfunc)BaseException_repr, /*tp_repr*/
372 0, /*tp_as_number*/
Brett Cannonba7bf492007-02-27 00:15:55 +0000373 0, /*tp_as_sequence*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000374 0, /*tp_as_mapping*/
375 0, /*tp_hash */
376 0, /*tp_call*/
377 (reprfunc)BaseException_str, /*tp_str*/
378 PyObject_GenericGetAttr, /*tp_getattro*/
379 PyObject_GenericSetAttr, /*tp_setattro*/
380 0, /*tp_as_buffer*/
Thomas Wouters27d517b2007-02-25 20:39:11 +0000381 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000383 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
384 (traverseproc)BaseException_traverse, /* tp_traverse */
385 (inquiry)BaseException_clear, /* tp_clear */
386 0, /* tp_richcompare */
387 0, /* tp_weaklistoffset */
388 0, /* tp_iter */
389 0, /* tp_iternext */
390 BaseException_methods, /* tp_methods */
Georg Brandlab6f2f62009-03-31 04:16:10 +0000391 0, /* tp_members */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000392 BaseException_getset, /* tp_getset */
393 0, /* tp_base */
394 0, /* tp_dict */
395 0, /* tp_descr_get */
396 0, /* tp_descr_set */
397 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
398 (initproc)BaseException_init, /* tp_init */
399 0, /* tp_alloc */
400 BaseException_new, /* tp_new */
401};
402/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
403from the previous implmentation and also allowing Python objects to be used
404in the API */
405PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
406
407/* note these macros omit the last semicolon so the macro invocation may
408 * include it and not look strange.
409 */
410#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
411static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000412 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000413 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000414 sizeof(PyBaseExceptionObject), \
415 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
416 0, 0, 0, 0, 0, 0, 0, \
417 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
418 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
419 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
420 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
421 (initproc)BaseException_init, 0, BaseException_new,\
422}; \
423PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
424
425#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
426static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000427 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000428 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000429 sizeof(Py ## EXCSTORE ## Object), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000430 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000431 0, 0, 0, 0, 0, \
432 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000433 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
434 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000435 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000436 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000437}; \
438PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
439
440#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
441static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000442 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000443 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000444 sizeof(Py ## EXCSTORE ## Object), 0, \
445 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
446 (reprfunc)EXCSTR, 0, 0, 0, \
447 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
448 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
449 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
450 EXCMEMBERS, 0, &_ ## EXCBASE, \
451 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000452 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000453}; \
454PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
455
456
457/*
458 * Exception extends BaseException
459 */
460SimpleExtendsException(PyExc_BaseException, Exception,
461 "Common base class for all non-exit exceptions.");
462
463
464/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000465 * TypeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000466 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000467SimpleExtendsException(PyExc_Exception, TypeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000468 "Inappropriate argument type.");
469
470
471/*
472 * StopIteration extends Exception
473 */
474SimpleExtendsException(PyExc_Exception, StopIteration,
Georg Brandla18af4e2007-04-21 15:47:16 +0000475 "Signal the end from iterator.__next__().");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000476
477
478/*
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000479 * GeneratorExit extends BaseException
Thomas Wouters477c8d52006-05-27 19:21:47 +0000480 */
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000481SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000482 "Request that a generator exit.");
483
484
485/*
486 * SystemExit extends BaseException
487 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000488
489static int
490SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
491{
492 Py_ssize_t size = PyTuple_GET_SIZE(args);
493
494 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
495 return -1;
496
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000497 if (size == 0)
498 return 0;
499 Py_CLEAR(self->code);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000500 if (size == 1)
501 self->code = PyTuple_GET_ITEM(args, 0);
Victor Stinner92236e52011-05-26 14:25:54 +0200502 else /* size > 1 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000503 self->code = args;
504 Py_INCREF(self->code);
505 return 0;
506}
507
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000508static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000509SystemExit_clear(PySystemExitObject *self)
510{
511 Py_CLEAR(self->code);
512 return BaseException_clear((PyBaseExceptionObject *)self);
513}
514
515static void
516SystemExit_dealloc(PySystemExitObject *self)
517{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000518 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000519 SystemExit_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000520 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000521}
522
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000523static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000524SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
525{
526 Py_VISIT(self->code);
527 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
528}
529
530static PyMemberDef SystemExit_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000531 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
532 PyDoc_STR("exception code")},
533 {NULL} /* Sentinel */
534};
535
536ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
537 SystemExit_dealloc, 0, SystemExit_members, 0,
538 "Request to exit from the interpreter.");
539
540/*
541 * KeyboardInterrupt extends BaseException
542 */
543SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
544 "Program interrupted by user.");
545
546
547/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000548 * ImportError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000549 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000550SimpleExtendsException(PyExc_Exception, ImportError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000551 "Import can't find module, or can't find name in module.");
552
553
554/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000555 * EnvironmentError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000556 */
557
Thomas Wouters477c8d52006-05-27 19:21:47 +0000558/* Where a function has a single filename, such as open() or some
559 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
560 * called, giving a third argument which is the filename. But, so
561 * that old code using in-place unpacking doesn't break, e.g.:
562 *
563 * except IOError, (errno, strerror):
564 *
565 * we hack args so that it only contains two items. This also
566 * means we need our own __str__() which prints out the filename
567 * when it was supplied.
568 */
569static int
570EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
571 PyObject *kwds)
572{
573 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
574 PyObject *subslice = NULL;
575
576 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
577 return -1;
578
Thomas Wouters89f507f2006-12-13 04:49:30 +0000579 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000580 return 0;
581 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000582
583 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000584 &myerrno, &strerror, &filename)) {
585 return -1;
586 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000587 Py_CLEAR(self->myerrno); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000588 self->myerrno = myerrno;
589 Py_INCREF(self->myerrno);
590
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000591 Py_CLEAR(self->strerror); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000592 self->strerror = strerror;
593 Py_INCREF(self->strerror);
594
595 /* self->filename will remain Py_None otherwise */
596 if (filename != NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000597 Py_CLEAR(self->filename); /* replacing */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000598 self->filename = filename;
599 Py_INCREF(self->filename);
600
601 subslice = PyTuple_GetSlice(args, 0, 2);
602 if (!subslice)
603 return -1;
604
605 Py_DECREF(self->args); /* replacing args */
606 self->args = subslice;
607 }
608 return 0;
609}
610
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000611static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000612EnvironmentError_clear(PyEnvironmentErrorObject *self)
613{
614 Py_CLEAR(self->myerrno);
615 Py_CLEAR(self->strerror);
616 Py_CLEAR(self->filename);
617 return BaseException_clear((PyBaseExceptionObject *)self);
618}
619
620static void
621EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
622{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000623 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624 EnvironmentError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000625 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000626}
627
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000628static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000629EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
630 void *arg)
631{
632 Py_VISIT(self->myerrno);
633 Py_VISIT(self->strerror);
634 Py_VISIT(self->filename);
635 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
636}
637
638static PyObject *
639EnvironmentError_str(PyEnvironmentErrorObject *self)
640{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000641 if (self->filename)
642 return PyUnicode_FromFormat("[Errno %S] %S: %R",
643 self->myerrno ? self->myerrno: Py_None,
644 self->strerror ? self->strerror: Py_None,
645 self->filename);
646 else if (self->myerrno && self->strerror)
647 return PyUnicode_FromFormat("[Errno %S] %S",
648 self->myerrno ? self->myerrno: Py_None,
649 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000650 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000651 return BaseException_str((PyBaseExceptionObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000652}
653
654static PyMemberDef EnvironmentError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000655 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
656 PyDoc_STR("exception errno")},
657 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
658 PyDoc_STR("exception strerror")},
659 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
660 PyDoc_STR("exception filename")},
661 {NULL} /* Sentinel */
662};
663
664
665static PyObject *
666EnvironmentError_reduce(PyEnvironmentErrorObject *self)
667{
668 PyObject *args = self->args;
669 PyObject *res = NULL, *tmp;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000670
Thomas Wouters477c8d52006-05-27 19:21:47 +0000671 /* self->args is only the first two real arguments if there was a
672 * file name given to EnvironmentError. */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000673 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000674 args = PyTuple_New(3);
675 if (!args) return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000676
677 tmp = PyTuple_GET_ITEM(self->args, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000678 Py_INCREF(tmp);
679 PyTuple_SET_ITEM(args, 0, tmp);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000680
681 tmp = PyTuple_GET_ITEM(self->args, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000682 Py_INCREF(tmp);
683 PyTuple_SET_ITEM(args, 1, tmp);
684
685 Py_INCREF(self->filename);
686 PyTuple_SET_ITEM(args, 2, self->filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000687 } else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000688 Py_INCREF(args);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000689
690 if (self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000691 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000692 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000693 res = PyTuple_Pack(2, Py_TYPE(self), args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694 Py_DECREF(args);
695 return res;
696}
697
698
699static PyMethodDef EnvironmentError_methods[] = {
700 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
701 {NULL}
702};
703
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000704ComplexExtendsException(PyExc_Exception, EnvironmentError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000705 EnvironmentError, EnvironmentError_dealloc,
706 EnvironmentError_methods, EnvironmentError_members,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000707 EnvironmentError_str,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000708 "Base class for I/O related errors.");
709
710
711/*
712 * IOError extends EnvironmentError
713 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000714MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000715 EnvironmentError, "I/O operation failed.");
716
717
718/*
719 * OSError extends EnvironmentError
720 */
721MiddlingExtendsException(PyExc_EnvironmentError, OSError,
722 EnvironmentError, "OS system call failed.");
723
724
725/*
726 * WindowsError extends OSError
727 */
728#ifdef MS_WINDOWS
729#include "errmap.h"
730
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000731static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000732WindowsError_clear(PyWindowsErrorObject *self)
733{
734 Py_CLEAR(self->myerrno);
735 Py_CLEAR(self->strerror);
736 Py_CLEAR(self->filename);
737 Py_CLEAR(self->winerror);
738 return BaseException_clear((PyBaseExceptionObject *)self);
739}
740
741static void
742WindowsError_dealloc(PyWindowsErrorObject *self)
743{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000744 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000745 WindowsError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000746 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000747}
748
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000749static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000750WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
751{
752 Py_VISIT(self->myerrno);
753 Py_VISIT(self->strerror);
754 Py_VISIT(self->filename);
755 Py_VISIT(self->winerror);
756 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
757}
758
Thomas Wouters477c8d52006-05-27 19:21:47 +0000759static int
760WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
761{
762 PyObject *o_errcode = NULL;
763 long errcode;
764 long posix_errno;
765
766 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
767 == -1)
768 return -1;
769
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000770 if (self->myerrno == NULL)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000771 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000772
773 /* Set errno to the POSIX errno, and winerror to the Win32
774 error code. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000775 errcode = PyLong_AsLong(self->myerrno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000776 if (errcode == -1 && PyErr_Occurred())
777 return -1;
778 posix_errno = winerror_to_errno(errcode);
779
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000780 Py_CLEAR(self->winerror);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000781 self->winerror = self->myerrno;
782
Christian Heimes217cfd12007-12-02 14:31:20 +0000783 o_errcode = PyLong_FromLong(posix_errno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000784 if (!o_errcode)
785 return -1;
786
787 self->myerrno = o_errcode;
788
789 return 0;
790}
791
792
793static PyObject *
794WindowsError_str(PyWindowsErrorObject *self)
795{
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000796 if (self->filename)
797 return PyUnicode_FromFormat("[Error %S] %S: %R",
798 self->winerror ? self->winerror: Py_None,
799 self->strerror ? self->strerror: Py_None,
800 self->filename);
801 else if (self->winerror && self->strerror)
802 return PyUnicode_FromFormat("[Error %S] %S",
803 self->winerror ? self->winerror: Py_None,
804 self->strerror ? self->strerror: Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000805 else
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000806 return EnvironmentError_str((PyEnvironmentErrorObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000807}
808
809static PyMemberDef WindowsError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000810 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
811 PyDoc_STR("POSIX exception code")},
812 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
813 PyDoc_STR("exception strerror")},
814 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
815 PyDoc_STR("exception filename")},
816 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
817 PyDoc_STR("Win32 exception code")},
818 {NULL} /* Sentinel */
819};
820
821ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
822 WindowsError_dealloc, 0, WindowsError_members,
823 WindowsError_str, "MS-Windows OS system call failed.");
824
825#endif /* MS_WINDOWS */
826
827
828/*
829 * VMSError extends OSError (I think)
830 */
831#ifdef __VMS
832MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
833 "OpenVMS OS system call failed.");
834#endif
835
836
837/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000838 * EOFError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000839 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000840SimpleExtendsException(PyExc_Exception, EOFError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841 "Read beyond end of file.");
842
843
844/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000845 * RuntimeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000846 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000847SimpleExtendsException(PyExc_Exception, RuntimeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000848 "Unspecified run-time error.");
849
850
851/*
852 * NotImplementedError extends RuntimeError
853 */
854SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
855 "Method or function hasn't been implemented yet.");
856
857/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000858 * NameError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000860SimpleExtendsException(PyExc_Exception, NameError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000861 "Name not found globally.");
862
863/*
864 * UnboundLocalError extends NameError
865 */
866SimpleExtendsException(PyExc_NameError, UnboundLocalError,
867 "Local name referenced but not bound to a value.");
868
869/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000870 * AttributeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000871 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000872SimpleExtendsException(PyExc_Exception, AttributeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000873 "Attribute not found.");
874
875
876/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000877 * SyntaxError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000878 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000879
880static int
881SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
882{
883 PyObject *info = NULL;
884 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
885
886 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
887 return -1;
888
889 if (lenargs >= 1) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000890 Py_CLEAR(self->msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000891 self->msg = PyTuple_GET_ITEM(args, 0);
892 Py_INCREF(self->msg);
893 }
894 if (lenargs == 2) {
895 info = PyTuple_GET_ITEM(args, 1);
896 info = PySequence_Tuple(info);
897 if (!info) return -1;
898
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000899 if (PyTuple_GET_SIZE(info) != 4) {
900 /* not a very good error message, but it's what Python 2.4 gives */
901 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
902 Py_DECREF(info);
903 return -1;
904 }
905
906 Py_CLEAR(self->filename);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907 self->filename = PyTuple_GET_ITEM(info, 0);
908 Py_INCREF(self->filename);
909
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000910 Py_CLEAR(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000911 self->lineno = PyTuple_GET_ITEM(info, 1);
912 Py_INCREF(self->lineno);
913
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000914 Py_CLEAR(self->offset);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000915 self->offset = PyTuple_GET_ITEM(info, 2);
916 Py_INCREF(self->offset);
917
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000918 Py_CLEAR(self->text);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000919 self->text = PyTuple_GET_ITEM(info, 3);
920 Py_INCREF(self->text);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000921
922 Py_DECREF(info);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000923 }
924 return 0;
925}
926
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000927static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928SyntaxError_clear(PySyntaxErrorObject *self)
929{
930 Py_CLEAR(self->msg);
931 Py_CLEAR(self->filename);
932 Py_CLEAR(self->lineno);
933 Py_CLEAR(self->offset);
934 Py_CLEAR(self->text);
935 Py_CLEAR(self->print_file_and_line);
936 return BaseException_clear((PyBaseExceptionObject *)self);
937}
938
939static void
940SyntaxError_dealloc(PySyntaxErrorObject *self)
941{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000942 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000943 SyntaxError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000944 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000945}
946
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000947static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000948SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
949{
950 Py_VISIT(self->msg);
951 Py_VISIT(self->filename);
952 Py_VISIT(self->lineno);
953 Py_VISIT(self->offset);
954 Py_VISIT(self->text);
955 Py_VISIT(self->print_file_and_line);
956 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
957}
958
959/* This is called "my_basename" instead of just "basename" to avoid name
960 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
961 defined, and Python does define that. */
Victor Stinner6237daf2010-04-28 17:26:19 +0000962static PyObject*
963my_basename(PyObject *name)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000964{
Victor Stinner6237daf2010-04-28 17:26:19 +0000965 Py_UNICODE *unicode;
966 Py_ssize_t i, size, offset;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000967
Victor Stinner6237daf2010-04-28 17:26:19 +0000968 unicode = PyUnicode_AS_UNICODE(name);
969 size = PyUnicode_GET_SIZE(name);
970 offset = 0;
971 for(i=0; i < size; i++) {
972 if (unicode[i] == SEP)
973 offset = i + 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000974 }
Victor Stinner6237daf2010-04-28 17:26:19 +0000975 if (offset != 0) {
976 return PyUnicode_FromUnicode(
977 PyUnicode_AS_UNICODE(name) + offset,
978 size - offset);
979 } else {
980 Py_INCREF(name);
981 return name;
982 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000983}
984
985
986static PyObject *
987SyntaxError_str(PySyntaxErrorObject *self)
988{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000989 int have_lineno = 0;
Victor Stinner6237daf2010-04-28 17:26:19 +0000990 PyObject *filename;
991 PyObject *result;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000992 /* Below, we always ignore overflow errors, just printing -1.
993 Still, we cannot allow an OverflowError to be raised, so
994 we need to call PyLong_AsLongAndOverflow. */
995 int overflow;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000996
997 /* XXX -- do all the additional formatting with filename and
998 lineno here */
999
Neal Norwitzed2b7392007-08-26 04:51:10 +00001000 if (self->filename && PyUnicode_Check(self->filename)) {
Victor Stinner6237daf2010-04-28 17:26:19 +00001001 filename = my_basename(self->filename);
1002 if (filename == NULL)
1003 return NULL;
1004 } else {
1005 filename = NULL;
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001006 }
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001007 have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001009 if (!filename && !have_lineno)
Thomas Heller519a0422007-11-15 20:48:54 +00001010 return PyObject_Str(self->msg ? self->msg : Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001011
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001012 if (filename && have_lineno)
Victor Stinner6237daf2010-04-28 17:26:19 +00001013 result = PyUnicode_FromFormat("%S (%U, line %ld)",
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001014 self->msg ? self->msg : Py_None,
Victor Stinner6237daf2010-04-28 17:26:19 +00001015 filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001017 else if (filename)
Victor Stinner6237daf2010-04-28 17:26:19 +00001018 result = PyUnicode_FromFormat("%S (%U)",
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001019 self->msg ? self->msg : Py_None,
Victor Stinner6237daf2010-04-28 17:26:19 +00001020 filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001021 else /* only have_lineno */
Victor Stinner6237daf2010-04-28 17:26:19 +00001022 result = PyUnicode_FromFormat("%S (line %ld)",
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001023 self->msg ? self->msg : Py_None,
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001024 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Victor Stinner6237daf2010-04-28 17:26:19 +00001025 Py_XDECREF(filename);
1026 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001027}
1028
1029static PyMemberDef SyntaxError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001030 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1031 PyDoc_STR("exception msg")},
1032 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1033 PyDoc_STR("exception filename")},
1034 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1035 PyDoc_STR("exception lineno")},
1036 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1037 PyDoc_STR("exception offset")},
1038 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1039 PyDoc_STR("exception text")},
1040 {"print_file_and_line", T_OBJECT,
1041 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1042 PyDoc_STR("exception print_file_and_line")},
1043 {NULL} /* Sentinel */
1044};
1045
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001046ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001047 SyntaxError_dealloc, 0, SyntaxError_members,
1048 SyntaxError_str, "Invalid syntax.");
1049
1050
1051/*
1052 * IndentationError extends SyntaxError
1053 */
1054MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
1055 "Improper indentation.");
1056
1057
1058/*
1059 * TabError extends IndentationError
1060 */
1061MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
1062 "Improper mixture of spaces and tabs.");
1063
1064
1065/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001066 * LookupError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001067 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001068SimpleExtendsException(PyExc_Exception, LookupError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001069 "Base class for lookup errors.");
1070
1071
1072/*
1073 * IndexError extends LookupError
1074 */
1075SimpleExtendsException(PyExc_LookupError, IndexError,
1076 "Sequence index out of range.");
1077
1078
1079/*
1080 * KeyError extends LookupError
1081 */
1082static PyObject *
1083KeyError_str(PyBaseExceptionObject *self)
1084{
1085 /* If args is a tuple of exactly one item, apply repr to args[0].
1086 This is done so that e.g. the exception raised by {}[''] prints
1087 KeyError: ''
1088 rather than the confusing
1089 KeyError
1090 alone. The downside is that if KeyError is raised with an explanatory
1091 string, that string will be displayed in quotes. Too bad.
1092 If args is anything else, use the default BaseException__str__().
1093 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001094 if (PyTuple_GET_SIZE(self->args) == 1) {
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001095 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001096 }
1097 return BaseException_str(self);
1098}
1099
1100ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
1101 0, 0, 0, KeyError_str, "Mapping key not found.");
1102
1103
1104/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001105 * ValueError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001106 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001107SimpleExtendsException(PyExc_Exception, ValueError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001108 "Inappropriate argument value (of correct type).");
1109
1110/*
1111 * UnicodeError extends ValueError
1112 */
1113
1114SimpleExtendsException(PyExc_ValueError, UnicodeError,
1115 "Unicode related error.");
1116
Thomas Wouters477c8d52006-05-27 19:21:47 +00001117static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001118get_string(PyObject *attr, const char *name)
Walter Dörwald612344f2007-05-04 19:28:21 +00001119{
1120 if (!attr) {
1121 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1122 return NULL;
1123 }
1124
Christian Heimes72b710a2008-05-26 13:28:38 +00001125 if (!PyBytes_Check(attr)) {
Walter Dörwald612344f2007-05-04 19:28:21 +00001126 PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
1127 return NULL;
1128 }
1129 Py_INCREF(attr);
1130 return attr;
1131}
1132
1133static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001134get_unicode(PyObject *attr, const char *name)
1135{
1136 if (!attr) {
1137 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1138 return NULL;
1139 }
1140
1141 if (!PyUnicode_Check(attr)) {
1142 PyErr_Format(PyExc_TypeError,
1143 "%.200s attribute must be unicode", name);
1144 return NULL;
1145 }
1146 Py_INCREF(attr);
1147 return attr;
1148}
1149
Walter Dörwaldd2034312007-05-18 16:29:38 +00001150static int
1151set_unicodefromstring(PyObject **attr, const char *value)
1152{
1153 PyObject *obj = PyUnicode_FromString(value);
1154 if (!obj)
1155 return -1;
1156 Py_CLEAR(*attr);
1157 *attr = obj;
1158 return 0;
1159}
1160
Thomas Wouters477c8d52006-05-27 19:21:47 +00001161PyObject *
1162PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1163{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001164 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001165}
1166
1167PyObject *
1168PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1169{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001170 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001171}
1172
1173PyObject *
1174PyUnicodeEncodeError_GetObject(PyObject *exc)
1175{
1176 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1177}
1178
1179PyObject *
1180PyUnicodeDecodeError_GetObject(PyObject *exc)
1181{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001182 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001183}
1184
1185PyObject *
1186PyUnicodeTranslateError_GetObject(PyObject *exc)
1187{
1188 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1189}
1190
1191int
1192PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1193{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001194 Py_ssize_t size;
1195 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1196 "object");
1197 if (!obj)
1198 return -1;
1199 *start = ((PyUnicodeErrorObject *)exc)->start;
1200 size = PyUnicode_GET_SIZE(obj);
1201 if (*start<0)
1202 *start = 0; /*XXX check for values <0*/
1203 if (*start>=size)
1204 *start = size-1;
1205 Py_DECREF(obj);
1206 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001207}
1208
1209
1210int
1211PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1212{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001213 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001214 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001215 if (!obj)
1216 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001217 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001218 *start = ((PyUnicodeErrorObject *)exc)->start;
1219 if (*start<0)
1220 *start = 0;
1221 if (*start>=size)
1222 *start = size-1;
1223 Py_DECREF(obj);
1224 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001225}
1226
1227
1228int
1229PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1230{
1231 return PyUnicodeEncodeError_GetStart(exc, start);
1232}
1233
1234
1235int
1236PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1237{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001238 ((PyUnicodeErrorObject *)exc)->start = start;
1239 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001240}
1241
1242
1243int
1244PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1245{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001246 ((PyUnicodeErrorObject *)exc)->start = start;
1247 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001248}
1249
1250
1251int
1252PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1253{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001254 ((PyUnicodeErrorObject *)exc)->start = start;
1255 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001256}
1257
1258
1259int
1260PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1261{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001262 Py_ssize_t size;
1263 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1264 "object");
1265 if (!obj)
1266 return -1;
1267 *end = ((PyUnicodeErrorObject *)exc)->end;
1268 size = PyUnicode_GET_SIZE(obj);
1269 if (*end<1)
1270 *end = 1;
1271 if (*end>size)
1272 *end = size;
1273 Py_DECREF(obj);
1274 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001275}
1276
1277
1278int
1279PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1280{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001281 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001282 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001283 if (!obj)
1284 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001285 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001286 *end = ((PyUnicodeErrorObject *)exc)->end;
1287 if (*end<1)
1288 *end = 1;
1289 if (*end>size)
1290 *end = size;
1291 Py_DECREF(obj);
1292 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001293}
1294
1295
1296int
1297PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1298{
1299 return PyUnicodeEncodeError_GetEnd(exc, start);
1300}
1301
1302
1303int
1304PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1305{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001306 ((PyUnicodeErrorObject *)exc)->end = end;
1307 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001308}
1309
1310
1311int
1312PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1313{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001314 ((PyUnicodeErrorObject *)exc)->end = end;
1315 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001316}
1317
1318
1319int
1320PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1321{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001322 ((PyUnicodeErrorObject *)exc)->end = end;
1323 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001324}
1325
1326PyObject *
1327PyUnicodeEncodeError_GetReason(PyObject *exc)
1328{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001329 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001330}
1331
1332
1333PyObject *
1334PyUnicodeDecodeError_GetReason(PyObject *exc)
1335{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001336 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001337}
1338
1339
1340PyObject *
1341PyUnicodeTranslateError_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
1347int
1348PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1349{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001350 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1351 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001352}
1353
1354
1355int
1356PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1357{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001358 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1359 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001360}
1361
1362
1363int
1364PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1365{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001366 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1367 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001368}
1369
1370
Thomas Wouters477c8d52006-05-27 19:21:47 +00001371static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001372UnicodeError_clear(PyUnicodeErrorObject *self)
1373{
1374 Py_CLEAR(self->encoding);
1375 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001376 Py_CLEAR(self->reason);
1377 return BaseException_clear((PyBaseExceptionObject *)self);
1378}
1379
1380static void
1381UnicodeError_dealloc(PyUnicodeErrorObject *self)
1382{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001383 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001384 UnicodeError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +00001385 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001386}
1387
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001388static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001389UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1390{
1391 Py_VISIT(self->encoding);
1392 Py_VISIT(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001393 Py_VISIT(self->reason);
1394 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1395}
1396
1397static PyMemberDef UnicodeError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001398 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1399 PyDoc_STR("exception encoding")},
1400 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1401 PyDoc_STR("exception object")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001402 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001403 PyDoc_STR("exception start")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001404 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001405 PyDoc_STR("exception end")},
1406 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1407 PyDoc_STR("exception reason")},
1408 {NULL} /* Sentinel */
1409};
1410
1411
1412/*
1413 * UnicodeEncodeError extends UnicodeError
1414 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001415
1416static int
1417UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1418{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001419 PyUnicodeErrorObject *err;
1420
Thomas Wouters477c8d52006-05-27 19:21:47 +00001421 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1422 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001423
1424 err = (PyUnicodeErrorObject *)self;
1425
1426 Py_CLEAR(err->encoding);
1427 Py_CLEAR(err->object);
1428 Py_CLEAR(err->reason);
1429
1430 if (!PyArg_ParseTuple(args, "O!O!nnO!",
1431 &PyUnicode_Type, &err->encoding,
1432 &PyUnicode_Type, &err->object,
1433 &err->start,
1434 &err->end,
1435 &PyUnicode_Type, &err->reason)) {
1436 err->encoding = err->object = err->reason = NULL;
1437 return -1;
1438 }
1439
1440 Py_INCREF(err->encoding);
1441 Py_INCREF(err->object);
1442 Py_INCREF(err->reason);
1443
1444 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001445}
1446
1447static PyObject *
1448UnicodeEncodeError_str(PyObject *self)
1449{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001450 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith0facd772010-02-24 15:42:29 +00001451 PyObject *result = NULL;
1452 PyObject *reason_str = NULL;
1453 PyObject *encoding_str = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001454
Eric Smith0facd772010-02-24 15:42:29 +00001455 /* Get reason and encoding as strings, which they might not be if
1456 they've been modified after we were contructed. */
1457 reason_str = PyObject_Str(uself->reason);
1458 if (reason_str == NULL)
1459 goto done;
1460 encoding_str = PyObject_Str(uself->encoding);
1461 if (encoding_str == NULL)
1462 goto done;
1463
1464 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001465 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001466 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001467 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001468 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001469 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001470 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001471 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001472 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
Eric Smith0facd772010-02-24 15:42:29 +00001473 result = PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001474 fmt,
Eric Smith0facd772010-02-24 15:42:29 +00001475 encoding_str,
Walter Dörwald787b03b2007-06-05 13:29:29 +00001476 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001477 uself->start,
Eric Smith0facd772010-02-24 15:42:29 +00001478 reason_str);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001479 }
Eric Smith0facd772010-02-24 15:42:29 +00001480 else {
1481 result = PyUnicode_FromFormat(
1482 "'%U' codec can't encode characters in position %zd-%zd: %U",
1483 encoding_str,
1484 uself->start,
1485 uself->end-1,
1486 reason_str);
1487 }
1488done:
1489 Py_XDECREF(reason_str);
1490 Py_XDECREF(encoding_str);
1491 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001492}
1493
1494static PyTypeObject _PyExc_UnicodeEncodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001495 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001496 "UnicodeEncodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001497 sizeof(PyUnicodeErrorObject), 0,
1498 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1499 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1500 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001501 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1502 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001503 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001504 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001505};
1506PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1507
1508PyObject *
1509PyUnicodeEncodeError_Create(
1510 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1511 Py_ssize_t start, Py_ssize_t end, const char *reason)
1512{
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001513 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001514 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001515}
1516
1517
1518/*
1519 * UnicodeDecodeError extends UnicodeError
1520 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001521
1522static int
1523UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1524{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001525 PyUnicodeErrorObject *ude;
1526 const char *data;
1527 Py_ssize_t size;
1528
Thomas Wouters477c8d52006-05-27 19:21:47 +00001529 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1530 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001531
1532 ude = (PyUnicodeErrorObject *)self;
1533
1534 Py_CLEAR(ude->encoding);
1535 Py_CLEAR(ude->object);
1536 Py_CLEAR(ude->reason);
1537
1538 if (!PyArg_ParseTuple(args, "O!OnnO!",
1539 &PyUnicode_Type, &ude->encoding,
1540 &ude->object,
1541 &ude->start,
1542 &ude->end,
1543 &PyUnicode_Type, &ude->reason)) {
1544 ude->encoding = ude->object = ude->reason = NULL;
1545 return -1;
1546 }
1547
Christian Heimes72b710a2008-05-26 13:28:38 +00001548 if (!PyBytes_Check(ude->object)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00001549 if (PyObject_AsReadBuffer(ude->object, (const void **)&data, &size)) {
1550 ude->encoding = ude->object = ude->reason = NULL;
1551 return -1;
1552 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001553 ude->object = PyBytes_FromStringAndSize(data, size);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001554 }
1555 else {
1556 Py_INCREF(ude->object);
1557 }
1558
1559 Py_INCREF(ude->encoding);
1560 Py_INCREF(ude->reason);
1561
1562 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001563}
1564
1565static PyObject *
1566UnicodeDecodeError_str(PyObject *self)
1567{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001568 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith0facd772010-02-24 15:42:29 +00001569 PyObject *result = NULL;
1570 PyObject *reason_str = NULL;
1571 PyObject *encoding_str = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001572
Eric Smith0facd772010-02-24 15:42:29 +00001573 /* Get reason and encoding as strings, which they might not be if
1574 they've been modified after we were contructed. */
1575 reason_str = PyObject_Str(uself->reason);
1576 if (reason_str == NULL)
1577 goto done;
1578 encoding_str = PyObject_Str(uself->encoding);
1579 if (encoding_str == NULL)
1580 goto done;
1581
1582 if (uself->start < PyBytes_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Christian Heimes72b710a2008-05-26 13:28:38 +00001583 int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
Eric Smith0facd772010-02-24 15:42:29 +00001584 result = PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001585 "'%U' codec can't decode byte 0x%02x in position %zd: %U",
Eric Smith0facd772010-02-24 15:42:29 +00001586 encoding_str,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001587 byte,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001588 uself->start,
Eric Smith0facd772010-02-24 15:42:29 +00001589 reason_str);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001590 }
Eric Smith0facd772010-02-24 15:42:29 +00001591 else {
1592 result = PyUnicode_FromFormat(
1593 "'%U' codec can't decode bytes in position %zd-%zd: %U",
1594 encoding_str,
1595 uself->start,
1596 uself->end-1,
1597 reason_str
1598 );
1599 }
1600done:
1601 Py_XDECREF(reason_str);
1602 Py_XDECREF(encoding_str);
1603 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001604}
1605
1606static PyTypeObject _PyExc_UnicodeDecodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001607 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001608 "UnicodeDecodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001609 sizeof(PyUnicodeErrorObject), 0,
1610 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1611 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1612 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001613 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1614 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001615 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001616 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001617};
1618PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1619
1620PyObject *
1621PyUnicodeDecodeError_Create(
1622 const char *encoding, const char *object, Py_ssize_t length,
1623 Py_ssize_t start, Py_ssize_t end, const char *reason)
1624{
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001625 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001626 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001627}
1628
1629
1630/*
1631 * UnicodeTranslateError extends UnicodeError
1632 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001633
1634static int
1635UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1636 PyObject *kwds)
1637{
1638 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1639 return -1;
1640
1641 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001642 Py_CLEAR(self->reason);
1643
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001644 if (!PyArg_ParseTuple(args, "O!nnO!",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001645 &PyUnicode_Type, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001646 &self->start,
1647 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001648 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001649 self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001650 return -1;
1651 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001652
Thomas Wouters477c8d52006-05-27 19:21:47 +00001653 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001654 Py_INCREF(self->reason);
1655
1656 return 0;
1657}
1658
1659
1660static PyObject *
1661UnicodeTranslateError_str(PyObject *self)
1662{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001663 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith0facd772010-02-24 15:42:29 +00001664 PyObject *result = NULL;
1665 PyObject *reason_str = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001666
Eric Smith0facd772010-02-24 15:42:29 +00001667 /* Get reason as a string, which it might not be if it's been
1668 modified after we were contructed. */
1669 reason_str = PyObject_Str(uself->reason);
1670 if (reason_str == NULL)
1671 goto done;
1672
1673 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001674 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Walter Dörwald787b03b2007-06-05 13:29:29 +00001675 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001676 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001677 fmt = "can't translate character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001678 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001679 fmt = "can't translate character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001680 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001681 fmt = "can't translate character '\\U%08x' in position %zd: %U";
Benjamin Petersonc5f4e1e2010-02-25 01:22:28 +00001682 result = PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001683 fmt,
1684 badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001685 uself->start,
Eric Smith0facd772010-02-24 15:42:29 +00001686 reason_str
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001687 );
Eric Smith0facd772010-02-24 15:42:29 +00001688 } else {
1689 result = PyUnicode_FromFormat(
1690 "can't translate characters in position %zd-%zd: %U",
1691 uself->start,
1692 uself->end-1,
1693 reason_str
1694 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001695 }
Eric Smith0facd772010-02-24 15:42:29 +00001696done:
1697 Py_XDECREF(reason_str);
1698 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001699}
1700
1701static PyTypeObject _PyExc_UnicodeTranslateError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001702 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001703 "UnicodeTranslateError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001704 sizeof(PyUnicodeErrorObject), 0,
1705 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1706 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1707 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001708 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001709 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1710 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001711 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001712};
1713PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1714
1715PyObject *
1716PyUnicodeTranslateError_Create(
1717 const Py_UNICODE *object, Py_ssize_t length,
1718 Py_ssize_t start, Py_ssize_t end, const char *reason)
1719{
1720 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001721 object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001722}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001723
1724
1725/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001726 * AssertionError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001727 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001728SimpleExtendsException(PyExc_Exception, AssertionError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001729 "Assertion failed.");
1730
1731
1732/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001733 * ArithmeticError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001734 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001735SimpleExtendsException(PyExc_Exception, ArithmeticError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001736 "Base class for arithmetic errors.");
1737
1738
1739/*
1740 * FloatingPointError extends ArithmeticError
1741 */
1742SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
1743 "Floating point operation failed.");
1744
1745
1746/*
1747 * OverflowError extends ArithmeticError
1748 */
1749SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
1750 "Result too large to be represented.");
1751
1752
1753/*
1754 * ZeroDivisionError extends ArithmeticError
1755 */
1756SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
1757 "Second argument to a division or modulo operation was zero.");
1758
1759
1760/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001761 * SystemError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001762 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001763SimpleExtendsException(PyExc_Exception, SystemError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001764 "Internal error in the Python interpreter.\n"
1765 "\n"
1766 "Please report this to the Python maintainer, along with the traceback,\n"
1767 "the Python version, and the hardware/OS platform and version.");
1768
1769
1770/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001771 * ReferenceError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001772 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001773SimpleExtendsException(PyExc_Exception, ReferenceError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001774 "Weak ref proxy used after referent went away.");
1775
1776
1777/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001778 * MemoryError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001779 */
Antoine Pitrou07e20ef2010-10-28 22:56:58 +00001780
1781#define MEMERRORS_SAVE 16
1782static PyBaseExceptionObject *memerrors_freelist = NULL;
1783static int memerrors_numfree = 0;
1784
1785static PyObject *
1786MemoryError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1787{
1788 PyBaseExceptionObject *self;
1789
1790 if (type != (PyTypeObject *) PyExc_MemoryError)
1791 return BaseException_new(type, args, kwds);
1792 if (memerrors_freelist == NULL)
1793 return BaseException_new(type, args, kwds);
1794 /* Fetch object from freelist and revive it */
1795 self = memerrors_freelist;
1796 self->args = PyTuple_New(0);
1797 /* This shouldn't happen since the empty tuple is persistent */
1798 if (self->args == NULL)
1799 return NULL;
1800 memerrors_freelist = (PyBaseExceptionObject *) self->dict;
1801 memerrors_numfree--;
1802 self->dict = NULL;
1803 _Py_NewReference((PyObject *)self);
1804 _PyObject_GC_TRACK(self);
1805 return (PyObject *)self;
1806}
1807
1808static void
1809MemoryError_dealloc(PyBaseExceptionObject *self)
1810{
1811 _PyObject_GC_UNTRACK(self);
1812 BaseException_clear(self);
1813 if (memerrors_numfree >= MEMERRORS_SAVE)
1814 Py_TYPE(self)->tp_free((PyObject *)self);
1815 else {
1816 self->dict = (PyObject *) memerrors_freelist;
1817 memerrors_freelist = self;
1818 memerrors_numfree++;
1819 }
1820}
1821
1822static void
1823preallocate_memerrors(void)
1824{
1825 /* We create enough MemoryErrors and then decref them, which will fill
1826 up the freelist. */
1827 int i;
1828 PyObject *errors[MEMERRORS_SAVE];
1829 for (i = 0; i < MEMERRORS_SAVE; i++) {
1830 errors[i] = MemoryError_new((PyTypeObject *) PyExc_MemoryError,
1831 NULL, NULL);
1832 if (!errors[i])
1833 Py_FatalError("Could not preallocate MemoryError object");
1834 }
1835 for (i = 0; i < MEMERRORS_SAVE; i++) {
1836 Py_DECREF(errors[i]);
1837 }
1838}
1839
1840static void
1841free_preallocated_memerrors(void)
1842{
1843 while (memerrors_freelist != NULL) {
1844 PyObject *self = (PyObject *) memerrors_freelist;
1845 memerrors_freelist = (PyBaseExceptionObject *) memerrors_freelist->dict;
1846 Py_TYPE(self)->tp_free((PyObject *)self);
1847 }
1848}
1849
1850
1851static PyTypeObject _PyExc_MemoryError = {
1852 PyVarObject_HEAD_INIT(NULL, 0)
1853 "MemoryError",
1854 sizeof(PyBaseExceptionObject),
1855 0, (destructor)MemoryError_dealloc, 0, 0, 0, 0, 0, 0, 0,
1856 0, 0, 0, 0, 0, 0, 0,
1857 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1858 PyDoc_STR("Out of memory."), (traverseproc)BaseException_traverse,
1859 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_PyExc_Exception,
1860 0, 0, 0, offsetof(PyBaseExceptionObject, dict),
1861 (initproc)BaseException_init, 0, MemoryError_new
1862};
1863PyObject *PyExc_MemoryError = (PyObject *) &_PyExc_MemoryError;
1864
Thomas Wouters477c8d52006-05-27 19:21:47 +00001865
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001866/*
1867 * BufferError extends Exception
1868 */
1869SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error.");
1870
Thomas Wouters477c8d52006-05-27 19:21:47 +00001871
1872/* Warning category docstrings */
1873
1874/*
1875 * Warning extends Exception
1876 */
1877SimpleExtendsException(PyExc_Exception, Warning,
1878 "Base class for warning categories.");
1879
1880
1881/*
1882 * UserWarning extends Warning
1883 */
1884SimpleExtendsException(PyExc_Warning, UserWarning,
1885 "Base class for warnings generated by user code.");
1886
1887
1888/*
1889 * DeprecationWarning extends Warning
1890 */
1891SimpleExtendsException(PyExc_Warning, DeprecationWarning,
1892 "Base class for warnings about deprecated features.");
1893
1894
1895/*
1896 * PendingDeprecationWarning extends Warning
1897 */
1898SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1899 "Base class for warnings about features which will be deprecated\n"
1900 "in the future.");
1901
1902
1903/*
1904 * SyntaxWarning extends Warning
1905 */
1906SimpleExtendsException(PyExc_Warning, SyntaxWarning,
1907 "Base class for warnings about dubious syntax.");
1908
1909
1910/*
1911 * RuntimeWarning extends Warning
1912 */
1913SimpleExtendsException(PyExc_Warning, RuntimeWarning,
1914 "Base class for warnings about dubious runtime behavior.");
1915
1916
1917/*
1918 * FutureWarning extends Warning
1919 */
1920SimpleExtendsException(PyExc_Warning, FutureWarning,
1921 "Base class for warnings about constructs that will change semantically\n"
1922 "in the future.");
1923
1924
1925/*
1926 * ImportWarning extends Warning
1927 */
1928SimpleExtendsException(PyExc_Warning, ImportWarning,
1929 "Base class for warnings about probable mistakes in module imports");
1930
1931
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001932/*
1933 * UnicodeWarning extends Warning
1934 */
1935SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1936 "Base class for warnings about Unicode related problems, mostly\n"
1937 "related to conversion problems.");
1938
Georg Brandl08be72d2010-10-24 15:11:22 +00001939
Guido van Rossum98297ee2007-11-06 21:34:58 +00001940/*
1941 * BytesWarning extends Warning
1942 */
1943SimpleExtendsException(PyExc_Warning, BytesWarning,
1944 "Base class for warnings about bytes and buffer related problems, mostly\n"
1945 "related to conversion from str or comparing to str.");
1946
1947
Georg Brandl08be72d2010-10-24 15:11:22 +00001948/*
1949 * ResourceWarning extends Warning
1950 */
1951SimpleExtendsException(PyExc_Warning, ResourceWarning,
1952 "Base class for warnings about resource usage.");
1953
1954
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001955
Thomas Wouters89d996e2007-09-08 17:39:28 +00001956/* Pre-computed RuntimeError instance for when recursion depth is reached.
1957 Meant to be used when normalizing the exception for exceeding the recursion
1958 depth will cause its own infinite recursion.
1959*/
1960PyObject *PyExc_RecursionErrorInst = NULL;
1961
Thomas Wouters477c8d52006-05-27 19:21:47 +00001962#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1963 Py_FatalError("exceptions bootstrapping error.");
1964
1965#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001966 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1967 Py_FatalError("Module dictionary insertion problem.");
1968
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001969
Martin v. Löwis1a214512008-06-11 05:26:20 +00001970void
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001971_PyExc_Init(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001972{
Neal Norwitz2633c692007-02-26 22:22:47 +00001973 PyObject *bltinmod, *bdict;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001974
1975 PRE_INIT(BaseException)
1976 PRE_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001977 PRE_INIT(TypeError)
1978 PRE_INIT(StopIteration)
1979 PRE_INIT(GeneratorExit)
1980 PRE_INIT(SystemExit)
1981 PRE_INIT(KeyboardInterrupt)
1982 PRE_INIT(ImportError)
1983 PRE_INIT(EnvironmentError)
1984 PRE_INIT(IOError)
1985 PRE_INIT(OSError)
1986#ifdef MS_WINDOWS
1987 PRE_INIT(WindowsError)
1988#endif
1989#ifdef __VMS
1990 PRE_INIT(VMSError)
1991#endif
1992 PRE_INIT(EOFError)
1993 PRE_INIT(RuntimeError)
1994 PRE_INIT(NotImplementedError)
1995 PRE_INIT(NameError)
1996 PRE_INIT(UnboundLocalError)
1997 PRE_INIT(AttributeError)
1998 PRE_INIT(SyntaxError)
1999 PRE_INIT(IndentationError)
2000 PRE_INIT(TabError)
2001 PRE_INIT(LookupError)
2002 PRE_INIT(IndexError)
2003 PRE_INIT(KeyError)
2004 PRE_INIT(ValueError)
2005 PRE_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002006 PRE_INIT(UnicodeEncodeError)
2007 PRE_INIT(UnicodeDecodeError)
2008 PRE_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002009 PRE_INIT(AssertionError)
2010 PRE_INIT(ArithmeticError)
2011 PRE_INIT(FloatingPointError)
2012 PRE_INIT(OverflowError)
2013 PRE_INIT(ZeroDivisionError)
2014 PRE_INIT(SystemError)
2015 PRE_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00002016 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002017 PRE_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00002018 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002019 PRE_INIT(Warning)
2020 PRE_INIT(UserWarning)
2021 PRE_INIT(DeprecationWarning)
2022 PRE_INIT(PendingDeprecationWarning)
2023 PRE_INIT(SyntaxWarning)
2024 PRE_INIT(RuntimeWarning)
2025 PRE_INIT(FutureWarning)
2026 PRE_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002027 PRE_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00002028 PRE_INIT(BytesWarning)
Georg Brandl08be72d2010-10-24 15:11:22 +00002029 PRE_INIT(ResourceWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002030
Georg Brandl1a3284e2007-12-02 09:40:06 +00002031 bltinmod = PyImport_ImportModule("builtins");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002032 if (bltinmod == NULL)
2033 Py_FatalError("exceptions bootstrapping error.");
2034 bdict = PyModule_GetDict(bltinmod);
2035 if (bdict == NULL)
2036 Py_FatalError("exceptions bootstrapping error.");
2037
2038 POST_INIT(BaseException)
2039 POST_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002040 POST_INIT(TypeError)
2041 POST_INIT(StopIteration)
2042 POST_INIT(GeneratorExit)
2043 POST_INIT(SystemExit)
2044 POST_INIT(KeyboardInterrupt)
2045 POST_INIT(ImportError)
2046 POST_INIT(EnvironmentError)
2047 POST_INIT(IOError)
2048 POST_INIT(OSError)
2049#ifdef MS_WINDOWS
2050 POST_INIT(WindowsError)
2051#endif
2052#ifdef __VMS
2053 POST_INIT(VMSError)
2054#endif
2055 POST_INIT(EOFError)
2056 POST_INIT(RuntimeError)
2057 POST_INIT(NotImplementedError)
2058 POST_INIT(NameError)
2059 POST_INIT(UnboundLocalError)
2060 POST_INIT(AttributeError)
2061 POST_INIT(SyntaxError)
2062 POST_INIT(IndentationError)
2063 POST_INIT(TabError)
2064 POST_INIT(LookupError)
2065 POST_INIT(IndexError)
2066 POST_INIT(KeyError)
2067 POST_INIT(ValueError)
2068 POST_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002069 POST_INIT(UnicodeEncodeError)
2070 POST_INIT(UnicodeDecodeError)
2071 POST_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002072 POST_INIT(AssertionError)
2073 POST_INIT(ArithmeticError)
2074 POST_INIT(FloatingPointError)
2075 POST_INIT(OverflowError)
2076 POST_INIT(ZeroDivisionError)
2077 POST_INIT(SystemError)
2078 POST_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00002079 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002080 POST_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00002081 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002082 POST_INIT(Warning)
2083 POST_INIT(UserWarning)
2084 POST_INIT(DeprecationWarning)
2085 POST_INIT(PendingDeprecationWarning)
2086 POST_INIT(SyntaxWarning)
2087 POST_INIT(RuntimeWarning)
2088 POST_INIT(FutureWarning)
2089 POST_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002090 POST_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00002091 POST_INIT(BytesWarning)
Georg Brandl08be72d2010-10-24 15:11:22 +00002092 POST_INIT(ResourceWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002093
Antoine Pitrou07e20ef2010-10-28 22:56:58 +00002094 preallocate_memerrors();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002095
Thomas Wouters89d996e2007-09-08 17:39:28 +00002096 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2097 if (!PyExc_RecursionErrorInst)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2099 "recursion errors");
Thomas Wouters89d996e2007-09-08 17:39:28 +00002100 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 PyBaseExceptionObject *err_inst =
2102 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2103 PyObject *args_tuple;
2104 PyObject *exc_message;
2105 exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
2106 if (!exc_message)
2107 Py_FatalError("cannot allocate argument for RuntimeError "
2108 "pre-allocation");
2109 args_tuple = PyTuple_Pack(1, exc_message);
2110 if (!args_tuple)
2111 Py_FatalError("cannot allocate tuple for RuntimeError "
2112 "pre-allocation");
2113 Py_DECREF(exc_message);
2114 if (BaseException_init(err_inst, args_tuple, NULL))
2115 Py_FatalError("init of pre-allocated RuntimeError failed");
2116 Py_DECREF(args_tuple);
Thomas Wouters89d996e2007-09-08 17:39:28 +00002117 }
2118
Thomas Wouters477c8d52006-05-27 19:21:47 +00002119 Py_DECREF(bltinmod);
2120}
2121
2122void
2123_PyExc_Fini(void)
2124{
Benjamin Peterson78565b22009-06-28 19:19:51 +00002125 Py_CLEAR(PyExc_RecursionErrorInst);
Antoine Pitrou07e20ef2010-10-28 22:56:58 +00002126 free_preallocated_memerrors();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002127}