blob: 1929777197fda77bace06d9518e13d9320797540 [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
Serhiy Storchaka5951f232015-12-24 10:35:35 +020061 Py_INCREF(args);
Serhiy Storchaka763a61c2016-04-10 18:05:12 +030062 Py_SETREF(self->args, args);
Richard Jones7b9558d2006-05-27 12:29:24 +000063
64 if (PyTuple_GET_SIZE(self->args) == 1) {
Serhiy Storchaka8688aca2015-12-27 12:38:48 +020065 Py_INCREF(PyTuple_GET_ITEM(self->args, 0));
Serhiy Storchakabc62af12016-04-06 09:51:18 +030066 Py_XSETREF(self->message, PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +000067 }
68 return 0;
69}
70
Michael W. Hudson96495ee2006-05-28 17:40:29 +000071static int
Richard Jones7b9558d2006-05-27 12:29:24 +000072BaseException_clear(PyBaseExceptionObject *self)
73{
74 Py_CLEAR(self->dict);
75 Py_CLEAR(self->args);
76 Py_CLEAR(self->message);
77 return 0;
78}
79
80static void
81BaseException_dealloc(PyBaseExceptionObject *self)
82{
Georg Brandl38f62372006-09-06 06:50:05 +000083 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +000084 BaseException_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +000085 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +000086}
87
Michael W. Hudson96495ee2006-05-28 17:40:29 +000088static int
Richard Jones7b9558d2006-05-27 12:29:24 +000089BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
90{
Michael W. Hudson22a80e72006-05-28 15:51:40 +000091 Py_VISIT(self->dict);
Richard Jones7b9558d2006-05-27 12:29:24 +000092 Py_VISIT(self->args);
93 Py_VISIT(self->message);
94 return 0;
95}
96
97static PyObject *
98BaseException_str(PyBaseExceptionObject *self)
99{
100 PyObject *out;
101
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000102 switch (PyTuple_GET_SIZE(self->args)) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000103 case 0:
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000104 out = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +0000105 break;
106 case 1:
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000107 out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +0000108 break;
Richard Jones7b9558d2006-05-27 12:29:24 +0000109 default:
110 out = PyObject_Str(self->args);
111 break;
112 }
113
114 return out;
115}
116
Nick Coghlan524b7772008-07-08 14:08:04 +0000117#ifdef Py_USING_UNICODE
118static PyObject *
119BaseException_unicode(PyBaseExceptionObject *self)
120{
121 PyObject *out;
122
Ezio Melottif84caf42009-12-24 22:25:17 +0000123 /* issue6108: if __str__ has been overridden in the subclass, unicode()
124 should return the message returned by __str__ as used to happen
125 before this method was implemented. */
126 if (Py_TYPE(self)->tp_str != (reprfunc)BaseException_str) {
127 PyObject *str;
128 /* Unlike PyObject_Str, tp_str can return unicode (i.e. return the
129 equivalent of unicode(e.__str__()) instead of unicode(str(e))). */
130 str = Py_TYPE(self)->tp_str((PyObject*)self);
131 if (str == NULL)
132 return NULL;
133 out = PyObject_Unicode(str);
134 Py_DECREF(str);
135 return out;
136 }
137
Nick Coghlan524b7772008-07-08 14:08:04 +0000138 switch (PyTuple_GET_SIZE(self->args)) {
139 case 0:
140 out = PyUnicode_FromString("");
141 break;
142 case 1:
143 out = PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));
144 break;
145 default:
146 out = PyObject_Unicode(self->args);
147 break;
148 }
149
150 return out;
151}
152#endif
153
Richard Jones7b9558d2006-05-27 12:29:24 +0000154static PyObject *
155BaseException_repr(PyBaseExceptionObject *self)
156{
Richard Jones7b9558d2006-05-27 12:29:24 +0000157 PyObject *repr_suffix;
158 PyObject *repr;
159 char *name;
160 char *dot;
161
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000162 repr_suffix = PyObject_Repr(self->args);
163 if (!repr_suffix)
Richard Jones7b9558d2006-05-27 12:29:24 +0000164 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000165
Christian Heimese93237d2007-12-19 02:37:44 +0000166 name = (char *)Py_TYPE(self)->tp_name;
Richard Jones7b9558d2006-05-27 12:29:24 +0000167 dot = strrchr(name, '.');
168 if (dot != NULL) name = dot+1;
169
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000170 repr = PyString_FromString(name);
Richard Jones7b9558d2006-05-27 12:29:24 +0000171 if (!repr) {
172 Py_DECREF(repr_suffix);
173 return NULL;
174 }
175
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000176 PyString_ConcatAndDel(&repr, repr_suffix);
Richard Jones7b9558d2006-05-27 12:29:24 +0000177 return repr;
178}
179
180/* Pickling support */
181static PyObject *
182BaseException_reduce(PyBaseExceptionObject *self)
183{
Georg Brandlddba4732006-05-30 07:04:55 +0000184 if (self->args && self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000185 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000186 else
Christian Heimese93237d2007-12-19 02:37:44 +0000187 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000188}
189
Georg Brandl85ac8502006-06-01 06:39:19 +0000190/*
191 * Needed for backward compatibility, since exceptions used to store
192 * all their attributes in the __dict__. Code is taken from cPickle's
193 * load_build function.
194 */
195static PyObject *
196BaseException_setstate(PyObject *self, PyObject *state)
197{
198 PyObject *d_key, *d_value;
199 Py_ssize_t i = 0;
200
201 if (state != Py_None) {
202 if (!PyDict_Check(state)) {
203 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
204 return NULL;
205 }
206 while (PyDict_Next(state, &i, &d_key, &d_value)) {
207 if (PyObject_SetAttr(self, d_key, d_value) < 0)
208 return NULL;
209 }
210 }
211 Py_RETURN_NONE;
212}
Richard Jones7b9558d2006-05-27 12:29:24 +0000213
Richard Jones7b9558d2006-05-27 12:29:24 +0000214
215static PyMethodDef BaseException_methods[] = {
216 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Georg Brandl85ac8502006-06-01 06:39:19 +0000217 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000218#ifdef Py_USING_UNICODE
Nick Coghlan524b7772008-07-08 14:08:04 +0000219 {"__unicode__", (PyCFunction)BaseException_unicode, METH_NOARGS },
220#endif
Richard Jones7b9558d2006-05-27 12:29:24 +0000221 {NULL, NULL, 0, NULL},
222};
223
224
225
226static PyObject *
227BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
228{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000229 if (PyErr_WarnPy3k("__getitem__ not supported for exception "
230 "classes in 3.x; use args attribute", 1) < 0)
231 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000232 return PySequence_GetItem(self->args, index);
233}
234
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000235static PyObject *
236BaseException_getslice(PyBaseExceptionObject *self,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000237 Py_ssize_t start, Py_ssize_t stop)
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000238{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000239 if (PyErr_WarnPy3k("__getslice__ not supported for exception "
240 "classes in 3.x; use args attribute", 1) < 0)
241 return NULL;
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000242 return PySequence_GetSlice(self->args, start, stop);
243}
244
Richard Jones7b9558d2006-05-27 12:29:24 +0000245static PySequenceMethods BaseException_as_sequence = {
246 0, /* sq_length; */
247 0, /* sq_concat; */
248 0, /* sq_repeat; */
249 (ssizeargfunc)BaseException_getitem, /* sq_item; */
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000250 (ssizessizeargfunc)BaseException_getslice, /* sq_slice; */
Richard Jones7b9558d2006-05-27 12:29:24 +0000251 0, /* sq_ass_item; */
252 0, /* sq_ass_slice; */
253 0, /* sq_contains; */
254 0, /* sq_inplace_concat; */
255 0 /* sq_inplace_repeat; */
256};
257
Richard Jones7b9558d2006-05-27 12:29:24 +0000258static PyObject *
259BaseException_get_dict(PyBaseExceptionObject *self)
260{
261 if (self->dict == NULL) {
262 self->dict = PyDict_New();
263 if (!self->dict)
264 return NULL;
265 }
266 Py_INCREF(self->dict);
267 return self->dict;
268}
269
270static int
271BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
272{
273 if (val == NULL) {
274 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
275 return -1;
276 }
277 if (!PyDict_Check(val)) {
278 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
279 return -1;
280 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000281 Py_INCREF(val);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300282 Py_XSETREF(self->dict, val);
Richard Jones7b9558d2006-05-27 12:29:24 +0000283 return 0;
284}
285
286static PyObject *
287BaseException_get_args(PyBaseExceptionObject *self)
288{
289 if (self->args == NULL) {
290 Py_INCREF(Py_None);
291 return Py_None;
292 }
293 Py_INCREF(self->args);
294 return self->args;
295}
296
297static int
298BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
299{
300 PyObject *seq;
301 if (val == NULL) {
302 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
303 return -1;
304 }
305 seq = PySequence_Tuple(val);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -0500306 if (!seq)
307 return -1;
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300308 Py_XSETREF(self->args, seq);
Richard Jones7b9558d2006-05-27 12:29:24 +0000309 return 0;
310}
311
Brett Cannon229cee22007-05-05 01:34:02 +0000312static PyObject *
313BaseException_get_message(PyBaseExceptionObject *self)
314{
Georg Brandl0674d3f2009-09-16 20:30:09 +0000315 PyObject *msg;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000316
Georg Brandl0674d3f2009-09-16 20:30:09 +0000317 /* if "message" is in self->dict, accessing a user-set message attribute */
318 if (self->dict &&
319 (msg = PyDict_GetItemString(self->dict, "message"))) {
320 Py_INCREF(msg);
321 return msg;
322 }
Brett Cannon229cee22007-05-05 01:34:02 +0000323
Georg Brandl0674d3f2009-09-16 20:30:09 +0000324 if (self->message == NULL) {
325 PyErr_SetString(PyExc_AttributeError, "message attribute was deleted");
326 return NULL;
327 }
328
329 /* accessing the deprecated "builtin" message attribute of Exception */
330 if (PyErr_WarnEx(PyExc_DeprecationWarning,
331 "BaseException.message has been deprecated as "
332 "of Python 2.6", 1) < 0)
333 return NULL;
334
335 Py_INCREF(self->message);
336 return self->message;
Brett Cannon229cee22007-05-05 01:34:02 +0000337}
338
339static int
340BaseException_set_message(PyBaseExceptionObject *self, PyObject *val)
341{
Georg Brandl0674d3f2009-09-16 20:30:09 +0000342 /* if val is NULL, delete the message attribute */
343 if (val == NULL) {
344 if (self->dict && PyDict_GetItemString(self->dict, "message")) {
345 if (PyDict_DelItemString(self->dict, "message") < 0)
346 return -1;
347 }
Mark Dickinson7cac1c22013-03-03 11:13:34 +0000348 Py_CLEAR(self->message);
Georg Brandl0674d3f2009-09-16 20:30:09 +0000349 return 0;
350 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000351
Georg Brandl0674d3f2009-09-16 20:30:09 +0000352 /* else set it in __dict__, but may need to create the dict first */
353 if (self->dict == NULL) {
354 self->dict = PyDict_New();
355 if (!self->dict)
356 return -1;
357 }
358 return PyDict_SetItemString(self->dict, "message", val);
Brett Cannon229cee22007-05-05 01:34:02 +0000359}
360
Richard Jones7b9558d2006-05-27 12:29:24 +0000361static PyGetSetDef BaseException_getset[] = {
362 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
363 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Brett Cannon229cee22007-05-05 01:34:02 +0000364 {"message", (getter)BaseException_get_message,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000365 (setter)BaseException_set_message},
Richard Jones7b9558d2006-05-27 12:29:24 +0000366 {NULL},
367};
368
369
370static PyTypeObject _PyExc_BaseException = {
371 PyObject_HEAD_INIT(NULL)
372 0, /*ob_size*/
373 EXC_MODULE_NAME "BaseException", /*tp_name*/
374 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
375 0, /*tp_itemsize*/
376 (destructor)BaseException_dealloc, /*tp_dealloc*/
377 0, /*tp_print*/
378 0, /*tp_getattr*/
379 0, /*tp_setattr*/
380 0, /* tp_compare; */
381 (reprfunc)BaseException_repr, /*tp_repr*/
382 0, /*tp_as_number*/
383 &BaseException_as_sequence, /*tp_as_sequence*/
384 0, /*tp_as_mapping*/
385 0, /*tp_hash */
386 0, /*tp_call*/
387 (reprfunc)BaseException_str, /*tp_str*/
388 PyObject_GenericGetAttr, /*tp_getattro*/
389 PyObject_GenericSetAttr, /*tp_setattro*/
390 0, /*tp_as_buffer*/
Neal Norwitzee3a1b52007-02-25 19:44:48 +0000391 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000392 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Richard Jones7b9558d2006-05-27 12:29:24 +0000393 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
394 (traverseproc)BaseException_traverse, /* tp_traverse */
395 (inquiry)BaseException_clear, /* tp_clear */
396 0, /* tp_richcompare */
397 0, /* tp_weaklistoffset */
398 0, /* tp_iter */
399 0, /* tp_iternext */
400 BaseException_methods, /* tp_methods */
Brett Cannon229cee22007-05-05 01:34:02 +0000401 0, /* tp_members */
Richard Jones7b9558d2006-05-27 12:29:24 +0000402 BaseException_getset, /* tp_getset */
403 0, /* tp_base */
404 0, /* tp_dict */
405 0, /* tp_descr_get */
406 0, /* tp_descr_set */
407 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
408 (initproc)BaseException_init, /* tp_init */
409 0, /* tp_alloc */
410 BaseException_new, /* tp_new */
411};
412/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
413from the previous implmentation and also allowing Python objects to be used
414in the API */
415PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
416
Richard Jones2d555b32006-05-27 16:15:11 +0000417/* note these macros omit the last semicolon so the macro invocation may
418 * include it and not look strange.
419 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000420#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
421static PyTypeObject _PyExc_ ## EXCNAME = { \
422 PyObject_HEAD_INIT(NULL) \
423 0, \
424 EXC_MODULE_NAME # EXCNAME, \
425 sizeof(PyBaseExceptionObject), \
426 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
427 0, 0, 0, 0, 0, 0, 0, \
428 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
429 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
430 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
431 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
432 (initproc)BaseException_init, 0, BaseException_new,\
433}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000434PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000435
436#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
437static PyTypeObject _PyExc_ ## EXCNAME = { \
438 PyObject_HEAD_INIT(NULL) \
439 0, \
440 EXC_MODULE_NAME # EXCNAME, \
441 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000442 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000443 0, 0, 0, 0, 0, \
444 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000445 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
446 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000447 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000448 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000449}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000450PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000451
452#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
453static PyTypeObject _PyExc_ ## EXCNAME = { \
454 PyObject_HEAD_INIT(NULL) \
455 0, \
456 EXC_MODULE_NAME # EXCNAME, \
457 sizeof(Py ## EXCSTORE ## Object), 0, \
458 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
459 (reprfunc)EXCSTR, 0, 0, 0, \
460 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
461 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
462 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
463 EXCMEMBERS, 0, &_ ## EXCBASE, \
464 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000465 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000466}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000467PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000468
469
470/*
471 * Exception extends BaseException
472 */
473SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000474 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000475
476
477/*
478 * StandardError extends Exception
479 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000480SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000481 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000482 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000483
484
485/*
486 * TypeError extends StandardError
487 */
488SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000489 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000490
491
492/*
493 * StopIteration extends Exception
494 */
495SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000496 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000497
498
499/*
Christian Heimes44eeaec2007-12-03 20:01:02 +0000500 * GeneratorExit extends BaseException
Richard Jones7b9558d2006-05-27 12:29:24 +0000501 */
Christian Heimes44eeaec2007-12-03 20:01:02 +0000502SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000503 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000504
505
506/*
507 * SystemExit extends BaseException
508 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000509
510static int
511SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
512{
513 Py_ssize_t size = PyTuple_GET_SIZE(args);
514
515 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
516 return -1;
517
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000518 if (size == 0)
519 return 0;
Serhiy Storchaka2e6c8292015-12-27 15:41:58 +0200520 if (size == 1) {
521 Py_INCREF(PyTuple_GET_ITEM(args, 0));
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300522 Py_XSETREF(self->code, PyTuple_GET_ITEM(args, 0));
Serhiy Storchaka2e6c8292015-12-27 15:41:58 +0200523 }
524 else { /* size > 1 */
525 Py_INCREF(args);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300526 Py_XSETREF(self->code, args);
Serhiy Storchaka2e6c8292015-12-27 15:41:58 +0200527 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000528 return 0;
529}
530
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000531static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000532SystemExit_clear(PySystemExitObject *self)
533{
534 Py_CLEAR(self->code);
535 return BaseException_clear((PyBaseExceptionObject *)self);
536}
537
538static void
539SystemExit_dealloc(PySystemExitObject *self)
540{
Georg Brandl38f62372006-09-06 06:50:05 +0000541 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000542 SystemExit_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000543 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000544}
545
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000546static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000547SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
548{
549 Py_VISIT(self->code);
550 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
551}
552
553static PyMemberDef SystemExit_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000554 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
555 PyDoc_STR("exception code")},
556 {NULL} /* Sentinel */
557};
558
559ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
560 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000561 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000562
563/*
564 * KeyboardInterrupt extends BaseException
565 */
566SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000567 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000568
569
570/*
571 * ImportError extends StandardError
572 */
573SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000574 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000575
576
577/*
578 * EnvironmentError extends StandardError
579 */
580
Richard Jones7b9558d2006-05-27 12:29:24 +0000581/* Where a function has a single filename, such as open() or some
582 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
583 * called, giving a third argument which is the filename. But, so
584 * that old code using in-place unpacking doesn't break, e.g.:
585 *
586 * except IOError, (errno, strerror):
587 *
588 * we hack args so that it only contains two items. This also
589 * means we need our own __str__() which prints out the filename
590 * when it was supplied.
591 */
592static int
593EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
594 PyObject *kwds)
595{
596 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
597 PyObject *subslice = NULL;
598
599 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
600 return -1;
601
Georg Brandl3267d282006-09-30 09:03:42 +0000602 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000603 return 0;
604 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000605
606 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000607 &myerrno, &strerror, &filename)) {
608 return -1;
609 }
Serhiy Storchaka8688aca2015-12-27 12:38:48 +0200610 Py_INCREF(myerrno);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300611 Py_XSETREF(self->myerrno, myerrno);
Richard Jones7b9558d2006-05-27 12:29:24 +0000612
Serhiy Storchaka8688aca2015-12-27 12:38:48 +0200613 Py_INCREF(strerror);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300614 Py_XSETREF(self->strerror, strerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000615
616 /* self->filename will remain Py_None otherwise */
617 if (filename != NULL) {
Serhiy Storchaka8688aca2015-12-27 12:38:48 +0200618 Py_INCREF(filename);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300619 Py_XSETREF(self->filename, filename);
Richard Jones7b9558d2006-05-27 12:29:24 +0000620
621 subslice = PyTuple_GetSlice(args, 0, 2);
622 if (!subslice)
623 return -1;
624
Serhiy Storchaka763a61c2016-04-10 18:05:12 +0300625 Py_SETREF(self->args, subslice);
Richard Jones7b9558d2006-05-27 12:29:24 +0000626 }
627 return 0;
628}
629
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000630static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000631EnvironmentError_clear(PyEnvironmentErrorObject *self)
632{
633 Py_CLEAR(self->myerrno);
634 Py_CLEAR(self->strerror);
635 Py_CLEAR(self->filename);
636 return BaseException_clear((PyBaseExceptionObject *)self);
637}
638
639static void
640EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
641{
Georg Brandl38f62372006-09-06 06:50:05 +0000642 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000643 EnvironmentError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000644 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000645}
646
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000647static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000648EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
649 void *arg)
650{
651 Py_VISIT(self->myerrno);
652 Py_VISIT(self->strerror);
653 Py_VISIT(self->filename);
654 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
655}
656
657static PyObject *
658EnvironmentError_str(PyEnvironmentErrorObject *self)
659{
660 PyObject *rtnval = NULL;
661
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000662 if (self->filename) {
663 PyObject *fmt;
664 PyObject *repr;
665 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000666
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000667 fmt = PyString_FromString("[Errno %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000668 if (!fmt)
669 return NULL;
670
671 repr = PyObject_Repr(self->filename);
672 if (!repr) {
673 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000674 return NULL;
675 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000676 tuple = PyTuple_New(3);
677 if (!tuple) {
678 Py_DECREF(repr);
679 Py_DECREF(fmt);
680 return NULL;
681 }
682
683 if (self->myerrno) {
684 Py_INCREF(self->myerrno);
685 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
686 }
687 else {
688 Py_INCREF(Py_None);
689 PyTuple_SET_ITEM(tuple, 0, Py_None);
690 }
691 if (self->strerror) {
692 Py_INCREF(self->strerror);
693 PyTuple_SET_ITEM(tuple, 1, self->strerror);
694 }
695 else {
696 Py_INCREF(Py_None);
697 PyTuple_SET_ITEM(tuple, 1, Py_None);
698 }
699
Richard Jones7b9558d2006-05-27 12:29:24 +0000700 PyTuple_SET_ITEM(tuple, 2, repr);
701
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000702 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000703
704 Py_DECREF(fmt);
705 Py_DECREF(tuple);
706 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000707 else if (self->myerrno && self->strerror) {
708 PyObject *fmt;
709 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000710
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000711 fmt = PyString_FromString("[Errno %s] %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000712 if (!fmt)
713 return NULL;
714
715 tuple = PyTuple_New(2);
716 if (!tuple) {
717 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000718 return NULL;
719 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000720
721 if (self->myerrno) {
722 Py_INCREF(self->myerrno);
723 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
724 }
725 else {
726 Py_INCREF(Py_None);
727 PyTuple_SET_ITEM(tuple, 0, Py_None);
728 }
729 if (self->strerror) {
730 Py_INCREF(self->strerror);
731 PyTuple_SET_ITEM(tuple, 1, self->strerror);
732 }
733 else {
734 Py_INCREF(Py_None);
735 PyTuple_SET_ITEM(tuple, 1, Py_None);
736 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000737
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000738 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000739
740 Py_DECREF(fmt);
741 Py_DECREF(tuple);
742 }
743 else
744 rtnval = BaseException_str((PyBaseExceptionObject *)self);
745
746 return rtnval;
747}
748
749static PyMemberDef EnvironmentError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000750 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000751 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000752 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000753 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000754 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000755 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000756 {NULL} /* Sentinel */
757};
758
759
760static PyObject *
761EnvironmentError_reduce(PyEnvironmentErrorObject *self)
762{
763 PyObject *args = self->args;
764 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000765
Richard Jones7b9558d2006-05-27 12:29:24 +0000766 /* self->args is only the first two real arguments if there was a
767 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000768 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000769 args = PyTuple_New(3);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -0500770 if (!args)
771 return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000772
773 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000774 Py_INCREF(tmp);
775 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000776
777 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000778 Py_INCREF(tmp);
779 PyTuple_SET_ITEM(args, 1, tmp);
780
781 Py_INCREF(self->filename);
782 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000783 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000784 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000785
786 if (self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000787 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000788 else
Christian Heimese93237d2007-12-19 02:37:44 +0000789 res = PyTuple_Pack(2, Py_TYPE(self), args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000790 Py_DECREF(args);
791 return res;
792}
793
794
795static PyMethodDef EnvironmentError_methods[] = {
796 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
797 {NULL}
798};
799
800ComplexExtendsException(PyExc_StandardError, EnvironmentError,
801 EnvironmentError, EnvironmentError_dealloc,
802 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000803 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000804 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000805
806
807/*
808 * IOError extends EnvironmentError
809 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000810MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000811 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000812
813
814/*
815 * OSError extends EnvironmentError
816 */
817MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000818 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000819
820
821/*
822 * WindowsError extends OSError
823 */
824#ifdef MS_WINDOWS
825#include "errmap.h"
826
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000827static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000828WindowsError_clear(PyWindowsErrorObject *self)
829{
830 Py_CLEAR(self->myerrno);
831 Py_CLEAR(self->strerror);
832 Py_CLEAR(self->filename);
833 Py_CLEAR(self->winerror);
834 return BaseException_clear((PyBaseExceptionObject *)self);
835}
836
837static void
838WindowsError_dealloc(PyWindowsErrorObject *self)
839{
Georg Brandl38f62372006-09-06 06:50:05 +0000840 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000841 WindowsError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000842 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000843}
844
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000845static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000846WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
847{
848 Py_VISIT(self->myerrno);
849 Py_VISIT(self->strerror);
850 Py_VISIT(self->filename);
851 Py_VISIT(self->winerror);
852 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
853}
854
Richard Jones7b9558d2006-05-27 12:29:24 +0000855static int
856WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
857{
858 PyObject *o_errcode = NULL;
859 long errcode;
860 long posix_errno;
861
862 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
863 == -1)
864 return -1;
865
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000866 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000867 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000868
869 /* Set errno to the POSIX errno, and winerror to the Win32
870 error code. */
871 errcode = PyInt_AsLong(self->myerrno);
872 if (errcode == -1 && PyErr_Occurred())
873 return -1;
874 posix_errno = winerror_to_errno(errcode);
875
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300876 Py_XSETREF(self->winerror, self->myerrno);
Richard Jones7b9558d2006-05-27 12:29:24 +0000877
878 o_errcode = PyInt_FromLong(posix_errno);
879 if (!o_errcode)
880 return -1;
881
882 self->myerrno = o_errcode;
883
884 return 0;
885}
886
887
888static PyObject *
889WindowsError_str(PyWindowsErrorObject *self)
890{
Richard Jones7b9558d2006-05-27 12:29:24 +0000891 PyObject *rtnval = NULL;
892
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000893 if (self->filename) {
894 PyObject *fmt;
895 PyObject *repr;
896 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000897
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000898 fmt = PyString_FromString("[Error %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000899 if (!fmt)
900 return NULL;
901
902 repr = PyObject_Repr(self->filename);
903 if (!repr) {
904 Py_DECREF(fmt);
905 return NULL;
906 }
907 tuple = PyTuple_New(3);
908 if (!tuple) {
909 Py_DECREF(repr);
910 Py_DECREF(fmt);
911 return NULL;
912 }
913
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000914 if (self->winerror) {
915 Py_INCREF(self->winerror);
916 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000917 }
918 else {
919 Py_INCREF(Py_None);
920 PyTuple_SET_ITEM(tuple, 0, Py_None);
921 }
922 if (self->strerror) {
923 Py_INCREF(self->strerror);
924 PyTuple_SET_ITEM(tuple, 1, self->strerror);
925 }
926 else {
927 Py_INCREF(Py_None);
928 PyTuple_SET_ITEM(tuple, 1, Py_None);
929 }
930
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000931 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000932
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000933 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000934
935 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000936 Py_DECREF(tuple);
937 }
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000938 else if (self->winerror && self->strerror) {
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000939 PyObject *fmt;
940 PyObject *tuple;
941
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000942 fmt = PyString_FromString("[Error %s] %s");
Richard Jones7b9558d2006-05-27 12:29:24 +0000943 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000944 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000945
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000946 tuple = PyTuple_New(2);
947 if (!tuple) {
948 Py_DECREF(fmt);
949 return NULL;
950 }
951
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000952 if (self->winerror) {
953 Py_INCREF(self->winerror);
954 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000955 }
956 else {
957 Py_INCREF(Py_None);
958 PyTuple_SET_ITEM(tuple, 0, Py_None);
959 }
960 if (self->strerror) {
961 Py_INCREF(self->strerror);
962 PyTuple_SET_ITEM(tuple, 1, self->strerror);
963 }
964 else {
965 Py_INCREF(Py_None);
966 PyTuple_SET_ITEM(tuple, 1, Py_None);
967 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000968
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000969 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000970
971 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000972 Py_DECREF(tuple);
973 }
974 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000975 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000976
Richard Jones7b9558d2006-05-27 12:29:24 +0000977 return rtnval;
978}
979
980static PyMemberDef WindowsError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000981 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000982 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000983 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000984 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000985 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000986 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000987 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000988 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000989 {NULL} /* Sentinel */
990};
991
Richard Jones2d555b32006-05-27 16:15:11 +0000992ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
993 WindowsError_dealloc, 0, WindowsError_members,
994 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000995
996#endif /* MS_WINDOWS */
997
998
999/*
1000 * VMSError extends OSError (I think)
1001 */
1002#ifdef __VMS
1003MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +00001004 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001005#endif
1006
1007
1008/*
1009 * EOFError extends StandardError
1010 */
1011SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +00001012 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001013
1014
1015/*
1016 * RuntimeError extends StandardError
1017 */
1018SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001019 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001020
1021
1022/*
1023 * NotImplementedError extends RuntimeError
1024 */
1025SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +00001026 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001027
1028/*
1029 * NameError extends StandardError
1030 */
1031SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +00001032 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001033
1034/*
1035 * UnboundLocalError extends NameError
1036 */
1037SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +00001038 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001039
1040/*
1041 * AttributeError extends StandardError
1042 */
1043SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001044 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001045
1046
1047/*
1048 * SyntaxError extends StandardError
1049 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001050
1051static int
1052SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
1053{
1054 PyObject *info = NULL;
1055 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
1056
1057 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1058 return -1;
1059
1060 if (lenargs >= 1) {
Serhiy Storchaka8688aca2015-12-27 12:38:48 +02001061 Py_INCREF(PyTuple_GET_ITEM(args, 0));
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001062 Py_XSETREF(self->msg, PyTuple_GET_ITEM(args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001063 }
1064 if (lenargs == 2) {
1065 info = PyTuple_GET_ITEM(args, 1);
1066 info = PySequence_Tuple(info);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -05001067 if (!info)
1068 return -1;
Richard Jones7b9558d2006-05-27 12:29:24 +00001069
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001070 if (PyTuple_GET_SIZE(info) != 4) {
1071 /* not a very good error message, but it's what Python 2.4 gives */
1072 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
1073 Py_DECREF(info);
1074 return -1;
1075 }
1076
Serhiy Storchaka8688aca2015-12-27 12:38:48 +02001077 Py_INCREF(PyTuple_GET_ITEM(info, 0));
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001078 Py_XSETREF(self->filename, PyTuple_GET_ITEM(info, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001079
Serhiy Storchaka8688aca2015-12-27 12:38:48 +02001080 Py_INCREF(PyTuple_GET_ITEM(info, 1));
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001081 Py_XSETREF(self->lineno, PyTuple_GET_ITEM(info, 1));
Richard Jones7b9558d2006-05-27 12:29:24 +00001082
Serhiy Storchaka8688aca2015-12-27 12:38:48 +02001083 Py_INCREF(PyTuple_GET_ITEM(info, 2));
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001084 Py_XSETREF(self->offset, PyTuple_GET_ITEM(info, 2));
Richard Jones7b9558d2006-05-27 12:29:24 +00001085
Serhiy Storchaka8688aca2015-12-27 12:38:48 +02001086 Py_INCREF(PyTuple_GET_ITEM(info, 3));
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001087 Py_XSETREF(self->text, PyTuple_GET_ITEM(info, 3));
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001088
1089 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001090 }
1091 return 0;
1092}
1093
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001094static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001095SyntaxError_clear(PySyntaxErrorObject *self)
1096{
1097 Py_CLEAR(self->msg);
1098 Py_CLEAR(self->filename);
1099 Py_CLEAR(self->lineno);
1100 Py_CLEAR(self->offset);
1101 Py_CLEAR(self->text);
1102 Py_CLEAR(self->print_file_and_line);
1103 return BaseException_clear((PyBaseExceptionObject *)self);
1104}
1105
1106static void
1107SyntaxError_dealloc(PySyntaxErrorObject *self)
1108{
Georg Brandl38f62372006-09-06 06:50:05 +00001109 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001110 SyntaxError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001111 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001112}
1113
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001114static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001115SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1116{
1117 Py_VISIT(self->msg);
1118 Py_VISIT(self->filename);
1119 Py_VISIT(self->lineno);
1120 Py_VISIT(self->offset);
1121 Py_VISIT(self->text);
1122 Py_VISIT(self->print_file_and_line);
1123 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1124}
1125
1126/* This is called "my_basename" instead of just "basename" to avoid name
1127 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1128 defined, and Python does define that. */
1129static char *
1130my_basename(char *name)
1131{
1132 char *cp = name;
1133 char *result = name;
1134
1135 if (name == NULL)
1136 return "???";
1137 while (*cp != '\0') {
1138 if (*cp == SEP)
1139 result = cp + 1;
1140 ++cp;
1141 }
1142 return result;
1143}
1144
1145
1146static PyObject *
1147SyntaxError_str(PySyntaxErrorObject *self)
1148{
1149 PyObject *str;
1150 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001151 int have_filename = 0;
1152 int have_lineno = 0;
1153 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001154 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001155
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001156 if (self->msg)
1157 str = PyObject_Str(self->msg);
1158 else
1159 str = PyObject_Str(Py_None);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -05001160 if (!str)
1161 return NULL;
Georg Brandl43ab1002006-05-28 20:57:09 +00001162 /* Don't fiddle with non-string return (shouldn't happen anyway) */
Benjamin Peterson4c79aec2012-02-03 19:22:31 -05001163 if (!PyString_Check(str))
1164 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001165
1166 /* XXX -- do all the additional formatting with filename and
1167 lineno here */
1168
Georg Brandl43ab1002006-05-28 20:57:09 +00001169 have_filename = (self->filename != NULL) &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001170 PyString_Check(self->filename);
Georg Brandl43ab1002006-05-28 20:57:09 +00001171 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001172
Georg Brandl43ab1002006-05-28 20:57:09 +00001173 if (!have_filename && !have_lineno)
1174 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001175
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001176 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001177 if (have_filename)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001178 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001179
Georg Brandl43ab1002006-05-28 20:57:09 +00001180 buffer = PyMem_MALLOC(bufsize);
1181 if (buffer == NULL)
1182 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001183
Georg Brandl43ab1002006-05-28 20:57:09 +00001184 if (have_filename && have_lineno)
1185 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001186 PyString_AS_STRING(str),
1187 my_basename(PyString_AS_STRING(self->filename)),
Georg Brandl43ab1002006-05-28 20:57:09 +00001188 PyInt_AsLong(self->lineno));
1189 else if (have_filename)
1190 PyOS_snprintf(buffer, bufsize, "%s (%s)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001191 PyString_AS_STRING(str),
1192 my_basename(PyString_AS_STRING(self->filename)));
Georg Brandl43ab1002006-05-28 20:57:09 +00001193 else /* only have_lineno */
1194 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001195 PyString_AS_STRING(str),
Georg Brandl43ab1002006-05-28 20:57:09 +00001196 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001197
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001198 result = PyString_FromString(buffer);
Georg Brandl43ab1002006-05-28 20:57:09 +00001199 PyMem_FREE(buffer);
1200
1201 if (result == NULL)
1202 result = str;
1203 else
1204 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001205 return result;
1206}
1207
1208static PyMemberDef SyntaxError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001209 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1210 PyDoc_STR("exception msg")},
1211 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1212 PyDoc_STR("exception filename")},
1213 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1214 PyDoc_STR("exception lineno")},
1215 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1216 PyDoc_STR("exception offset")},
1217 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1218 PyDoc_STR("exception text")},
1219 {"print_file_and_line", T_OBJECT,
1220 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1221 PyDoc_STR("exception print_file_and_line")},
1222 {NULL} /* Sentinel */
1223};
1224
1225ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1226 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001227 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001228
1229
1230/*
1231 * IndentationError extends SyntaxError
1232 */
1233MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001234 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001235
1236
1237/*
1238 * TabError extends IndentationError
1239 */
1240MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001241 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001242
1243
1244/*
1245 * LookupError extends StandardError
1246 */
1247SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001248 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001249
1250
1251/*
1252 * IndexError extends LookupError
1253 */
1254SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001255 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001256
1257
1258/*
1259 * KeyError extends LookupError
1260 */
1261static PyObject *
1262KeyError_str(PyBaseExceptionObject *self)
1263{
1264 /* If args is a tuple of exactly one item, apply repr to args[0].
1265 This is done so that e.g. the exception raised by {}[''] prints
1266 KeyError: ''
1267 rather than the confusing
1268 KeyError
1269 alone. The downside is that if KeyError is raised with an explanatory
1270 string, that string will be displayed in quotes. Too bad.
1271 If args is anything else, use the default BaseException__str__().
1272 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001273 if (PyTuple_GET_SIZE(self->args) == 1) {
1274 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001275 }
1276 return BaseException_str(self);
1277}
1278
1279ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001280 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001281
1282
1283/*
1284 * ValueError extends StandardError
1285 */
1286SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001287 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001288
1289/*
1290 * UnicodeError extends ValueError
1291 */
1292
1293SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001294 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001295
1296#ifdef Py_USING_UNICODE
Richard Jones7b9558d2006-05-27 12:29:24 +00001297static PyObject *
1298get_string(PyObject *attr, const char *name)
1299{
1300 if (!attr) {
1301 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1302 return NULL;
1303 }
1304
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001305 if (!PyString_Check(attr)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001306 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1307 return NULL;
1308 }
1309 Py_INCREF(attr);
1310 return attr;
1311}
1312
1313
1314static int
1315set_string(PyObject **attr, const char *value)
1316{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001317 PyObject *obj = PyString_FromString(value);
Richard Jones7b9558d2006-05-27 12:29:24 +00001318 if (!obj)
1319 return -1;
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001320 Py_XSETREF(*attr, obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001321 return 0;
1322}
1323
1324
1325static PyObject *
1326get_unicode(PyObject *attr, const char *name)
1327{
1328 if (!attr) {
1329 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1330 return NULL;
1331 }
1332
1333 if (!PyUnicode_Check(attr)) {
1334 PyErr_Format(PyExc_TypeError,
1335 "%.200s attribute must be unicode", name);
1336 return NULL;
1337 }
1338 Py_INCREF(attr);
1339 return attr;
1340}
1341
1342PyObject *
1343PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1344{
1345 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1346}
1347
1348PyObject *
1349PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1350{
1351 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1352}
1353
1354PyObject *
1355PyUnicodeEncodeError_GetObject(PyObject *exc)
1356{
1357 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1358}
1359
1360PyObject *
1361PyUnicodeDecodeError_GetObject(PyObject *exc)
1362{
1363 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1364}
1365
1366PyObject *
1367PyUnicodeTranslateError_GetObject(PyObject *exc)
1368{
1369 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1370}
1371
1372int
1373PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1374{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001375 Py_ssize_t size;
1376 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1377 "object");
1378 if (!obj)
1379 return -1;
1380 *start = ((PyUnicodeErrorObject *)exc)->start;
1381 size = PyUnicode_GET_SIZE(obj);
1382 if (*start<0)
1383 *start = 0; /*XXX check for values <0*/
1384 if (*start>=size)
1385 *start = size-1;
1386 Py_DECREF(obj);
1387 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001388}
1389
1390
1391int
1392PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1393{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001394 Py_ssize_t size;
1395 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1396 "object");
1397 if (!obj)
1398 return -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001399 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001400 *start = ((PyUnicodeErrorObject *)exc)->start;
1401 if (*start<0)
1402 *start = 0;
1403 if (*start>=size)
1404 *start = size-1;
1405 Py_DECREF(obj);
1406 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001407}
1408
1409
1410int
1411PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1412{
1413 return PyUnicodeEncodeError_GetStart(exc, start);
1414}
1415
1416
1417int
1418PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1419{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001420 ((PyUnicodeErrorObject *)exc)->start = start;
1421 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001422}
1423
1424
1425int
1426PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1427{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001428 ((PyUnicodeErrorObject *)exc)->start = start;
1429 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001430}
1431
1432
1433int
1434PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1435{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001436 ((PyUnicodeErrorObject *)exc)->start = start;
1437 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001438}
1439
1440
1441int
1442PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1443{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001444 Py_ssize_t size;
1445 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1446 "object");
1447 if (!obj)
1448 return -1;
1449 *end = ((PyUnicodeErrorObject *)exc)->end;
1450 size = PyUnicode_GET_SIZE(obj);
1451 if (*end<1)
1452 *end = 1;
1453 if (*end>size)
1454 *end = size;
1455 Py_DECREF(obj);
1456 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001457}
1458
1459
1460int
1461PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1462{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001463 Py_ssize_t size;
1464 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1465 "object");
1466 if (!obj)
1467 return -1;
1468 *end = ((PyUnicodeErrorObject *)exc)->end;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001469 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001470 if (*end<1)
1471 *end = 1;
1472 if (*end>size)
1473 *end = size;
1474 Py_DECREF(obj);
1475 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001476}
1477
1478
1479int
1480PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1481{
1482 return PyUnicodeEncodeError_GetEnd(exc, start);
1483}
1484
1485
1486int
1487PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1488{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001489 ((PyUnicodeErrorObject *)exc)->end = end;
1490 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001491}
1492
1493
1494int
1495PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1496{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001497 ((PyUnicodeErrorObject *)exc)->end = end;
1498 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001499}
1500
1501
1502int
1503PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1504{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001505 ((PyUnicodeErrorObject *)exc)->end = end;
1506 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001507}
1508
1509PyObject *
1510PyUnicodeEncodeError_GetReason(PyObject *exc)
1511{
1512 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1513}
1514
1515
1516PyObject *
1517PyUnicodeDecodeError_GetReason(PyObject *exc)
1518{
1519 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1520}
1521
1522
1523PyObject *
1524PyUnicodeTranslateError_GetReason(PyObject *exc)
1525{
1526 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1527}
1528
1529
1530int
1531PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1532{
1533 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1534}
1535
1536
1537int
1538PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1539{
1540 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1541}
1542
1543
1544int
1545PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1546{
1547 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1548}
1549
1550
Richard Jones7b9558d2006-05-27 12:29:24 +00001551static int
1552UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1553 PyTypeObject *objecttype)
1554{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001555 Py_CLEAR(self->encoding);
1556 Py_CLEAR(self->object);
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001557 Py_CLEAR(self->reason);
1558
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001559 if (!PyArg_ParseTuple(args, "O!O!nnO!",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001560 &PyString_Type, &self->encoding,
Richard Jones7b9558d2006-05-27 12:29:24 +00001561 objecttype, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001562 &self->start,
1563 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001564 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001565 self->encoding = self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001566 return -1;
1567 }
1568
1569 Py_INCREF(self->encoding);
1570 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001571 Py_INCREF(self->reason);
1572
1573 return 0;
1574}
1575
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001576static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001577UnicodeError_clear(PyUnicodeErrorObject *self)
1578{
1579 Py_CLEAR(self->encoding);
1580 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001581 Py_CLEAR(self->reason);
1582 return BaseException_clear((PyBaseExceptionObject *)self);
1583}
1584
1585static void
1586UnicodeError_dealloc(PyUnicodeErrorObject *self)
1587{
Georg Brandl38f62372006-09-06 06:50:05 +00001588 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001589 UnicodeError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001590 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001591}
1592
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001593static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001594UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1595{
1596 Py_VISIT(self->encoding);
1597 Py_VISIT(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001598 Py_VISIT(self->reason);
1599 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1600}
1601
1602static PyMemberDef UnicodeError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001603 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1604 PyDoc_STR("exception encoding")},
1605 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1606 PyDoc_STR("exception object")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001607 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001608 PyDoc_STR("exception start")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001609 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001610 PyDoc_STR("exception end")},
1611 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1612 PyDoc_STR("exception reason")},
1613 {NULL} /* Sentinel */
1614};
1615
1616
1617/*
1618 * UnicodeEncodeError extends UnicodeError
1619 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001620
1621static int
1622UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1623{
1624 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1625 return -1;
1626 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1627 kwds, &PyUnicode_Type);
1628}
1629
1630static PyObject *
1631UnicodeEncodeError_str(PyObject *self)
1632{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001633 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith2d9856d2010-02-24 14:15:36 +00001634 PyObject *result = NULL;
1635 PyObject *reason_str = NULL;
1636 PyObject *encoding_str = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001637
Benjamin Petersonc4e6e0a2014-04-02 12:15:06 -04001638 if (!uself->object)
1639 /* Not properly initialized. */
1640 return PyUnicode_FromString("");
1641
Eric Smith2d9856d2010-02-24 14:15:36 +00001642 /* Get reason and encoding as strings, which they might not be if
Martin Pantera850ef62016-07-28 01:11:04 +00001643 they've been modified after we were constructed. */
Eric Smith2d9856d2010-02-24 14:15:36 +00001644 reason_str = PyObject_Str(uself->reason);
1645 if (reason_str == NULL)
1646 goto done;
1647 encoding_str = PyObject_Str(uself->encoding);
1648 if (encoding_str == NULL)
1649 goto done;
1650
1651 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001652 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001653 char badchar_str[20];
1654 if (badchar <= 0xff)
1655 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1656 else if (badchar <= 0xffff)
1657 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1658 else
1659 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Eric Smith2d9856d2010-02-24 14:15:36 +00001660 result = PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001661 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Eric Smith2d9856d2010-02-24 14:15:36 +00001662 PyString_AS_STRING(encoding_str),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001663 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001664 uself->start,
Eric Smith2d9856d2010-02-24 14:15:36 +00001665 PyString_AS_STRING(reason_str));
Richard Jones7b9558d2006-05-27 12:29:24 +00001666 }
Eric Smith2d9856d2010-02-24 14:15:36 +00001667 else {
1668 result = PyString_FromFormat(
1669 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
1670 PyString_AS_STRING(encoding_str),
1671 uself->start,
1672 uself->end-1,
1673 PyString_AS_STRING(reason_str));
1674 }
1675done:
1676 Py_XDECREF(reason_str);
1677 Py_XDECREF(encoding_str);
1678 return result;
Richard Jones7b9558d2006-05-27 12:29:24 +00001679}
1680
1681static PyTypeObject _PyExc_UnicodeEncodeError = {
1682 PyObject_HEAD_INIT(NULL)
1683 0,
Georg Brandl38f62372006-09-06 06:50:05 +00001684 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001685 sizeof(PyUnicodeErrorObject), 0,
1686 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1687 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1688 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001689 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1690 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001691 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001692 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001693};
1694PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1695
1696PyObject *
1697PyUnicodeEncodeError_Create(
1698 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1699 Py_ssize_t start, Py_ssize_t end, const char *reason)
1700{
1701 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001702 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001703}
1704
1705
1706/*
1707 * UnicodeDecodeError extends UnicodeError
1708 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001709
1710static int
1711UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1712{
1713 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1714 return -1;
1715 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001716 kwds, &PyString_Type);
Richard Jones7b9558d2006-05-27 12:29:24 +00001717}
1718
1719static PyObject *
1720UnicodeDecodeError_str(PyObject *self)
1721{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001722 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith2d9856d2010-02-24 14:15:36 +00001723 PyObject *result = NULL;
1724 PyObject *reason_str = NULL;
1725 PyObject *encoding_str = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001726
Benjamin Petersonc4e6e0a2014-04-02 12:15:06 -04001727 if (!uself->object)
1728 /* Not properly initialized. */
1729 return PyUnicode_FromString("");
1730
Eric Smith2d9856d2010-02-24 14:15:36 +00001731 /* Get reason and encoding as strings, which they might not be if
Martin Pantera850ef62016-07-28 01:11:04 +00001732 they've been modified after we were constructed. */
Eric Smith2d9856d2010-02-24 14:15:36 +00001733 reason_str = PyObject_Str(uself->reason);
1734 if (reason_str == NULL)
1735 goto done;
1736 encoding_str = PyObject_Str(uself->encoding);
1737 if (encoding_str == NULL)
1738 goto done;
1739
1740 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001741 /* FromFormat does not support %02x, so format that separately */
1742 char byte[4];
1743 PyOS_snprintf(byte, sizeof(byte), "%02x",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001744 ((int)PyString_AS_STRING(uself->object)[uself->start])&0xff);
Eric Smith2d9856d2010-02-24 14:15:36 +00001745 result = PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001746 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Eric Smith2d9856d2010-02-24 14:15:36 +00001747 PyString_AS_STRING(encoding_str),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001748 byte,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001749 uself->start,
Eric Smith2d9856d2010-02-24 14:15:36 +00001750 PyString_AS_STRING(reason_str));
Richard Jones7b9558d2006-05-27 12:29:24 +00001751 }
Eric Smith2d9856d2010-02-24 14:15:36 +00001752 else {
1753 result = PyString_FromFormat(
1754 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
1755 PyString_AS_STRING(encoding_str),
1756 uself->start,
1757 uself->end-1,
1758 PyString_AS_STRING(reason_str));
1759 }
1760done:
1761 Py_XDECREF(reason_str);
1762 Py_XDECREF(encoding_str);
1763 return result;
Richard Jones7b9558d2006-05-27 12:29:24 +00001764}
1765
1766static PyTypeObject _PyExc_UnicodeDecodeError = {
1767 PyObject_HEAD_INIT(NULL)
1768 0,
1769 EXC_MODULE_NAME "UnicodeDecodeError",
1770 sizeof(PyUnicodeErrorObject), 0,
1771 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1772 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1773 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001774 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1775 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001776 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001777 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001778};
1779PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1780
1781PyObject *
1782PyUnicodeDecodeError_Create(
1783 const char *encoding, const char *object, Py_ssize_t length,
1784 Py_ssize_t start, Py_ssize_t end, const char *reason)
1785{
Richard Jones7b9558d2006-05-27 12:29:24 +00001786 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001787 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001788}
1789
1790
1791/*
1792 * UnicodeTranslateError extends UnicodeError
1793 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001794
1795static int
1796UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1797 PyObject *kwds)
1798{
1799 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1800 return -1;
1801
1802 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001803 Py_CLEAR(self->reason);
1804
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001805 if (!PyArg_ParseTuple(args, "O!nnO!",
Richard Jones7b9558d2006-05-27 12:29:24 +00001806 &PyUnicode_Type, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001807 &self->start,
1808 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001809 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001810 self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001811 return -1;
1812 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001813
Richard Jones7b9558d2006-05-27 12:29:24 +00001814 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001815 Py_INCREF(self->reason);
1816
1817 return 0;
1818}
1819
1820
1821static PyObject *
1822UnicodeTranslateError_str(PyObject *self)
1823{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001824 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith2d9856d2010-02-24 14:15:36 +00001825 PyObject *result = NULL;
1826 PyObject *reason_str = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001827
Benjamin Petersonc4e6e0a2014-04-02 12:15:06 -04001828 if (!uself->object)
1829 /* Not properly initialized. */
1830 return PyUnicode_FromString("");
1831
Eric Smith2d9856d2010-02-24 14:15:36 +00001832 /* Get reason as a string, which it might not be if it's been
Martin Pantera850ef62016-07-28 01:11:04 +00001833 modified after we were constructed. */
Eric Smith2d9856d2010-02-24 14:15:36 +00001834 reason_str = PyObject_Str(uself->reason);
1835 if (reason_str == NULL)
1836 goto done;
1837
1838 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001839 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001840 char badchar_str[20];
1841 if (badchar <= 0xff)
1842 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1843 else if (badchar <= 0xffff)
1844 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1845 else
1846 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Eric Smith2d9856d2010-02-24 14:15:36 +00001847 result = PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001848 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001849 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001850 uself->start,
Eric Smith2d9856d2010-02-24 14:15:36 +00001851 PyString_AS_STRING(reason_str));
1852 } else {
1853 result = PyString_FromFormat(
1854 "can't translate characters in position %zd-%zd: %.400s",
1855 uself->start,
1856 uself->end-1,
1857 PyString_AS_STRING(reason_str));
Richard Jones7b9558d2006-05-27 12:29:24 +00001858 }
Eric Smith2d9856d2010-02-24 14:15:36 +00001859done:
1860 Py_XDECREF(reason_str);
1861 return result;
Richard Jones7b9558d2006-05-27 12:29:24 +00001862}
1863
1864static PyTypeObject _PyExc_UnicodeTranslateError = {
1865 PyObject_HEAD_INIT(NULL)
1866 0,
1867 EXC_MODULE_NAME "UnicodeTranslateError",
1868 sizeof(PyUnicodeErrorObject), 0,
1869 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1870 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1871 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001872 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001873 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1874 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001875 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001876};
1877PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1878
1879PyObject *
1880PyUnicodeTranslateError_Create(
1881 const Py_UNICODE *object, Py_ssize_t length,
1882 Py_ssize_t start, Py_ssize_t end, const char *reason)
1883{
1884 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001885 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001886}
1887#endif
1888
1889
1890/*
1891 * AssertionError extends StandardError
1892 */
1893SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001894 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001895
1896
1897/*
1898 * ArithmeticError extends StandardError
1899 */
1900SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001901 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001902
1903
1904/*
1905 * FloatingPointError extends ArithmeticError
1906 */
1907SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001908 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001909
1910
1911/*
1912 * OverflowError extends ArithmeticError
1913 */
1914SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001915 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001916
1917
1918/*
1919 * ZeroDivisionError extends ArithmeticError
1920 */
1921SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001922 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001923
1924
1925/*
1926 * SystemError extends StandardError
1927 */
1928SimpleExtendsException(PyExc_StandardError, SystemError,
1929 "Internal error in the Python interpreter.\n"
1930 "\n"
1931 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001932 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001933
1934
1935/*
1936 * ReferenceError extends StandardError
1937 */
1938SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001939 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001940
1941
1942/*
1943 * MemoryError extends StandardError
1944 */
Richard Jones2d555b32006-05-27 16:15:11 +00001945SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001946
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00001947/*
1948 * BufferError extends StandardError
1949 */
1950SimpleExtendsException(PyExc_StandardError, BufferError, "Buffer error.");
1951
Richard Jones7b9558d2006-05-27 12:29:24 +00001952
1953/* Warning category docstrings */
1954
1955/*
1956 * Warning extends Exception
1957 */
1958SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001959 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001960
1961
1962/*
1963 * UserWarning extends Warning
1964 */
1965SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001966 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001967
1968
1969/*
1970 * DeprecationWarning extends Warning
1971 */
1972SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001973 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001974
1975
1976/*
1977 * PendingDeprecationWarning extends Warning
1978 */
1979SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1980 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001981 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001982
1983
1984/*
1985 * SyntaxWarning extends Warning
1986 */
1987SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001988 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001989
1990
1991/*
1992 * RuntimeWarning extends Warning
1993 */
1994SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001995 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001996
1997
1998/*
1999 * FutureWarning extends Warning
2000 */
2001SimpleExtendsException(PyExc_Warning, FutureWarning,
2002 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00002003 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00002004
2005
2006/*
2007 * ImportWarning extends Warning
2008 */
2009SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00002010 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00002011
2012
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002013/*
2014 * UnicodeWarning extends Warning
2015 */
2016SimpleExtendsException(PyExc_Warning, UnicodeWarning,
2017 "Base class for warnings about Unicode related problems, mostly\n"
2018 "related to conversion problems.");
2019
Christian Heimes1a6387e2008-03-26 12:49:49 +00002020/*
2021 * BytesWarning extends Warning
2022 */
2023SimpleExtendsException(PyExc_Warning, BytesWarning,
2024 "Base class for warnings about bytes and buffer related problems, mostly\n"
2025 "related to conversion from str or comparing to str.");
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002026
Richard Jones7b9558d2006-05-27 12:29:24 +00002027/* Pre-computed MemoryError instance. Best to create this as early as
2028 * possible and not wait until a MemoryError is actually raised!
2029 */
2030PyObject *PyExc_MemoryErrorInst=NULL;
2031
Brett Cannon1e534b52007-09-07 04:18:30 +00002032/* Pre-computed RuntimeError instance for when recursion depth is reached.
2033 Meant to be used when normalizing the exception for exceeding the recursion
2034 depth will cause its own infinite recursion.
2035*/
2036PyObject *PyExc_RecursionErrorInst = NULL;
2037
Richard Jones7b9558d2006-05-27 12:29:24 +00002038/* module global functions */
2039static PyMethodDef functions[] = {
2040 /* Sentinel */
2041 {NULL, NULL}
2042};
2043
2044#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
2045 Py_FatalError("exceptions bootstrapping error.");
2046
2047#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
2048 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
2049 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
2050 Py_FatalError("Module dictionary insertion problem.");
2051
KristjĂ¡n Valur JĂ³nssonf6083172006-06-12 15:45:12 +00002052
Richard Jones7b9558d2006-05-27 12:29:24 +00002053PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00002054_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00002055{
2056 PyObject *m, *bltinmod, *bdict;
2057
2058 PRE_INIT(BaseException)
2059 PRE_INIT(Exception)
2060 PRE_INIT(StandardError)
2061 PRE_INIT(TypeError)
2062 PRE_INIT(StopIteration)
2063 PRE_INIT(GeneratorExit)
2064 PRE_INIT(SystemExit)
2065 PRE_INIT(KeyboardInterrupt)
2066 PRE_INIT(ImportError)
2067 PRE_INIT(EnvironmentError)
2068 PRE_INIT(IOError)
2069 PRE_INIT(OSError)
2070#ifdef MS_WINDOWS
2071 PRE_INIT(WindowsError)
2072#endif
2073#ifdef __VMS
2074 PRE_INIT(VMSError)
2075#endif
2076 PRE_INIT(EOFError)
2077 PRE_INIT(RuntimeError)
2078 PRE_INIT(NotImplementedError)
2079 PRE_INIT(NameError)
2080 PRE_INIT(UnboundLocalError)
2081 PRE_INIT(AttributeError)
2082 PRE_INIT(SyntaxError)
2083 PRE_INIT(IndentationError)
2084 PRE_INIT(TabError)
2085 PRE_INIT(LookupError)
2086 PRE_INIT(IndexError)
2087 PRE_INIT(KeyError)
2088 PRE_INIT(ValueError)
2089 PRE_INIT(UnicodeError)
2090#ifdef Py_USING_UNICODE
2091 PRE_INIT(UnicodeEncodeError)
2092 PRE_INIT(UnicodeDecodeError)
2093 PRE_INIT(UnicodeTranslateError)
2094#endif
2095 PRE_INIT(AssertionError)
2096 PRE_INIT(ArithmeticError)
2097 PRE_INIT(FloatingPointError)
2098 PRE_INIT(OverflowError)
2099 PRE_INIT(ZeroDivisionError)
2100 PRE_INIT(SystemError)
2101 PRE_INIT(ReferenceError)
2102 PRE_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002103 PRE_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002104 PRE_INIT(Warning)
2105 PRE_INIT(UserWarning)
2106 PRE_INIT(DeprecationWarning)
2107 PRE_INIT(PendingDeprecationWarning)
2108 PRE_INIT(SyntaxWarning)
2109 PRE_INIT(RuntimeWarning)
2110 PRE_INIT(FutureWarning)
2111 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002112 PRE_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002113 PRE_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002114
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002115 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2116 (PyObject *)NULL, PYTHON_API_VERSION);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -05002117 if (m == NULL)
2118 return;
Richard Jones7b9558d2006-05-27 12:29:24 +00002119
2120 bltinmod = PyImport_ImportModule("__builtin__");
2121 if (bltinmod == NULL)
2122 Py_FatalError("exceptions bootstrapping error.");
2123 bdict = PyModule_GetDict(bltinmod);
2124 if (bdict == NULL)
2125 Py_FatalError("exceptions bootstrapping error.");
2126
2127 POST_INIT(BaseException)
2128 POST_INIT(Exception)
2129 POST_INIT(StandardError)
2130 POST_INIT(TypeError)
2131 POST_INIT(StopIteration)
2132 POST_INIT(GeneratorExit)
2133 POST_INIT(SystemExit)
2134 POST_INIT(KeyboardInterrupt)
2135 POST_INIT(ImportError)
2136 POST_INIT(EnvironmentError)
2137 POST_INIT(IOError)
2138 POST_INIT(OSError)
2139#ifdef MS_WINDOWS
2140 POST_INIT(WindowsError)
2141#endif
2142#ifdef __VMS
2143 POST_INIT(VMSError)
2144#endif
2145 POST_INIT(EOFError)
2146 POST_INIT(RuntimeError)
2147 POST_INIT(NotImplementedError)
2148 POST_INIT(NameError)
2149 POST_INIT(UnboundLocalError)
2150 POST_INIT(AttributeError)
2151 POST_INIT(SyntaxError)
2152 POST_INIT(IndentationError)
2153 POST_INIT(TabError)
2154 POST_INIT(LookupError)
2155 POST_INIT(IndexError)
2156 POST_INIT(KeyError)
2157 POST_INIT(ValueError)
2158 POST_INIT(UnicodeError)
2159#ifdef Py_USING_UNICODE
2160 POST_INIT(UnicodeEncodeError)
2161 POST_INIT(UnicodeDecodeError)
2162 POST_INIT(UnicodeTranslateError)
2163#endif
2164 POST_INIT(AssertionError)
2165 POST_INIT(ArithmeticError)
2166 POST_INIT(FloatingPointError)
2167 POST_INIT(OverflowError)
2168 POST_INIT(ZeroDivisionError)
2169 POST_INIT(SystemError)
2170 POST_INIT(ReferenceError)
2171 POST_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002172 POST_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002173 POST_INIT(Warning)
2174 POST_INIT(UserWarning)
2175 POST_INIT(DeprecationWarning)
2176 POST_INIT(PendingDeprecationWarning)
2177 POST_INIT(SyntaxWarning)
2178 POST_INIT(RuntimeWarning)
2179 POST_INIT(FutureWarning)
2180 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002181 POST_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002182 POST_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002183
2184 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2185 if (!PyExc_MemoryErrorInst)
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +00002186 Py_FatalError("Cannot pre-allocate MemoryError instance");
Richard Jones7b9558d2006-05-27 12:29:24 +00002187
Brett Cannon1e534b52007-09-07 04:18:30 +00002188 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2189 if (!PyExc_RecursionErrorInst)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002190 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2191 "recursion errors");
Brett Cannon1e534b52007-09-07 04:18:30 +00002192 else {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002193 PyBaseExceptionObject *err_inst =
2194 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2195 PyObject *args_tuple;
2196 PyObject *exc_message;
2197 exc_message = PyString_FromString("maximum recursion depth exceeded");
2198 if (!exc_message)
2199 Py_FatalError("cannot allocate argument for RuntimeError "
2200 "pre-allocation");
2201 args_tuple = PyTuple_Pack(1, exc_message);
2202 if (!args_tuple)
2203 Py_FatalError("cannot allocate tuple for RuntimeError "
2204 "pre-allocation");
2205 Py_DECREF(exc_message);
2206 if (BaseException_init(err_inst, args_tuple, NULL))
2207 Py_FatalError("init of pre-allocated RuntimeError failed");
2208 Py_DECREF(args_tuple);
Brett Cannon1e534b52007-09-07 04:18:30 +00002209 }
Benjamin Peterson0f7e2df2012-02-10 08:46:54 -05002210 Py_DECREF(bltinmod);
Richard Jones7b9558d2006-05-27 12:29:24 +00002211}
2212
2213void
2214_PyExc_Fini(void)
2215{
Alexandre Vassalotti55bd1ef2009-06-12 18:56:57 +00002216 Py_CLEAR(PyExc_MemoryErrorInst);
2217 Py_CLEAR(PyExc_RecursionErrorInst);
Richard Jones7b9558d2006-05-27 12:29:24 +00002218}