blob: fbdc781162ee4bd29b1f0497ff41105a15fa5fca [file] [log] [blame]
Georg Brandl43ab1002006-05-28 20:57:09 +00001/*
2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
3 *
4 * Thanks go to Tim Peters and Michael Hudson for debugging.
5 */
6
Richard Jones7b9558d2006-05-27 12:29:24 +00007#define PY_SSIZE_T_CLEAN
8#include <Python.h>
9#include "structmember.h"
10#include "osdefs.h"
11
Richard Jones7b9558d2006-05-27 12:29:24 +000012#define EXC_MODULE_NAME "exceptions."
13
Richard Jonesc5b2a2e2006-05-27 16:07:28 +000014/* NOTE: If the exception class hierarchy changes, don't forget to update
15 * Lib/test/exception_hierarchy.txt
16 */
17
18PyDoc_STRVAR(exceptions_doc, "Python's standard exception class hierarchy.\n\
19\n\
20Exceptions found here are defined both in the exceptions module and the\n\
21built-in namespace. It is recommended that user-defined exceptions\n\
22inherit from Exception. See the documentation for the exception\n\
23inheritance hierarchy.\n\
24");
25
Richard Jones7b9558d2006-05-27 12:29:24 +000026/*
27 * BaseException
28 */
29static PyObject *
30BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
31{
32 PyBaseExceptionObject *self;
33
34 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
Georg Brandlc02e1312007-04-11 17:16:24 +000035 if (!self)
36 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +000037 /* the dict is created on the fly in PyObject_GenericSetAttr */
38 self->message = self->dict = NULL;
39
Georg Brandld7e9f602007-08-21 06:03:43 +000040 self->args = PyTuple_New(0);
41 if (!self->args) {
Richard Jones7b9558d2006-05-27 12:29:24 +000042 Py_DECREF(self);
43 return NULL;
44 }
Georg Brandld7e9f602007-08-21 06:03:43 +000045
Gregory P. Smithdd96db62008-06-09 04:58:54 +000046 self->message = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +000047 if (!self->message) {
48 Py_DECREF(self);
49 return NULL;
50 }
Georg Brandld7e9f602007-08-21 06:03:43 +000051
Richard Jones7b9558d2006-05-27 12:29:24 +000052 return (PyObject *)self;
53}
54
55static int
56BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
57{
Christian Heimese93237d2007-12-19 02:37:44 +000058 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
Georg Brandlb0432bc2006-05-30 08:17:00 +000059 return -1;
60
Richard Jones7b9558d2006-05-27 12:29:24 +000061 Py_DECREF(self->args);
62 self->args = args;
63 Py_INCREF(self->args);
64
65 if (PyTuple_GET_SIZE(self->args) == 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +000066 Py_CLEAR(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000067 self->message = PyTuple_GET_ITEM(self->args, 0);
Michael W. Hudson22a80e72006-05-28 15:51:40 +000068 Py_INCREF(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000069 }
70 return 0;
71}
72
Michael W. Hudson96495ee2006-05-28 17:40:29 +000073static int
Richard Jones7b9558d2006-05-27 12:29:24 +000074BaseException_clear(PyBaseExceptionObject *self)
75{
76 Py_CLEAR(self->dict);
77 Py_CLEAR(self->args);
78 Py_CLEAR(self->message);
79 return 0;
80}
81
82static void
83BaseException_dealloc(PyBaseExceptionObject *self)
84{
Georg Brandl38f62372006-09-06 06:50:05 +000085 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +000086 BaseException_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +000087 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +000088}
89
Michael W. Hudson96495ee2006-05-28 17:40:29 +000090static int
Richard Jones7b9558d2006-05-27 12:29:24 +000091BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
92{
Michael W. Hudson22a80e72006-05-28 15:51:40 +000093 Py_VISIT(self->dict);
Richard Jones7b9558d2006-05-27 12:29:24 +000094 Py_VISIT(self->args);
95 Py_VISIT(self->message);
96 return 0;
97}
98
99static PyObject *
100BaseException_str(PyBaseExceptionObject *self)
101{
102 PyObject *out;
103
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000104 switch (PyTuple_GET_SIZE(self->args)) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000105 case 0:
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000106 out = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +0000107 break;
108 case 1:
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000109 out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +0000110 break;
Richard Jones7b9558d2006-05-27 12:29:24 +0000111 default:
112 out = PyObject_Str(self->args);
113 break;
114 }
115
116 return out;
117}
118
Nick Coghlan524b7772008-07-08 14:08:04 +0000119#ifdef Py_USING_UNICODE
120static PyObject *
121BaseException_unicode(PyBaseExceptionObject *self)
122{
123 PyObject *out;
124
125 switch (PyTuple_GET_SIZE(self->args)) {
126 case 0:
127 out = PyUnicode_FromString("");
128 break;
129 case 1:
130 out = PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));
131 break;
132 default:
133 out = PyObject_Unicode(self->args);
134 break;
135 }
136
137 return out;
138}
139#endif
140
Richard Jones7b9558d2006-05-27 12:29:24 +0000141static PyObject *
142BaseException_repr(PyBaseExceptionObject *self)
143{
Richard Jones7b9558d2006-05-27 12:29:24 +0000144 PyObject *repr_suffix;
145 PyObject *repr;
146 char *name;
147 char *dot;
148
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000149 repr_suffix = PyObject_Repr(self->args);
150 if (!repr_suffix)
Richard Jones7b9558d2006-05-27 12:29:24 +0000151 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000152
Christian Heimese93237d2007-12-19 02:37:44 +0000153 name = (char *)Py_TYPE(self)->tp_name;
Richard Jones7b9558d2006-05-27 12:29:24 +0000154 dot = strrchr(name, '.');
155 if (dot != NULL) name = dot+1;
156
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000157 repr = PyString_FromString(name);
Richard Jones7b9558d2006-05-27 12:29:24 +0000158 if (!repr) {
159 Py_DECREF(repr_suffix);
160 return NULL;
161 }
162
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000163 PyString_ConcatAndDel(&repr, repr_suffix);
Richard Jones7b9558d2006-05-27 12:29:24 +0000164 return repr;
165}
166
167/* Pickling support */
168static PyObject *
169BaseException_reduce(PyBaseExceptionObject *self)
170{
Georg Brandlddba4732006-05-30 07:04:55 +0000171 if (self->args && self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000172 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000173 else
Christian Heimese93237d2007-12-19 02:37:44 +0000174 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000175}
176
Georg Brandl85ac8502006-06-01 06:39:19 +0000177/*
178 * Needed for backward compatibility, since exceptions used to store
179 * all their attributes in the __dict__. Code is taken from cPickle's
180 * load_build function.
181 */
182static PyObject *
183BaseException_setstate(PyObject *self, PyObject *state)
184{
185 PyObject *d_key, *d_value;
186 Py_ssize_t i = 0;
187
188 if (state != Py_None) {
189 if (!PyDict_Check(state)) {
190 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
191 return NULL;
192 }
193 while (PyDict_Next(state, &i, &d_key, &d_value)) {
194 if (PyObject_SetAttr(self, d_key, d_value) < 0)
195 return NULL;
196 }
197 }
198 Py_RETURN_NONE;
199}
Richard Jones7b9558d2006-05-27 12:29:24 +0000200
Richard Jones7b9558d2006-05-27 12:29:24 +0000201
202static PyMethodDef BaseException_methods[] = {
203 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Georg Brandl85ac8502006-06-01 06:39:19 +0000204 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Nick Coghlan524b7772008-07-08 14:08:04 +0000205#ifdef Py_USING_UNICODE
206 {"__unicode__", (PyCFunction)BaseException_unicode, METH_NOARGS },
207#endif
Richard Jones7b9558d2006-05-27 12:29:24 +0000208 {NULL, NULL, 0, NULL},
209};
210
211
212
213static PyObject *
214BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
215{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000216 if (PyErr_WarnPy3k("__getitem__ not supported for exception "
217 "classes in 3.x; use args attribute", 1) < 0)
218 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000219 return PySequence_GetItem(self->args, index);
220}
221
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000222static PyObject *
223BaseException_getslice(PyBaseExceptionObject *self,
224 Py_ssize_t start, Py_ssize_t stop)
225{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000226 if (PyErr_WarnPy3k("__getslice__ not supported for exception "
227 "classes in 3.x; use args attribute", 1) < 0)
228 return NULL;
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000229 return PySequence_GetSlice(self->args, start, stop);
230}
231
Richard Jones7b9558d2006-05-27 12:29:24 +0000232static PySequenceMethods BaseException_as_sequence = {
233 0, /* sq_length; */
234 0, /* sq_concat; */
235 0, /* sq_repeat; */
236 (ssizeargfunc)BaseException_getitem, /* sq_item; */
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000237 (ssizessizeargfunc)BaseException_getslice, /* sq_slice; */
Richard Jones7b9558d2006-05-27 12:29:24 +0000238 0, /* sq_ass_item; */
239 0, /* sq_ass_slice; */
240 0, /* sq_contains; */
241 0, /* sq_inplace_concat; */
242 0 /* sq_inplace_repeat; */
243};
244
Richard Jones7b9558d2006-05-27 12:29:24 +0000245static PyObject *
246BaseException_get_dict(PyBaseExceptionObject *self)
247{
248 if (self->dict == NULL) {
249 self->dict = PyDict_New();
250 if (!self->dict)
251 return NULL;
252 }
253 Py_INCREF(self->dict);
254 return self->dict;
255}
256
257static int
258BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
259{
260 if (val == NULL) {
261 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
262 return -1;
263 }
264 if (!PyDict_Check(val)) {
265 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
266 return -1;
267 }
268 Py_CLEAR(self->dict);
269 Py_INCREF(val);
270 self->dict = val;
271 return 0;
272}
273
274static PyObject *
275BaseException_get_args(PyBaseExceptionObject *self)
276{
277 if (self->args == NULL) {
278 Py_INCREF(Py_None);
279 return Py_None;
280 }
281 Py_INCREF(self->args);
282 return self->args;
283}
284
285static int
286BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
287{
288 PyObject *seq;
289 if (val == NULL) {
290 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
291 return -1;
292 }
293 seq = PySequence_Tuple(val);
294 if (!seq) return -1;
Georg Brandlc7c51142006-05-29 09:46:51 +0000295 Py_CLEAR(self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000296 self->args = seq;
297 return 0;
298}
299
Brett Cannon229cee22007-05-05 01:34:02 +0000300static PyObject *
301BaseException_get_message(PyBaseExceptionObject *self)
302{
303 int ret;
304 ret = PyErr_WarnEx(PyExc_DeprecationWarning,
305 "BaseException.message has been deprecated as "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000306 "of Python 2.6", 1);
307 if (ret < 0)
Brett Cannon229cee22007-05-05 01:34:02 +0000308 return NULL;
309
310 Py_INCREF(self->message);
311 return self->message;
312}
313
314static int
315BaseException_set_message(PyBaseExceptionObject *self, PyObject *val)
316{
317 int ret;
318 ret = PyErr_WarnEx(PyExc_DeprecationWarning,
319 "BaseException.message has been deprecated as "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000320 "of Python 2.6", 1);
321 if (ret < 0)
Brett Cannon229cee22007-05-05 01:34:02 +0000322 return -1;
323 Py_INCREF(val);
324 Py_DECREF(self->message);
325 self->message = val;
326 return 0;
327}
328
Richard Jones7b9558d2006-05-27 12:29:24 +0000329static PyGetSetDef BaseException_getset[] = {
330 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
331 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Brett Cannon229cee22007-05-05 01:34:02 +0000332 {"message", (getter)BaseException_get_message,
333 (setter)BaseException_set_message},
Richard Jones7b9558d2006-05-27 12:29:24 +0000334 {NULL},
335};
336
337
338static PyTypeObject _PyExc_BaseException = {
339 PyObject_HEAD_INIT(NULL)
340 0, /*ob_size*/
341 EXC_MODULE_NAME "BaseException", /*tp_name*/
342 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
343 0, /*tp_itemsize*/
344 (destructor)BaseException_dealloc, /*tp_dealloc*/
345 0, /*tp_print*/
346 0, /*tp_getattr*/
347 0, /*tp_setattr*/
348 0, /* tp_compare; */
349 (reprfunc)BaseException_repr, /*tp_repr*/
350 0, /*tp_as_number*/
351 &BaseException_as_sequence, /*tp_as_sequence*/
352 0, /*tp_as_mapping*/
353 0, /*tp_hash */
354 0, /*tp_call*/
355 (reprfunc)BaseException_str, /*tp_str*/
356 PyObject_GenericGetAttr, /*tp_getattro*/
357 PyObject_GenericSetAttr, /*tp_setattro*/
358 0, /*tp_as_buffer*/
Neal Norwitzee3a1b52007-02-25 19:44:48 +0000359 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
360 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Richard Jones7b9558d2006-05-27 12:29:24 +0000361 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
362 (traverseproc)BaseException_traverse, /* tp_traverse */
363 (inquiry)BaseException_clear, /* tp_clear */
364 0, /* tp_richcompare */
365 0, /* tp_weaklistoffset */
366 0, /* tp_iter */
367 0, /* tp_iternext */
368 BaseException_methods, /* tp_methods */
Brett Cannon229cee22007-05-05 01:34:02 +0000369 0, /* tp_members */
Richard Jones7b9558d2006-05-27 12:29:24 +0000370 BaseException_getset, /* tp_getset */
371 0, /* tp_base */
372 0, /* tp_dict */
373 0, /* tp_descr_get */
374 0, /* tp_descr_set */
375 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
376 (initproc)BaseException_init, /* tp_init */
377 0, /* tp_alloc */
378 BaseException_new, /* tp_new */
379};
380/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
381from the previous implmentation and also allowing Python objects to be used
382in the API */
383PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
384
Richard Jones2d555b32006-05-27 16:15:11 +0000385/* note these macros omit the last semicolon so the macro invocation may
386 * include it and not look strange.
387 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000388#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
389static PyTypeObject _PyExc_ ## EXCNAME = { \
390 PyObject_HEAD_INIT(NULL) \
391 0, \
392 EXC_MODULE_NAME # EXCNAME, \
393 sizeof(PyBaseExceptionObject), \
394 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
395 0, 0, 0, 0, 0, 0, 0, \
396 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
397 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
398 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
399 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
400 (initproc)BaseException_init, 0, BaseException_new,\
401}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000402PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000403
404#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
405static PyTypeObject _PyExc_ ## EXCNAME = { \
406 PyObject_HEAD_INIT(NULL) \
407 0, \
408 EXC_MODULE_NAME # EXCNAME, \
409 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000410 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000411 0, 0, 0, 0, 0, \
412 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000413 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
414 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000415 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000416 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000417}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000418PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000419
420#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
421static PyTypeObject _PyExc_ ## EXCNAME = { \
422 PyObject_HEAD_INIT(NULL) \
423 0, \
424 EXC_MODULE_NAME # EXCNAME, \
425 sizeof(Py ## EXCSTORE ## Object), 0, \
426 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
427 (reprfunc)EXCSTR, 0, 0, 0, \
428 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
429 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
430 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
431 EXCMEMBERS, 0, &_ ## EXCBASE, \
432 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000433 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000434}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000435PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000436
437
438/*
439 * Exception extends BaseException
440 */
441SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000442 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000443
444
445/*
446 * StandardError extends Exception
447 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000448SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000449 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000450 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000451
452
453/*
454 * TypeError extends StandardError
455 */
456SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000457 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000458
459
460/*
461 * StopIteration extends Exception
462 */
463SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000464 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000465
466
467/*
Christian Heimes44eeaec2007-12-03 20:01:02 +0000468 * GeneratorExit extends BaseException
Richard Jones7b9558d2006-05-27 12:29:24 +0000469 */
Christian Heimes44eeaec2007-12-03 20:01:02 +0000470SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000471 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000472
473
474/*
475 * SystemExit extends BaseException
476 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000477
478static int
479SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
480{
481 Py_ssize_t size = PyTuple_GET_SIZE(args);
482
483 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
484 return -1;
485
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000486 if (size == 0)
487 return 0;
488 Py_CLEAR(self->code);
Richard Jones7b9558d2006-05-27 12:29:24 +0000489 if (size == 1)
490 self->code = PyTuple_GET_ITEM(args, 0);
491 else if (size > 1)
492 self->code = args;
493 Py_INCREF(self->code);
494 return 0;
495}
496
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000497static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000498SystemExit_clear(PySystemExitObject *self)
499{
500 Py_CLEAR(self->code);
501 return BaseException_clear((PyBaseExceptionObject *)self);
502}
503
504static void
505SystemExit_dealloc(PySystemExitObject *self)
506{
Georg Brandl38f62372006-09-06 06:50:05 +0000507 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000508 SystemExit_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000509 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000510}
511
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000512static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000513SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
514{
515 Py_VISIT(self->code);
516 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
517}
518
519static PyMemberDef SystemExit_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000520 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
521 PyDoc_STR("exception code")},
522 {NULL} /* Sentinel */
523};
524
525ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
526 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000527 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000528
529/*
530 * KeyboardInterrupt extends BaseException
531 */
532SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000533 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000534
535
536/*
537 * ImportError extends StandardError
538 */
539SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000540 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000541
542
543/*
544 * EnvironmentError extends StandardError
545 */
546
Richard Jones7b9558d2006-05-27 12:29:24 +0000547/* Where a function has a single filename, such as open() or some
548 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
549 * called, giving a third argument which is the filename. But, so
550 * that old code using in-place unpacking doesn't break, e.g.:
551 *
552 * except IOError, (errno, strerror):
553 *
554 * we hack args so that it only contains two items. This also
555 * means we need our own __str__() which prints out the filename
556 * when it was supplied.
557 */
558static int
559EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
560 PyObject *kwds)
561{
562 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
563 PyObject *subslice = NULL;
564
565 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
566 return -1;
567
Georg Brandl3267d282006-09-30 09:03:42 +0000568 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000569 return 0;
570 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000571
572 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000573 &myerrno, &strerror, &filename)) {
574 return -1;
575 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000576 Py_CLEAR(self->myerrno); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000577 self->myerrno = myerrno;
578 Py_INCREF(self->myerrno);
579
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000580 Py_CLEAR(self->strerror); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000581 self->strerror = strerror;
582 Py_INCREF(self->strerror);
583
584 /* self->filename will remain Py_None otherwise */
585 if (filename != NULL) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000586 Py_CLEAR(self->filename); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000587 self->filename = filename;
588 Py_INCREF(self->filename);
589
590 subslice = PyTuple_GetSlice(args, 0, 2);
591 if (!subslice)
592 return -1;
593
594 Py_DECREF(self->args); /* replacing args */
595 self->args = subslice;
596 }
597 return 0;
598}
599
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000600static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000601EnvironmentError_clear(PyEnvironmentErrorObject *self)
602{
603 Py_CLEAR(self->myerrno);
604 Py_CLEAR(self->strerror);
605 Py_CLEAR(self->filename);
606 return BaseException_clear((PyBaseExceptionObject *)self);
607}
608
609static void
610EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
611{
Georg Brandl38f62372006-09-06 06:50:05 +0000612 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000613 EnvironmentError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000614 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000615}
616
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000617static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000618EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
619 void *arg)
620{
621 Py_VISIT(self->myerrno);
622 Py_VISIT(self->strerror);
623 Py_VISIT(self->filename);
624 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
625}
626
627static PyObject *
628EnvironmentError_str(PyEnvironmentErrorObject *self)
629{
630 PyObject *rtnval = NULL;
631
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000632 if (self->filename) {
633 PyObject *fmt;
634 PyObject *repr;
635 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000636
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000637 fmt = PyString_FromString("[Errno %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000638 if (!fmt)
639 return NULL;
640
641 repr = PyObject_Repr(self->filename);
642 if (!repr) {
643 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000644 return NULL;
645 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000646 tuple = PyTuple_New(3);
647 if (!tuple) {
648 Py_DECREF(repr);
649 Py_DECREF(fmt);
650 return NULL;
651 }
652
653 if (self->myerrno) {
654 Py_INCREF(self->myerrno);
655 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
656 }
657 else {
658 Py_INCREF(Py_None);
659 PyTuple_SET_ITEM(tuple, 0, Py_None);
660 }
661 if (self->strerror) {
662 Py_INCREF(self->strerror);
663 PyTuple_SET_ITEM(tuple, 1, self->strerror);
664 }
665 else {
666 Py_INCREF(Py_None);
667 PyTuple_SET_ITEM(tuple, 1, Py_None);
668 }
669
Richard Jones7b9558d2006-05-27 12:29:24 +0000670 PyTuple_SET_ITEM(tuple, 2, repr);
671
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000672 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000673
674 Py_DECREF(fmt);
675 Py_DECREF(tuple);
676 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000677 else if (self->myerrno && self->strerror) {
678 PyObject *fmt;
679 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000680
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000681 fmt = PyString_FromString("[Errno %s] %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000682 if (!fmt)
683 return NULL;
684
685 tuple = PyTuple_New(2);
686 if (!tuple) {
687 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000688 return NULL;
689 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000690
691 if (self->myerrno) {
692 Py_INCREF(self->myerrno);
693 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
694 }
695 else {
696 Py_INCREF(Py_None);
697 PyTuple_SET_ITEM(tuple, 0, Py_None);
698 }
699 if (self->strerror) {
700 Py_INCREF(self->strerror);
701 PyTuple_SET_ITEM(tuple, 1, self->strerror);
702 }
703 else {
704 Py_INCREF(Py_None);
705 PyTuple_SET_ITEM(tuple, 1, Py_None);
706 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000707
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000708 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000709
710 Py_DECREF(fmt);
711 Py_DECREF(tuple);
712 }
713 else
714 rtnval = BaseException_str((PyBaseExceptionObject *)self);
715
716 return rtnval;
717}
718
719static PyMemberDef EnvironmentError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000720 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000721 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000722 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000723 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000724 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000725 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000726 {NULL} /* Sentinel */
727};
728
729
730static PyObject *
731EnvironmentError_reduce(PyEnvironmentErrorObject *self)
732{
733 PyObject *args = self->args;
734 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000735
Richard Jones7b9558d2006-05-27 12:29:24 +0000736 /* self->args is only the first two real arguments if there was a
737 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000738 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000739 args = PyTuple_New(3);
740 if (!args) return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000741
742 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000743 Py_INCREF(tmp);
744 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000745
746 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000747 Py_INCREF(tmp);
748 PyTuple_SET_ITEM(args, 1, tmp);
749
750 Py_INCREF(self->filename);
751 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000752 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000753 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000754
755 if (self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000756 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000757 else
Christian Heimese93237d2007-12-19 02:37:44 +0000758 res = PyTuple_Pack(2, Py_TYPE(self), args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000759 Py_DECREF(args);
760 return res;
761}
762
763
764static PyMethodDef EnvironmentError_methods[] = {
765 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
766 {NULL}
767};
768
769ComplexExtendsException(PyExc_StandardError, EnvironmentError,
770 EnvironmentError, EnvironmentError_dealloc,
771 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000772 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000773 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000774
775
776/*
777 * IOError extends EnvironmentError
778 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000779MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000780 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000781
782
783/*
784 * OSError extends EnvironmentError
785 */
786MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000787 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000788
789
790/*
791 * WindowsError extends OSError
792 */
793#ifdef MS_WINDOWS
794#include "errmap.h"
795
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000796static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000797WindowsError_clear(PyWindowsErrorObject *self)
798{
799 Py_CLEAR(self->myerrno);
800 Py_CLEAR(self->strerror);
801 Py_CLEAR(self->filename);
802 Py_CLEAR(self->winerror);
803 return BaseException_clear((PyBaseExceptionObject *)self);
804}
805
806static void
807WindowsError_dealloc(PyWindowsErrorObject *self)
808{
Georg Brandl38f62372006-09-06 06:50:05 +0000809 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000810 WindowsError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000811 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000812}
813
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000814static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000815WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
816{
817 Py_VISIT(self->myerrno);
818 Py_VISIT(self->strerror);
819 Py_VISIT(self->filename);
820 Py_VISIT(self->winerror);
821 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
822}
823
Richard Jones7b9558d2006-05-27 12:29:24 +0000824static int
825WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
826{
827 PyObject *o_errcode = NULL;
828 long errcode;
829 long posix_errno;
830
831 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
832 == -1)
833 return -1;
834
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000835 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000836 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000837
838 /* Set errno to the POSIX errno, and winerror to the Win32
839 error code. */
840 errcode = PyInt_AsLong(self->myerrno);
841 if (errcode == -1 && PyErr_Occurred())
842 return -1;
843 posix_errno = winerror_to_errno(errcode);
844
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000845 Py_CLEAR(self->winerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000846 self->winerror = self->myerrno;
847
848 o_errcode = PyInt_FromLong(posix_errno);
849 if (!o_errcode)
850 return -1;
851
852 self->myerrno = o_errcode;
853
854 return 0;
855}
856
857
858static PyObject *
859WindowsError_str(PyWindowsErrorObject *self)
860{
Richard Jones7b9558d2006-05-27 12:29:24 +0000861 PyObject *rtnval = NULL;
862
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000863 if (self->filename) {
864 PyObject *fmt;
865 PyObject *repr;
866 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000867
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000868 fmt = PyString_FromString("[Error %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000869 if (!fmt)
870 return NULL;
871
872 repr = PyObject_Repr(self->filename);
873 if (!repr) {
874 Py_DECREF(fmt);
875 return NULL;
876 }
877 tuple = PyTuple_New(3);
878 if (!tuple) {
879 Py_DECREF(repr);
880 Py_DECREF(fmt);
881 return NULL;
882 }
883
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000884 if (self->winerror) {
885 Py_INCREF(self->winerror);
886 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000887 }
888 else {
889 Py_INCREF(Py_None);
890 PyTuple_SET_ITEM(tuple, 0, Py_None);
891 }
892 if (self->strerror) {
893 Py_INCREF(self->strerror);
894 PyTuple_SET_ITEM(tuple, 1, self->strerror);
895 }
896 else {
897 Py_INCREF(Py_None);
898 PyTuple_SET_ITEM(tuple, 1, Py_None);
899 }
900
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000901 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000902
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000903 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000904
905 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000906 Py_DECREF(tuple);
907 }
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000908 else if (self->winerror && self->strerror) {
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000909 PyObject *fmt;
910 PyObject *tuple;
911
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000912 fmt = PyString_FromString("[Error %s] %s");
Richard Jones7b9558d2006-05-27 12:29:24 +0000913 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000914 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000915
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000916 tuple = PyTuple_New(2);
917 if (!tuple) {
918 Py_DECREF(fmt);
919 return NULL;
920 }
921
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000922 if (self->winerror) {
923 Py_INCREF(self->winerror);
924 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000925 }
926 else {
927 Py_INCREF(Py_None);
928 PyTuple_SET_ITEM(tuple, 0, Py_None);
929 }
930 if (self->strerror) {
931 Py_INCREF(self->strerror);
932 PyTuple_SET_ITEM(tuple, 1, self->strerror);
933 }
934 else {
935 Py_INCREF(Py_None);
936 PyTuple_SET_ITEM(tuple, 1, Py_None);
937 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000938
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000939 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000940
941 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000942 Py_DECREF(tuple);
943 }
944 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000945 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000946
Richard Jones7b9558d2006-05-27 12:29:24 +0000947 return rtnval;
948}
949
950static PyMemberDef WindowsError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000951 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000952 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000953 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000954 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000955 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000956 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000957 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000958 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000959 {NULL} /* Sentinel */
960};
961
Richard Jones2d555b32006-05-27 16:15:11 +0000962ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
963 WindowsError_dealloc, 0, WindowsError_members,
964 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000965
966#endif /* MS_WINDOWS */
967
968
969/*
970 * VMSError extends OSError (I think)
971 */
972#ifdef __VMS
973MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +0000974 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000975#endif
976
977
978/*
979 * EOFError extends StandardError
980 */
981SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +0000982 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000983
984
985/*
986 * RuntimeError extends StandardError
987 */
988SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000989 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000990
991
992/*
993 * NotImplementedError extends RuntimeError
994 */
995SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +0000996 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000997
998/*
999 * NameError extends StandardError
1000 */
1001SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +00001002 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001003
1004/*
1005 * UnboundLocalError extends NameError
1006 */
1007SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +00001008 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001009
1010/*
1011 * AttributeError extends StandardError
1012 */
1013SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001014 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001015
1016
1017/*
1018 * SyntaxError extends StandardError
1019 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001020
1021static int
1022SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
1023{
1024 PyObject *info = NULL;
1025 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
1026
1027 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1028 return -1;
1029
1030 if (lenargs >= 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001031 Py_CLEAR(self->msg);
Richard Jones7b9558d2006-05-27 12:29:24 +00001032 self->msg = PyTuple_GET_ITEM(args, 0);
1033 Py_INCREF(self->msg);
1034 }
1035 if (lenargs == 2) {
1036 info = PyTuple_GET_ITEM(args, 1);
1037 info = PySequence_Tuple(info);
1038 if (!info) return -1;
1039
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001040 if (PyTuple_GET_SIZE(info) != 4) {
1041 /* not a very good error message, but it's what Python 2.4 gives */
1042 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
1043 Py_DECREF(info);
1044 return -1;
1045 }
1046
1047 Py_CLEAR(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001048 self->filename = PyTuple_GET_ITEM(info, 0);
1049 Py_INCREF(self->filename);
1050
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001051 Py_CLEAR(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001052 self->lineno = PyTuple_GET_ITEM(info, 1);
1053 Py_INCREF(self->lineno);
1054
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001055 Py_CLEAR(self->offset);
Richard Jones7b9558d2006-05-27 12:29:24 +00001056 self->offset = PyTuple_GET_ITEM(info, 2);
1057 Py_INCREF(self->offset);
1058
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001059 Py_CLEAR(self->text);
Richard Jones7b9558d2006-05-27 12:29:24 +00001060 self->text = PyTuple_GET_ITEM(info, 3);
1061 Py_INCREF(self->text);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001062
1063 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001064 }
1065 return 0;
1066}
1067
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001068static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001069SyntaxError_clear(PySyntaxErrorObject *self)
1070{
1071 Py_CLEAR(self->msg);
1072 Py_CLEAR(self->filename);
1073 Py_CLEAR(self->lineno);
1074 Py_CLEAR(self->offset);
1075 Py_CLEAR(self->text);
1076 Py_CLEAR(self->print_file_and_line);
1077 return BaseException_clear((PyBaseExceptionObject *)self);
1078}
1079
1080static void
1081SyntaxError_dealloc(PySyntaxErrorObject *self)
1082{
Georg Brandl38f62372006-09-06 06:50:05 +00001083 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001084 SyntaxError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001085 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001086}
1087
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001088static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001089SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1090{
1091 Py_VISIT(self->msg);
1092 Py_VISIT(self->filename);
1093 Py_VISIT(self->lineno);
1094 Py_VISIT(self->offset);
1095 Py_VISIT(self->text);
1096 Py_VISIT(self->print_file_and_line);
1097 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1098}
1099
1100/* This is called "my_basename" instead of just "basename" to avoid name
1101 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1102 defined, and Python does define that. */
1103static char *
1104my_basename(char *name)
1105{
1106 char *cp = name;
1107 char *result = name;
1108
1109 if (name == NULL)
1110 return "???";
1111 while (*cp != '\0') {
1112 if (*cp == SEP)
1113 result = cp + 1;
1114 ++cp;
1115 }
1116 return result;
1117}
1118
1119
1120static PyObject *
1121SyntaxError_str(PySyntaxErrorObject *self)
1122{
1123 PyObject *str;
1124 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001125 int have_filename = 0;
1126 int have_lineno = 0;
1127 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001128 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001129
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001130 if (self->msg)
1131 str = PyObject_Str(self->msg);
1132 else
1133 str = PyObject_Str(Py_None);
Georg Brandl43ab1002006-05-28 20:57:09 +00001134 if (!str) return NULL;
1135 /* Don't fiddle with non-string return (shouldn't happen anyway) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001136 if (!PyString_Check(str)) return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001137
1138 /* XXX -- do all the additional formatting with filename and
1139 lineno here */
1140
Georg Brandl43ab1002006-05-28 20:57:09 +00001141 have_filename = (self->filename != NULL) &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001142 PyString_Check(self->filename);
Georg Brandl43ab1002006-05-28 20:57:09 +00001143 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001144
Georg Brandl43ab1002006-05-28 20:57:09 +00001145 if (!have_filename && !have_lineno)
1146 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001147
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001148 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001149 if (have_filename)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001150 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001151
Georg Brandl43ab1002006-05-28 20:57:09 +00001152 buffer = PyMem_MALLOC(bufsize);
1153 if (buffer == NULL)
1154 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001155
Georg Brandl43ab1002006-05-28 20:57:09 +00001156 if (have_filename && have_lineno)
1157 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001158 PyString_AS_STRING(str),
1159 my_basename(PyString_AS_STRING(self->filename)),
Georg Brandl43ab1002006-05-28 20:57:09 +00001160 PyInt_AsLong(self->lineno));
1161 else if (have_filename)
1162 PyOS_snprintf(buffer, bufsize, "%s (%s)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001163 PyString_AS_STRING(str),
1164 my_basename(PyString_AS_STRING(self->filename)));
Georg Brandl43ab1002006-05-28 20:57:09 +00001165 else /* only have_lineno */
1166 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001167 PyString_AS_STRING(str),
Georg Brandl43ab1002006-05-28 20:57:09 +00001168 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001169
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001170 result = PyString_FromString(buffer);
Georg Brandl43ab1002006-05-28 20:57:09 +00001171 PyMem_FREE(buffer);
1172
1173 if (result == NULL)
1174 result = str;
1175 else
1176 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001177 return result;
1178}
1179
1180static PyMemberDef SyntaxError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001181 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1182 PyDoc_STR("exception msg")},
1183 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1184 PyDoc_STR("exception filename")},
1185 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1186 PyDoc_STR("exception lineno")},
1187 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1188 PyDoc_STR("exception offset")},
1189 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1190 PyDoc_STR("exception text")},
1191 {"print_file_and_line", T_OBJECT,
1192 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1193 PyDoc_STR("exception print_file_and_line")},
1194 {NULL} /* Sentinel */
1195};
1196
1197ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1198 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001199 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001200
1201
1202/*
1203 * IndentationError extends SyntaxError
1204 */
1205MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001206 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001207
1208
1209/*
1210 * TabError extends IndentationError
1211 */
1212MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001213 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001214
1215
1216/*
1217 * LookupError extends StandardError
1218 */
1219SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001220 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001221
1222
1223/*
1224 * IndexError extends LookupError
1225 */
1226SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001227 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001228
1229
1230/*
1231 * KeyError extends LookupError
1232 */
1233static PyObject *
1234KeyError_str(PyBaseExceptionObject *self)
1235{
1236 /* If args is a tuple of exactly one item, apply repr to args[0].
1237 This is done so that e.g. the exception raised by {}[''] prints
1238 KeyError: ''
1239 rather than the confusing
1240 KeyError
1241 alone. The downside is that if KeyError is raised with an explanatory
1242 string, that string will be displayed in quotes. Too bad.
1243 If args is anything else, use the default BaseException__str__().
1244 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001245 if (PyTuple_GET_SIZE(self->args) == 1) {
1246 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001247 }
1248 return BaseException_str(self);
1249}
1250
1251ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001252 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001253
1254
1255/*
1256 * ValueError extends StandardError
1257 */
1258SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001259 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001260
1261/*
1262 * UnicodeError extends ValueError
1263 */
1264
1265SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001266 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001267
1268#ifdef Py_USING_UNICODE
Richard Jones7b9558d2006-05-27 12:29:24 +00001269static PyObject *
1270get_string(PyObject *attr, const char *name)
1271{
1272 if (!attr) {
1273 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1274 return NULL;
1275 }
1276
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001277 if (!PyString_Check(attr)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001278 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1279 return NULL;
1280 }
1281 Py_INCREF(attr);
1282 return attr;
1283}
1284
1285
1286static int
1287set_string(PyObject **attr, const char *value)
1288{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001289 PyObject *obj = PyString_FromString(value);
Richard Jones7b9558d2006-05-27 12:29:24 +00001290 if (!obj)
1291 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001292 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001293 *attr = obj;
1294 return 0;
1295}
1296
1297
1298static PyObject *
1299get_unicode(PyObject *attr, const char *name)
1300{
1301 if (!attr) {
1302 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1303 return NULL;
1304 }
1305
1306 if (!PyUnicode_Check(attr)) {
1307 PyErr_Format(PyExc_TypeError,
1308 "%.200s attribute must be unicode", name);
1309 return NULL;
1310 }
1311 Py_INCREF(attr);
1312 return attr;
1313}
1314
1315PyObject *
1316PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1317{
1318 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1319}
1320
1321PyObject *
1322PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1323{
1324 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1325}
1326
1327PyObject *
1328PyUnicodeEncodeError_GetObject(PyObject *exc)
1329{
1330 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1331}
1332
1333PyObject *
1334PyUnicodeDecodeError_GetObject(PyObject *exc)
1335{
1336 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1337}
1338
1339PyObject *
1340PyUnicodeTranslateError_GetObject(PyObject *exc)
1341{
1342 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1343}
1344
1345int
1346PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1347{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001348 Py_ssize_t size;
1349 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1350 "object");
1351 if (!obj)
1352 return -1;
1353 *start = ((PyUnicodeErrorObject *)exc)->start;
1354 size = PyUnicode_GET_SIZE(obj);
1355 if (*start<0)
1356 *start = 0; /*XXX check for values <0*/
1357 if (*start>=size)
1358 *start = size-1;
1359 Py_DECREF(obj);
1360 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001361}
1362
1363
1364int
1365PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1366{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001367 Py_ssize_t size;
1368 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1369 "object");
1370 if (!obj)
1371 return -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001372 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001373 *start = ((PyUnicodeErrorObject *)exc)->start;
1374 if (*start<0)
1375 *start = 0;
1376 if (*start>=size)
1377 *start = size-1;
1378 Py_DECREF(obj);
1379 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001380}
1381
1382
1383int
1384PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1385{
1386 return PyUnicodeEncodeError_GetStart(exc, start);
1387}
1388
1389
1390int
1391PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1392{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001393 ((PyUnicodeErrorObject *)exc)->start = start;
1394 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001395}
1396
1397
1398int
1399PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1400{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001401 ((PyUnicodeErrorObject *)exc)->start = start;
1402 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001403}
1404
1405
1406int
1407PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1408{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001409 ((PyUnicodeErrorObject *)exc)->start = start;
1410 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001411}
1412
1413
1414int
1415PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1416{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001417 Py_ssize_t size;
1418 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1419 "object");
1420 if (!obj)
1421 return -1;
1422 *end = ((PyUnicodeErrorObject *)exc)->end;
1423 size = PyUnicode_GET_SIZE(obj);
1424 if (*end<1)
1425 *end = 1;
1426 if (*end>size)
1427 *end = size;
1428 Py_DECREF(obj);
1429 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001430}
1431
1432
1433int
1434PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1435{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001436 Py_ssize_t size;
1437 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1438 "object");
1439 if (!obj)
1440 return -1;
1441 *end = ((PyUnicodeErrorObject *)exc)->end;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001442 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001443 if (*end<1)
1444 *end = 1;
1445 if (*end>size)
1446 *end = size;
1447 Py_DECREF(obj);
1448 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001449}
1450
1451
1452int
1453PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1454{
1455 return PyUnicodeEncodeError_GetEnd(exc, start);
1456}
1457
1458
1459int
1460PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1461{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001462 ((PyUnicodeErrorObject *)exc)->end = end;
1463 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001464}
1465
1466
1467int
1468PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1469{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001470 ((PyUnicodeErrorObject *)exc)->end = end;
1471 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001472}
1473
1474
1475int
1476PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1477{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001478 ((PyUnicodeErrorObject *)exc)->end = end;
1479 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001480}
1481
1482PyObject *
1483PyUnicodeEncodeError_GetReason(PyObject *exc)
1484{
1485 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1486}
1487
1488
1489PyObject *
1490PyUnicodeDecodeError_GetReason(PyObject *exc)
1491{
1492 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1493}
1494
1495
1496PyObject *
1497PyUnicodeTranslateError_GetReason(PyObject *exc)
1498{
1499 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1500}
1501
1502
1503int
1504PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1505{
1506 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1507}
1508
1509
1510int
1511PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1512{
1513 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1514}
1515
1516
1517int
1518PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1519{
1520 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1521}
1522
1523
Richard Jones7b9558d2006-05-27 12:29:24 +00001524static int
1525UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1526 PyTypeObject *objecttype)
1527{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001528 Py_CLEAR(self->encoding);
1529 Py_CLEAR(self->object);
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001530 Py_CLEAR(self->reason);
1531
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001532 if (!PyArg_ParseTuple(args, "O!O!nnO!",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001533 &PyString_Type, &self->encoding,
Richard Jones7b9558d2006-05-27 12:29:24 +00001534 objecttype, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001535 &self->start,
1536 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001537 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001538 self->encoding = self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001539 return -1;
1540 }
1541
1542 Py_INCREF(self->encoding);
1543 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001544 Py_INCREF(self->reason);
1545
1546 return 0;
1547}
1548
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001549static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001550UnicodeError_clear(PyUnicodeErrorObject *self)
1551{
1552 Py_CLEAR(self->encoding);
1553 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001554 Py_CLEAR(self->reason);
1555 return BaseException_clear((PyBaseExceptionObject *)self);
1556}
1557
1558static void
1559UnicodeError_dealloc(PyUnicodeErrorObject *self)
1560{
Georg Brandl38f62372006-09-06 06:50:05 +00001561 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001562 UnicodeError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001563 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001564}
1565
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001566static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001567UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1568{
1569 Py_VISIT(self->encoding);
1570 Py_VISIT(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001571 Py_VISIT(self->reason);
1572 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1573}
1574
1575static PyMemberDef UnicodeError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001576 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1577 PyDoc_STR("exception encoding")},
1578 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1579 PyDoc_STR("exception object")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001580 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001581 PyDoc_STR("exception start")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001582 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001583 PyDoc_STR("exception end")},
1584 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1585 PyDoc_STR("exception reason")},
1586 {NULL} /* Sentinel */
1587};
1588
1589
1590/*
1591 * UnicodeEncodeError extends UnicodeError
1592 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001593
1594static int
1595UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1596{
1597 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1598 return -1;
1599 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1600 kwds, &PyUnicode_Type);
1601}
1602
1603static PyObject *
1604UnicodeEncodeError_str(PyObject *self)
1605{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001606 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001607
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001608 if (uself->end==uself->start+1) {
1609 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001610 char badchar_str[20];
1611 if (badchar <= 0xff)
1612 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1613 else if (badchar <= 0xffff)
1614 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1615 else
1616 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001617 return PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001618 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001619 PyString_AS_STRING(uself->encoding),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001620 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001621 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001622 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001623 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001624 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001625 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001626 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001627 PyString_AS_STRING(uself->encoding),
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001628 uself->start,
1629 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001630 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001631 );
1632}
1633
1634static PyTypeObject _PyExc_UnicodeEncodeError = {
1635 PyObject_HEAD_INIT(NULL)
1636 0,
Georg Brandl38f62372006-09-06 06:50:05 +00001637 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001638 sizeof(PyUnicodeErrorObject), 0,
1639 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1640 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1641 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001642 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1643 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001644 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001645 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001646};
1647PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1648
1649PyObject *
1650PyUnicodeEncodeError_Create(
1651 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1652 Py_ssize_t start, Py_ssize_t end, const char *reason)
1653{
1654 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001655 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001656}
1657
1658
1659/*
1660 * UnicodeDecodeError extends UnicodeError
1661 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001662
1663static int
1664UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1665{
1666 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1667 return -1;
1668 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001669 kwds, &PyString_Type);
Richard Jones7b9558d2006-05-27 12:29:24 +00001670}
1671
1672static PyObject *
1673UnicodeDecodeError_str(PyObject *self)
1674{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001675 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001676
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001677 if (uself->end==uself->start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001678 /* FromFormat does not support %02x, so format that separately */
1679 char byte[4];
1680 PyOS_snprintf(byte, sizeof(byte), "%02x",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001681 ((int)PyString_AS_STRING(uself->object)[uself->start])&0xff);
1682 return PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001683 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001684 PyString_AS_STRING(uself->encoding),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001685 byte,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001686 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001687 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001688 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001689 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001690 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001691 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001692 PyString_AS_STRING(uself->encoding),
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001693 uself->start,
1694 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001695 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001696 );
1697}
1698
1699static PyTypeObject _PyExc_UnicodeDecodeError = {
1700 PyObject_HEAD_INIT(NULL)
1701 0,
1702 EXC_MODULE_NAME "UnicodeDecodeError",
1703 sizeof(PyUnicodeErrorObject), 0,
1704 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1705 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1706 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001707 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1708 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001709 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001710 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001711};
1712PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1713
1714PyObject *
1715PyUnicodeDecodeError_Create(
1716 const char *encoding, const char *object, Py_ssize_t length,
1717 Py_ssize_t start, Py_ssize_t end, const char *reason)
1718{
1719 assert(length < INT_MAX);
1720 assert(start < INT_MAX);
1721 assert(end < INT_MAX);
1722 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001723 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001724}
1725
1726
1727/*
1728 * UnicodeTranslateError extends UnicodeError
1729 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001730
1731static int
1732UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1733 PyObject *kwds)
1734{
1735 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1736 return -1;
1737
1738 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001739 Py_CLEAR(self->reason);
1740
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001741 if (!PyArg_ParseTuple(args, "O!nnO!",
Richard Jones7b9558d2006-05-27 12:29:24 +00001742 &PyUnicode_Type, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001743 &self->start,
1744 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001745 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001746 self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001747 return -1;
1748 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001749
Richard Jones7b9558d2006-05-27 12:29:24 +00001750 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001751 Py_INCREF(self->reason);
1752
1753 return 0;
1754}
1755
1756
1757static PyObject *
1758UnicodeTranslateError_str(PyObject *self)
1759{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001760 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001761
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001762 if (uself->end==uself->start+1) {
1763 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001764 char badchar_str[20];
1765 if (badchar <= 0xff)
1766 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1767 else if (badchar <= 0xffff)
1768 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1769 else
1770 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001771 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001772 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001773 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001774 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001775 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001776 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001777 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001778 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001779 "can't translate characters in position %zd-%zd: %.400s",
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001780 uself->start,
1781 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001782 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001783 );
1784}
1785
1786static PyTypeObject _PyExc_UnicodeTranslateError = {
1787 PyObject_HEAD_INIT(NULL)
1788 0,
1789 EXC_MODULE_NAME "UnicodeTranslateError",
1790 sizeof(PyUnicodeErrorObject), 0,
1791 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1792 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1793 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001794 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001795 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1796 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001797 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001798};
1799PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1800
1801PyObject *
1802PyUnicodeTranslateError_Create(
1803 const Py_UNICODE *object, Py_ssize_t length,
1804 Py_ssize_t start, Py_ssize_t end, const char *reason)
1805{
1806 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001807 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001808}
1809#endif
1810
1811
1812/*
1813 * AssertionError extends StandardError
1814 */
1815SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001816 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001817
1818
1819/*
1820 * ArithmeticError extends StandardError
1821 */
1822SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001823 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001824
1825
1826/*
1827 * FloatingPointError extends ArithmeticError
1828 */
1829SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001830 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001831
1832
1833/*
1834 * OverflowError extends ArithmeticError
1835 */
1836SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001837 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001838
1839
1840/*
1841 * ZeroDivisionError extends ArithmeticError
1842 */
1843SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001844 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001845
1846
1847/*
1848 * SystemError extends StandardError
1849 */
1850SimpleExtendsException(PyExc_StandardError, SystemError,
1851 "Internal error in the Python interpreter.\n"
1852 "\n"
1853 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001854 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001855
1856
1857/*
1858 * ReferenceError extends StandardError
1859 */
1860SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001861 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001862
1863
1864/*
1865 * MemoryError extends StandardError
1866 */
Richard Jones2d555b32006-05-27 16:15:11 +00001867SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001868
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00001869/*
1870 * BufferError extends StandardError
1871 */
1872SimpleExtendsException(PyExc_StandardError, BufferError, "Buffer error.");
1873
Richard Jones7b9558d2006-05-27 12:29:24 +00001874
1875/* Warning category docstrings */
1876
1877/*
1878 * Warning extends Exception
1879 */
1880SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001881 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001882
1883
1884/*
1885 * UserWarning extends Warning
1886 */
1887SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001888 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001889
1890
1891/*
1892 * DeprecationWarning extends Warning
1893 */
1894SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001895 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001896
1897
1898/*
1899 * PendingDeprecationWarning extends Warning
1900 */
1901SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1902 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001903 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001904
1905
1906/*
1907 * SyntaxWarning extends Warning
1908 */
1909SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001910 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001911
1912
1913/*
1914 * RuntimeWarning extends Warning
1915 */
1916SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001917 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001918
1919
1920/*
1921 * FutureWarning extends Warning
1922 */
1923SimpleExtendsException(PyExc_Warning, FutureWarning,
1924 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001925 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001926
1927
1928/*
1929 * ImportWarning extends Warning
1930 */
1931SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001932 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00001933
1934
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001935/*
1936 * UnicodeWarning extends Warning
1937 */
1938SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1939 "Base class for warnings about Unicode related problems, mostly\n"
1940 "related to conversion problems.");
1941
Christian Heimes1a6387e2008-03-26 12:49:49 +00001942/*
1943 * BytesWarning extends Warning
1944 */
1945SimpleExtendsException(PyExc_Warning, BytesWarning,
1946 "Base class for warnings about bytes and buffer related problems, mostly\n"
1947 "related to conversion from str or comparing to str.");
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001948
Richard Jones7b9558d2006-05-27 12:29:24 +00001949/* Pre-computed MemoryError instance. Best to create this as early as
1950 * possible and not wait until a MemoryError is actually raised!
1951 */
1952PyObject *PyExc_MemoryErrorInst=NULL;
1953
Brett Cannon1e534b52007-09-07 04:18:30 +00001954/* Pre-computed RuntimeError instance for when recursion depth is reached.
1955 Meant to be used when normalizing the exception for exceeding the recursion
1956 depth will cause its own infinite recursion.
1957*/
1958PyObject *PyExc_RecursionErrorInst = NULL;
1959
Richard Jones7b9558d2006-05-27 12:29:24 +00001960/* module global functions */
1961static PyMethodDef functions[] = {
1962 /* Sentinel */
1963 {NULL, NULL}
1964};
1965
1966#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1967 Py_FatalError("exceptions bootstrapping error.");
1968
1969#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1970 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1971 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1972 Py_FatalError("Module dictionary insertion problem.");
1973
KristjĂ¡n Valur JĂ³nssonf6083172006-06-12 15:45:12 +00001974
Richard Jones7b9558d2006-05-27 12:29:24 +00001975PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001976_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00001977{
1978 PyObject *m, *bltinmod, *bdict;
1979
1980 PRE_INIT(BaseException)
1981 PRE_INIT(Exception)
1982 PRE_INIT(StandardError)
1983 PRE_INIT(TypeError)
1984 PRE_INIT(StopIteration)
1985 PRE_INIT(GeneratorExit)
1986 PRE_INIT(SystemExit)
1987 PRE_INIT(KeyboardInterrupt)
1988 PRE_INIT(ImportError)
1989 PRE_INIT(EnvironmentError)
1990 PRE_INIT(IOError)
1991 PRE_INIT(OSError)
1992#ifdef MS_WINDOWS
1993 PRE_INIT(WindowsError)
1994#endif
1995#ifdef __VMS
1996 PRE_INIT(VMSError)
1997#endif
1998 PRE_INIT(EOFError)
1999 PRE_INIT(RuntimeError)
2000 PRE_INIT(NotImplementedError)
2001 PRE_INIT(NameError)
2002 PRE_INIT(UnboundLocalError)
2003 PRE_INIT(AttributeError)
2004 PRE_INIT(SyntaxError)
2005 PRE_INIT(IndentationError)
2006 PRE_INIT(TabError)
2007 PRE_INIT(LookupError)
2008 PRE_INIT(IndexError)
2009 PRE_INIT(KeyError)
2010 PRE_INIT(ValueError)
2011 PRE_INIT(UnicodeError)
2012#ifdef Py_USING_UNICODE
2013 PRE_INIT(UnicodeEncodeError)
2014 PRE_INIT(UnicodeDecodeError)
2015 PRE_INIT(UnicodeTranslateError)
2016#endif
2017 PRE_INIT(AssertionError)
2018 PRE_INIT(ArithmeticError)
2019 PRE_INIT(FloatingPointError)
2020 PRE_INIT(OverflowError)
2021 PRE_INIT(ZeroDivisionError)
2022 PRE_INIT(SystemError)
2023 PRE_INIT(ReferenceError)
2024 PRE_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002025 PRE_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002026 PRE_INIT(Warning)
2027 PRE_INIT(UserWarning)
2028 PRE_INIT(DeprecationWarning)
2029 PRE_INIT(PendingDeprecationWarning)
2030 PRE_INIT(SyntaxWarning)
2031 PRE_INIT(RuntimeWarning)
2032 PRE_INIT(FutureWarning)
2033 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002034 PRE_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002035 PRE_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002036
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002037 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2038 (PyObject *)NULL, PYTHON_API_VERSION);
Richard Jones7b9558d2006-05-27 12:29:24 +00002039 if (m == NULL) return;
2040
2041 bltinmod = PyImport_ImportModule("__builtin__");
2042 if (bltinmod == NULL)
2043 Py_FatalError("exceptions bootstrapping error.");
2044 bdict = PyModule_GetDict(bltinmod);
2045 if (bdict == NULL)
2046 Py_FatalError("exceptions bootstrapping error.");
2047
2048 POST_INIT(BaseException)
2049 POST_INIT(Exception)
2050 POST_INIT(StandardError)
2051 POST_INIT(TypeError)
2052 POST_INIT(StopIteration)
2053 POST_INIT(GeneratorExit)
2054 POST_INIT(SystemExit)
2055 POST_INIT(KeyboardInterrupt)
2056 POST_INIT(ImportError)
2057 POST_INIT(EnvironmentError)
2058 POST_INIT(IOError)
2059 POST_INIT(OSError)
2060#ifdef MS_WINDOWS
2061 POST_INIT(WindowsError)
2062#endif
2063#ifdef __VMS
2064 POST_INIT(VMSError)
2065#endif
2066 POST_INIT(EOFError)
2067 POST_INIT(RuntimeError)
2068 POST_INIT(NotImplementedError)
2069 POST_INIT(NameError)
2070 POST_INIT(UnboundLocalError)
2071 POST_INIT(AttributeError)
2072 POST_INIT(SyntaxError)
2073 POST_INIT(IndentationError)
2074 POST_INIT(TabError)
2075 POST_INIT(LookupError)
2076 POST_INIT(IndexError)
2077 POST_INIT(KeyError)
2078 POST_INIT(ValueError)
2079 POST_INIT(UnicodeError)
2080#ifdef Py_USING_UNICODE
2081 POST_INIT(UnicodeEncodeError)
2082 POST_INIT(UnicodeDecodeError)
2083 POST_INIT(UnicodeTranslateError)
2084#endif
2085 POST_INIT(AssertionError)
2086 POST_INIT(ArithmeticError)
2087 POST_INIT(FloatingPointError)
2088 POST_INIT(OverflowError)
2089 POST_INIT(ZeroDivisionError)
2090 POST_INIT(SystemError)
2091 POST_INIT(ReferenceError)
2092 POST_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002093 POST_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002094 POST_INIT(Warning)
2095 POST_INIT(UserWarning)
2096 POST_INIT(DeprecationWarning)
2097 POST_INIT(PendingDeprecationWarning)
2098 POST_INIT(SyntaxWarning)
2099 POST_INIT(RuntimeWarning)
2100 POST_INIT(FutureWarning)
2101 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002102 POST_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002103 POST_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002104
2105 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2106 if (!PyExc_MemoryErrorInst)
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +00002107 Py_FatalError("Cannot pre-allocate MemoryError instance");
Richard Jones7b9558d2006-05-27 12:29:24 +00002108
Brett Cannon1e534b52007-09-07 04:18:30 +00002109 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2110 if (!PyExc_RecursionErrorInst)
2111 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2112 "recursion errors");
2113 else {
2114 PyBaseExceptionObject *err_inst =
2115 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2116 PyObject *args_tuple;
2117 PyObject *exc_message;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002118 exc_message = PyString_FromString("maximum recursion depth exceeded");
Brett Cannon1e534b52007-09-07 04:18:30 +00002119 if (!exc_message)
2120 Py_FatalError("cannot allocate argument for RuntimeError "
2121 "pre-allocation");
2122 args_tuple = PyTuple_Pack(1, exc_message);
2123 if (!args_tuple)
2124 Py_FatalError("cannot allocate tuple for RuntimeError "
2125 "pre-allocation");
2126 Py_DECREF(exc_message);
2127 if (BaseException_init(err_inst, args_tuple, NULL))
2128 Py_FatalError("init of pre-allocated RuntimeError failed");
2129 Py_DECREF(args_tuple);
2130 }
2131
Richard Jones7b9558d2006-05-27 12:29:24 +00002132 Py_DECREF(bltinmod);
2133}
2134
2135void
2136_PyExc_Fini(void)
2137{
Alexandre Vassalotti55bd1ef2009-06-12 18:56:57 +00002138 Py_CLEAR(PyExc_MemoryErrorInst);
2139 Py_CLEAR(PyExc_RecursionErrorInst);
Richard Jones7b9558d2006-05-27 12:29:24 +00002140}