blob: 224d1ba08afb5ca9b5ce6c5f03c6c16ba31cd72a [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 = {
Benjamin Petersona72d15c2017-09-13 21:20:29 -0700371 PyVarObject_HEAD_INIT(NULL, 0)
Richard Jones7b9558d2006-05-27 12:29:24 +0000372 EXC_MODULE_NAME "BaseException", /*tp_name*/
373 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
374 0, /*tp_itemsize*/
375 (destructor)BaseException_dealloc, /*tp_dealloc*/
376 0, /*tp_print*/
377 0, /*tp_getattr*/
378 0, /*tp_setattr*/
379 0, /* tp_compare; */
380 (reprfunc)BaseException_repr, /*tp_repr*/
381 0, /*tp_as_number*/
382 &BaseException_as_sequence, /*tp_as_sequence*/
383 0, /*tp_as_mapping*/
384 0, /*tp_hash */
385 0, /*tp_call*/
386 (reprfunc)BaseException_str, /*tp_str*/
387 PyObject_GenericGetAttr, /*tp_getattro*/
388 PyObject_GenericSetAttr, /*tp_setattro*/
389 0, /*tp_as_buffer*/
Neal Norwitzee3a1b52007-02-25 19:44:48 +0000390 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000391 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Richard Jones7b9558d2006-05-27 12:29:24 +0000392 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
393 (traverseproc)BaseException_traverse, /* tp_traverse */
394 (inquiry)BaseException_clear, /* tp_clear */
395 0, /* tp_richcompare */
396 0, /* tp_weaklistoffset */
397 0, /* tp_iter */
398 0, /* tp_iternext */
399 BaseException_methods, /* tp_methods */
Brett Cannon229cee22007-05-05 01:34:02 +0000400 0, /* tp_members */
Richard Jones7b9558d2006-05-27 12:29:24 +0000401 BaseException_getset, /* tp_getset */
402 0, /* tp_base */
403 0, /* tp_dict */
404 0, /* tp_descr_get */
405 0, /* tp_descr_set */
406 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
407 (initproc)BaseException_init, /* tp_init */
408 0, /* tp_alloc */
409 BaseException_new, /* tp_new */
410};
411/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
412from the previous implmentation and also allowing Python objects to be used
413in the API */
414PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
415
Richard Jones2d555b32006-05-27 16:15:11 +0000416/* note these macros omit the last semicolon so the macro invocation may
417 * include it and not look strange.
418 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000419#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
420static PyTypeObject _PyExc_ ## EXCNAME = { \
Benjamin Petersona72d15c2017-09-13 21:20:29 -0700421 PyVarObject_HEAD_INIT(NULL, 0) \
Richard Jones7b9558d2006-05-27 12:29:24 +0000422 EXC_MODULE_NAME # EXCNAME, \
423 sizeof(PyBaseExceptionObject), \
424 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
425 0, 0, 0, 0, 0, 0, 0, \
426 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
427 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
428 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
429 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
430 (initproc)BaseException_init, 0, BaseException_new,\
431}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000432PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000433
434#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
435static PyTypeObject _PyExc_ ## EXCNAME = { \
Benjamin Petersona72d15c2017-09-13 21:20:29 -0700436 PyVarObject_HEAD_INIT(NULL, 0) \
Richard Jones7b9558d2006-05-27 12:29:24 +0000437 EXC_MODULE_NAME # EXCNAME, \
438 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000439 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000440 0, 0, 0, 0, 0, \
441 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000442 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
443 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000444 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000445 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000446}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000447PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000448
449#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
450static PyTypeObject _PyExc_ ## EXCNAME = { \
Benjamin Petersona72d15c2017-09-13 21:20:29 -0700451 PyVarObject_HEAD_INIT(NULL, 0) \
Richard Jones7b9558d2006-05-27 12:29:24 +0000452 EXC_MODULE_NAME # EXCNAME, \
453 sizeof(Py ## EXCSTORE ## Object), 0, \
454 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
455 (reprfunc)EXCSTR, 0, 0, 0, \
456 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
457 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
458 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
459 EXCMEMBERS, 0, &_ ## EXCBASE, \
460 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000461 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000462}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000463PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000464
465
466/*
467 * Exception extends BaseException
468 */
469SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000470 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000471
472
473/*
474 * StandardError extends Exception
475 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000476SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000477 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000478 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000479
480
481/*
482 * TypeError extends StandardError
483 */
484SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000485 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000486
487
488/*
489 * StopIteration extends Exception
490 */
491SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000492 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000493
494
495/*
Christian Heimes44eeaec2007-12-03 20:01:02 +0000496 * GeneratorExit extends BaseException
Richard Jones7b9558d2006-05-27 12:29:24 +0000497 */
Christian Heimes44eeaec2007-12-03 20:01:02 +0000498SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000499 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000500
501
502/*
503 * SystemExit extends BaseException
504 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000505
506static int
507SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
508{
509 Py_ssize_t size = PyTuple_GET_SIZE(args);
510
511 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
512 return -1;
513
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000514 if (size == 0)
515 return 0;
Serhiy Storchaka2e6c8292015-12-27 15:41:58 +0200516 if (size == 1) {
517 Py_INCREF(PyTuple_GET_ITEM(args, 0));
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300518 Py_XSETREF(self->code, PyTuple_GET_ITEM(args, 0));
Serhiy Storchaka2e6c8292015-12-27 15:41:58 +0200519 }
520 else { /* size > 1 */
521 Py_INCREF(args);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300522 Py_XSETREF(self->code, args);
Serhiy Storchaka2e6c8292015-12-27 15:41:58 +0200523 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000524 return 0;
525}
526
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000527static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000528SystemExit_clear(PySystemExitObject *self)
529{
530 Py_CLEAR(self->code);
531 return BaseException_clear((PyBaseExceptionObject *)self);
532}
533
534static void
535SystemExit_dealloc(PySystemExitObject *self)
536{
Georg Brandl38f62372006-09-06 06:50:05 +0000537 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000538 SystemExit_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000539 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000540}
541
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000542static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000543SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
544{
545 Py_VISIT(self->code);
546 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
547}
548
549static PyMemberDef SystemExit_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000550 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
551 PyDoc_STR("exception code")},
552 {NULL} /* Sentinel */
553};
554
555ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
556 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000557 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000558
559/*
560 * KeyboardInterrupt extends BaseException
561 */
562SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000563 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000564
565
566/*
567 * ImportError extends StandardError
568 */
569SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000570 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000571
572
573/*
574 * EnvironmentError extends StandardError
575 */
576
Richard Jones7b9558d2006-05-27 12:29:24 +0000577/* Where a function has a single filename, such as open() or some
578 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
579 * called, giving a third argument which is the filename. But, so
580 * that old code using in-place unpacking doesn't break, e.g.:
581 *
582 * except IOError, (errno, strerror):
583 *
584 * we hack args so that it only contains two items. This also
585 * means we need our own __str__() which prints out the filename
586 * when it was supplied.
587 */
588static int
589EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
590 PyObject *kwds)
591{
592 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
593 PyObject *subslice = NULL;
594
595 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
596 return -1;
597
Georg Brandl3267d282006-09-30 09:03:42 +0000598 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000599 return 0;
600 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000601
602 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000603 &myerrno, &strerror, &filename)) {
604 return -1;
605 }
Serhiy Storchaka8688aca2015-12-27 12:38:48 +0200606 Py_INCREF(myerrno);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300607 Py_XSETREF(self->myerrno, myerrno);
Richard Jones7b9558d2006-05-27 12:29:24 +0000608
Serhiy Storchaka8688aca2015-12-27 12:38:48 +0200609 Py_INCREF(strerror);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300610 Py_XSETREF(self->strerror, strerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000611
612 /* self->filename will remain Py_None otherwise */
613 if (filename != NULL) {
Serhiy Storchaka8688aca2015-12-27 12:38:48 +0200614 Py_INCREF(filename);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300615 Py_XSETREF(self->filename, filename);
Richard Jones7b9558d2006-05-27 12:29:24 +0000616
617 subslice = PyTuple_GetSlice(args, 0, 2);
618 if (!subslice)
619 return -1;
620
Serhiy Storchaka763a61c2016-04-10 18:05:12 +0300621 Py_SETREF(self->args, subslice);
Richard Jones7b9558d2006-05-27 12:29:24 +0000622 }
623 return 0;
624}
625
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000626static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000627EnvironmentError_clear(PyEnvironmentErrorObject *self)
628{
629 Py_CLEAR(self->myerrno);
630 Py_CLEAR(self->strerror);
631 Py_CLEAR(self->filename);
632 return BaseException_clear((PyBaseExceptionObject *)self);
633}
634
635static void
636EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
637{
Georg Brandl38f62372006-09-06 06:50:05 +0000638 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000639 EnvironmentError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000640 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000641}
642
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000643static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000644EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
645 void *arg)
646{
647 Py_VISIT(self->myerrno);
648 Py_VISIT(self->strerror);
649 Py_VISIT(self->filename);
650 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
651}
652
653static PyObject *
654EnvironmentError_str(PyEnvironmentErrorObject *self)
655{
656 PyObject *rtnval = NULL;
657
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000658 if (self->filename) {
659 PyObject *fmt;
660 PyObject *repr;
661 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000662
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000663 fmt = PyString_FromString("[Errno %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000664 if (!fmt)
665 return NULL;
666
667 repr = PyObject_Repr(self->filename);
668 if (!repr) {
669 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000670 return NULL;
671 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000672 tuple = PyTuple_New(3);
673 if (!tuple) {
674 Py_DECREF(repr);
675 Py_DECREF(fmt);
676 return NULL;
677 }
678
679 if (self->myerrno) {
680 Py_INCREF(self->myerrno);
681 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
682 }
683 else {
684 Py_INCREF(Py_None);
685 PyTuple_SET_ITEM(tuple, 0, Py_None);
686 }
687 if (self->strerror) {
688 Py_INCREF(self->strerror);
689 PyTuple_SET_ITEM(tuple, 1, self->strerror);
690 }
691 else {
692 Py_INCREF(Py_None);
693 PyTuple_SET_ITEM(tuple, 1, Py_None);
694 }
695
Richard Jones7b9558d2006-05-27 12:29:24 +0000696 PyTuple_SET_ITEM(tuple, 2, repr);
697
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000698 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000699
700 Py_DECREF(fmt);
701 Py_DECREF(tuple);
702 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000703 else if (self->myerrno && self->strerror) {
704 PyObject *fmt;
705 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000706
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000707 fmt = PyString_FromString("[Errno %s] %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000708 if (!fmt)
709 return NULL;
710
711 tuple = PyTuple_New(2);
712 if (!tuple) {
713 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000714 return NULL;
715 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000716
717 if (self->myerrno) {
718 Py_INCREF(self->myerrno);
719 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
720 }
721 else {
722 Py_INCREF(Py_None);
723 PyTuple_SET_ITEM(tuple, 0, Py_None);
724 }
725 if (self->strerror) {
726 Py_INCREF(self->strerror);
727 PyTuple_SET_ITEM(tuple, 1, self->strerror);
728 }
729 else {
730 Py_INCREF(Py_None);
731 PyTuple_SET_ITEM(tuple, 1, Py_None);
732 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000733
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000734 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000735
736 Py_DECREF(fmt);
737 Py_DECREF(tuple);
738 }
739 else
740 rtnval = BaseException_str((PyBaseExceptionObject *)self);
741
742 return rtnval;
743}
744
745static PyMemberDef EnvironmentError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000746 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000747 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000748 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000749 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000750 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000751 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000752 {NULL} /* Sentinel */
753};
754
755
756static PyObject *
757EnvironmentError_reduce(PyEnvironmentErrorObject *self)
758{
759 PyObject *args = self->args;
760 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000761
Richard Jones7b9558d2006-05-27 12:29:24 +0000762 /* self->args is only the first two real arguments if there was a
763 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000764 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000765 args = PyTuple_New(3);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -0500766 if (!args)
767 return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000768
769 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000770 Py_INCREF(tmp);
771 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000772
773 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000774 Py_INCREF(tmp);
775 PyTuple_SET_ITEM(args, 1, tmp);
776
777 Py_INCREF(self->filename);
778 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000779 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000780 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000781
782 if (self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000783 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000784 else
Christian Heimese93237d2007-12-19 02:37:44 +0000785 res = PyTuple_Pack(2, Py_TYPE(self), args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000786 Py_DECREF(args);
787 return res;
788}
789
790
791static PyMethodDef EnvironmentError_methods[] = {
792 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
793 {NULL}
794};
795
796ComplexExtendsException(PyExc_StandardError, EnvironmentError,
797 EnvironmentError, EnvironmentError_dealloc,
798 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000799 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000800 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000801
802
803/*
804 * IOError extends EnvironmentError
805 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000806MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000807 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000808
809
810/*
811 * OSError extends EnvironmentError
812 */
813MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000814 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000815
816
817/*
818 * WindowsError extends OSError
819 */
820#ifdef MS_WINDOWS
821#include "errmap.h"
822
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000823static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000824WindowsError_clear(PyWindowsErrorObject *self)
825{
826 Py_CLEAR(self->myerrno);
827 Py_CLEAR(self->strerror);
828 Py_CLEAR(self->filename);
829 Py_CLEAR(self->winerror);
830 return BaseException_clear((PyBaseExceptionObject *)self);
831}
832
833static void
834WindowsError_dealloc(PyWindowsErrorObject *self)
835{
Georg Brandl38f62372006-09-06 06:50:05 +0000836 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000837 WindowsError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000838 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000839}
840
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000841static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000842WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
843{
844 Py_VISIT(self->myerrno);
845 Py_VISIT(self->strerror);
846 Py_VISIT(self->filename);
847 Py_VISIT(self->winerror);
848 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
849}
850
Richard Jones7b9558d2006-05-27 12:29:24 +0000851static int
852WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
853{
854 PyObject *o_errcode = NULL;
855 long errcode;
856 long posix_errno;
857
858 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
859 == -1)
860 return -1;
861
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000862 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000863 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000864
865 /* Set errno to the POSIX errno, and winerror to the Win32
866 error code. */
867 errcode = PyInt_AsLong(self->myerrno);
868 if (errcode == -1 && PyErr_Occurred())
869 return -1;
870 posix_errno = winerror_to_errno(errcode);
871
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300872 Py_XSETREF(self->winerror, self->myerrno);
Richard Jones7b9558d2006-05-27 12:29:24 +0000873
874 o_errcode = PyInt_FromLong(posix_errno);
875 if (!o_errcode)
876 return -1;
877
878 self->myerrno = o_errcode;
879
880 return 0;
881}
882
883
884static PyObject *
885WindowsError_str(PyWindowsErrorObject *self)
886{
Richard Jones7b9558d2006-05-27 12:29:24 +0000887 PyObject *rtnval = NULL;
888
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000889 if (self->filename) {
890 PyObject *fmt;
891 PyObject *repr;
892 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000893
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000894 fmt = PyString_FromString("[Error %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000895 if (!fmt)
896 return NULL;
897
898 repr = PyObject_Repr(self->filename);
899 if (!repr) {
900 Py_DECREF(fmt);
901 return NULL;
902 }
903 tuple = PyTuple_New(3);
904 if (!tuple) {
905 Py_DECREF(repr);
906 Py_DECREF(fmt);
907 return NULL;
908 }
909
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000910 if (self->winerror) {
911 Py_INCREF(self->winerror);
912 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000913 }
914 else {
915 Py_INCREF(Py_None);
916 PyTuple_SET_ITEM(tuple, 0, Py_None);
917 }
918 if (self->strerror) {
919 Py_INCREF(self->strerror);
920 PyTuple_SET_ITEM(tuple, 1, self->strerror);
921 }
922 else {
923 Py_INCREF(Py_None);
924 PyTuple_SET_ITEM(tuple, 1, Py_None);
925 }
926
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000927 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000928
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000929 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000930
931 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000932 Py_DECREF(tuple);
933 }
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000934 else if (self->winerror && self->strerror) {
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000935 PyObject *fmt;
936 PyObject *tuple;
937
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000938 fmt = PyString_FromString("[Error %s] %s");
Richard Jones7b9558d2006-05-27 12:29:24 +0000939 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000940 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000941
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000942 tuple = PyTuple_New(2);
943 if (!tuple) {
944 Py_DECREF(fmt);
945 return NULL;
946 }
947
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000948 if (self->winerror) {
949 Py_INCREF(self->winerror);
950 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000951 }
952 else {
953 Py_INCREF(Py_None);
954 PyTuple_SET_ITEM(tuple, 0, Py_None);
955 }
956 if (self->strerror) {
957 Py_INCREF(self->strerror);
958 PyTuple_SET_ITEM(tuple, 1, self->strerror);
959 }
960 else {
961 Py_INCREF(Py_None);
962 PyTuple_SET_ITEM(tuple, 1, Py_None);
963 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000964
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000965 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000966
967 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000968 Py_DECREF(tuple);
969 }
970 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000971 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000972
Richard Jones7b9558d2006-05-27 12:29:24 +0000973 return rtnval;
974}
975
976static PyMemberDef WindowsError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000977 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000978 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000979 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000980 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000981 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000982 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000983 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000984 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000985 {NULL} /* Sentinel */
986};
987
Richard Jones2d555b32006-05-27 16:15:11 +0000988ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
989 WindowsError_dealloc, 0, WindowsError_members,
990 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000991
992#endif /* MS_WINDOWS */
993
994
995/*
996 * VMSError extends OSError (I think)
997 */
998#ifdef __VMS
999MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +00001000 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001001#endif
1002
1003
1004/*
1005 * EOFError extends StandardError
1006 */
1007SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +00001008 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001009
1010
1011/*
1012 * RuntimeError extends StandardError
1013 */
1014SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001015 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001016
1017
1018/*
1019 * NotImplementedError extends RuntimeError
1020 */
1021SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +00001022 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001023
1024/*
1025 * NameError extends StandardError
1026 */
1027SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +00001028 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001029
1030/*
1031 * UnboundLocalError extends NameError
1032 */
1033SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +00001034 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001035
1036/*
1037 * AttributeError extends StandardError
1038 */
1039SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001040 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001041
1042
1043/*
1044 * SyntaxError extends StandardError
1045 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001046
1047static int
1048SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
1049{
1050 PyObject *info = NULL;
1051 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
1052
1053 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1054 return -1;
1055
1056 if (lenargs >= 1) {
Serhiy Storchaka8688aca2015-12-27 12:38:48 +02001057 Py_INCREF(PyTuple_GET_ITEM(args, 0));
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001058 Py_XSETREF(self->msg, PyTuple_GET_ITEM(args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001059 }
1060 if (lenargs == 2) {
1061 info = PyTuple_GET_ITEM(args, 1);
1062 info = PySequence_Tuple(info);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -05001063 if (!info)
1064 return -1;
Richard Jones7b9558d2006-05-27 12:29:24 +00001065
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001066 if (PyTuple_GET_SIZE(info) != 4) {
1067 /* not a very good error message, but it's what Python 2.4 gives */
1068 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
1069 Py_DECREF(info);
1070 return -1;
1071 }
1072
Serhiy Storchaka8688aca2015-12-27 12:38:48 +02001073 Py_INCREF(PyTuple_GET_ITEM(info, 0));
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001074 Py_XSETREF(self->filename, PyTuple_GET_ITEM(info, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001075
Serhiy Storchaka8688aca2015-12-27 12:38:48 +02001076 Py_INCREF(PyTuple_GET_ITEM(info, 1));
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001077 Py_XSETREF(self->lineno, PyTuple_GET_ITEM(info, 1));
Richard Jones7b9558d2006-05-27 12:29:24 +00001078
Serhiy Storchaka8688aca2015-12-27 12:38:48 +02001079 Py_INCREF(PyTuple_GET_ITEM(info, 2));
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001080 Py_XSETREF(self->offset, PyTuple_GET_ITEM(info, 2));
Richard Jones7b9558d2006-05-27 12:29:24 +00001081
Serhiy Storchaka8688aca2015-12-27 12:38:48 +02001082 Py_INCREF(PyTuple_GET_ITEM(info, 3));
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001083 Py_XSETREF(self->text, PyTuple_GET_ITEM(info, 3));
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001084
1085 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001086 }
1087 return 0;
1088}
1089
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001090static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001091SyntaxError_clear(PySyntaxErrorObject *self)
1092{
1093 Py_CLEAR(self->msg);
1094 Py_CLEAR(self->filename);
1095 Py_CLEAR(self->lineno);
1096 Py_CLEAR(self->offset);
1097 Py_CLEAR(self->text);
1098 Py_CLEAR(self->print_file_and_line);
1099 return BaseException_clear((PyBaseExceptionObject *)self);
1100}
1101
1102static void
1103SyntaxError_dealloc(PySyntaxErrorObject *self)
1104{
Georg Brandl38f62372006-09-06 06:50:05 +00001105 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001106 SyntaxError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001107 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001108}
1109
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001110static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001111SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1112{
1113 Py_VISIT(self->msg);
1114 Py_VISIT(self->filename);
1115 Py_VISIT(self->lineno);
1116 Py_VISIT(self->offset);
1117 Py_VISIT(self->text);
1118 Py_VISIT(self->print_file_and_line);
1119 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1120}
1121
1122/* This is called "my_basename" instead of just "basename" to avoid name
1123 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1124 defined, and Python does define that. */
1125static char *
1126my_basename(char *name)
1127{
1128 char *cp = name;
1129 char *result = name;
1130
1131 if (name == NULL)
1132 return "???";
1133 while (*cp != '\0') {
1134 if (*cp == SEP)
1135 result = cp + 1;
1136 ++cp;
1137 }
1138 return result;
1139}
1140
1141
1142static PyObject *
1143SyntaxError_str(PySyntaxErrorObject *self)
1144{
1145 PyObject *str;
1146 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001147 int have_filename = 0;
1148 int have_lineno = 0;
1149 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001150 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001151
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001152 if (self->msg)
1153 str = PyObject_Str(self->msg);
1154 else
1155 str = PyObject_Str(Py_None);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -05001156 if (!str)
1157 return NULL;
Georg Brandl43ab1002006-05-28 20:57:09 +00001158 /* Don't fiddle with non-string return (shouldn't happen anyway) */
Benjamin Peterson4c79aec2012-02-03 19:22:31 -05001159 if (!PyString_Check(str))
1160 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001161
1162 /* XXX -- do all the additional formatting with filename and
1163 lineno here */
1164
Georg Brandl43ab1002006-05-28 20:57:09 +00001165 have_filename = (self->filename != NULL) &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001166 PyString_Check(self->filename);
Georg Brandl43ab1002006-05-28 20:57:09 +00001167 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001168
Georg Brandl43ab1002006-05-28 20:57:09 +00001169 if (!have_filename && !have_lineno)
1170 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001171
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001172 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001173 if (have_filename)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001174 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001175
Georg Brandl43ab1002006-05-28 20:57:09 +00001176 buffer = PyMem_MALLOC(bufsize);
1177 if (buffer == NULL)
1178 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001179
Georg Brandl43ab1002006-05-28 20:57:09 +00001180 if (have_filename && have_lineno)
1181 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001182 PyString_AS_STRING(str),
1183 my_basename(PyString_AS_STRING(self->filename)),
Georg Brandl43ab1002006-05-28 20:57:09 +00001184 PyInt_AsLong(self->lineno));
1185 else if (have_filename)
1186 PyOS_snprintf(buffer, bufsize, "%s (%s)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001187 PyString_AS_STRING(str),
1188 my_basename(PyString_AS_STRING(self->filename)));
Georg Brandl43ab1002006-05-28 20:57:09 +00001189 else /* only have_lineno */
1190 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001191 PyString_AS_STRING(str),
Georg Brandl43ab1002006-05-28 20:57:09 +00001192 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001193
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001194 result = PyString_FromString(buffer);
Georg Brandl43ab1002006-05-28 20:57:09 +00001195 PyMem_FREE(buffer);
1196
1197 if (result == NULL)
1198 result = str;
1199 else
1200 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001201 return result;
1202}
1203
1204static PyMemberDef SyntaxError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001205 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1206 PyDoc_STR("exception msg")},
1207 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1208 PyDoc_STR("exception filename")},
1209 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1210 PyDoc_STR("exception lineno")},
1211 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1212 PyDoc_STR("exception offset")},
1213 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1214 PyDoc_STR("exception text")},
1215 {"print_file_and_line", T_OBJECT,
1216 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1217 PyDoc_STR("exception print_file_and_line")},
1218 {NULL} /* Sentinel */
1219};
1220
1221ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1222 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001223 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001224
1225
1226/*
1227 * IndentationError extends SyntaxError
1228 */
1229MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001230 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001231
1232
1233/*
1234 * TabError extends IndentationError
1235 */
1236MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001237 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001238
1239
1240/*
1241 * LookupError extends StandardError
1242 */
1243SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001244 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001245
1246
1247/*
1248 * IndexError extends LookupError
1249 */
1250SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001251 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001252
1253
1254/*
1255 * KeyError extends LookupError
1256 */
1257static PyObject *
1258KeyError_str(PyBaseExceptionObject *self)
1259{
1260 /* If args is a tuple of exactly one item, apply repr to args[0].
1261 This is done so that e.g. the exception raised by {}[''] prints
1262 KeyError: ''
1263 rather than the confusing
1264 KeyError
1265 alone. The downside is that if KeyError is raised with an explanatory
1266 string, that string will be displayed in quotes. Too bad.
1267 If args is anything else, use the default BaseException__str__().
1268 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001269 if (PyTuple_GET_SIZE(self->args) == 1) {
1270 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001271 }
1272 return BaseException_str(self);
1273}
1274
1275ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001276 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001277
1278
1279/*
1280 * ValueError extends StandardError
1281 */
1282SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001283 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001284
1285/*
1286 * UnicodeError extends ValueError
1287 */
1288
1289SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001290 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001291
1292#ifdef Py_USING_UNICODE
Richard Jones7b9558d2006-05-27 12:29:24 +00001293static PyObject *
1294get_string(PyObject *attr, const char *name)
1295{
1296 if (!attr) {
1297 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1298 return NULL;
1299 }
1300
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001301 if (!PyString_Check(attr)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001302 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1303 return NULL;
1304 }
1305 Py_INCREF(attr);
1306 return attr;
1307}
1308
1309
1310static int
1311set_string(PyObject **attr, const char *value)
1312{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001313 PyObject *obj = PyString_FromString(value);
Richard Jones7b9558d2006-05-27 12:29:24 +00001314 if (!obj)
1315 return -1;
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001316 Py_XSETREF(*attr, obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001317 return 0;
1318}
1319
1320
1321static PyObject *
1322get_unicode(PyObject *attr, const char *name)
1323{
1324 if (!attr) {
1325 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1326 return NULL;
1327 }
1328
1329 if (!PyUnicode_Check(attr)) {
1330 PyErr_Format(PyExc_TypeError,
1331 "%.200s attribute must be unicode", name);
1332 return NULL;
1333 }
1334 Py_INCREF(attr);
1335 return attr;
1336}
1337
1338PyObject *
1339PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1340{
1341 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1342}
1343
1344PyObject *
1345PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1346{
1347 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1348}
1349
1350PyObject *
1351PyUnicodeEncodeError_GetObject(PyObject *exc)
1352{
1353 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1354}
1355
1356PyObject *
1357PyUnicodeDecodeError_GetObject(PyObject *exc)
1358{
1359 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1360}
1361
1362PyObject *
1363PyUnicodeTranslateError_GetObject(PyObject *exc)
1364{
1365 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1366}
1367
1368int
1369PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1370{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001371 Py_ssize_t size;
1372 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1373 "object");
1374 if (!obj)
1375 return -1;
1376 *start = ((PyUnicodeErrorObject *)exc)->start;
1377 size = PyUnicode_GET_SIZE(obj);
1378 if (*start<0)
1379 *start = 0; /*XXX check for values <0*/
1380 if (*start>=size)
1381 *start = size-1;
1382 Py_DECREF(obj);
1383 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001384}
1385
1386
1387int
1388PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1389{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001390 Py_ssize_t size;
1391 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1392 "object");
1393 if (!obj)
1394 return -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001395 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001396 *start = ((PyUnicodeErrorObject *)exc)->start;
1397 if (*start<0)
1398 *start = 0;
1399 if (*start>=size)
1400 *start = size-1;
1401 Py_DECREF(obj);
1402 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001403}
1404
1405
1406int
1407PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1408{
1409 return PyUnicodeEncodeError_GetStart(exc, start);
1410}
1411
1412
1413int
1414PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1415{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001416 ((PyUnicodeErrorObject *)exc)->start = start;
1417 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001418}
1419
1420
1421int
1422PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1423{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001424 ((PyUnicodeErrorObject *)exc)->start = start;
1425 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001426}
1427
1428
1429int
1430PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1431{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001432 ((PyUnicodeErrorObject *)exc)->start = start;
1433 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001434}
1435
1436
1437int
1438PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1439{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001440 Py_ssize_t size;
1441 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1442 "object");
1443 if (!obj)
1444 return -1;
1445 *end = ((PyUnicodeErrorObject *)exc)->end;
1446 size = PyUnicode_GET_SIZE(obj);
1447 if (*end<1)
1448 *end = 1;
1449 if (*end>size)
1450 *end = size;
1451 Py_DECREF(obj);
1452 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001453}
1454
1455
1456int
1457PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1458{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001459 Py_ssize_t size;
1460 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1461 "object");
1462 if (!obj)
1463 return -1;
1464 *end = ((PyUnicodeErrorObject *)exc)->end;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001465 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001466 if (*end<1)
1467 *end = 1;
1468 if (*end>size)
1469 *end = size;
1470 Py_DECREF(obj);
1471 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001472}
1473
1474
1475int
1476PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1477{
1478 return PyUnicodeEncodeError_GetEnd(exc, start);
1479}
1480
1481
1482int
1483PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1484{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001485 ((PyUnicodeErrorObject *)exc)->end = end;
1486 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001487}
1488
1489
1490int
1491PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1492{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001493 ((PyUnicodeErrorObject *)exc)->end = end;
1494 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001495}
1496
1497
1498int
1499PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1500{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001501 ((PyUnicodeErrorObject *)exc)->end = end;
1502 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001503}
1504
1505PyObject *
1506PyUnicodeEncodeError_GetReason(PyObject *exc)
1507{
1508 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1509}
1510
1511
1512PyObject *
1513PyUnicodeDecodeError_GetReason(PyObject *exc)
1514{
1515 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1516}
1517
1518
1519PyObject *
1520PyUnicodeTranslateError_GetReason(PyObject *exc)
1521{
1522 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1523}
1524
1525
1526int
1527PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1528{
1529 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1530}
1531
1532
1533int
1534PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1535{
1536 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1537}
1538
1539
1540int
1541PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1542{
1543 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1544}
1545
1546
Richard Jones7b9558d2006-05-27 12:29:24 +00001547static int
1548UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1549 PyTypeObject *objecttype)
1550{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001551 Py_CLEAR(self->encoding);
1552 Py_CLEAR(self->object);
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001553 Py_CLEAR(self->reason);
1554
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001555 if (!PyArg_ParseTuple(args, "O!O!nnO!",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001556 &PyString_Type, &self->encoding,
Richard Jones7b9558d2006-05-27 12:29:24 +00001557 objecttype, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001558 &self->start,
1559 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001560 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001561 self->encoding = self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001562 return -1;
1563 }
1564
1565 Py_INCREF(self->encoding);
1566 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001567 Py_INCREF(self->reason);
1568
1569 return 0;
1570}
1571
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001572static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001573UnicodeError_clear(PyUnicodeErrorObject *self)
1574{
1575 Py_CLEAR(self->encoding);
1576 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001577 Py_CLEAR(self->reason);
1578 return BaseException_clear((PyBaseExceptionObject *)self);
1579}
1580
1581static void
1582UnicodeError_dealloc(PyUnicodeErrorObject *self)
1583{
Georg Brandl38f62372006-09-06 06:50:05 +00001584 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001585 UnicodeError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001586 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001587}
1588
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001589static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001590UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1591{
1592 Py_VISIT(self->encoding);
1593 Py_VISIT(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001594 Py_VISIT(self->reason);
1595 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1596}
1597
1598static PyMemberDef UnicodeError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001599 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1600 PyDoc_STR("exception encoding")},
1601 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1602 PyDoc_STR("exception object")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001603 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001604 PyDoc_STR("exception start")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001605 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001606 PyDoc_STR("exception end")},
1607 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1608 PyDoc_STR("exception reason")},
1609 {NULL} /* Sentinel */
1610};
1611
1612
1613/*
1614 * UnicodeEncodeError extends UnicodeError
1615 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001616
1617static int
1618UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1619{
1620 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1621 return -1;
1622 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1623 kwds, &PyUnicode_Type);
1624}
1625
1626static PyObject *
1627UnicodeEncodeError_str(PyObject *self)
1628{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001629 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith2d9856d2010-02-24 14:15:36 +00001630 PyObject *result = NULL;
1631 PyObject *reason_str = NULL;
1632 PyObject *encoding_str = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001633
Benjamin Petersonc4e6e0a2014-04-02 12:15:06 -04001634 if (!uself->object)
1635 /* Not properly initialized. */
1636 return PyUnicode_FromString("");
1637
Eric Smith2d9856d2010-02-24 14:15:36 +00001638 /* Get reason and encoding as strings, which they might not be if
Martin Pantera850ef62016-07-28 01:11:04 +00001639 they've been modified after we were constructed. */
Eric Smith2d9856d2010-02-24 14:15:36 +00001640 reason_str = PyObject_Str(uself->reason);
1641 if (reason_str == NULL)
1642 goto done;
1643 encoding_str = PyObject_Str(uself->encoding);
1644 if (encoding_str == NULL)
1645 goto done;
1646
1647 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001648 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001649 char badchar_str[20];
1650 if (badchar <= 0xff)
1651 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1652 else if (badchar <= 0xffff)
1653 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1654 else
1655 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Eric Smith2d9856d2010-02-24 14:15:36 +00001656 result = PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001657 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Eric Smith2d9856d2010-02-24 14:15:36 +00001658 PyString_AS_STRING(encoding_str),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001659 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001660 uself->start,
Eric Smith2d9856d2010-02-24 14:15:36 +00001661 PyString_AS_STRING(reason_str));
Richard Jones7b9558d2006-05-27 12:29:24 +00001662 }
Eric Smith2d9856d2010-02-24 14:15:36 +00001663 else {
1664 result = PyString_FromFormat(
1665 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
1666 PyString_AS_STRING(encoding_str),
1667 uself->start,
1668 uself->end-1,
1669 PyString_AS_STRING(reason_str));
1670 }
1671done:
1672 Py_XDECREF(reason_str);
1673 Py_XDECREF(encoding_str);
1674 return result;
Richard Jones7b9558d2006-05-27 12:29:24 +00001675}
1676
1677static PyTypeObject _PyExc_UnicodeEncodeError = {
Benjamin Petersona72d15c2017-09-13 21:20:29 -07001678 PyVarObject_HEAD_INIT(NULL, 0)
Georg Brandl38f62372006-09-06 06:50:05 +00001679 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001680 sizeof(PyUnicodeErrorObject), 0,
1681 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1682 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1683 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001684 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1685 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001686 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001687 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001688};
1689PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1690
1691PyObject *
1692PyUnicodeEncodeError_Create(
1693 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1694 Py_ssize_t start, Py_ssize_t end, const char *reason)
1695{
1696 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001697 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001698}
1699
1700
1701/*
1702 * UnicodeDecodeError extends UnicodeError
1703 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001704
1705static int
1706UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1707{
1708 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1709 return -1;
1710 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001711 kwds, &PyString_Type);
Richard Jones7b9558d2006-05-27 12:29:24 +00001712}
1713
1714static PyObject *
1715UnicodeDecodeError_str(PyObject *self)
1716{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001717 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith2d9856d2010-02-24 14:15:36 +00001718 PyObject *result = NULL;
1719 PyObject *reason_str = NULL;
1720 PyObject *encoding_str = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001721
Benjamin Petersonc4e6e0a2014-04-02 12:15:06 -04001722 if (!uself->object)
1723 /* Not properly initialized. */
1724 return PyUnicode_FromString("");
1725
Eric Smith2d9856d2010-02-24 14:15:36 +00001726 /* Get reason and encoding as strings, which they might not be if
Martin Pantera850ef62016-07-28 01:11:04 +00001727 they've been modified after we were constructed. */
Eric Smith2d9856d2010-02-24 14:15:36 +00001728 reason_str = PyObject_Str(uself->reason);
1729 if (reason_str == NULL)
1730 goto done;
1731 encoding_str = PyObject_Str(uself->encoding);
1732 if (encoding_str == NULL)
1733 goto done;
1734
1735 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001736 /* FromFormat does not support %02x, so format that separately */
1737 char byte[4];
1738 PyOS_snprintf(byte, sizeof(byte), "%02x",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001739 ((int)PyString_AS_STRING(uself->object)[uself->start])&0xff);
Eric Smith2d9856d2010-02-24 14:15:36 +00001740 result = PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001741 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Eric Smith2d9856d2010-02-24 14:15:36 +00001742 PyString_AS_STRING(encoding_str),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001743 byte,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001744 uself->start,
Eric Smith2d9856d2010-02-24 14:15:36 +00001745 PyString_AS_STRING(reason_str));
Richard Jones7b9558d2006-05-27 12:29:24 +00001746 }
Eric Smith2d9856d2010-02-24 14:15:36 +00001747 else {
1748 result = PyString_FromFormat(
1749 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
1750 PyString_AS_STRING(encoding_str),
1751 uself->start,
1752 uself->end-1,
1753 PyString_AS_STRING(reason_str));
1754 }
1755done:
1756 Py_XDECREF(reason_str);
1757 Py_XDECREF(encoding_str);
1758 return result;
Richard Jones7b9558d2006-05-27 12:29:24 +00001759}
1760
1761static PyTypeObject _PyExc_UnicodeDecodeError = {
Benjamin Petersona72d15c2017-09-13 21:20:29 -07001762 PyVarObject_HEAD_INIT(NULL, 0)
Richard Jones7b9558d2006-05-27 12:29:24 +00001763 EXC_MODULE_NAME "UnicodeDecodeError",
1764 sizeof(PyUnicodeErrorObject), 0,
1765 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1766 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1767 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001768 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1769 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001770 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001771 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001772};
1773PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1774
1775PyObject *
1776PyUnicodeDecodeError_Create(
1777 const char *encoding, const char *object, Py_ssize_t length,
1778 Py_ssize_t start, Py_ssize_t end, const char *reason)
1779{
Richard Jones7b9558d2006-05-27 12:29:24 +00001780 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001781 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001782}
1783
1784
1785/*
1786 * UnicodeTranslateError extends UnicodeError
1787 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001788
1789static int
1790UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1791 PyObject *kwds)
1792{
1793 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1794 return -1;
1795
1796 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001797 Py_CLEAR(self->reason);
1798
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001799 if (!PyArg_ParseTuple(args, "O!nnO!",
Richard Jones7b9558d2006-05-27 12:29:24 +00001800 &PyUnicode_Type, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001801 &self->start,
1802 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001803 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001804 self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001805 return -1;
1806 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001807
Richard Jones7b9558d2006-05-27 12:29:24 +00001808 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001809 Py_INCREF(self->reason);
1810
1811 return 0;
1812}
1813
1814
1815static PyObject *
1816UnicodeTranslateError_str(PyObject *self)
1817{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001818 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith2d9856d2010-02-24 14:15:36 +00001819 PyObject *result = NULL;
1820 PyObject *reason_str = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001821
Benjamin Petersonc4e6e0a2014-04-02 12:15:06 -04001822 if (!uself->object)
1823 /* Not properly initialized. */
1824 return PyUnicode_FromString("");
1825
Eric Smith2d9856d2010-02-24 14:15:36 +00001826 /* Get reason as a string, which it might not be if it's been
Martin Pantera850ef62016-07-28 01:11:04 +00001827 modified after we were constructed. */
Eric Smith2d9856d2010-02-24 14:15:36 +00001828 reason_str = PyObject_Str(uself->reason);
1829 if (reason_str == NULL)
1830 goto done;
1831
1832 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001833 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001834 char badchar_str[20];
1835 if (badchar <= 0xff)
1836 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1837 else if (badchar <= 0xffff)
1838 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1839 else
1840 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Eric Smith2d9856d2010-02-24 14:15:36 +00001841 result = PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001842 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001843 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001844 uself->start,
Eric Smith2d9856d2010-02-24 14:15:36 +00001845 PyString_AS_STRING(reason_str));
1846 } else {
1847 result = PyString_FromFormat(
1848 "can't translate characters in position %zd-%zd: %.400s",
1849 uself->start,
1850 uself->end-1,
1851 PyString_AS_STRING(reason_str));
Richard Jones7b9558d2006-05-27 12:29:24 +00001852 }
Eric Smith2d9856d2010-02-24 14:15:36 +00001853done:
1854 Py_XDECREF(reason_str);
1855 return result;
Richard Jones7b9558d2006-05-27 12:29:24 +00001856}
1857
1858static PyTypeObject _PyExc_UnicodeTranslateError = {
Benjamin Petersona72d15c2017-09-13 21:20:29 -07001859 PyVarObject_HEAD_INIT(NULL, 0)
Richard Jones7b9558d2006-05-27 12:29:24 +00001860 EXC_MODULE_NAME "UnicodeTranslateError",
1861 sizeof(PyUnicodeErrorObject), 0,
1862 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1863 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1864 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001865 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001866 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1867 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001868 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001869};
1870PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1871
1872PyObject *
1873PyUnicodeTranslateError_Create(
1874 const Py_UNICODE *object, Py_ssize_t length,
1875 Py_ssize_t start, Py_ssize_t end, const char *reason)
1876{
1877 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001878 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001879}
1880#endif
1881
1882
1883/*
1884 * AssertionError extends StandardError
1885 */
1886SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001887 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001888
1889
1890/*
1891 * ArithmeticError extends StandardError
1892 */
1893SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001894 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001895
1896
1897/*
1898 * FloatingPointError extends ArithmeticError
1899 */
1900SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001901 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001902
1903
1904/*
1905 * OverflowError extends ArithmeticError
1906 */
1907SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001908 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001909
1910
1911/*
1912 * ZeroDivisionError extends ArithmeticError
1913 */
1914SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001915 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001916
1917
1918/*
1919 * SystemError extends StandardError
1920 */
1921SimpleExtendsException(PyExc_StandardError, SystemError,
1922 "Internal error in the Python interpreter.\n"
1923 "\n"
1924 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001925 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001926
1927
1928/*
1929 * ReferenceError extends StandardError
1930 */
1931SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001932 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001933
1934
1935/*
1936 * MemoryError extends StandardError
1937 */
Richard Jones2d555b32006-05-27 16:15:11 +00001938SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001939
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00001940/*
1941 * BufferError extends StandardError
1942 */
1943SimpleExtendsException(PyExc_StandardError, BufferError, "Buffer error.");
1944
Richard Jones7b9558d2006-05-27 12:29:24 +00001945
1946/* Warning category docstrings */
1947
1948/*
1949 * Warning extends Exception
1950 */
1951SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001952 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001953
1954
1955/*
1956 * UserWarning extends Warning
1957 */
1958SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001959 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001960
1961
1962/*
1963 * DeprecationWarning extends Warning
1964 */
1965SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001966 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001967
1968
1969/*
1970 * PendingDeprecationWarning extends Warning
1971 */
1972SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1973 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001974 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001975
1976
1977/*
1978 * SyntaxWarning extends Warning
1979 */
1980SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001981 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001982
1983
1984/*
1985 * RuntimeWarning extends Warning
1986 */
1987SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001988 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001989
1990
1991/*
1992 * FutureWarning extends Warning
1993 */
1994SimpleExtendsException(PyExc_Warning, FutureWarning,
1995 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001996 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001997
1998
1999/*
2000 * ImportWarning extends Warning
2001 */
2002SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00002003 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00002004
2005
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002006/*
2007 * UnicodeWarning extends Warning
2008 */
2009SimpleExtendsException(PyExc_Warning, UnicodeWarning,
2010 "Base class for warnings about Unicode related problems, mostly\n"
2011 "related to conversion problems.");
2012
Christian Heimes1a6387e2008-03-26 12:49:49 +00002013/*
2014 * BytesWarning extends Warning
2015 */
2016SimpleExtendsException(PyExc_Warning, BytesWarning,
2017 "Base class for warnings about bytes and buffer related problems, mostly\n"
2018 "related to conversion from str or comparing to str.");
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002019
Richard Jones7b9558d2006-05-27 12:29:24 +00002020/* Pre-computed MemoryError instance. Best to create this as early as
2021 * possible and not wait until a MemoryError is actually raised!
2022 */
2023PyObject *PyExc_MemoryErrorInst=NULL;
2024
Brett Cannon1e534b52007-09-07 04:18:30 +00002025/* Pre-computed RuntimeError instance for when recursion depth is reached.
2026 Meant to be used when normalizing the exception for exceeding the recursion
2027 depth will cause its own infinite recursion.
2028*/
2029PyObject *PyExc_RecursionErrorInst = NULL;
2030
Richard Jones7b9558d2006-05-27 12:29:24 +00002031/* module global functions */
2032static PyMethodDef functions[] = {
2033 /* Sentinel */
2034 {NULL, NULL}
2035};
2036
2037#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
2038 Py_FatalError("exceptions bootstrapping error.");
2039
2040#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
2041 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
2042 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
2043 Py_FatalError("Module dictionary insertion problem.");
2044
KristjĂ¡n Valur JĂ³nssonf6083172006-06-12 15:45:12 +00002045
Richard Jones7b9558d2006-05-27 12:29:24 +00002046PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00002047_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00002048{
2049 PyObject *m, *bltinmod, *bdict;
2050
2051 PRE_INIT(BaseException)
2052 PRE_INIT(Exception)
2053 PRE_INIT(StandardError)
2054 PRE_INIT(TypeError)
2055 PRE_INIT(StopIteration)
2056 PRE_INIT(GeneratorExit)
2057 PRE_INIT(SystemExit)
2058 PRE_INIT(KeyboardInterrupt)
2059 PRE_INIT(ImportError)
2060 PRE_INIT(EnvironmentError)
2061 PRE_INIT(IOError)
2062 PRE_INIT(OSError)
2063#ifdef MS_WINDOWS
2064 PRE_INIT(WindowsError)
2065#endif
2066#ifdef __VMS
2067 PRE_INIT(VMSError)
2068#endif
2069 PRE_INIT(EOFError)
2070 PRE_INIT(RuntimeError)
2071 PRE_INIT(NotImplementedError)
2072 PRE_INIT(NameError)
2073 PRE_INIT(UnboundLocalError)
2074 PRE_INIT(AttributeError)
2075 PRE_INIT(SyntaxError)
2076 PRE_INIT(IndentationError)
2077 PRE_INIT(TabError)
2078 PRE_INIT(LookupError)
2079 PRE_INIT(IndexError)
2080 PRE_INIT(KeyError)
2081 PRE_INIT(ValueError)
2082 PRE_INIT(UnicodeError)
2083#ifdef Py_USING_UNICODE
2084 PRE_INIT(UnicodeEncodeError)
2085 PRE_INIT(UnicodeDecodeError)
2086 PRE_INIT(UnicodeTranslateError)
2087#endif
2088 PRE_INIT(AssertionError)
2089 PRE_INIT(ArithmeticError)
2090 PRE_INIT(FloatingPointError)
2091 PRE_INIT(OverflowError)
2092 PRE_INIT(ZeroDivisionError)
2093 PRE_INIT(SystemError)
2094 PRE_INIT(ReferenceError)
2095 PRE_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002096 PRE_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002097 PRE_INIT(Warning)
2098 PRE_INIT(UserWarning)
2099 PRE_INIT(DeprecationWarning)
2100 PRE_INIT(PendingDeprecationWarning)
2101 PRE_INIT(SyntaxWarning)
2102 PRE_INIT(RuntimeWarning)
2103 PRE_INIT(FutureWarning)
2104 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002105 PRE_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002106 PRE_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002107
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002108 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2109 (PyObject *)NULL, PYTHON_API_VERSION);
Benjamin Peterson4c79aec2012-02-03 19:22:31 -05002110 if (m == NULL)
2111 return;
Richard Jones7b9558d2006-05-27 12:29:24 +00002112
2113 bltinmod = PyImport_ImportModule("__builtin__");
2114 if (bltinmod == NULL)
2115 Py_FatalError("exceptions bootstrapping error.");
2116 bdict = PyModule_GetDict(bltinmod);
2117 if (bdict == NULL)
2118 Py_FatalError("exceptions bootstrapping error.");
2119
2120 POST_INIT(BaseException)
2121 POST_INIT(Exception)
2122 POST_INIT(StandardError)
2123 POST_INIT(TypeError)
2124 POST_INIT(StopIteration)
2125 POST_INIT(GeneratorExit)
2126 POST_INIT(SystemExit)
2127 POST_INIT(KeyboardInterrupt)
2128 POST_INIT(ImportError)
2129 POST_INIT(EnvironmentError)
2130 POST_INIT(IOError)
2131 POST_INIT(OSError)
2132#ifdef MS_WINDOWS
2133 POST_INIT(WindowsError)
2134#endif
2135#ifdef __VMS
2136 POST_INIT(VMSError)
2137#endif
2138 POST_INIT(EOFError)
2139 POST_INIT(RuntimeError)
2140 POST_INIT(NotImplementedError)
2141 POST_INIT(NameError)
2142 POST_INIT(UnboundLocalError)
2143 POST_INIT(AttributeError)
2144 POST_INIT(SyntaxError)
2145 POST_INIT(IndentationError)
2146 POST_INIT(TabError)
2147 POST_INIT(LookupError)
2148 POST_INIT(IndexError)
2149 POST_INIT(KeyError)
2150 POST_INIT(ValueError)
2151 POST_INIT(UnicodeError)
2152#ifdef Py_USING_UNICODE
2153 POST_INIT(UnicodeEncodeError)
2154 POST_INIT(UnicodeDecodeError)
2155 POST_INIT(UnicodeTranslateError)
2156#endif
2157 POST_INIT(AssertionError)
2158 POST_INIT(ArithmeticError)
2159 POST_INIT(FloatingPointError)
2160 POST_INIT(OverflowError)
2161 POST_INIT(ZeroDivisionError)
2162 POST_INIT(SystemError)
2163 POST_INIT(ReferenceError)
2164 POST_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002165 POST_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002166 POST_INIT(Warning)
2167 POST_INIT(UserWarning)
2168 POST_INIT(DeprecationWarning)
2169 POST_INIT(PendingDeprecationWarning)
2170 POST_INIT(SyntaxWarning)
2171 POST_INIT(RuntimeWarning)
2172 POST_INIT(FutureWarning)
2173 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002174 POST_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002175 POST_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002176
2177 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2178 if (!PyExc_MemoryErrorInst)
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +00002179 Py_FatalError("Cannot pre-allocate MemoryError instance");
Richard Jones7b9558d2006-05-27 12:29:24 +00002180
Brett Cannon1e534b52007-09-07 04:18:30 +00002181 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2182 if (!PyExc_RecursionErrorInst)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002183 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2184 "recursion errors");
Brett Cannon1e534b52007-09-07 04:18:30 +00002185 else {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002186 PyBaseExceptionObject *err_inst =
2187 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2188 PyObject *args_tuple;
2189 PyObject *exc_message;
2190 exc_message = PyString_FromString("maximum recursion depth exceeded");
2191 if (!exc_message)
2192 Py_FatalError("cannot allocate argument for RuntimeError "
2193 "pre-allocation");
2194 args_tuple = PyTuple_Pack(1, exc_message);
2195 if (!args_tuple)
2196 Py_FatalError("cannot allocate tuple for RuntimeError "
2197 "pre-allocation");
2198 Py_DECREF(exc_message);
2199 if (BaseException_init(err_inst, args_tuple, NULL))
2200 Py_FatalError("init of pre-allocated RuntimeError failed");
2201 Py_DECREF(args_tuple);
Brett Cannon1e534b52007-09-07 04:18:30 +00002202 }
Benjamin Peterson0f7e2df2012-02-10 08:46:54 -05002203 Py_DECREF(bltinmod);
Richard Jones7b9558d2006-05-27 12:29:24 +00002204}
2205
2206void
2207_PyExc_Fini(void)
2208{
Alexandre Vassalotti55bd1ef2009-06-12 18:56:57 +00002209 Py_CLEAR(PyExc_MemoryErrorInst);
2210 Py_CLEAR(PyExc_RecursionErrorInst);
Richard Jones7b9558d2006-05-27 12:29:24 +00002211}