blob: 48b47b06102747e214cfce232f2fdb90d2734d4f [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
12#define MAKE_IT_NONE(x) (x) = Py_None; Py_INCREF(Py_None);
13#define EXC_MODULE_NAME "exceptions."
14
Richard Jonesc5b2a2e2006-05-27 16:07:28 +000015/* NOTE: If the exception class hierarchy changes, don't forget to update
16 * Lib/test/exception_hierarchy.txt
17 */
18
19PyDoc_STRVAR(exceptions_doc, "Python's standard exception class hierarchy.\n\
20\n\
21Exceptions found here are defined both in the exceptions module and the\n\
22built-in namespace. It is recommended that user-defined exceptions\n\
23inherit from Exception. See the documentation for the exception\n\
24inheritance hierarchy.\n\
25");
26
Richard Jones7b9558d2006-05-27 12:29:24 +000027/*
28 * BaseException
29 */
30static PyObject *
31BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
32{
33 PyBaseExceptionObject *self;
34
35 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
Georg Brandlc02e1312007-04-11 17:16:24 +000036 if (!self)
37 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +000038 /* the dict is created on the fly in PyObject_GenericSetAttr */
39 self->message = self->dict = NULL;
40
Georg Brandld7e9f602007-08-21 06:03:43 +000041 self->args = PyTuple_New(0);
42 if (!self->args) {
Richard Jones7b9558d2006-05-27 12:29:24 +000043 Py_DECREF(self);
44 return NULL;
45 }
Georg Brandld7e9f602007-08-21 06:03:43 +000046
Gregory P. Smithdd96db62008-06-09 04:58:54 +000047 self->message = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +000048 if (!self->message) {
49 Py_DECREF(self);
50 return NULL;
51 }
Georg Brandld7e9f602007-08-21 06:03:43 +000052
Richard Jones7b9558d2006-05-27 12:29:24 +000053 return (PyObject *)self;
54}
55
56static int
57BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
58{
Christian Heimese93237d2007-12-19 02:37:44 +000059 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
Georg Brandlb0432bc2006-05-30 08:17:00 +000060 return -1;
61
Richard Jones7b9558d2006-05-27 12:29:24 +000062 Py_DECREF(self->args);
63 self->args = args;
64 Py_INCREF(self->args);
65
66 if (PyTuple_GET_SIZE(self->args) == 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +000067 Py_CLEAR(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000068 self->message = PyTuple_GET_ITEM(self->args, 0);
Michael W. Hudson22a80e72006-05-28 15:51:40 +000069 Py_INCREF(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000070 }
71 return 0;
72}
73
Michael W. Hudson96495ee2006-05-28 17:40:29 +000074static int
Richard Jones7b9558d2006-05-27 12:29:24 +000075BaseException_clear(PyBaseExceptionObject *self)
76{
77 Py_CLEAR(self->dict);
78 Py_CLEAR(self->args);
79 Py_CLEAR(self->message);
80 return 0;
81}
82
83static void
84BaseException_dealloc(PyBaseExceptionObject *self)
85{
Georg Brandl38f62372006-09-06 06:50:05 +000086 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +000087 BaseException_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +000088 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +000089}
90
Michael W. Hudson96495ee2006-05-28 17:40:29 +000091static int
Richard Jones7b9558d2006-05-27 12:29:24 +000092BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
93{
Michael W. Hudson22a80e72006-05-28 15:51:40 +000094 Py_VISIT(self->dict);
Richard Jones7b9558d2006-05-27 12:29:24 +000095 Py_VISIT(self->args);
96 Py_VISIT(self->message);
97 return 0;
98}
99
100static PyObject *
101BaseException_str(PyBaseExceptionObject *self)
102{
103 PyObject *out;
104
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000105 switch (PyTuple_GET_SIZE(self->args)) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000106 case 0:
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000107 out = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +0000108 break;
109 case 1:
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000110 out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +0000111 break;
Richard Jones7b9558d2006-05-27 12:29:24 +0000112 default:
113 out = PyObject_Str(self->args);
114 break;
115 }
116
117 return out;
118}
119
120static PyObject *
121BaseException_repr(PyBaseExceptionObject *self)
122{
Richard Jones7b9558d2006-05-27 12:29:24 +0000123 PyObject *repr_suffix;
124 PyObject *repr;
125 char *name;
126 char *dot;
127
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000128 repr_suffix = PyObject_Repr(self->args);
129 if (!repr_suffix)
Richard Jones7b9558d2006-05-27 12:29:24 +0000130 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000131
Christian Heimese93237d2007-12-19 02:37:44 +0000132 name = (char *)Py_TYPE(self)->tp_name;
Richard Jones7b9558d2006-05-27 12:29:24 +0000133 dot = strrchr(name, '.');
134 if (dot != NULL) name = dot+1;
135
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000136 repr = PyString_FromString(name);
Richard Jones7b9558d2006-05-27 12:29:24 +0000137 if (!repr) {
138 Py_DECREF(repr_suffix);
139 return NULL;
140 }
141
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000142 PyString_ConcatAndDel(&repr, repr_suffix);
Richard Jones7b9558d2006-05-27 12:29:24 +0000143 return repr;
144}
145
146/* Pickling support */
147static PyObject *
148BaseException_reduce(PyBaseExceptionObject *self)
149{
Georg Brandlddba4732006-05-30 07:04:55 +0000150 if (self->args && self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000151 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000152 else
Christian Heimese93237d2007-12-19 02:37:44 +0000153 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000154}
155
Georg Brandl85ac8502006-06-01 06:39:19 +0000156/*
157 * Needed for backward compatibility, since exceptions used to store
158 * all their attributes in the __dict__. Code is taken from cPickle's
159 * load_build function.
160 */
161static PyObject *
162BaseException_setstate(PyObject *self, PyObject *state)
163{
164 PyObject *d_key, *d_value;
165 Py_ssize_t i = 0;
166
167 if (state != Py_None) {
168 if (!PyDict_Check(state)) {
169 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
170 return NULL;
171 }
172 while (PyDict_Next(state, &i, &d_key, &d_value)) {
173 if (PyObject_SetAttr(self, d_key, d_value) < 0)
174 return NULL;
175 }
176 }
177 Py_RETURN_NONE;
178}
Richard Jones7b9558d2006-05-27 12:29:24 +0000179
Richard Jones7b9558d2006-05-27 12:29:24 +0000180
181static PyMethodDef BaseException_methods[] = {
182 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Georg Brandl85ac8502006-06-01 06:39:19 +0000183 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Richard Jones7b9558d2006-05-27 12:29:24 +0000184 {NULL, NULL, 0, NULL},
185};
186
187
188
189static PyObject *
190BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
191{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000192 if (PyErr_WarnPy3k("__getitem__ not supported for exception "
193 "classes in 3.x; use args attribute", 1) < 0)
194 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000195 return PySequence_GetItem(self->args, index);
196}
197
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000198static PyObject *
199BaseException_getslice(PyBaseExceptionObject *self,
200 Py_ssize_t start, Py_ssize_t stop)
201{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000202 if (PyErr_WarnPy3k("__getslice__ not supported for exception "
203 "classes in 3.x; use args attribute", 1) < 0)
204 return NULL;
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000205 return PySequence_GetSlice(self->args, start, stop);
206}
207
Richard Jones7b9558d2006-05-27 12:29:24 +0000208static PySequenceMethods BaseException_as_sequence = {
209 0, /* sq_length; */
210 0, /* sq_concat; */
211 0, /* sq_repeat; */
212 (ssizeargfunc)BaseException_getitem, /* sq_item; */
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000213 (ssizessizeargfunc)BaseException_getslice, /* sq_slice; */
Richard Jones7b9558d2006-05-27 12:29:24 +0000214 0, /* sq_ass_item; */
215 0, /* sq_ass_slice; */
216 0, /* sq_contains; */
217 0, /* sq_inplace_concat; */
218 0 /* sq_inplace_repeat; */
219};
220
Richard Jones7b9558d2006-05-27 12:29:24 +0000221static PyObject *
222BaseException_get_dict(PyBaseExceptionObject *self)
223{
224 if (self->dict == NULL) {
225 self->dict = PyDict_New();
226 if (!self->dict)
227 return NULL;
228 }
229 Py_INCREF(self->dict);
230 return self->dict;
231}
232
233static int
234BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
235{
236 if (val == NULL) {
237 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
238 return -1;
239 }
240 if (!PyDict_Check(val)) {
241 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
242 return -1;
243 }
244 Py_CLEAR(self->dict);
245 Py_INCREF(val);
246 self->dict = val;
247 return 0;
248}
249
250static PyObject *
251BaseException_get_args(PyBaseExceptionObject *self)
252{
253 if (self->args == NULL) {
254 Py_INCREF(Py_None);
255 return Py_None;
256 }
257 Py_INCREF(self->args);
258 return self->args;
259}
260
261static int
262BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
263{
264 PyObject *seq;
265 if (val == NULL) {
266 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
267 return -1;
268 }
269 seq = PySequence_Tuple(val);
270 if (!seq) return -1;
Georg Brandlc7c51142006-05-29 09:46:51 +0000271 Py_CLEAR(self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000272 self->args = seq;
273 return 0;
274}
275
Brett Cannon229cee22007-05-05 01:34:02 +0000276static PyObject *
277BaseException_get_message(PyBaseExceptionObject *self)
278{
279 int ret;
280 ret = PyErr_WarnEx(PyExc_DeprecationWarning,
281 "BaseException.message has been deprecated as "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000282 "of Python 2.6", 1);
283 if (ret < 0)
Brett Cannon229cee22007-05-05 01:34:02 +0000284 return NULL;
285
286 Py_INCREF(self->message);
287 return self->message;
288}
289
290static int
291BaseException_set_message(PyBaseExceptionObject *self, PyObject *val)
292{
293 int ret;
294 ret = PyErr_WarnEx(PyExc_DeprecationWarning,
295 "BaseException.message has been deprecated as "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000296 "of Python 2.6", 1);
297 if (ret < 0)
Brett Cannon229cee22007-05-05 01:34:02 +0000298 return -1;
299 Py_INCREF(val);
300 Py_DECREF(self->message);
301 self->message = val;
302 return 0;
303}
304
Richard Jones7b9558d2006-05-27 12:29:24 +0000305static PyGetSetDef BaseException_getset[] = {
306 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
307 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Brett Cannon229cee22007-05-05 01:34:02 +0000308 {"message", (getter)BaseException_get_message,
309 (setter)BaseException_set_message},
Richard Jones7b9558d2006-05-27 12:29:24 +0000310 {NULL},
311};
312
313
314static PyTypeObject _PyExc_BaseException = {
315 PyObject_HEAD_INIT(NULL)
316 0, /*ob_size*/
317 EXC_MODULE_NAME "BaseException", /*tp_name*/
318 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
319 0, /*tp_itemsize*/
320 (destructor)BaseException_dealloc, /*tp_dealloc*/
321 0, /*tp_print*/
322 0, /*tp_getattr*/
323 0, /*tp_setattr*/
324 0, /* tp_compare; */
325 (reprfunc)BaseException_repr, /*tp_repr*/
326 0, /*tp_as_number*/
327 &BaseException_as_sequence, /*tp_as_sequence*/
328 0, /*tp_as_mapping*/
329 0, /*tp_hash */
330 0, /*tp_call*/
331 (reprfunc)BaseException_str, /*tp_str*/
332 PyObject_GenericGetAttr, /*tp_getattro*/
333 PyObject_GenericSetAttr, /*tp_setattro*/
334 0, /*tp_as_buffer*/
Neal Norwitzee3a1b52007-02-25 19:44:48 +0000335 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
336 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Richard Jones7b9558d2006-05-27 12:29:24 +0000337 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
338 (traverseproc)BaseException_traverse, /* tp_traverse */
339 (inquiry)BaseException_clear, /* tp_clear */
340 0, /* tp_richcompare */
341 0, /* tp_weaklistoffset */
342 0, /* tp_iter */
343 0, /* tp_iternext */
344 BaseException_methods, /* tp_methods */
Brett Cannon229cee22007-05-05 01:34:02 +0000345 0, /* tp_members */
Richard Jones7b9558d2006-05-27 12:29:24 +0000346 BaseException_getset, /* tp_getset */
347 0, /* tp_base */
348 0, /* tp_dict */
349 0, /* tp_descr_get */
350 0, /* tp_descr_set */
351 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
352 (initproc)BaseException_init, /* tp_init */
353 0, /* tp_alloc */
354 BaseException_new, /* tp_new */
355};
356/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
357from the previous implmentation and also allowing Python objects to be used
358in the API */
359PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
360
Richard Jones2d555b32006-05-27 16:15:11 +0000361/* note these macros omit the last semicolon so the macro invocation may
362 * include it and not look strange.
363 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000364#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
365static PyTypeObject _PyExc_ ## EXCNAME = { \
366 PyObject_HEAD_INIT(NULL) \
367 0, \
368 EXC_MODULE_NAME # EXCNAME, \
369 sizeof(PyBaseExceptionObject), \
370 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
371 0, 0, 0, 0, 0, 0, 0, \
372 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
373 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
374 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
375 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
376 (initproc)BaseException_init, 0, BaseException_new,\
377}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000378PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000379
380#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
381static PyTypeObject _PyExc_ ## EXCNAME = { \
382 PyObject_HEAD_INIT(NULL) \
383 0, \
384 EXC_MODULE_NAME # EXCNAME, \
385 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000386 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000387 0, 0, 0, 0, 0, \
388 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000389 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
390 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000391 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000392 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000393}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000394PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000395
396#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
397static PyTypeObject _PyExc_ ## EXCNAME = { \
398 PyObject_HEAD_INIT(NULL) \
399 0, \
400 EXC_MODULE_NAME # EXCNAME, \
401 sizeof(Py ## EXCSTORE ## Object), 0, \
402 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
403 (reprfunc)EXCSTR, 0, 0, 0, \
404 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
405 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
406 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
407 EXCMEMBERS, 0, &_ ## EXCBASE, \
408 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000409 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000410}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000411PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000412
413
414/*
415 * Exception extends BaseException
416 */
417SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000418 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000419
420
421/*
422 * StandardError extends Exception
423 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000424SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000425 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000426 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000427
428
429/*
430 * TypeError extends StandardError
431 */
432SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000433 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000434
435
436/*
437 * StopIteration extends Exception
438 */
439SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000440 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000441
442
443/*
Christian Heimes44eeaec2007-12-03 20:01:02 +0000444 * GeneratorExit extends BaseException
Richard Jones7b9558d2006-05-27 12:29:24 +0000445 */
Christian Heimes44eeaec2007-12-03 20:01:02 +0000446SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000447 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000448
449
450/*
451 * SystemExit extends BaseException
452 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000453
454static int
455SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
456{
457 Py_ssize_t size = PyTuple_GET_SIZE(args);
458
459 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
460 return -1;
461
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000462 if (size == 0)
463 return 0;
464 Py_CLEAR(self->code);
Richard Jones7b9558d2006-05-27 12:29:24 +0000465 if (size == 1)
466 self->code = PyTuple_GET_ITEM(args, 0);
467 else if (size > 1)
468 self->code = args;
469 Py_INCREF(self->code);
470 return 0;
471}
472
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000473static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000474SystemExit_clear(PySystemExitObject *self)
475{
476 Py_CLEAR(self->code);
477 return BaseException_clear((PyBaseExceptionObject *)self);
478}
479
480static void
481SystemExit_dealloc(PySystemExitObject *self)
482{
Georg Brandl38f62372006-09-06 06:50:05 +0000483 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000484 SystemExit_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000485 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000486}
487
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000488static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000489SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
490{
491 Py_VISIT(self->code);
492 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
493}
494
495static PyMemberDef SystemExit_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000496 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
497 PyDoc_STR("exception code")},
498 {NULL} /* Sentinel */
499};
500
501ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
502 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000503 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000504
505/*
506 * KeyboardInterrupt extends BaseException
507 */
508SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000509 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000510
511
512/*
513 * ImportError extends StandardError
514 */
515SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000516 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000517
518
519/*
520 * EnvironmentError extends StandardError
521 */
522
Richard Jones7b9558d2006-05-27 12:29:24 +0000523/* Where a function has a single filename, such as open() or some
524 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
525 * called, giving a third argument which is the filename. But, so
526 * that old code using in-place unpacking doesn't break, e.g.:
527 *
528 * except IOError, (errno, strerror):
529 *
530 * we hack args so that it only contains two items. This also
531 * means we need our own __str__() which prints out the filename
532 * when it was supplied.
533 */
534static int
535EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
536 PyObject *kwds)
537{
538 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
539 PyObject *subslice = NULL;
540
541 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
542 return -1;
543
Georg Brandl3267d282006-09-30 09:03:42 +0000544 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000545 return 0;
546 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000547
548 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000549 &myerrno, &strerror, &filename)) {
550 return -1;
551 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000552 Py_CLEAR(self->myerrno); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000553 self->myerrno = myerrno;
554 Py_INCREF(self->myerrno);
555
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000556 Py_CLEAR(self->strerror); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000557 self->strerror = strerror;
558 Py_INCREF(self->strerror);
559
560 /* self->filename will remain Py_None otherwise */
561 if (filename != NULL) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000562 Py_CLEAR(self->filename); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000563 self->filename = filename;
564 Py_INCREF(self->filename);
565
566 subslice = PyTuple_GetSlice(args, 0, 2);
567 if (!subslice)
568 return -1;
569
570 Py_DECREF(self->args); /* replacing args */
571 self->args = subslice;
572 }
573 return 0;
574}
575
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000576static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000577EnvironmentError_clear(PyEnvironmentErrorObject *self)
578{
579 Py_CLEAR(self->myerrno);
580 Py_CLEAR(self->strerror);
581 Py_CLEAR(self->filename);
582 return BaseException_clear((PyBaseExceptionObject *)self);
583}
584
585static void
586EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
587{
Georg Brandl38f62372006-09-06 06:50:05 +0000588 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000589 EnvironmentError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000590 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000591}
592
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000593static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000594EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
595 void *arg)
596{
597 Py_VISIT(self->myerrno);
598 Py_VISIT(self->strerror);
599 Py_VISIT(self->filename);
600 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
601}
602
603static PyObject *
604EnvironmentError_str(PyEnvironmentErrorObject *self)
605{
606 PyObject *rtnval = NULL;
607
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000608 if (self->filename) {
609 PyObject *fmt;
610 PyObject *repr;
611 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000612
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000613 fmt = PyString_FromString("[Errno %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000614 if (!fmt)
615 return NULL;
616
617 repr = PyObject_Repr(self->filename);
618 if (!repr) {
619 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000620 return NULL;
621 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000622 tuple = PyTuple_New(3);
623 if (!tuple) {
624 Py_DECREF(repr);
625 Py_DECREF(fmt);
626 return NULL;
627 }
628
629 if (self->myerrno) {
630 Py_INCREF(self->myerrno);
631 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
632 }
633 else {
634 Py_INCREF(Py_None);
635 PyTuple_SET_ITEM(tuple, 0, Py_None);
636 }
637 if (self->strerror) {
638 Py_INCREF(self->strerror);
639 PyTuple_SET_ITEM(tuple, 1, self->strerror);
640 }
641 else {
642 Py_INCREF(Py_None);
643 PyTuple_SET_ITEM(tuple, 1, Py_None);
644 }
645
Richard Jones7b9558d2006-05-27 12:29:24 +0000646 PyTuple_SET_ITEM(tuple, 2, repr);
647
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000648 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000649
650 Py_DECREF(fmt);
651 Py_DECREF(tuple);
652 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000653 else if (self->myerrno && self->strerror) {
654 PyObject *fmt;
655 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000656
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000657 fmt = PyString_FromString("[Errno %s] %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000658 if (!fmt)
659 return NULL;
660
661 tuple = PyTuple_New(2);
662 if (!tuple) {
663 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000664 return NULL;
665 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000666
667 if (self->myerrno) {
668 Py_INCREF(self->myerrno);
669 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
670 }
671 else {
672 Py_INCREF(Py_None);
673 PyTuple_SET_ITEM(tuple, 0, Py_None);
674 }
675 if (self->strerror) {
676 Py_INCREF(self->strerror);
677 PyTuple_SET_ITEM(tuple, 1, self->strerror);
678 }
679 else {
680 Py_INCREF(Py_None);
681 PyTuple_SET_ITEM(tuple, 1, Py_None);
682 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000683
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000684 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000685
686 Py_DECREF(fmt);
687 Py_DECREF(tuple);
688 }
689 else
690 rtnval = BaseException_str((PyBaseExceptionObject *)self);
691
692 return rtnval;
693}
694
695static PyMemberDef EnvironmentError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000696 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000697 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000698 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000699 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000700 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000701 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000702 {NULL} /* Sentinel */
703};
704
705
706static PyObject *
707EnvironmentError_reduce(PyEnvironmentErrorObject *self)
708{
709 PyObject *args = self->args;
710 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000711
Richard Jones7b9558d2006-05-27 12:29:24 +0000712 /* self->args is only the first two real arguments if there was a
713 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000714 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000715 args = PyTuple_New(3);
716 if (!args) return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000717
718 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000719 Py_INCREF(tmp);
720 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000721
722 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000723 Py_INCREF(tmp);
724 PyTuple_SET_ITEM(args, 1, tmp);
725
726 Py_INCREF(self->filename);
727 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000728 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000729 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000730
731 if (self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000732 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000733 else
Christian Heimese93237d2007-12-19 02:37:44 +0000734 res = PyTuple_Pack(2, Py_TYPE(self), args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000735 Py_DECREF(args);
736 return res;
737}
738
739
740static PyMethodDef EnvironmentError_methods[] = {
741 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
742 {NULL}
743};
744
745ComplexExtendsException(PyExc_StandardError, EnvironmentError,
746 EnvironmentError, EnvironmentError_dealloc,
747 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000748 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000749 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000750
751
752/*
753 * IOError extends EnvironmentError
754 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000755MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000756 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000757
758
759/*
760 * OSError extends EnvironmentError
761 */
762MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000763 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000764
765
766/*
767 * WindowsError extends OSError
768 */
769#ifdef MS_WINDOWS
770#include "errmap.h"
771
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000772static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000773WindowsError_clear(PyWindowsErrorObject *self)
774{
775 Py_CLEAR(self->myerrno);
776 Py_CLEAR(self->strerror);
777 Py_CLEAR(self->filename);
778 Py_CLEAR(self->winerror);
779 return BaseException_clear((PyBaseExceptionObject *)self);
780}
781
782static void
783WindowsError_dealloc(PyWindowsErrorObject *self)
784{
Georg Brandl38f62372006-09-06 06:50:05 +0000785 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000786 WindowsError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000787 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000788}
789
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000790static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000791WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
792{
793 Py_VISIT(self->myerrno);
794 Py_VISIT(self->strerror);
795 Py_VISIT(self->filename);
796 Py_VISIT(self->winerror);
797 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
798}
799
Richard Jones7b9558d2006-05-27 12:29:24 +0000800static int
801WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
802{
803 PyObject *o_errcode = NULL;
804 long errcode;
805 long posix_errno;
806
807 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
808 == -1)
809 return -1;
810
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000811 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000812 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000813
814 /* Set errno to the POSIX errno, and winerror to the Win32
815 error code. */
816 errcode = PyInt_AsLong(self->myerrno);
817 if (errcode == -1 && PyErr_Occurred())
818 return -1;
819 posix_errno = winerror_to_errno(errcode);
820
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000821 Py_CLEAR(self->winerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000822 self->winerror = self->myerrno;
823
824 o_errcode = PyInt_FromLong(posix_errno);
825 if (!o_errcode)
826 return -1;
827
828 self->myerrno = o_errcode;
829
830 return 0;
831}
832
833
834static PyObject *
835WindowsError_str(PyWindowsErrorObject *self)
836{
Richard Jones7b9558d2006-05-27 12:29:24 +0000837 PyObject *rtnval = NULL;
838
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000839 if (self->filename) {
840 PyObject *fmt;
841 PyObject *repr;
842 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000843
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000844 fmt = PyString_FromString("[Error %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000845 if (!fmt)
846 return NULL;
847
848 repr = PyObject_Repr(self->filename);
849 if (!repr) {
850 Py_DECREF(fmt);
851 return NULL;
852 }
853 tuple = PyTuple_New(3);
854 if (!tuple) {
855 Py_DECREF(repr);
856 Py_DECREF(fmt);
857 return NULL;
858 }
859
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000860 if (self->winerror) {
861 Py_INCREF(self->winerror);
862 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000863 }
864 else {
865 Py_INCREF(Py_None);
866 PyTuple_SET_ITEM(tuple, 0, Py_None);
867 }
868 if (self->strerror) {
869 Py_INCREF(self->strerror);
870 PyTuple_SET_ITEM(tuple, 1, self->strerror);
871 }
872 else {
873 Py_INCREF(Py_None);
874 PyTuple_SET_ITEM(tuple, 1, Py_None);
875 }
876
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000877 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000878
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000879 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000880
881 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000882 Py_DECREF(tuple);
883 }
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000884 else if (self->winerror && self->strerror) {
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000885 PyObject *fmt;
886 PyObject *tuple;
887
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000888 fmt = PyString_FromString("[Error %s] %s");
Richard Jones7b9558d2006-05-27 12:29:24 +0000889 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000890 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000891
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000892 tuple = PyTuple_New(2);
893 if (!tuple) {
894 Py_DECREF(fmt);
895 return NULL;
896 }
897
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000898 if (self->winerror) {
899 Py_INCREF(self->winerror);
900 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000901 }
902 else {
903 Py_INCREF(Py_None);
904 PyTuple_SET_ITEM(tuple, 0, Py_None);
905 }
906 if (self->strerror) {
907 Py_INCREF(self->strerror);
908 PyTuple_SET_ITEM(tuple, 1, self->strerror);
909 }
910 else {
911 Py_INCREF(Py_None);
912 PyTuple_SET_ITEM(tuple, 1, Py_None);
913 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000914
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000915 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000916
917 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000918 Py_DECREF(tuple);
919 }
920 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000921 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000922
Richard Jones7b9558d2006-05-27 12:29:24 +0000923 return rtnval;
924}
925
926static PyMemberDef WindowsError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000927 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000928 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000929 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000930 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000931 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000932 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000933 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000934 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000935 {NULL} /* Sentinel */
936};
937
Richard Jones2d555b32006-05-27 16:15:11 +0000938ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
939 WindowsError_dealloc, 0, WindowsError_members,
940 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000941
942#endif /* MS_WINDOWS */
943
944
945/*
946 * VMSError extends OSError (I think)
947 */
948#ifdef __VMS
949MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +0000950 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000951#endif
952
953
954/*
955 * EOFError extends StandardError
956 */
957SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +0000958 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000959
960
961/*
962 * RuntimeError extends StandardError
963 */
964SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000965 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000966
967
968/*
969 * NotImplementedError extends RuntimeError
970 */
971SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +0000972 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000973
974/*
975 * NameError extends StandardError
976 */
977SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +0000978 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000979
980/*
981 * UnboundLocalError extends NameError
982 */
983SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +0000984 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000985
986/*
987 * AttributeError extends StandardError
988 */
989SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000990 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000991
992
993/*
994 * SyntaxError extends StandardError
995 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000996
997static int
998SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
999{
1000 PyObject *info = NULL;
1001 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
1002
1003 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1004 return -1;
1005
1006 if (lenargs >= 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001007 Py_CLEAR(self->msg);
Richard Jones7b9558d2006-05-27 12:29:24 +00001008 self->msg = PyTuple_GET_ITEM(args, 0);
1009 Py_INCREF(self->msg);
1010 }
1011 if (lenargs == 2) {
1012 info = PyTuple_GET_ITEM(args, 1);
1013 info = PySequence_Tuple(info);
1014 if (!info) return -1;
1015
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001016 if (PyTuple_GET_SIZE(info) != 4) {
1017 /* not a very good error message, but it's what Python 2.4 gives */
1018 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
1019 Py_DECREF(info);
1020 return -1;
1021 }
1022
1023 Py_CLEAR(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001024 self->filename = PyTuple_GET_ITEM(info, 0);
1025 Py_INCREF(self->filename);
1026
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001027 Py_CLEAR(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001028 self->lineno = PyTuple_GET_ITEM(info, 1);
1029 Py_INCREF(self->lineno);
1030
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001031 Py_CLEAR(self->offset);
Richard Jones7b9558d2006-05-27 12:29:24 +00001032 self->offset = PyTuple_GET_ITEM(info, 2);
1033 Py_INCREF(self->offset);
1034
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001035 Py_CLEAR(self->text);
Richard Jones7b9558d2006-05-27 12:29:24 +00001036 self->text = PyTuple_GET_ITEM(info, 3);
1037 Py_INCREF(self->text);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001038
1039 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001040 }
1041 return 0;
1042}
1043
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001044static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001045SyntaxError_clear(PySyntaxErrorObject *self)
1046{
1047 Py_CLEAR(self->msg);
1048 Py_CLEAR(self->filename);
1049 Py_CLEAR(self->lineno);
1050 Py_CLEAR(self->offset);
1051 Py_CLEAR(self->text);
1052 Py_CLEAR(self->print_file_and_line);
1053 return BaseException_clear((PyBaseExceptionObject *)self);
1054}
1055
1056static void
1057SyntaxError_dealloc(PySyntaxErrorObject *self)
1058{
Georg Brandl38f62372006-09-06 06:50:05 +00001059 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001060 SyntaxError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001061 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001062}
1063
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001064static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001065SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1066{
1067 Py_VISIT(self->msg);
1068 Py_VISIT(self->filename);
1069 Py_VISIT(self->lineno);
1070 Py_VISIT(self->offset);
1071 Py_VISIT(self->text);
1072 Py_VISIT(self->print_file_and_line);
1073 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1074}
1075
1076/* This is called "my_basename" instead of just "basename" to avoid name
1077 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1078 defined, and Python does define that. */
1079static char *
1080my_basename(char *name)
1081{
1082 char *cp = name;
1083 char *result = name;
1084
1085 if (name == NULL)
1086 return "???";
1087 while (*cp != '\0') {
1088 if (*cp == SEP)
1089 result = cp + 1;
1090 ++cp;
1091 }
1092 return result;
1093}
1094
1095
1096static PyObject *
1097SyntaxError_str(PySyntaxErrorObject *self)
1098{
1099 PyObject *str;
1100 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001101 int have_filename = 0;
1102 int have_lineno = 0;
1103 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001104 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001105
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001106 if (self->msg)
1107 str = PyObject_Str(self->msg);
1108 else
1109 str = PyObject_Str(Py_None);
Georg Brandl43ab1002006-05-28 20:57:09 +00001110 if (!str) return NULL;
1111 /* Don't fiddle with non-string return (shouldn't happen anyway) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001112 if (!PyString_Check(str)) return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001113
1114 /* XXX -- do all the additional formatting with filename and
1115 lineno here */
1116
Georg Brandl43ab1002006-05-28 20:57:09 +00001117 have_filename = (self->filename != NULL) &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001118 PyString_Check(self->filename);
Georg Brandl43ab1002006-05-28 20:57:09 +00001119 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001120
Georg Brandl43ab1002006-05-28 20:57:09 +00001121 if (!have_filename && !have_lineno)
1122 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001123
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001124 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001125 if (have_filename)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001126 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001127
Georg Brandl43ab1002006-05-28 20:57:09 +00001128 buffer = PyMem_MALLOC(bufsize);
1129 if (buffer == NULL)
1130 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001131
Georg Brandl43ab1002006-05-28 20:57:09 +00001132 if (have_filename && have_lineno)
1133 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001134 PyString_AS_STRING(str),
1135 my_basename(PyString_AS_STRING(self->filename)),
Georg Brandl43ab1002006-05-28 20:57:09 +00001136 PyInt_AsLong(self->lineno));
1137 else if (have_filename)
1138 PyOS_snprintf(buffer, bufsize, "%s (%s)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001139 PyString_AS_STRING(str),
1140 my_basename(PyString_AS_STRING(self->filename)));
Georg Brandl43ab1002006-05-28 20:57:09 +00001141 else /* only have_lineno */
1142 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001143 PyString_AS_STRING(str),
Georg Brandl43ab1002006-05-28 20:57:09 +00001144 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001145
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001146 result = PyString_FromString(buffer);
Georg Brandl43ab1002006-05-28 20:57:09 +00001147 PyMem_FREE(buffer);
1148
1149 if (result == NULL)
1150 result = str;
1151 else
1152 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001153 return result;
1154}
1155
1156static PyMemberDef SyntaxError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001157 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1158 PyDoc_STR("exception msg")},
1159 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1160 PyDoc_STR("exception filename")},
1161 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1162 PyDoc_STR("exception lineno")},
1163 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1164 PyDoc_STR("exception offset")},
1165 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1166 PyDoc_STR("exception text")},
1167 {"print_file_and_line", T_OBJECT,
1168 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1169 PyDoc_STR("exception print_file_and_line")},
1170 {NULL} /* Sentinel */
1171};
1172
1173ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1174 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001175 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001176
1177
1178/*
1179 * IndentationError extends SyntaxError
1180 */
1181MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001182 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001183
1184
1185/*
1186 * TabError extends IndentationError
1187 */
1188MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001189 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001190
1191
1192/*
1193 * LookupError extends StandardError
1194 */
1195SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001196 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001197
1198
1199/*
1200 * IndexError extends LookupError
1201 */
1202SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001203 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001204
1205
1206/*
1207 * KeyError extends LookupError
1208 */
1209static PyObject *
1210KeyError_str(PyBaseExceptionObject *self)
1211{
1212 /* If args is a tuple of exactly one item, apply repr to args[0].
1213 This is done so that e.g. the exception raised by {}[''] prints
1214 KeyError: ''
1215 rather than the confusing
1216 KeyError
1217 alone. The downside is that if KeyError is raised with an explanatory
1218 string, that string will be displayed in quotes. Too bad.
1219 If args is anything else, use the default BaseException__str__().
1220 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001221 if (PyTuple_GET_SIZE(self->args) == 1) {
1222 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001223 }
1224 return BaseException_str(self);
1225}
1226
1227ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001228 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001229
1230
1231/*
1232 * ValueError extends StandardError
1233 */
1234SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001235 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001236
1237/*
1238 * UnicodeError extends ValueError
1239 */
1240
1241SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001242 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001243
1244#ifdef Py_USING_UNICODE
Richard Jones7b9558d2006-05-27 12:29:24 +00001245static PyObject *
1246get_string(PyObject *attr, const char *name)
1247{
1248 if (!attr) {
1249 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1250 return NULL;
1251 }
1252
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001253 if (!PyString_Check(attr)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001254 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1255 return NULL;
1256 }
1257 Py_INCREF(attr);
1258 return attr;
1259}
1260
1261
1262static int
1263set_string(PyObject **attr, const char *value)
1264{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001265 PyObject *obj = PyString_FromString(value);
Richard Jones7b9558d2006-05-27 12:29:24 +00001266 if (!obj)
1267 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001268 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001269 *attr = obj;
1270 return 0;
1271}
1272
1273
1274static PyObject *
1275get_unicode(PyObject *attr, const char *name)
1276{
1277 if (!attr) {
1278 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1279 return NULL;
1280 }
1281
1282 if (!PyUnicode_Check(attr)) {
1283 PyErr_Format(PyExc_TypeError,
1284 "%.200s attribute must be unicode", name);
1285 return NULL;
1286 }
1287 Py_INCREF(attr);
1288 return attr;
1289}
1290
1291PyObject *
1292PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1293{
1294 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1295}
1296
1297PyObject *
1298PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1299{
1300 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1301}
1302
1303PyObject *
1304PyUnicodeEncodeError_GetObject(PyObject *exc)
1305{
1306 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1307}
1308
1309PyObject *
1310PyUnicodeDecodeError_GetObject(PyObject *exc)
1311{
1312 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1313}
1314
1315PyObject *
1316PyUnicodeTranslateError_GetObject(PyObject *exc)
1317{
1318 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1319}
1320
1321int
1322PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1323{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001324 Py_ssize_t size;
1325 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1326 "object");
1327 if (!obj)
1328 return -1;
1329 *start = ((PyUnicodeErrorObject *)exc)->start;
1330 size = PyUnicode_GET_SIZE(obj);
1331 if (*start<0)
1332 *start = 0; /*XXX check for values <0*/
1333 if (*start>=size)
1334 *start = size-1;
1335 Py_DECREF(obj);
1336 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001337}
1338
1339
1340int
1341PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1342{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001343 Py_ssize_t size;
1344 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1345 "object");
1346 if (!obj)
1347 return -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001348 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001349 *start = ((PyUnicodeErrorObject *)exc)->start;
1350 if (*start<0)
1351 *start = 0;
1352 if (*start>=size)
1353 *start = size-1;
1354 Py_DECREF(obj);
1355 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001356}
1357
1358
1359int
1360PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1361{
1362 return PyUnicodeEncodeError_GetStart(exc, start);
1363}
1364
1365
1366int
1367PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1368{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001369 ((PyUnicodeErrorObject *)exc)->start = start;
1370 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001371}
1372
1373
1374int
1375PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1376{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001377 ((PyUnicodeErrorObject *)exc)->start = start;
1378 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001379}
1380
1381
1382int
1383PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1384{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001385 ((PyUnicodeErrorObject *)exc)->start = start;
1386 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001387}
1388
1389
1390int
1391PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1392{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001393 Py_ssize_t size;
1394 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1395 "object");
1396 if (!obj)
1397 return -1;
1398 *end = ((PyUnicodeErrorObject *)exc)->end;
1399 size = PyUnicode_GET_SIZE(obj);
1400 if (*end<1)
1401 *end = 1;
1402 if (*end>size)
1403 *end = size;
1404 Py_DECREF(obj);
1405 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001406}
1407
1408
1409int
1410PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1411{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001412 Py_ssize_t size;
1413 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1414 "object");
1415 if (!obj)
1416 return -1;
1417 *end = ((PyUnicodeErrorObject *)exc)->end;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001418 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001419 if (*end<1)
1420 *end = 1;
1421 if (*end>size)
1422 *end = size;
1423 Py_DECREF(obj);
1424 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001425}
1426
1427
1428int
1429PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1430{
1431 return PyUnicodeEncodeError_GetEnd(exc, start);
1432}
1433
1434
1435int
1436PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1437{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001438 ((PyUnicodeErrorObject *)exc)->end = end;
1439 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001440}
1441
1442
1443int
1444PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1445{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001446 ((PyUnicodeErrorObject *)exc)->end = end;
1447 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001448}
1449
1450
1451int
1452PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1453{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001454 ((PyUnicodeErrorObject *)exc)->end = end;
1455 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001456}
1457
1458PyObject *
1459PyUnicodeEncodeError_GetReason(PyObject *exc)
1460{
1461 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1462}
1463
1464
1465PyObject *
1466PyUnicodeDecodeError_GetReason(PyObject *exc)
1467{
1468 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1469}
1470
1471
1472PyObject *
1473PyUnicodeTranslateError_GetReason(PyObject *exc)
1474{
1475 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1476}
1477
1478
1479int
1480PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1481{
1482 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1483}
1484
1485
1486int
1487PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1488{
1489 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1490}
1491
1492
1493int
1494PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1495{
1496 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1497}
1498
1499
Richard Jones7b9558d2006-05-27 12:29:24 +00001500static int
1501UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1502 PyTypeObject *objecttype)
1503{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001504 Py_CLEAR(self->encoding);
1505 Py_CLEAR(self->object);
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001506 Py_CLEAR(self->reason);
1507
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001508 if (!PyArg_ParseTuple(args, "O!O!nnO!",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001509 &PyString_Type, &self->encoding,
Richard Jones7b9558d2006-05-27 12:29:24 +00001510 objecttype, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001511 &self->start,
1512 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001513 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001514 self->encoding = self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001515 return -1;
1516 }
1517
1518 Py_INCREF(self->encoding);
1519 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001520 Py_INCREF(self->reason);
1521
1522 return 0;
1523}
1524
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001525static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001526UnicodeError_clear(PyUnicodeErrorObject *self)
1527{
1528 Py_CLEAR(self->encoding);
1529 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001530 Py_CLEAR(self->reason);
1531 return BaseException_clear((PyBaseExceptionObject *)self);
1532}
1533
1534static void
1535UnicodeError_dealloc(PyUnicodeErrorObject *self)
1536{
Georg Brandl38f62372006-09-06 06:50:05 +00001537 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001538 UnicodeError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001539 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001540}
1541
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001542static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001543UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1544{
1545 Py_VISIT(self->encoding);
1546 Py_VISIT(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001547 Py_VISIT(self->reason);
1548 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1549}
1550
1551static PyMemberDef UnicodeError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001552 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1553 PyDoc_STR("exception encoding")},
1554 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1555 PyDoc_STR("exception object")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001556 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001557 PyDoc_STR("exception start")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001558 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001559 PyDoc_STR("exception end")},
1560 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1561 PyDoc_STR("exception reason")},
1562 {NULL} /* Sentinel */
1563};
1564
1565
1566/*
1567 * UnicodeEncodeError extends UnicodeError
1568 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001569
1570static int
1571UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1572{
1573 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1574 return -1;
1575 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1576 kwds, &PyUnicode_Type);
1577}
1578
1579static PyObject *
1580UnicodeEncodeError_str(PyObject *self)
1581{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001582 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001583
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001584 if (uself->end==uself->start+1) {
1585 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001586 char badchar_str[20];
1587 if (badchar <= 0xff)
1588 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1589 else if (badchar <= 0xffff)
1590 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1591 else
1592 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001593 return PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001594 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001595 PyString_AS_STRING(uself->encoding),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001596 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001597 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001598 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001599 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001600 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001601 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001602 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001603 PyString_AS_STRING(uself->encoding),
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001604 uself->start,
1605 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001606 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001607 );
1608}
1609
1610static PyTypeObject _PyExc_UnicodeEncodeError = {
1611 PyObject_HEAD_INIT(NULL)
1612 0,
Georg Brandl38f62372006-09-06 06:50:05 +00001613 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001614 sizeof(PyUnicodeErrorObject), 0,
1615 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1616 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1617 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001618 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1619 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001620 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001621 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001622};
1623PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1624
1625PyObject *
1626PyUnicodeEncodeError_Create(
1627 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1628 Py_ssize_t start, Py_ssize_t end, const char *reason)
1629{
1630 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001631 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001632}
1633
1634
1635/*
1636 * UnicodeDecodeError extends UnicodeError
1637 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001638
1639static int
1640UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1641{
1642 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1643 return -1;
1644 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001645 kwds, &PyString_Type);
Richard Jones7b9558d2006-05-27 12:29:24 +00001646}
1647
1648static PyObject *
1649UnicodeDecodeError_str(PyObject *self)
1650{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001651 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001652
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001653 if (uself->end==uself->start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001654 /* FromFormat does not support %02x, so format that separately */
1655 char byte[4];
1656 PyOS_snprintf(byte, sizeof(byte), "%02x",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001657 ((int)PyString_AS_STRING(uself->object)[uself->start])&0xff);
1658 return PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001659 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001660 PyString_AS_STRING(uself->encoding),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001661 byte,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001662 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001663 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001664 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001665 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001666 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001667 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001668 PyString_AS_STRING(uself->encoding),
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001669 uself->start,
1670 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001671 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001672 );
1673}
1674
1675static PyTypeObject _PyExc_UnicodeDecodeError = {
1676 PyObject_HEAD_INIT(NULL)
1677 0,
1678 EXC_MODULE_NAME "UnicodeDecodeError",
1679 sizeof(PyUnicodeErrorObject), 0,
1680 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1681 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1682 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001683 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1684 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001685 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001686 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001687};
1688PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1689
1690PyObject *
1691PyUnicodeDecodeError_Create(
1692 const char *encoding, const char *object, Py_ssize_t length,
1693 Py_ssize_t start, Py_ssize_t end, const char *reason)
1694{
1695 assert(length < INT_MAX);
1696 assert(start < INT_MAX);
1697 assert(end < INT_MAX);
1698 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001699 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001700}
1701
1702
1703/*
1704 * UnicodeTranslateError extends UnicodeError
1705 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001706
1707static int
1708UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1709 PyObject *kwds)
1710{
1711 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1712 return -1;
1713
1714 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001715 Py_CLEAR(self->reason);
1716
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001717 if (!PyArg_ParseTuple(args, "O!nnO!",
Richard Jones7b9558d2006-05-27 12:29:24 +00001718 &PyUnicode_Type, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001719 &self->start,
1720 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001721 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001722 self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001723 return -1;
1724 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001725
Richard Jones7b9558d2006-05-27 12:29:24 +00001726 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001727 Py_INCREF(self->reason);
1728
1729 return 0;
1730}
1731
1732
1733static PyObject *
1734UnicodeTranslateError_str(PyObject *self)
1735{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001736 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001737
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001738 if (uself->end==uself->start+1) {
1739 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001740 char badchar_str[20];
1741 if (badchar <= 0xff)
1742 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1743 else if (badchar <= 0xffff)
1744 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1745 else
1746 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001747 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001748 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001749 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001750 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001751 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001752 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001753 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001754 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001755 "can't translate characters in position %zd-%zd: %.400s",
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001756 uself->start,
1757 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001758 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001759 );
1760}
1761
1762static PyTypeObject _PyExc_UnicodeTranslateError = {
1763 PyObject_HEAD_INIT(NULL)
1764 0,
1765 EXC_MODULE_NAME "UnicodeTranslateError",
1766 sizeof(PyUnicodeErrorObject), 0,
1767 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1768 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1769 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001770 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001771 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1772 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001773 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001774};
1775PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1776
1777PyObject *
1778PyUnicodeTranslateError_Create(
1779 const Py_UNICODE *object, Py_ssize_t length,
1780 Py_ssize_t start, Py_ssize_t end, const char *reason)
1781{
1782 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001783 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001784}
1785#endif
1786
1787
1788/*
1789 * AssertionError extends StandardError
1790 */
1791SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001792 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001793
1794
1795/*
1796 * ArithmeticError extends StandardError
1797 */
1798SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001799 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001800
1801
1802/*
1803 * FloatingPointError extends ArithmeticError
1804 */
1805SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001806 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001807
1808
1809/*
1810 * OverflowError extends ArithmeticError
1811 */
1812SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001813 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001814
1815
1816/*
1817 * ZeroDivisionError extends ArithmeticError
1818 */
1819SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001820 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001821
1822
1823/*
1824 * SystemError extends StandardError
1825 */
1826SimpleExtendsException(PyExc_StandardError, SystemError,
1827 "Internal error in the Python interpreter.\n"
1828 "\n"
1829 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001830 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001831
1832
1833/*
1834 * ReferenceError extends StandardError
1835 */
1836SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001837 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001838
1839
1840/*
1841 * MemoryError extends StandardError
1842 */
Richard Jones2d555b32006-05-27 16:15:11 +00001843SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001844
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00001845/*
1846 * BufferError extends StandardError
1847 */
1848SimpleExtendsException(PyExc_StandardError, BufferError, "Buffer error.");
1849
Richard Jones7b9558d2006-05-27 12:29:24 +00001850
1851/* Warning category docstrings */
1852
1853/*
1854 * Warning extends Exception
1855 */
1856SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001857 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001858
1859
1860/*
1861 * UserWarning extends Warning
1862 */
1863SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001864 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001865
1866
1867/*
1868 * DeprecationWarning extends Warning
1869 */
1870SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001871 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001872
1873
1874/*
1875 * PendingDeprecationWarning extends Warning
1876 */
1877SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1878 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001879 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001880
1881
1882/*
1883 * SyntaxWarning extends Warning
1884 */
1885SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001886 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001887
1888
1889/*
1890 * RuntimeWarning extends Warning
1891 */
1892SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001893 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001894
1895
1896/*
1897 * FutureWarning extends Warning
1898 */
1899SimpleExtendsException(PyExc_Warning, FutureWarning,
1900 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001901 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001902
1903
1904/*
1905 * ImportWarning extends Warning
1906 */
1907SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001908 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00001909
1910
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001911/*
1912 * UnicodeWarning extends Warning
1913 */
1914SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1915 "Base class for warnings about Unicode related problems, mostly\n"
1916 "related to conversion problems.");
1917
Christian Heimes1a6387e2008-03-26 12:49:49 +00001918/*
1919 * BytesWarning extends Warning
1920 */
1921SimpleExtendsException(PyExc_Warning, BytesWarning,
1922 "Base class for warnings about bytes and buffer related problems, mostly\n"
1923 "related to conversion from str or comparing to str.");
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001924
Richard Jones7b9558d2006-05-27 12:29:24 +00001925/* Pre-computed MemoryError instance. Best to create this as early as
1926 * possible and not wait until a MemoryError is actually raised!
1927 */
1928PyObject *PyExc_MemoryErrorInst=NULL;
1929
Brett Cannon1e534b52007-09-07 04:18:30 +00001930/* Pre-computed RuntimeError instance for when recursion depth is reached.
1931 Meant to be used when normalizing the exception for exceeding the recursion
1932 depth will cause its own infinite recursion.
1933*/
1934PyObject *PyExc_RecursionErrorInst = NULL;
1935
Richard Jones7b9558d2006-05-27 12:29:24 +00001936/* module global functions */
1937static PyMethodDef functions[] = {
1938 /* Sentinel */
1939 {NULL, NULL}
1940};
1941
1942#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1943 Py_FatalError("exceptions bootstrapping error.");
1944
1945#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1946 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1947 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1948 Py_FatalError("Module dictionary insertion problem.");
1949
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00001950#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00001951/* crt variable checking in VisualStudio .NET 2005 */
1952#include <crtdbg.h>
1953
1954static int prevCrtReportMode;
1955static _invalid_parameter_handler prevCrtHandler;
1956
1957/* Invalid parameter handler. Sets a ValueError exception */
1958static void
1959InvalidParameterHandler(
1960 const wchar_t * expression,
1961 const wchar_t * function,
1962 const wchar_t * file,
1963 unsigned int line,
1964 uintptr_t pReserved)
1965{
1966 /* Do nothing, allow execution to continue. Usually this
1967 * means that the CRT will set errno to EINVAL
1968 */
1969}
1970#endif
1971
1972
Richard Jones7b9558d2006-05-27 12:29:24 +00001973PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001974_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00001975{
1976 PyObject *m, *bltinmod, *bdict;
1977
1978 PRE_INIT(BaseException)
1979 PRE_INIT(Exception)
1980 PRE_INIT(StandardError)
1981 PRE_INIT(TypeError)
1982 PRE_INIT(StopIteration)
1983 PRE_INIT(GeneratorExit)
1984 PRE_INIT(SystemExit)
1985 PRE_INIT(KeyboardInterrupt)
1986 PRE_INIT(ImportError)
1987 PRE_INIT(EnvironmentError)
1988 PRE_INIT(IOError)
1989 PRE_INIT(OSError)
1990#ifdef MS_WINDOWS
1991 PRE_INIT(WindowsError)
1992#endif
1993#ifdef __VMS
1994 PRE_INIT(VMSError)
1995#endif
1996 PRE_INIT(EOFError)
1997 PRE_INIT(RuntimeError)
1998 PRE_INIT(NotImplementedError)
1999 PRE_INIT(NameError)
2000 PRE_INIT(UnboundLocalError)
2001 PRE_INIT(AttributeError)
2002 PRE_INIT(SyntaxError)
2003 PRE_INIT(IndentationError)
2004 PRE_INIT(TabError)
2005 PRE_INIT(LookupError)
2006 PRE_INIT(IndexError)
2007 PRE_INIT(KeyError)
2008 PRE_INIT(ValueError)
2009 PRE_INIT(UnicodeError)
2010#ifdef Py_USING_UNICODE
2011 PRE_INIT(UnicodeEncodeError)
2012 PRE_INIT(UnicodeDecodeError)
2013 PRE_INIT(UnicodeTranslateError)
2014#endif
2015 PRE_INIT(AssertionError)
2016 PRE_INIT(ArithmeticError)
2017 PRE_INIT(FloatingPointError)
2018 PRE_INIT(OverflowError)
2019 PRE_INIT(ZeroDivisionError)
2020 PRE_INIT(SystemError)
2021 PRE_INIT(ReferenceError)
2022 PRE_INIT(MemoryError)
2023 PRE_INIT(Warning)
2024 PRE_INIT(UserWarning)
2025 PRE_INIT(DeprecationWarning)
2026 PRE_INIT(PendingDeprecationWarning)
2027 PRE_INIT(SyntaxWarning)
2028 PRE_INIT(RuntimeWarning)
2029 PRE_INIT(FutureWarning)
2030 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002031 PRE_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002032 PRE_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002033
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002034 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2035 (PyObject *)NULL, PYTHON_API_VERSION);
Richard Jones7b9558d2006-05-27 12:29:24 +00002036 if (m == NULL) return;
2037
2038 bltinmod = PyImport_ImportModule("__builtin__");
2039 if (bltinmod == NULL)
2040 Py_FatalError("exceptions bootstrapping error.");
2041 bdict = PyModule_GetDict(bltinmod);
2042 if (bdict == NULL)
2043 Py_FatalError("exceptions bootstrapping error.");
2044
2045 POST_INIT(BaseException)
2046 POST_INIT(Exception)
2047 POST_INIT(StandardError)
2048 POST_INIT(TypeError)
2049 POST_INIT(StopIteration)
2050 POST_INIT(GeneratorExit)
2051 POST_INIT(SystemExit)
2052 POST_INIT(KeyboardInterrupt)
2053 POST_INIT(ImportError)
2054 POST_INIT(EnvironmentError)
2055 POST_INIT(IOError)
2056 POST_INIT(OSError)
2057#ifdef MS_WINDOWS
2058 POST_INIT(WindowsError)
2059#endif
2060#ifdef __VMS
2061 POST_INIT(VMSError)
2062#endif
2063 POST_INIT(EOFError)
2064 POST_INIT(RuntimeError)
2065 POST_INIT(NotImplementedError)
2066 POST_INIT(NameError)
2067 POST_INIT(UnboundLocalError)
2068 POST_INIT(AttributeError)
2069 POST_INIT(SyntaxError)
2070 POST_INIT(IndentationError)
2071 POST_INIT(TabError)
2072 POST_INIT(LookupError)
2073 POST_INIT(IndexError)
2074 POST_INIT(KeyError)
2075 POST_INIT(ValueError)
2076 POST_INIT(UnicodeError)
2077#ifdef Py_USING_UNICODE
2078 POST_INIT(UnicodeEncodeError)
2079 POST_INIT(UnicodeDecodeError)
2080 POST_INIT(UnicodeTranslateError)
2081#endif
2082 POST_INIT(AssertionError)
2083 POST_INIT(ArithmeticError)
2084 POST_INIT(FloatingPointError)
2085 POST_INIT(OverflowError)
2086 POST_INIT(ZeroDivisionError)
2087 POST_INIT(SystemError)
2088 POST_INIT(ReferenceError)
2089 POST_INIT(MemoryError)
2090 POST_INIT(Warning)
2091 POST_INIT(UserWarning)
2092 POST_INIT(DeprecationWarning)
2093 POST_INIT(PendingDeprecationWarning)
2094 POST_INIT(SyntaxWarning)
2095 POST_INIT(RuntimeWarning)
2096 POST_INIT(FutureWarning)
2097 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002098 POST_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002099 POST_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002100
2101 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2102 if (!PyExc_MemoryErrorInst)
2103 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
2104
Brett Cannon1e534b52007-09-07 04:18:30 +00002105 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2106 if (!PyExc_RecursionErrorInst)
2107 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2108 "recursion errors");
2109 else {
2110 PyBaseExceptionObject *err_inst =
2111 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2112 PyObject *args_tuple;
2113 PyObject *exc_message;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002114 exc_message = PyString_FromString("maximum recursion depth exceeded");
Brett Cannon1e534b52007-09-07 04:18:30 +00002115 if (!exc_message)
2116 Py_FatalError("cannot allocate argument for RuntimeError "
2117 "pre-allocation");
2118 args_tuple = PyTuple_Pack(1, exc_message);
2119 if (!args_tuple)
2120 Py_FatalError("cannot allocate tuple for RuntimeError "
2121 "pre-allocation");
2122 Py_DECREF(exc_message);
2123 if (BaseException_init(err_inst, args_tuple, NULL))
2124 Py_FatalError("init of pre-allocated RuntimeError failed");
2125 Py_DECREF(args_tuple);
2126 }
2127
Richard Jones7b9558d2006-05-27 12:29:24 +00002128 Py_DECREF(bltinmod);
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002129
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002130#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002131 /* Set CRT argument error handler */
2132 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
2133 /* turn off assertions in debug mode */
2134 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
2135#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002136}
2137
2138void
2139_PyExc_Fini(void)
2140{
2141 Py_XDECREF(PyExc_MemoryErrorInst);
2142 PyExc_MemoryErrorInst = NULL;
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002143#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002144 /* reset CRT error handling */
2145 _set_invalid_parameter_handler(prevCrtHandler);
2146 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
2147#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002148}