blob: 26571856492856d36452f522d84aa36071a74dc3 [file] [log] [blame]
Georg Brandl43ab1002006-05-28 20:57:09 +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
Richard Jones7b9558d2006-05-27 12:29:24 +00007#define PY_SSIZE_T_CLEAN
8#include <Python.h>
9#include "structmember.h"
10#include "osdefs.h"
11
Richard Jones7b9558d2006-05-27 12:29:24 +000012#define EXC_MODULE_NAME "exceptions."
13
Richard Jonesc5b2a2e2006-05-27 16:07:28 +000014/* NOTE: If the exception class hierarchy changes, don't forget to update
15 * Lib/test/exception_hierarchy.txt
16 */
17
18PyDoc_STRVAR(exceptions_doc, "Python's standard exception class hierarchy.\n\
19\n\
20Exceptions found here are defined both in the exceptions module and the\n\
21built-in namespace. It is recommended that user-defined exceptions\n\
22inherit from Exception. See the documentation for the exception\n\
23inheritance hierarchy.\n\
24");
25
Richard Jones7b9558d2006-05-27 12:29:24 +000026/*
27 * BaseException
28 */
29static PyObject *
30BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
31{
32 PyBaseExceptionObject *self;
33
34 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
Georg Brandlc02e1312007-04-11 17:16:24 +000035 if (!self)
36 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +000037 /* the dict is created on the fly in PyObject_GenericSetAttr */
38 self->message = self->dict = NULL;
39
Georg Brandld7e9f602007-08-21 06:03:43 +000040 self->args = PyTuple_New(0);
41 if (!self->args) {
Richard Jones7b9558d2006-05-27 12:29:24 +000042 Py_DECREF(self);
43 return NULL;
44 }
Georg Brandld7e9f602007-08-21 06:03:43 +000045
Gregory P. Smithdd96db62008-06-09 04:58:54 +000046 self->message = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +000047 if (!self->message) {
48 Py_DECREF(self);
49 return NULL;
50 }
Georg Brandld7e9f602007-08-21 06:03:43 +000051
Richard Jones7b9558d2006-05-27 12:29:24 +000052 return (PyObject *)self;
53}
54
55static int
56BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
57{
Christian Heimese93237d2007-12-19 02:37:44 +000058 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
Georg Brandlb0432bc2006-05-30 08:17:00 +000059 return -1;
60
Richard Jones7b9558d2006-05-27 12:29:24 +000061 Py_DECREF(self->args);
62 self->args = args;
63 Py_INCREF(self->args);
64
65 if (PyTuple_GET_SIZE(self->args) == 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +000066 Py_CLEAR(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000067 self->message = PyTuple_GET_ITEM(self->args, 0);
Michael W. Hudson22a80e72006-05-28 15:51:40 +000068 Py_INCREF(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000069 }
70 return 0;
71}
72
Michael W. Hudson96495ee2006-05-28 17:40:29 +000073static int
Richard Jones7b9558d2006-05-27 12:29:24 +000074BaseException_clear(PyBaseExceptionObject *self)
75{
76 Py_CLEAR(self->dict);
77 Py_CLEAR(self->args);
78 Py_CLEAR(self->message);
79 return 0;
80}
81
82static void
83BaseException_dealloc(PyBaseExceptionObject *self)
84{
Georg Brandl38f62372006-09-06 06:50:05 +000085 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +000086 BaseException_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +000087 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +000088}
89
Michael W. Hudson96495ee2006-05-28 17:40:29 +000090static int
Richard Jones7b9558d2006-05-27 12:29:24 +000091BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
92{
Michael W. Hudson22a80e72006-05-28 15:51:40 +000093 Py_VISIT(self->dict);
Richard Jones7b9558d2006-05-27 12:29:24 +000094 Py_VISIT(self->args);
95 Py_VISIT(self->message);
96 return 0;
97}
98
99static PyObject *
100BaseException_str(PyBaseExceptionObject *self)
101{
102 PyObject *out;
103
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000104 switch (PyTuple_GET_SIZE(self->args)) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000105 case 0:
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000106 out = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +0000107 break;
108 case 1:
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000109 out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +0000110 break;
Richard Jones7b9558d2006-05-27 12:29:24 +0000111 default:
112 out = PyObject_Str(self->args);
113 break;
114 }
115
116 return out;
117}
118
Nick Coghlan524b7772008-07-08 14:08:04 +0000119#ifdef Py_USING_UNICODE
120static PyObject *
121BaseException_unicode(PyBaseExceptionObject *self)
122{
123 PyObject *out;
124
Ezio Melottif84caf42009-12-24 22:25:17 +0000125 /* issue6108: if __str__ has been overridden in the subclass, unicode()
126 should return the message returned by __str__ as used to happen
127 before this method was implemented. */
128 if (Py_TYPE(self)->tp_str != (reprfunc)BaseException_str) {
129 PyObject *str;
130 /* Unlike PyObject_Str, tp_str can return unicode (i.e. return the
131 equivalent of unicode(e.__str__()) instead of unicode(str(e))). */
132 str = Py_TYPE(self)->tp_str((PyObject*)self);
133 if (str == NULL)
134 return NULL;
135 out = PyObject_Unicode(str);
136 Py_DECREF(str);
137 return out;
138 }
139
Nick Coghlan524b7772008-07-08 14:08:04 +0000140 switch (PyTuple_GET_SIZE(self->args)) {
141 case 0:
142 out = PyUnicode_FromString("");
143 break;
144 case 1:
145 out = PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));
146 break;
147 default:
148 out = PyObject_Unicode(self->args);
149 break;
150 }
151
152 return out;
153}
154#endif
155
Richard Jones7b9558d2006-05-27 12:29:24 +0000156static PyObject *
157BaseException_repr(PyBaseExceptionObject *self)
158{
Richard Jones7b9558d2006-05-27 12:29:24 +0000159 PyObject *repr_suffix;
160 PyObject *repr;
161 char *name;
162 char *dot;
163
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000164 repr_suffix = PyObject_Repr(self->args);
165 if (!repr_suffix)
Richard Jones7b9558d2006-05-27 12:29:24 +0000166 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000167
Christian Heimese93237d2007-12-19 02:37:44 +0000168 name = (char *)Py_TYPE(self)->tp_name;
Richard Jones7b9558d2006-05-27 12:29:24 +0000169 dot = strrchr(name, '.');
170 if (dot != NULL) name = dot+1;
171
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000172 repr = PyString_FromString(name);
Richard Jones7b9558d2006-05-27 12:29:24 +0000173 if (!repr) {
174 Py_DECREF(repr_suffix);
175 return NULL;
176 }
177
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000178 PyString_ConcatAndDel(&repr, repr_suffix);
Richard Jones7b9558d2006-05-27 12:29:24 +0000179 return repr;
180}
181
182/* Pickling support */
183static PyObject *
184BaseException_reduce(PyBaseExceptionObject *self)
185{
Georg Brandlddba4732006-05-30 07:04:55 +0000186 if (self->args && self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000187 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000188 else
Christian Heimese93237d2007-12-19 02:37:44 +0000189 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000190}
191
Georg Brandl85ac8502006-06-01 06:39:19 +0000192/*
193 * Needed for backward compatibility, since exceptions used to store
194 * all their attributes in the __dict__. Code is taken from cPickle's
195 * load_build function.
196 */
197static PyObject *
198BaseException_setstate(PyObject *self, PyObject *state)
199{
200 PyObject *d_key, *d_value;
201 Py_ssize_t i = 0;
202
203 if (state != Py_None) {
204 if (!PyDict_Check(state)) {
205 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
206 return NULL;
207 }
208 while (PyDict_Next(state, &i, &d_key, &d_value)) {
209 if (PyObject_SetAttr(self, d_key, d_value) < 0)
210 return NULL;
211 }
212 }
213 Py_RETURN_NONE;
214}
Richard Jones7b9558d2006-05-27 12:29:24 +0000215
Richard Jones7b9558d2006-05-27 12:29:24 +0000216
217static PyMethodDef BaseException_methods[] = {
218 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Georg Brandl85ac8502006-06-01 06:39:19 +0000219 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000220#ifdef Py_USING_UNICODE
Nick Coghlan524b7772008-07-08 14:08:04 +0000221 {"__unicode__", (PyCFunction)BaseException_unicode, METH_NOARGS },
222#endif
Richard Jones7b9558d2006-05-27 12:29:24 +0000223 {NULL, NULL, 0, NULL},
224};
225
226
227
228static PyObject *
229BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
230{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000231 if (PyErr_WarnPy3k("__getitem__ not supported for exception "
232 "classes in 3.x; use args attribute", 1) < 0)
233 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000234 return PySequence_GetItem(self->args, index);
235}
236
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000237static PyObject *
238BaseException_getslice(PyBaseExceptionObject *self,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000239 Py_ssize_t start, Py_ssize_t stop)
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000240{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000241 if (PyErr_WarnPy3k("__getslice__ not supported for exception "
242 "classes in 3.x; use args attribute", 1) < 0)
243 return NULL;
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000244 return PySequence_GetSlice(self->args, start, stop);
245}
246
Richard Jones7b9558d2006-05-27 12:29:24 +0000247static PySequenceMethods BaseException_as_sequence = {
248 0, /* sq_length; */
249 0, /* sq_concat; */
250 0, /* sq_repeat; */
251 (ssizeargfunc)BaseException_getitem, /* sq_item; */
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000252 (ssizessizeargfunc)BaseException_getslice, /* sq_slice; */
Richard Jones7b9558d2006-05-27 12:29:24 +0000253 0, /* sq_ass_item; */
254 0, /* sq_ass_slice; */
255 0, /* sq_contains; */
256 0, /* sq_inplace_concat; */
257 0 /* sq_inplace_repeat; */
258};
259
Richard Jones7b9558d2006-05-27 12:29:24 +0000260static PyObject *
261BaseException_get_dict(PyBaseExceptionObject *self)
262{
263 if (self->dict == NULL) {
264 self->dict = PyDict_New();
265 if (!self->dict)
266 return NULL;
267 }
268 Py_INCREF(self->dict);
269 return self->dict;
270}
271
272static int
273BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
274{
275 if (val == NULL) {
276 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
277 return -1;
278 }
279 if (!PyDict_Check(val)) {
280 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
281 return -1;
282 }
283 Py_CLEAR(self->dict);
284 Py_INCREF(val);
285 self->dict = val;
286 return 0;
287}
288
289static PyObject *
290BaseException_get_args(PyBaseExceptionObject *self)
291{
292 if (self->args == NULL) {
293 Py_INCREF(Py_None);
294 return Py_None;
295 }
296 Py_INCREF(self->args);
297 return self->args;
298}
299
300static int
301BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
302{
303 PyObject *seq;
304 if (val == NULL) {
305 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
306 return -1;
307 }
308 seq = PySequence_Tuple(val);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -0500309 if (!seq)
310 return -1;
Georg Brandlc7c51142006-05-29 09:46:51 +0000311 Py_CLEAR(self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000312 self->args = seq;
313 return 0;
314}
315
Brett Cannon229cee22007-05-05 01:34:02 +0000316static PyObject *
317BaseException_get_message(PyBaseExceptionObject *self)
318{
Georg Brandl0674d3f2009-09-16 20:30:09 +0000319 PyObject *msg;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000320
Georg Brandl0674d3f2009-09-16 20:30:09 +0000321 /* if "message" is in self->dict, accessing a user-set message attribute */
322 if (self->dict &&
323 (msg = PyDict_GetItemString(self->dict, "message"))) {
324 Py_INCREF(msg);
325 return msg;
326 }
Brett Cannon229cee22007-05-05 01:34:02 +0000327
Georg Brandl0674d3f2009-09-16 20:30:09 +0000328 if (self->message == NULL) {
329 PyErr_SetString(PyExc_AttributeError, "message attribute was deleted");
330 return NULL;
331 }
332
333 /* accessing the deprecated "builtin" message attribute of Exception */
334 if (PyErr_WarnEx(PyExc_DeprecationWarning,
335 "BaseException.message has been deprecated as "
336 "of Python 2.6", 1) < 0)
337 return NULL;
338
339 Py_INCREF(self->message);
340 return self->message;
Brett Cannon229cee22007-05-05 01:34:02 +0000341}
342
343static int
344BaseException_set_message(PyBaseExceptionObject *self, PyObject *val)
345{
Georg Brandl0674d3f2009-09-16 20:30:09 +0000346 /* if val is NULL, delete the message attribute */
347 if (val == NULL) {
348 if (self->dict && PyDict_GetItemString(self->dict, "message")) {
349 if (PyDict_DelItemString(self->dict, "message") < 0)
350 return -1;
351 }
352 Py_XDECREF(self->message);
353 self->message = NULL;
354 return 0;
355 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000356
Georg Brandl0674d3f2009-09-16 20:30:09 +0000357 /* else set it in __dict__, but may need to create the dict first */
358 if (self->dict == NULL) {
359 self->dict = PyDict_New();
360 if (!self->dict)
361 return -1;
362 }
363 return PyDict_SetItemString(self->dict, "message", val);
Brett Cannon229cee22007-05-05 01:34:02 +0000364}
365
Richard Jones7b9558d2006-05-27 12:29:24 +0000366static PyGetSetDef BaseException_getset[] = {
367 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
368 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Brett Cannon229cee22007-05-05 01:34:02 +0000369 {"message", (getter)BaseException_get_message,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000370 (setter)BaseException_set_message},
Richard Jones7b9558d2006-05-27 12:29:24 +0000371 {NULL},
372};
373
374
375static PyTypeObject _PyExc_BaseException = {
376 PyObject_HEAD_INIT(NULL)
377 0, /*ob_size*/
378 EXC_MODULE_NAME "BaseException", /*tp_name*/
379 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
380 0, /*tp_itemsize*/
381 (destructor)BaseException_dealloc, /*tp_dealloc*/
382 0, /*tp_print*/
383 0, /*tp_getattr*/
384 0, /*tp_setattr*/
385 0, /* tp_compare; */
386 (reprfunc)BaseException_repr, /*tp_repr*/
387 0, /*tp_as_number*/
388 &BaseException_as_sequence, /*tp_as_sequence*/
389 0, /*tp_as_mapping*/
390 0, /*tp_hash */
391 0, /*tp_call*/
392 (reprfunc)BaseException_str, /*tp_str*/
393 PyObject_GenericGetAttr, /*tp_getattro*/
394 PyObject_GenericSetAttr, /*tp_setattro*/
395 0, /*tp_as_buffer*/
Neal Norwitzee3a1b52007-02-25 19:44:48 +0000396 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000397 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Richard Jones7b9558d2006-05-27 12:29:24 +0000398 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
399 (traverseproc)BaseException_traverse, /* tp_traverse */
400 (inquiry)BaseException_clear, /* tp_clear */
401 0, /* tp_richcompare */
402 0, /* tp_weaklistoffset */
403 0, /* tp_iter */
404 0, /* tp_iternext */
405 BaseException_methods, /* tp_methods */
Brett Cannon229cee22007-05-05 01:34:02 +0000406 0, /* tp_members */
Richard Jones7b9558d2006-05-27 12:29:24 +0000407 BaseException_getset, /* tp_getset */
408 0, /* tp_base */
409 0, /* tp_dict */
410 0, /* tp_descr_get */
411 0, /* tp_descr_set */
412 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
413 (initproc)BaseException_init, /* tp_init */
414 0, /* tp_alloc */
415 BaseException_new, /* tp_new */
416};
417/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
418from the previous implmentation and also allowing Python objects to be used
419in the API */
420PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
421
Richard Jones2d555b32006-05-27 16:15:11 +0000422/* note these macros omit the last semicolon so the macro invocation may
423 * include it and not look strange.
424 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000425#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
426static PyTypeObject _PyExc_ ## EXCNAME = { \
427 PyObject_HEAD_INIT(NULL) \
428 0, \
429 EXC_MODULE_NAME # EXCNAME, \
430 sizeof(PyBaseExceptionObject), \
431 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
432 0, 0, 0, 0, 0, 0, 0, \
433 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
434 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
435 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
436 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
437 (initproc)BaseException_init, 0, BaseException_new,\
438}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000439PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000440
441#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
442static PyTypeObject _PyExc_ ## EXCNAME = { \
443 PyObject_HEAD_INIT(NULL) \
444 0, \
445 EXC_MODULE_NAME # EXCNAME, \
446 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000447 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000448 0, 0, 0, 0, 0, \
449 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000450 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
451 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000452 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000453 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000454}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000455PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000456
457#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
458static PyTypeObject _PyExc_ ## EXCNAME = { \
459 PyObject_HEAD_INIT(NULL) \
460 0, \
461 EXC_MODULE_NAME # EXCNAME, \
462 sizeof(Py ## EXCSTORE ## Object), 0, \
463 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
464 (reprfunc)EXCSTR, 0, 0, 0, \
465 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
466 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
467 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
468 EXCMEMBERS, 0, &_ ## EXCBASE, \
469 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000470 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000471}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000472PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000473
474
475/*
476 * Exception extends BaseException
477 */
478SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000479 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000480
481
482/*
483 * StandardError extends Exception
484 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000485SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000486 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000487 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000488
489
490/*
491 * TypeError extends StandardError
492 */
493SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000494 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000495
496
497/*
498 * StopIteration extends Exception
499 */
500SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000501 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000502
503
504/*
Christian Heimes44eeaec2007-12-03 20:01:02 +0000505 * GeneratorExit extends BaseException
Richard Jones7b9558d2006-05-27 12:29:24 +0000506 */
Christian Heimes44eeaec2007-12-03 20:01:02 +0000507SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000508 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000509
510
511/*
512 * SystemExit extends BaseException
513 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000514
515static int
516SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
517{
518 Py_ssize_t size = PyTuple_GET_SIZE(args);
519
520 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
521 return -1;
522
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000523 if (size == 0)
524 return 0;
525 Py_CLEAR(self->code);
Richard Jones7b9558d2006-05-27 12:29:24 +0000526 if (size == 1)
527 self->code = PyTuple_GET_ITEM(args, 0);
528 else if (size > 1)
529 self->code = args;
530 Py_INCREF(self->code);
531 return 0;
532}
533
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000534static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000535SystemExit_clear(PySystemExitObject *self)
536{
537 Py_CLEAR(self->code);
538 return BaseException_clear((PyBaseExceptionObject *)self);
539}
540
541static void
542SystemExit_dealloc(PySystemExitObject *self)
543{
Georg Brandl38f62372006-09-06 06:50:05 +0000544 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000545 SystemExit_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000546 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000547}
548
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000549static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000550SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
551{
552 Py_VISIT(self->code);
553 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
554}
555
556static PyMemberDef SystemExit_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000557 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
558 PyDoc_STR("exception code")},
559 {NULL} /* Sentinel */
560};
561
562ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
563 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000564 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000565
566/*
567 * KeyboardInterrupt extends BaseException
568 */
569SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000570 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000571
572
573/*
574 * ImportError extends StandardError
575 */
576SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000577 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000578
579
580/*
581 * EnvironmentError extends StandardError
582 */
583
Richard Jones7b9558d2006-05-27 12:29:24 +0000584/* Where a function has a single filename, such as open() or some
585 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
586 * called, giving a third argument which is the filename. But, so
587 * that old code using in-place unpacking doesn't break, e.g.:
588 *
589 * except IOError, (errno, strerror):
590 *
591 * we hack args so that it only contains two items. This also
592 * means we need our own __str__() which prints out the filename
593 * when it was supplied.
594 */
595static int
596EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
597 PyObject *kwds)
598{
599 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
600 PyObject *subslice = NULL;
601
602 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
603 return -1;
604
Georg Brandl3267d282006-09-30 09:03:42 +0000605 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000606 return 0;
607 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000608
609 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000610 &myerrno, &strerror, &filename)) {
611 return -1;
612 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000613 Py_CLEAR(self->myerrno); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000614 self->myerrno = myerrno;
615 Py_INCREF(self->myerrno);
616
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000617 Py_CLEAR(self->strerror); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000618 self->strerror = strerror;
619 Py_INCREF(self->strerror);
620
621 /* self->filename will remain Py_None otherwise */
622 if (filename != NULL) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000623 Py_CLEAR(self->filename); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000624 self->filename = filename;
625 Py_INCREF(self->filename);
626
627 subslice = PyTuple_GetSlice(args, 0, 2);
628 if (!subslice)
629 return -1;
630
631 Py_DECREF(self->args); /* replacing args */
632 self->args = subslice;
633 }
634 return 0;
635}
636
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000637static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000638EnvironmentError_clear(PyEnvironmentErrorObject *self)
639{
640 Py_CLEAR(self->myerrno);
641 Py_CLEAR(self->strerror);
642 Py_CLEAR(self->filename);
643 return BaseException_clear((PyBaseExceptionObject *)self);
644}
645
646static void
647EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
648{
Georg Brandl38f62372006-09-06 06:50:05 +0000649 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000650 EnvironmentError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000651 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000652}
653
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000654static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000655EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
656 void *arg)
657{
658 Py_VISIT(self->myerrno);
659 Py_VISIT(self->strerror);
660 Py_VISIT(self->filename);
661 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
662}
663
664static PyObject *
665EnvironmentError_str(PyEnvironmentErrorObject *self)
666{
667 PyObject *rtnval = NULL;
668
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000669 if (self->filename) {
670 PyObject *fmt;
671 PyObject *repr;
672 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000673
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000674 fmt = PyString_FromString("[Errno %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000675 if (!fmt)
676 return NULL;
677
678 repr = PyObject_Repr(self->filename);
679 if (!repr) {
680 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000681 return NULL;
682 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000683 tuple = PyTuple_New(3);
684 if (!tuple) {
685 Py_DECREF(repr);
686 Py_DECREF(fmt);
687 return NULL;
688 }
689
690 if (self->myerrno) {
691 Py_INCREF(self->myerrno);
692 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
693 }
694 else {
695 Py_INCREF(Py_None);
696 PyTuple_SET_ITEM(tuple, 0, Py_None);
697 }
698 if (self->strerror) {
699 Py_INCREF(self->strerror);
700 PyTuple_SET_ITEM(tuple, 1, self->strerror);
701 }
702 else {
703 Py_INCREF(Py_None);
704 PyTuple_SET_ITEM(tuple, 1, Py_None);
705 }
706
Richard Jones7b9558d2006-05-27 12:29:24 +0000707 PyTuple_SET_ITEM(tuple, 2, repr);
708
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000709 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000710
711 Py_DECREF(fmt);
712 Py_DECREF(tuple);
713 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000714 else if (self->myerrno && self->strerror) {
715 PyObject *fmt;
716 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000717
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000718 fmt = PyString_FromString("[Errno %s] %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000719 if (!fmt)
720 return NULL;
721
722 tuple = PyTuple_New(2);
723 if (!tuple) {
724 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000725 return NULL;
726 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000727
728 if (self->myerrno) {
729 Py_INCREF(self->myerrno);
730 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
731 }
732 else {
733 Py_INCREF(Py_None);
734 PyTuple_SET_ITEM(tuple, 0, Py_None);
735 }
736 if (self->strerror) {
737 Py_INCREF(self->strerror);
738 PyTuple_SET_ITEM(tuple, 1, self->strerror);
739 }
740 else {
741 Py_INCREF(Py_None);
742 PyTuple_SET_ITEM(tuple, 1, Py_None);
743 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000744
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000745 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000746
747 Py_DECREF(fmt);
748 Py_DECREF(tuple);
749 }
750 else
751 rtnval = BaseException_str((PyBaseExceptionObject *)self);
752
753 return rtnval;
754}
755
756static PyMemberDef EnvironmentError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000757 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000758 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000759 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000760 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000761 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000762 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000763 {NULL} /* Sentinel */
764};
765
766
767static PyObject *
768EnvironmentError_reduce(PyEnvironmentErrorObject *self)
769{
770 PyObject *args = self->args;
771 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000772
Richard Jones7b9558d2006-05-27 12:29:24 +0000773 /* self->args is only the first two real arguments if there was a
774 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000775 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000776 args = PyTuple_New(3);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -0500777 if (!args)
778 return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000779
780 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000781 Py_INCREF(tmp);
782 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000783
784 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000785 Py_INCREF(tmp);
786 PyTuple_SET_ITEM(args, 1, tmp);
787
788 Py_INCREF(self->filename);
789 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000790 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000791 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000792
793 if (self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000794 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000795 else
Christian Heimese93237d2007-12-19 02:37:44 +0000796 res = PyTuple_Pack(2, Py_TYPE(self), args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000797 Py_DECREF(args);
798 return res;
799}
800
801
802static PyMethodDef EnvironmentError_methods[] = {
803 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
804 {NULL}
805};
806
807ComplexExtendsException(PyExc_StandardError, EnvironmentError,
808 EnvironmentError, EnvironmentError_dealloc,
809 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000810 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000811 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000812
813
814/*
815 * IOError extends EnvironmentError
816 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000817MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000818 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000819
820
821/*
822 * OSError extends EnvironmentError
823 */
824MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000825 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000826
827
828/*
829 * WindowsError extends OSError
830 */
831#ifdef MS_WINDOWS
832#include "errmap.h"
833
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000834static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000835WindowsError_clear(PyWindowsErrorObject *self)
836{
837 Py_CLEAR(self->myerrno);
838 Py_CLEAR(self->strerror);
839 Py_CLEAR(self->filename);
840 Py_CLEAR(self->winerror);
841 return BaseException_clear((PyBaseExceptionObject *)self);
842}
843
844static void
845WindowsError_dealloc(PyWindowsErrorObject *self)
846{
Georg Brandl38f62372006-09-06 06:50:05 +0000847 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000848 WindowsError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000849 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000850}
851
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000852static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000853WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
854{
855 Py_VISIT(self->myerrno);
856 Py_VISIT(self->strerror);
857 Py_VISIT(self->filename);
858 Py_VISIT(self->winerror);
859 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
860}
861
Richard Jones7b9558d2006-05-27 12:29:24 +0000862static int
863WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
864{
865 PyObject *o_errcode = NULL;
866 long errcode;
867 long posix_errno;
868
869 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
870 == -1)
871 return -1;
872
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000873 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000874 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000875
876 /* Set errno to the POSIX errno, and winerror to the Win32
877 error code. */
878 errcode = PyInt_AsLong(self->myerrno);
879 if (errcode == -1 && PyErr_Occurred())
880 return -1;
881 posix_errno = winerror_to_errno(errcode);
882
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000883 Py_CLEAR(self->winerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000884 self->winerror = self->myerrno;
885
886 o_errcode = PyInt_FromLong(posix_errno);
887 if (!o_errcode)
888 return -1;
889
890 self->myerrno = o_errcode;
891
892 return 0;
893}
894
895
896static PyObject *
897WindowsError_str(PyWindowsErrorObject *self)
898{
Richard Jones7b9558d2006-05-27 12:29:24 +0000899 PyObject *rtnval = NULL;
900
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000901 if (self->filename) {
902 PyObject *fmt;
903 PyObject *repr;
904 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000905
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000906 fmt = PyString_FromString("[Error %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000907 if (!fmt)
908 return NULL;
909
910 repr = PyObject_Repr(self->filename);
911 if (!repr) {
912 Py_DECREF(fmt);
913 return NULL;
914 }
915 tuple = PyTuple_New(3);
916 if (!tuple) {
917 Py_DECREF(repr);
918 Py_DECREF(fmt);
919 return NULL;
920 }
921
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000922 if (self->winerror) {
923 Py_INCREF(self->winerror);
924 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000925 }
926 else {
927 Py_INCREF(Py_None);
928 PyTuple_SET_ITEM(tuple, 0, Py_None);
929 }
930 if (self->strerror) {
931 Py_INCREF(self->strerror);
932 PyTuple_SET_ITEM(tuple, 1, self->strerror);
933 }
934 else {
935 Py_INCREF(Py_None);
936 PyTuple_SET_ITEM(tuple, 1, Py_None);
937 }
938
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000939 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000940
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000941 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000942
943 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000944 Py_DECREF(tuple);
945 }
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000946 else if (self->winerror && self->strerror) {
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000947 PyObject *fmt;
948 PyObject *tuple;
949
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000950 fmt = PyString_FromString("[Error %s] %s");
Richard Jones7b9558d2006-05-27 12:29:24 +0000951 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000952 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000953
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000954 tuple = PyTuple_New(2);
955 if (!tuple) {
956 Py_DECREF(fmt);
957 return NULL;
958 }
959
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000960 if (self->winerror) {
961 Py_INCREF(self->winerror);
962 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000963 }
964 else {
965 Py_INCREF(Py_None);
966 PyTuple_SET_ITEM(tuple, 0, Py_None);
967 }
968 if (self->strerror) {
969 Py_INCREF(self->strerror);
970 PyTuple_SET_ITEM(tuple, 1, self->strerror);
971 }
972 else {
973 Py_INCREF(Py_None);
974 PyTuple_SET_ITEM(tuple, 1, Py_None);
975 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000976
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000977 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000978
979 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000980 Py_DECREF(tuple);
981 }
982 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000983 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000984
Richard Jones7b9558d2006-05-27 12:29:24 +0000985 return rtnval;
986}
987
988static PyMemberDef WindowsError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000989 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000990 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000991 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000992 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000993 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000994 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000995 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000996 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000997 {NULL} /* Sentinel */
998};
999
Richard Jones2d555b32006-05-27 16:15:11 +00001000ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
1001 WindowsError_dealloc, 0, WindowsError_members,
1002 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001003
1004#endif /* MS_WINDOWS */
1005
1006
1007/*
1008 * VMSError extends OSError (I think)
1009 */
1010#ifdef __VMS
1011MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +00001012 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001013#endif
1014
1015
1016/*
1017 * EOFError extends StandardError
1018 */
1019SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +00001020 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001021
1022
1023/*
1024 * RuntimeError extends StandardError
1025 */
1026SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001027 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001028
1029
1030/*
1031 * NotImplementedError extends RuntimeError
1032 */
1033SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +00001034 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001035
1036/*
1037 * NameError extends StandardError
1038 */
1039SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +00001040 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001041
1042/*
1043 * UnboundLocalError extends NameError
1044 */
1045SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +00001046 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001047
1048/*
1049 * AttributeError extends StandardError
1050 */
1051SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001052 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001053
1054
1055/*
1056 * SyntaxError extends StandardError
1057 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001058
1059static int
1060SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
1061{
1062 PyObject *info = NULL;
1063 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
1064
1065 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1066 return -1;
1067
1068 if (lenargs >= 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001069 Py_CLEAR(self->msg);
Richard Jones7b9558d2006-05-27 12:29:24 +00001070 self->msg = PyTuple_GET_ITEM(args, 0);
1071 Py_INCREF(self->msg);
1072 }
1073 if (lenargs == 2) {
1074 info = PyTuple_GET_ITEM(args, 1);
1075 info = PySequence_Tuple(info);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -05001076 if (!info)
1077 return -1;
Richard Jones7b9558d2006-05-27 12:29:24 +00001078
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001079 if (PyTuple_GET_SIZE(info) != 4) {
1080 /* not a very good error message, but it's what Python 2.4 gives */
1081 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
1082 Py_DECREF(info);
1083 return -1;
1084 }
1085
1086 Py_CLEAR(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001087 self->filename = PyTuple_GET_ITEM(info, 0);
1088 Py_INCREF(self->filename);
1089
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001090 Py_CLEAR(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001091 self->lineno = PyTuple_GET_ITEM(info, 1);
1092 Py_INCREF(self->lineno);
1093
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001094 Py_CLEAR(self->offset);
Richard Jones7b9558d2006-05-27 12:29:24 +00001095 self->offset = PyTuple_GET_ITEM(info, 2);
1096 Py_INCREF(self->offset);
1097
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001098 Py_CLEAR(self->text);
Richard Jones7b9558d2006-05-27 12:29:24 +00001099 self->text = PyTuple_GET_ITEM(info, 3);
1100 Py_INCREF(self->text);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001101
1102 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001103 }
1104 return 0;
1105}
1106
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001107static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001108SyntaxError_clear(PySyntaxErrorObject *self)
1109{
1110 Py_CLEAR(self->msg);
1111 Py_CLEAR(self->filename);
1112 Py_CLEAR(self->lineno);
1113 Py_CLEAR(self->offset);
1114 Py_CLEAR(self->text);
1115 Py_CLEAR(self->print_file_and_line);
1116 return BaseException_clear((PyBaseExceptionObject *)self);
1117}
1118
1119static void
1120SyntaxError_dealloc(PySyntaxErrorObject *self)
1121{
Georg Brandl38f62372006-09-06 06:50:05 +00001122 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001123 SyntaxError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001124 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001125}
1126
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001127static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001128SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1129{
1130 Py_VISIT(self->msg);
1131 Py_VISIT(self->filename);
1132 Py_VISIT(self->lineno);
1133 Py_VISIT(self->offset);
1134 Py_VISIT(self->text);
1135 Py_VISIT(self->print_file_and_line);
1136 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1137}
1138
1139/* This is called "my_basename" instead of just "basename" to avoid name
1140 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1141 defined, and Python does define that. */
1142static char *
1143my_basename(char *name)
1144{
1145 char *cp = name;
1146 char *result = name;
1147
1148 if (name == NULL)
1149 return "???";
1150 while (*cp != '\0') {
1151 if (*cp == SEP)
1152 result = cp + 1;
1153 ++cp;
1154 }
1155 return result;
1156}
1157
1158
1159static PyObject *
1160SyntaxError_str(PySyntaxErrorObject *self)
1161{
1162 PyObject *str;
1163 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001164 int have_filename = 0;
1165 int have_lineno = 0;
1166 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001167 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001168
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001169 if (self->msg)
1170 str = PyObject_Str(self->msg);
1171 else
1172 str = PyObject_Str(Py_None);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -05001173 if (!str)
1174 return NULL;
Georg Brandl43ab1002006-05-28 20:57:09 +00001175 /* Don't fiddle with non-string return (shouldn't happen anyway) */
Benjamin Peterson4c79aec2012-02-03 19:22:31 -05001176 if (!PyString_Check(str))
1177 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001178
1179 /* XXX -- do all the additional formatting with filename and
1180 lineno here */
1181
Georg Brandl43ab1002006-05-28 20:57:09 +00001182 have_filename = (self->filename != NULL) &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001183 PyString_Check(self->filename);
Georg Brandl43ab1002006-05-28 20:57:09 +00001184 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001185
Georg Brandl43ab1002006-05-28 20:57:09 +00001186 if (!have_filename && !have_lineno)
1187 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001188
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001189 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001190 if (have_filename)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001191 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001192
Georg Brandl43ab1002006-05-28 20:57:09 +00001193 buffer = PyMem_MALLOC(bufsize);
1194 if (buffer == NULL)
1195 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001196
Georg Brandl43ab1002006-05-28 20:57:09 +00001197 if (have_filename && have_lineno)
1198 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001199 PyString_AS_STRING(str),
1200 my_basename(PyString_AS_STRING(self->filename)),
Georg Brandl43ab1002006-05-28 20:57:09 +00001201 PyInt_AsLong(self->lineno));
1202 else if (have_filename)
1203 PyOS_snprintf(buffer, bufsize, "%s (%s)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001204 PyString_AS_STRING(str),
1205 my_basename(PyString_AS_STRING(self->filename)));
Georg Brandl43ab1002006-05-28 20:57:09 +00001206 else /* only have_lineno */
1207 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001208 PyString_AS_STRING(str),
Georg Brandl43ab1002006-05-28 20:57:09 +00001209 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001210
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001211 result = PyString_FromString(buffer);
Georg Brandl43ab1002006-05-28 20:57:09 +00001212 PyMem_FREE(buffer);
1213
1214 if (result == NULL)
1215 result = str;
1216 else
1217 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001218 return result;
1219}
1220
1221static PyMemberDef SyntaxError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001222 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1223 PyDoc_STR("exception msg")},
1224 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1225 PyDoc_STR("exception filename")},
1226 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1227 PyDoc_STR("exception lineno")},
1228 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1229 PyDoc_STR("exception offset")},
1230 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1231 PyDoc_STR("exception text")},
1232 {"print_file_and_line", T_OBJECT,
1233 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1234 PyDoc_STR("exception print_file_and_line")},
1235 {NULL} /* Sentinel */
1236};
1237
1238ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1239 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001240 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001241
1242
1243/*
1244 * IndentationError extends SyntaxError
1245 */
1246MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001247 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001248
1249
1250/*
1251 * TabError extends IndentationError
1252 */
1253MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001254 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001255
1256
1257/*
1258 * LookupError extends StandardError
1259 */
1260SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001261 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001262
1263
1264/*
1265 * IndexError extends LookupError
1266 */
1267SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001268 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001269
1270
1271/*
1272 * KeyError extends LookupError
1273 */
1274static PyObject *
1275KeyError_str(PyBaseExceptionObject *self)
1276{
1277 /* If args is a tuple of exactly one item, apply repr to args[0].
1278 This is done so that e.g. the exception raised by {}[''] prints
1279 KeyError: ''
1280 rather than the confusing
1281 KeyError
1282 alone. The downside is that if KeyError is raised with an explanatory
1283 string, that string will be displayed in quotes. Too bad.
1284 If args is anything else, use the default BaseException__str__().
1285 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001286 if (PyTuple_GET_SIZE(self->args) == 1) {
1287 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001288 }
1289 return BaseException_str(self);
1290}
1291
1292ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001293 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001294
1295
1296/*
1297 * ValueError extends StandardError
1298 */
1299SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001300 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001301
1302/*
1303 * UnicodeError extends ValueError
1304 */
1305
1306SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001307 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001308
1309#ifdef Py_USING_UNICODE
Richard Jones7b9558d2006-05-27 12:29:24 +00001310static PyObject *
1311get_string(PyObject *attr, const char *name)
1312{
1313 if (!attr) {
1314 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1315 return NULL;
1316 }
1317
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001318 if (!PyString_Check(attr)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001319 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1320 return NULL;
1321 }
1322 Py_INCREF(attr);
1323 return attr;
1324}
1325
1326
1327static int
1328set_string(PyObject **attr, const char *value)
1329{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001330 PyObject *obj = PyString_FromString(value);
Richard Jones7b9558d2006-05-27 12:29:24 +00001331 if (!obj)
1332 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001333 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001334 *attr = obj;
1335 return 0;
1336}
1337
1338
1339static PyObject *
1340get_unicode(PyObject *attr, const char *name)
1341{
1342 if (!attr) {
1343 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1344 return NULL;
1345 }
1346
1347 if (!PyUnicode_Check(attr)) {
1348 PyErr_Format(PyExc_TypeError,
1349 "%.200s attribute must be unicode", name);
1350 return NULL;
1351 }
1352 Py_INCREF(attr);
1353 return attr;
1354}
1355
1356PyObject *
1357PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1358{
1359 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1360}
1361
1362PyObject *
1363PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1364{
1365 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1366}
1367
1368PyObject *
1369PyUnicodeEncodeError_GetObject(PyObject *exc)
1370{
1371 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1372}
1373
1374PyObject *
1375PyUnicodeDecodeError_GetObject(PyObject *exc)
1376{
1377 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1378}
1379
1380PyObject *
1381PyUnicodeTranslateError_GetObject(PyObject *exc)
1382{
1383 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1384}
1385
1386int
1387PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1388{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001389 Py_ssize_t size;
1390 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1391 "object");
1392 if (!obj)
1393 return -1;
1394 *start = ((PyUnicodeErrorObject *)exc)->start;
1395 size = PyUnicode_GET_SIZE(obj);
1396 if (*start<0)
1397 *start = 0; /*XXX check for values <0*/
1398 if (*start>=size)
1399 *start = size-1;
1400 Py_DECREF(obj);
1401 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001402}
1403
1404
1405int
1406PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1407{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001408 Py_ssize_t size;
1409 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1410 "object");
1411 if (!obj)
1412 return -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001413 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001414 *start = ((PyUnicodeErrorObject *)exc)->start;
1415 if (*start<0)
1416 *start = 0;
1417 if (*start>=size)
1418 *start = size-1;
1419 Py_DECREF(obj);
1420 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001421}
1422
1423
1424int
1425PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1426{
1427 return PyUnicodeEncodeError_GetStart(exc, start);
1428}
1429
1430
1431int
1432PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1433{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001434 ((PyUnicodeErrorObject *)exc)->start = start;
1435 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001436}
1437
1438
1439int
1440PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1441{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001442 ((PyUnicodeErrorObject *)exc)->start = start;
1443 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001444}
1445
1446
1447int
1448PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1449{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001450 ((PyUnicodeErrorObject *)exc)->start = start;
1451 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001452}
1453
1454
1455int
1456PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1457{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001458 Py_ssize_t size;
1459 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1460 "object");
1461 if (!obj)
1462 return -1;
1463 *end = ((PyUnicodeErrorObject *)exc)->end;
1464 size = PyUnicode_GET_SIZE(obj);
1465 if (*end<1)
1466 *end = 1;
1467 if (*end>size)
1468 *end = size;
1469 Py_DECREF(obj);
1470 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001471}
1472
1473
1474int
1475PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1476{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001477 Py_ssize_t size;
1478 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1479 "object");
1480 if (!obj)
1481 return -1;
1482 *end = ((PyUnicodeErrorObject *)exc)->end;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001483 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001484 if (*end<1)
1485 *end = 1;
1486 if (*end>size)
1487 *end = size;
1488 Py_DECREF(obj);
1489 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001490}
1491
1492
1493int
1494PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1495{
1496 return PyUnicodeEncodeError_GetEnd(exc, start);
1497}
1498
1499
1500int
1501PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1502{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001503 ((PyUnicodeErrorObject *)exc)->end = end;
1504 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001505}
1506
1507
1508int
1509PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1510{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001511 ((PyUnicodeErrorObject *)exc)->end = end;
1512 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001513}
1514
1515
1516int
1517PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1518{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001519 ((PyUnicodeErrorObject *)exc)->end = end;
1520 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001521}
1522
1523PyObject *
1524PyUnicodeEncodeError_GetReason(PyObject *exc)
1525{
1526 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1527}
1528
1529
1530PyObject *
1531PyUnicodeDecodeError_GetReason(PyObject *exc)
1532{
1533 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1534}
1535
1536
1537PyObject *
1538PyUnicodeTranslateError_GetReason(PyObject *exc)
1539{
1540 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1541}
1542
1543
1544int
1545PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1546{
1547 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1548}
1549
1550
1551int
1552PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1553{
1554 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1555}
1556
1557
1558int
1559PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1560{
1561 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1562}
1563
1564
Richard Jones7b9558d2006-05-27 12:29:24 +00001565static int
1566UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1567 PyTypeObject *objecttype)
1568{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001569 Py_CLEAR(self->encoding);
1570 Py_CLEAR(self->object);
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001571 Py_CLEAR(self->reason);
1572
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001573 if (!PyArg_ParseTuple(args, "O!O!nnO!",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001574 &PyString_Type, &self->encoding,
Richard Jones7b9558d2006-05-27 12:29:24 +00001575 objecttype, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001576 &self->start,
1577 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001578 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001579 self->encoding = self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001580 return -1;
1581 }
1582
1583 Py_INCREF(self->encoding);
1584 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001585 Py_INCREF(self->reason);
1586
1587 return 0;
1588}
1589
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001590static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001591UnicodeError_clear(PyUnicodeErrorObject *self)
1592{
1593 Py_CLEAR(self->encoding);
1594 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001595 Py_CLEAR(self->reason);
1596 return BaseException_clear((PyBaseExceptionObject *)self);
1597}
1598
1599static void
1600UnicodeError_dealloc(PyUnicodeErrorObject *self)
1601{
Georg Brandl38f62372006-09-06 06:50:05 +00001602 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001603 UnicodeError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001604 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001605}
1606
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001607static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001608UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1609{
1610 Py_VISIT(self->encoding);
1611 Py_VISIT(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001612 Py_VISIT(self->reason);
1613 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1614}
1615
1616static PyMemberDef UnicodeError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001617 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1618 PyDoc_STR("exception encoding")},
1619 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1620 PyDoc_STR("exception object")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001621 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001622 PyDoc_STR("exception start")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001623 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001624 PyDoc_STR("exception end")},
1625 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1626 PyDoc_STR("exception reason")},
1627 {NULL} /* Sentinel */
1628};
1629
1630
1631/*
1632 * UnicodeEncodeError extends UnicodeError
1633 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001634
1635static int
1636UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1637{
1638 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1639 return -1;
1640 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1641 kwds, &PyUnicode_Type);
1642}
1643
1644static PyObject *
1645UnicodeEncodeError_str(PyObject *self)
1646{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001647 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith2d9856d2010-02-24 14:15:36 +00001648 PyObject *result = NULL;
1649 PyObject *reason_str = NULL;
1650 PyObject *encoding_str = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001651
Eric Smith2d9856d2010-02-24 14:15:36 +00001652 /* Get reason and encoding as strings, which they might not be if
1653 they've been modified after we were contructed. */
1654 reason_str = PyObject_Str(uself->reason);
1655 if (reason_str == NULL)
1656 goto done;
1657 encoding_str = PyObject_Str(uself->encoding);
1658 if (encoding_str == NULL)
1659 goto done;
1660
1661 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001662 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001663 char badchar_str[20];
1664 if (badchar <= 0xff)
1665 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1666 else if (badchar <= 0xffff)
1667 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1668 else
1669 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Eric Smith2d9856d2010-02-24 14:15:36 +00001670 result = PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001671 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Eric Smith2d9856d2010-02-24 14:15:36 +00001672 PyString_AS_STRING(encoding_str),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001673 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001674 uself->start,
Eric Smith2d9856d2010-02-24 14:15:36 +00001675 PyString_AS_STRING(reason_str));
Richard Jones7b9558d2006-05-27 12:29:24 +00001676 }
Eric Smith2d9856d2010-02-24 14:15:36 +00001677 else {
1678 result = PyString_FromFormat(
1679 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
1680 PyString_AS_STRING(encoding_str),
1681 uself->start,
1682 uself->end-1,
1683 PyString_AS_STRING(reason_str));
1684 }
1685done:
1686 Py_XDECREF(reason_str);
1687 Py_XDECREF(encoding_str);
1688 return result;
Richard Jones7b9558d2006-05-27 12:29:24 +00001689}
1690
1691static PyTypeObject _PyExc_UnicodeEncodeError = {
1692 PyObject_HEAD_INIT(NULL)
1693 0,
Georg Brandl38f62372006-09-06 06:50:05 +00001694 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001695 sizeof(PyUnicodeErrorObject), 0,
1696 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1697 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1698 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001699 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1700 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001701 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001702 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001703};
1704PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1705
1706PyObject *
1707PyUnicodeEncodeError_Create(
1708 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1709 Py_ssize_t start, Py_ssize_t end, const char *reason)
1710{
1711 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001712 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001713}
1714
1715
1716/*
1717 * UnicodeDecodeError extends UnicodeError
1718 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001719
1720static int
1721UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1722{
1723 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1724 return -1;
1725 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001726 kwds, &PyString_Type);
Richard Jones7b9558d2006-05-27 12:29:24 +00001727}
1728
1729static PyObject *
1730UnicodeDecodeError_str(PyObject *self)
1731{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001732 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith2d9856d2010-02-24 14:15:36 +00001733 PyObject *result = NULL;
1734 PyObject *reason_str = NULL;
1735 PyObject *encoding_str = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001736
Eric Smith2d9856d2010-02-24 14:15:36 +00001737 /* Get reason and encoding as strings, which they might not be if
1738 they've been modified after we were contructed. */
1739 reason_str = PyObject_Str(uself->reason);
1740 if (reason_str == NULL)
1741 goto done;
1742 encoding_str = PyObject_Str(uself->encoding);
1743 if (encoding_str == NULL)
1744 goto done;
1745
1746 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001747 /* FromFormat does not support %02x, so format that separately */
1748 char byte[4];
1749 PyOS_snprintf(byte, sizeof(byte), "%02x",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001750 ((int)PyString_AS_STRING(uself->object)[uself->start])&0xff);
Eric Smith2d9856d2010-02-24 14:15:36 +00001751 result = PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001752 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Eric Smith2d9856d2010-02-24 14:15:36 +00001753 PyString_AS_STRING(encoding_str),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001754 byte,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001755 uself->start,
Eric Smith2d9856d2010-02-24 14:15:36 +00001756 PyString_AS_STRING(reason_str));
Richard Jones7b9558d2006-05-27 12:29:24 +00001757 }
Eric Smith2d9856d2010-02-24 14:15:36 +00001758 else {
1759 result = PyString_FromFormat(
1760 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
1761 PyString_AS_STRING(encoding_str),
1762 uself->start,
1763 uself->end-1,
1764 PyString_AS_STRING(reason_str));
1765 }
1766done:
1767 Py_XDECREF(reason_str);
1768 Py_XDECREF(encoding_str);
1769 return result;
Richard Jones7b9558d2006-05-27 12:29:24 +00001770}
1771
1772static PyTypeObject _PyExc_UnicodeDecodeError = {
1773 PyObject_HEAD_INIT(NULL)
1774 0,
1775 EXC_MODULE_NAME "UnicodeDecodeError",
1776 sizeof(PyUnicodeErrorObject), 0,
1777 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1778 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1779 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001780 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1781 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001782 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001783 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001784};
1785PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1786
1787PyObject *
1788PyUnicodeDecodeError_Create(
1789 const char *encoding, const char *object, Py_ssize_t length,
1790 Py_ssize_t start, Py_ssize_t end, const char *reason)
1791{
Richard Jones7b9558d2006-05-27 12:29:24 +00001792 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001793 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001794}
1795
1796
1797/*
1798 * UnicodeTranslateError extends UnicodeError
1799 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001800
1801static int
1802UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1803 PyObject *kwds)
1804{
1805 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1806 return -1;
1807
1808 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001809 Py_CLEAR(self->reason);
1810
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001811 if (!PyArg_ParseTuple(args, "O!nnO!",
Richard Jones7b9558d2006-05-27 12:29:24 +00001812 &PyUnicode_Type, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001813 &self->start,
1814 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001815 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001816 self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001817 return -1;
1818 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001819
Richard Jones7b9558d2006-05-27 12:29:24 +00001820 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001821 Py_INCREF(self->reason);
1822
1823 return 0;
1824}
1825
1826
1827static PyObject *
1828UnicodeTranslateError_str(PyObject *self)
1829{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001830 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith2d9856d2010-02-24 14:15:36 +00001831 PyObject *result = NULL;
1832 PyObject *reason_str = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001833
Eric Smith2d9856d2010-02-24 14:15:36 +00001834 /* Get reason as a string, which it might not be if it's been
1835 modified after we were contructed. */
1836 reason_str = PyObject_Str(uself->reason);
1837 if (reason_str == NULL)
1838 goto done;
1839
1840 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001841 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001842 char badchar_str[20];
1843 if (badchar <= 0xff)
1844 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1845 else if (badchar <= 0xffff)
1846 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1847 else
1848 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Eric Smith2d9856d2010-02-24 14:15:36 +00001849 result = PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001850 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001851 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001852 uself->start,
Eric Smith2d9856d2010-02-24 14:15:36 +00001853 PyString_AS_STRING(reason_str));
1854 } else {
1855 result = PyString_FromFormat(
1856 "can't translate characters in position %zd-%zd: %.400s",
1857 uself->start,
1858 uself->end-1,
1859 PyString_AS_STRING(reason_str));
Richard Jones7b9558d2006-05-27 12:29:24 +00001860 }
Eric Smith2d9856d2010-02-24 14:15:36 +00001861done:
1862 Py_XDECREF(reason_str);
1863 return result;
Richard Jones7b9558d2006-05-27 12:29:24 +00001864}
1865
1866static PyTypeObject _PyExc_UnicodeTranslateError = {
1867 PyObject_HEAD_INIT(NULL)
1868 0,
1869 EXC_MODULE_NAME "UnicodeTranslateError",
1870 sizeof(PyUnicodeErrorObject), 0,
1871 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1872 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1873 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001874 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001875 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1876 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001877 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001878};
1879PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1880
1881PyObject *
1882PyUnicodeTranslateError_Create(
1883 const Py_UNICODE *object, Py_ssize_t length,
1884 Py_ssize_t start, Py_ssize_t end, const char *reason)
1885{
1886 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001887 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001888}
1889#endif
1890
1891
1892/*
1893 * AssertionError extends StandardError
1894 */
1895SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001896 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001897
1898
1899/*
1900 * ArithmeticError extends StandardError
1901 */
1902SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001903 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001904
1905
1906/*
1907 * FloatingPointError extends ArithmeticError
1908 */
1909SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001910 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001911
1912
1913/*
1914 * OverflowError extends ArithmeticError
1915 */
1916SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001917 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001918
1919
1920/*
1921 * ZeroDivisionError extends ArithmeticError
1922 */
1923SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001924 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001925
1926
1927/*
1928 * SystemError extends StandardError
1929 */
1930SimpleExtendsException(PyExc_StandardError, SystemError,
1931 "Internal error in the Python interpreter.\n"
1932 "\n"
1933 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001934 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001935
1936
1937/*
1938 * ReferenceError extends StandardError
1939 */
1940SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001941 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001942
1943
1944/*
1945 * MemoryError extends StandardError
1946 */
Richard Jones2d555b32006-05-27 16:15:11 +00001947SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001948
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00001949/*
1950 * BufferError extends StandardError
1951 */
1952SimpleExtendsException(PyExc_StandardError, BufferError, "Buffer error.");
1953
Richard Jones7b9558d2006-05-27 12:29:24 +00001954
1955/* Warning category docstrings */
1956
1957/*
1958 * Warning extends Exception
1959 */
1960SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001961 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001962
1963
1964/*
1965 * UserWarning extends Warning
1966 */
1967SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001968 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001969
1970
1971/*
1972 * DeprecationWarning extends Warning
1973 */
1974SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001975 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001976
1977
1978/*
1979 * PendingDeprecationWarning extends Warning
1980 */
1981SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1982 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001983 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001984
1985
1986/*
1987 * SyntaxWarning extends Warning
1988 */
1989SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001990 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001991
1992
1993/*
1994 * RuntimeWarning extends Warning
1995 */
1996SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001997 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001998
1999
2000/*
2001 * FutureWarning extends Warning
2002 */
2003SimpleExtendsException(PyExc_Warning, FutureWarning,
2004 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00002005 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00002006
2007
2008/*
2009 * ImportWarning extends Warning
2010 */
2011SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00002012 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00002013
2014
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002015/*
2016 * UnicodeWarning extends Warning
2017 */
2018SimpleExtendsException(PyExc_Warning, UnicodeWarning,
2019 "Base class for warnings about Unicode related problems, mostly\n"
2020 "related to conversion problems.");
2021
Christian Heimes1a6387e2008-03-26 12:49:49 +00002022/*
2023 * BytesWarning extends Warning
2024 */
2025SimpleExtendsException(PyExc_Warning, BytesWarning,
2026 "Base class for warnings about bytes and buffer related problems, mostly\n"
2027 "related to conversion from str or comparing to str.");
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002028
Richard Jones7b9558d2006-05-27 12:29:24 +00002029/* Pre-computed MemoryError instance. Best to create this as early as
2030 * possible and not wait until a MemoryError is actually raised!
2031 */
2032PyObject *PyExc_MemoryErrorInst=NULL;
2033
Brett Cannon1e534b52007-09-07 04:18:30 +00002034/* Pre-computed RuntimeError instance for when recursion depth is reached.
2035 Meant to be used when normalizing the exception for exceeding the recursion
2036 depth will cause its own infinite recursion.
2037*/
2038PyObject *PyExc_RecursionErrorInst = NULL;
2039
Richard Jones7b9558d2006-05-27 12:29:24 +00002040/* module global functions */
2041static PyMethodDef functions[] = {
2042 /* Sentinel */
2043 {NULL, NULL}
2044};
2045
2046#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
2047 Py_FatalError("exceptions bootstrapping error.");
2048
2049#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
2050 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
2051 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
2052 Py_FatalError("Module dictionary insertion problem.");
2053
KristjĂ¡n Valur JĂ³nssonf6083172006-06-12 15:45:12 +00002054
Richard Jones7b9558d2006-05-27 12:29:24 +00002055PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00002056_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00002057{
2058 PyObject *m, *bltinmod, *bdict;
2059
2060 PRE_INIT(BaseException)
2061 PRE_INIT(Exception)
2062 PRE_INIT(StandardError)
2063 PRE_INIT(TypeError)
2064 PRE_INIT(StopIteration)
2065 PRE_INIT(GeneratorExit)
2066 PRE_INIT(SystemExit)
2067 PRE_INIT(KeyboardInterrupt)
2068 PRE_INIT(ImportError)
2069 PRE_INIT(EnvironmentError)
2070 PRE_INIT(IOError)
2071 PRE_INIT(OSError)
2072#ifdef MS_WINDOWS
2073 PRE_INIT(WindowsError)
2074#endif
2075#ifdef __VMS
2076 PRE_INIT(VMSError)
2077#endif
2078 PRE_INIT(EOFError)
2079 PRE_INIT(RuntimeError)
2080 PRE_INIT(NotImplementedError)
2081 PRE_INIT(NameError)
2082 PRE_INIT(UnboundLocalError)
2083 PRE_INIT(AttributeError)
2084 PRE_INIT(SyntaxError)
2085 PRE_INIT(IndentationError)
2086 PRE_INIT(TabError)
2087 PRE_INIT(LookupError)
2088 PRE_INIT(IndexError)
2089 PRE_INIT(KeyError)
2090 PRE_INIT(ValueError)
2091 PRE_INIT(UnicodeError)
2092#ifdef Py_USING_UNICODE
2093 PRE_INIT(UnicodeEncodeError)
2094 PRE_INIT(UnicodeDecodeError)
2095 PRE_INIT(UnicodeTranslateError)
2096#endif
2097 PRE_INIT(AssertionError)
2098 PRE_INIT(ArithmeticError)
2099 PRE_INIT(FloatingPointError)
2100 PRE_INIT(OverflowError)
2101 PRE_INIT(ZeroDivisionError)
2102 PRE_INIT(SystemError)
2103 PRE_INIT(ReferenceError)
2104 PRE_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002105 PRE_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002106 PRE_INIT(Warning)
2107 PRE_INIT(UserWarning)
2108 PRE_INIT(DeprecationWarning)
2109 PRE_INIT(PendingDeprecationWarning)
2110 PRE_INIT(SyntaxWarning)
2111 PRE_INIT(RuntimeWarning)
2112 PRE_INIT(FutureWarning)
2113 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002114 PRE_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002115 PRE_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002116
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002117 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2118 (PyObject *)NULL, PYTHON_API_VERSION);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -05002119 if (m == NULL)
2120 return;
Richard Jones7b9558d2006-05-27 12:29:24 +00002121
2122 bltinmod = PyImport_ImportModule("__builtin__");
2123 if (bltinmod == NULL)
2124 Py_FatalError("exceptions bootstrapping error.");
2125 bdict = PyModule_GetDict(bltinmod);
2126 if (bdict == NULL)
2127 Py_FatalError("exceptions bootstrapping error.");
2128
2129 POST_INIT(BaseException)
2130 POST_INIT(Exception)
2131 POST_INIT(StandardError)
2132 POST_INIT(TypeError)
2133 POST_INIT(StopIteration)
2134 POST_INIT(GeneratorExit)
2135 POST_INIT(SystemExit)
2136 POST_INIT(KeyboardInterrupt)
2137 POST_INIT(ImportError)
2138 POST_INIT(EnvironmentError)
2139 POST_INIT(IOError)
2140 POST_INIT(OSError)
2141#ifdef MS_WINDOWS
2142 POST_INIT(WindowsError)
2143#endif
2144#ifdef __VMS
2145 POST_INIT(VMSError)
2146#endif
2147 POST_INIT(EOFError)
2148 POST_INIT(RuntimeError)
2149 POST_INIT(NotImplementedError)
2150 POST_INIT(NameError)
2151 POST_INIT(UnboundLocalError)
2152 POST_INIT(AttributeError)
2153 POST_INIT(SyntaxError)
2154 POST_INIT(IndentationError)
2155 POST_INIT(TabError)
2156 POST_INIT(LookupError)
2157 POST_INIT(IndexError)
2158 POST_INIT(KeyError)
2159 POST_INIT(ValueError)
2160 POST_INIT(UnicodeError)
2161#ifdef Py_USING_UNICODE
2162 POST_INIT(UnicodeEncodeError)
2163 POST_INIT(UnicodeDecodeError)
2164 POST_INIT(UnicodeTranslateError)
2165#endif
2166 POST_INIT(AssertionError)
2167 POST_INIT(ArithmeticError)
2168 POST_INIT(FloatingPointError)
2169 POST_INIT(OverflowError)
2170 POST_INIT(ZeroDivisionError)
2171 POST_INIT(SystemError)
2172 POST_INIT(ReferenceError)
2173 POST_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002174 POST_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002175 POST_INIT(Warning)
2176 POST_INIT(UserWarning)
2177 POST_INIT(DeprecationWarning)
2178 POST_INIT(PendingDeprecationWarning)
2179 POST_INIT(SyntaxWarning)
2180 POST_INIT(RuntimeWarning)
2181 POST_INIT(FutureWarning)
2182 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002183 POST_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002184 POST_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002185
2186 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2187 if (!PyExc_MemoryErrorInst)
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +00002188 Py_FatalError("Cannot pre-allocate MemoryError instance");
Richard Jones7b9558d2006-05-27 12:29:24 +00002189
Brett Cannon1e534b52007-09-07 04:18:30 +00002190 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2191 if (!PyExc_RecursionErrorInst)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002192 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2193 "recursion errors");
Brett Cannon1e534b52007-09-07 04:18:30 +00002194 else {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002195 PyBaseExceptionObject *err_inst =
2196 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2197 PyObject *args_tuple;
2198 PyObject *exc_message;
2199 exc_message = PyString_FromString("maximum recursion depth exceeded");
2200 if (!exc_message)
2201 Py_FatalError("cannot allocate argument for RuntimeError "
2202 "pre-allocation");
2203 args_tuple = PyTuple_Pack(1, exc_message);
2204 if (!args_tuple)
2205 Py_FatalError("cannot allocate tuple for RuntimeError "
2206 "pre-allocation");
2207 Py_DECREF(exc_message);
2208 if (BaseException_init(err_inst, args_tuple, NULL))
2209 Py_FatalError("init of pre-allocated RuntimeError failed");
2210 Py_DECREF(args_tuple);
Brett Cannon1e534b52007-09-07 04:18:30 +00002211 }
2212
Richard Jones7b9558d2006-05-27 12:29:24 +00002213 Py_DECREF(bltinmod);
2214}
2215
2216void
2217_PyExc_Fini(void)
2218{
Alexandre Vassalotti55bd1ef2009-06-12 18:56:57 +00002219 Py_CLEAR(PyExc_MemoryErrorInst);
2220 Py_CLEAR(PyExc_RecursionErrorInst);
Richard Jones7b9558d2006-05-27 12:29:24 +00002221}