blob: 99cfb2b15d00188300b4b514183566523bbc7c26 [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
Nick Coghlan524b7772008-07-08 14:08:04 +0000120#ifdef Py_USING_UNICODE
121static PyObject *
122BaseException_unicode(PyBaseExceptionObject *self)
123{
124 PyObject *out;
125
126 switch (PyTuple_GET_SIZE(self->args)) {
127 case 0:
128 out = PyUnicode_FromString("");
129 break;
130 case 1:
131 out = PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));
132 break;
133 default:
134 out = PyObject_Unicode(self->args);
135 break;
136 }
137
138 return out;
139}
140#endif
141
Richard Jones7b9558d2006-05-27 12:29:24 +0000142static PyObject *
143BaseException_repr(PyBaseExceptionObject *self)
144{
Richard Jones7b9558d2006-05-27 12:29:24 +0000145 PyObject *repr_suffix;
146 PyObject *repr;
147 char *name;
148 char *dot;
149
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000150 repr_suffix = PyObject_Repr(self->args);
151 if (!repr_suffix)
Richard Jones7b9558d2006-05-27 12:29:24 +0000152 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000153
Christian Heimese93237d2007-12-19 02:37:44 +0000154 name = (char *)Py_TYPE(self)->tp_name;
Richard Jones7b9558d2006-05-27 12:29:24 +0000155 dot = strrchr(name, '.');
156 if (dot != NULL) name = dot+1;
157
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000158 repr = PyString_FromString(name);
Richard Jones7b9558d2006-05-27 12:29:24 +0000159 if (!repr) {
160 Py_DECREF(repr_suffix);
161 return NULL;
162 }
163
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000164 PyString_ConcatAndDel(&repr, repr_suffix);
Richard Jones7b9558d2006-05-27 12:29:24 +0000165 return repr;
166}
167
168/* Pickling support */
169static PyObject *
170BaseException_reduce(PyBaseExceptionObject *self)
171{
Georg Brandlddba4732006-05-30 07:04:55 +0000172 if (self->args && self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000173 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000174 else
Christian Heimese93237d2007-12-19 02:37:44 +0000175 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000176}
177
Georg Brandl85ac8502006-06-01 06:39:19 +0000178/*
179 * Needed for backward compatibility, since exceptions used to store
180 * all their attributes in the __dict__. Code is taken from cPickle's
181 * load_build function.
182 */
183static PyObject *
184BaseException_setstate(PyObject *self, PyObject *state)
185{
186 PyObject *d_key, *d_value;
187 Py_ssize_t i = 0;
188
189 if (state != Py_None) {
190 if (!PyDict_Check(state)) {
191 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
192 return NULL;
193 }
194 while (PyDict_Next(state, &i, &d_key, &d_value)) {
195 if (PyObject_SetAttr(self, d_key, d_value) < 0)
196 return NULL;
197 }
198 }
199 Py_RETURN_NONE;
200}
Richard Jones7b9558d2006-05-27 12:29:24 +0000201
Richard Jones7b9558d2006-05-27 12:29:24 +0000202
203static PyMethodDef BaseException_methods[] = {
204 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Georg Brandl85ac8502006-06-01 06:39:19 +0000205 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Nick Coghlan524b7772008-07-08 14:08:04 +0000206#ifdef Py_USING_UNICODE
207 {"__unicode__", (PyCFunction)BaseException_unicode, METH_NOARGS },
208#endif
Richard Jones7b9558d2006-05-27 12:29:24 +0000209 {NULL, NULL, 0, NULL},
210};
211
212
213
214static PyObject *
215BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
216{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000217 if (PyErr_WarnPy3k("__getitem__ not supported for exception "
218 "classes in 3.x; use args attribute", 1) < 0)
219 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000220 return PySequence_GetItem(self->args, index);
221}
222
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000223static PyObject *
224BaseException_getslice(PyBaseExceptionObject *self,
225 Py_ssize_t start, Py_ssize_t stop)
226{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000227 if (PyErr_WarnPy3k("__getslice__ not supported for exception "
228 "classes in 3.x; use args attribute", 1) < 0)
229 return NULL;
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000230 return PySequence_GetSlice(self->args, start, stop);
231}
232
Richard Jones7b9558d2006-05-27 12:29:24 +0000233static PySequenceMethods BaseException_as_sequence = {
234 0, /* sq_length; */
235 0, /* sq_concat; */
236 0, /* sq_repeat; */
237 (ssizeargfunc)BaseException_getitem, /* sq_item; */
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000238 (ssizessizeargfunc)BaseException_getslice, /* sq_slice; */
Richard Jones7b9558d2006-05-27 12:29:24 +0000239 0, /* sq_ass_item; */
240 0, /* sq_ass_slice; */
241 0, /* sq_contains; */
242 0, /* sq_inplace_concat; */
243 0 /* sq_inplace_repeat; */
244};
245
Richard Jones7b9558d2006-05-27 12:29:24 +0000246static PyObject *
247BaseException_get_dict(PyBaseExceptionObject *self)
248{
249 if (self->dict == NULL) {
250 self->dict = PyDict_New();
251 if (!self->dict)
252 return NULL;
253 }
254 Py_INCREF(self->dict);
255 return self->dict;
256}
257
258static int
259BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
260{
261 if (val == NULL) {
262 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
263 return -1;
264 }
265 if (!PyDict_Check(val)) {
266 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
267 return -1;
268 }
269 Py_CLEAR(self->dict);
270 Py_INCREF(val);
271 self->dict = val;
272 return 0;
273}
274
275static PyObject *
276BaseException_get_args(PyBaseExceptionObject *self)
277{
278 if (self->args == NULL) {
279 Py_INCREF(Py_None);
280 return Py_None;
281 }
282 Py_INCREF(self->args);
283 return self->args;
284}
285
286static int
287BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
288{
289 PyObject *seq;
290 if (val == NULL) {
291 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
292 return -1;
293 }
294 seq = PySequence_Tuple(val);
295 if (!seq) return -1;
Georg Brandlc7c51142006-05-29 09:46:51 +0000296 Py_CLEAR(self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000297 self->args = seq;
298 return 0;
299}
300
Brett Cannon229cee22007-05-05 01:34:02 +0000301static PyObject *
302BaseException_get_message(PyBaseExceptionObject *self)
303{
304 int ret;
305 ret = PyErr_WarnEx(PyExc_DeprecationWarning,
306 "BaseException.message has been deprecated as "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000307 "of Python 2.6", 1);
308 if (ret < 0)
Brett Cannon229cee22007-05-05 01:34:02 +0000309 return NULL;
310
311 Py_INCREF(self->message);
312 return self->message;
313}
314
315static int
316BaseException_set_message(PyBaseExceptionObject *self, PyObject *val)
317{
318 int ret;
319 ret = PyErr_WarnEx(PyExc_DeprecationWarning,
320 "BaseException.message has been deprecated as "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000321 "of Python 2.6", 1);
322 if (ret < 0)
Brett Cannon229cee22007-05-05 01:34:02 +0000323 return -1;
324 Py_INCREF(val);
325 Py_DECREF(self->message);
326 self->message = val;
327 return 0;
328}
329
Richard Jones7b9558d2006-05-27 12:29:24 +0000330static PyGetSetDef BaseException_getset[] = {
331 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
332 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Brett Cannon229cee22007-05-05 01:34:02 +0000333 {"message", (getter)BaseException_get_message,
334 (setter)BaseException_set_message},
Richard Jones7b9558d2006-05-27 12:29:24 +0000335 {NULL},
336};
337
338
339static PyTypeObject _PyExc_BaseException = {
340 PyObject_HEAD_INIT(NULL)
341 0, /*ob_size*/
342 EXC_MODULE_NAME "BaseException", /*tp_name*/
343 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
344 0, /*tp_itemsize*/
345 (destructor)BaseException_dealloc, /*tp_dealloc*/
346 0, /*tp_print*/
347 0, /*tp_getattr*/
348 0, /*tp_setattr*/
349 0, /* tp_compare; */
350 (reprfunc)BaseException_repr, /*tp_repr*/
351 0, /*tp_as_number*/
352 &BaseException_as_sequence, /*tp_as_sequence*/
353 0, /*tp_as_mapping*/
354 0, /*tp_hash */
355 0, /*tp_call*/
356 (reprfunc)BaseException_str, /*tp_str*/
357 PyObject_GenericGetAttr, /*tp_getattro*/
358 PyObject_GenericSetAttr, /*tp_setattro*/
359 0, /*tp_as_buffer*/
Neal Norwitzee3a1b52007-02-25 19:44:48 +0000360 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
361 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Richard Jones7b9558d2006-05-27 12:29:24 +0000362 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
363 (traverseproc)BaseException_traverse, /* tp_traverse */
364 (inquiry)BaseException_clear, /* tp_clear */
365 0, /* tp_richcompare */
366 0, /* tp_weaklistoffset */
367 0, /* tp_iter */
368 0, /* tp_iternext */
369 BaseException_methods, /* tp_methods */
Brett Cannon229cee22007-05-05 01:34:02 +0000370 0, /* tp_members */
Richard Jones7b9558d2006-05-27 12:29:24 +0000371 BaseException_getset, /* tp_getset */
372 0, /* tp_base */
373 0, /* tp_dict */
374 0, /* tp_descr_get */
375 0, /* tp_descr_set */
376 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
377 (initproc)BaseException_init, /* tp_init */
378 0, /* tp_alloc */
379 BaseException_new, /* tp_new */
380};
381/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
382from the previous implmentation and also allowing Python objects to be used
383in the API */
384PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
385
Richard Jones2d555b32006-05-27 16:15:11 +0000386/* note these macros omit the last semicolon so the macro invocation may
387 * include it and not look strange.
388 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000389#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
390static PyTypeObject _PyExc_ ## EXCNAME = { \
391 PyObject_HEAD_INIT(NULL) \
392 0, \
393 EXC_MODULE_NAME # EXCNAME, \
394 sizeof(PyBaseExceptionObject), \
395 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
396 0, 0, 0, 0, 0, 0, 0, \
397 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
398 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
399 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
400 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
401 (initproc)BaseException_init, 0, BaseException_new,\
402}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000403PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000404
405#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
406static PyTypeObject _PyExc_ ## EXCNAME = { \
407 PyObject_HEAD_INIT(NULL) \
408 0, \
409 EXC_MODULE_NAME # EXCNAME, \
410 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000411 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000412 0, 0, 0, 0, 0, \
413 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000414 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
415 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000416 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000417 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000418}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000419PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000420
421#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
422static PyTypeObject _PyExc_ ## EXCNAME = { \
423 PyObject_HEAD_INIT(NULL) \
424 0, \
425 EXC_MODULE_NAME # EXCNAME, \
426 sizeof(Py ## EXCSTORE ## Object), 0, \
427 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
428 (reprfunc)EXCSTR, 0, 0, 0, \
429 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
430 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
431 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
432 EXCMEMBERS, 0, &_ ## EXCBASE, \
433 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000434 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000435}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000436PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000437
438
439/*
440 * Exception extends BaseException
441 */
442SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000443 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000444
445
446/*
447 * StandardError extends Exception
448 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000449SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000450 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000451 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000452
453
454/*
455 * TypeError extends StandardError
456 */
457SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000458 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000459
460
461/*
462 * StopIteration extends Exception
463 */
464SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000465 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000466
467
468/*
Christian Heimes44eeaec2007-12-03 20:01:02 +0000469 * GeneratorExit extends BaseException
Richard Jones7b9558d2006-05-27 12:29:24 +0000470 */
Christian Heimes44eeaec2007-12-03 20:01:02 +0000471SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000472 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000473
474
475/*
476 * SystemExit extends BaseException
477 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000478
479static int
480SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
481{
482 Py_ssize_t size = PyTuple_GET_SIZE(args);
483
484 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
485 return -1;
486
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000487 if (size == 0)
488 return 0;
489 Py_CLEAR(self->code);
Richard Jones7b9558d2006-05-27 12:29:24 +0000490 if (size == 1)
491 self->code = PyTuple_GET_ITEM(args, 0);
492 else if (size > 1)
493 self->code = args;
494 Py_INCREF(self->code);
495 return 0;
496}
497
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000498static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000499SystemExit_clear(PySystemExitObject *self)
500{
501 Py_CLEAR(self->code);
502 return BaseException_clear((PyBaseExceptionObject *)self);
503}
504
505static void
506SystemExit_dealloc(PySystemExitObject *self)
507{
Georg Brandl38f62372006-09-06 06:50:05 +0000508 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000509 SystemExit_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000510 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000511}
512
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000513static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000514SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
515{
516 Py_VISIT(self->code);
517 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
518}
519
520static PyMemberDef SystemExit_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000521 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
522 PyDoc_STR("exception code")},
523 {NULL} /* Sentinel */
524};
525
526ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
527 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000528 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000529
530/*
531 * KeyboardInterrupt extends BaseException
532 */
533SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000534 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000535
536
537/*
538 * ImportError extends StandardError
539 */
540SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000541 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000542
543
544/*
545 * EnvironmentError extends StandardError
546 */
547
Richard Jones7b9558d2006-05-27 12:29:24 +0000548/* Where a function has a single filename, such as open() or some
549 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
550 * called, giving a third argument which is the filename. But, so
551 * that old code using in-place unpacking doesn't break, e.g.:
552 *
553 * except IOError, (errno, strerror):
554 *
555 * we hack args so that it only contains two items. This also
556 * means we need our own __str__() which prints out the filename
557 * when it was supplied.
558 */
559static int
560EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
561 PyObject *kwds)
562{
563 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
564 PyObject *subslice = NULL;
565
566 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
567 return -1;
568
Georg Brandl3267d282006-09-30 09:03:42 +0000569 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000570 return 0;
571 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000572
573 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000574 &myerrno, &strerror, &filename)) {
575 return -1;
576 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000577 Py_CLEAR(self->myerrno); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000578 self->myerrno = myerrno;
579 Py_INCREF(self->myerrno);
580
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000581 Py_CLEAR(self->strerror); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000582 self->strerror = strerror;
583 Py_INCREF(self->strerror);
584
585 /* self->filename will remain Py_None otherwise */
586 if (filename != NULL) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000587 Py_CLEAR(self->filename); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000588 self->filename = filename;
589 Py_INCREF(self->filename);
590
591 subslice = PyTuple_GetSlice(args, 0, 2);
592 if (!subslice)
593 return -1;
594
595 Py_DECREF(self->args); /* replacing args */
596 self->args = subslice;
597 }
598 return 0;
599}
600
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000601static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000602EnvironmentError_clear(PyEnvironmentErrorObject *self)
603{
604 Py_CLEAR(self->myerrno);
605 Py_CLEAR(self->strerror);
606 Py_CLEAR(self->filename);
607 return BaseException_clear((PyBaseExceptionObject *)self);
608}
609
610static void
611EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
612{
Georg Brandl38f62372006-09-06 06:50:05 +0000613 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000614 EnvironmentError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000615 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000616}
617
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000618static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000619EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
620 void *arg)
621{
622 Py_VISIT(self->myerrno);
623 Py_VISIT(self->strerror);
624 Py_VISIT(self->filename);
625 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
626}
627
628static PyObject *
629EnvironmentError_str(PyEnvironmentErrorObject *self)
630{
631 PyObject *rtnval = NULL;
632
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000633 if (self->filename) {
634 PyObject *fmt;
635 PyObject *repr;
636 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000637
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000638 fmt = PyString_FromString("[Errno %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000639 if (!fmt)
640 return NULL;
641
642 repr = PyObject_Repr(self->filename);
643 if (!repr) {
644 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000645 return NULL;
646 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000647 tuple = PyTuple_New(3);
648 if (!tuple) {
649 Py_DECREF(repr);
650 Py_DECREF(fmt);
651 return NULL;
652 }
653
654 if (self->myerrno) {
655 Py_INCREF(self->myerrno);
656 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
657 }
658 else {
659 Py_INCREF(Py_None);
660 PyTuple_SET_ITEM(tuple, 0, Py_None);
661 }
662 if (self->strerror) {
663 Py_INCREF(self->strerror);
664 PyTuple_SET_ITEM(tuple, 1, self->strerror);
665 }
666 else {
667 Py_INCREF(Py_None);
668 PyTuple_SET_ITEM(tuple, 1, Py_None);
669 }
670
Richard Jones7b9558d2006-05-27 12:29:24 +0000671 PyTuple_SET_ITEM(tuple, 2, repr);
672
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000673 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000674
675 Py_DECREF(fmt);
676 Py_DECREF(tuple);
677 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000678 else if (self->myerrno && self->strerror) {
679 PyObject *fmt;
680 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000681
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000682 fmt = PyString_FromString("[Errno %s] %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000683 if (!fmt)
684 return NULL;
685
686 tuple = PyTuple_New(2);
687 if (!tuple) {
688 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000689 return NULL;
690 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000691
692 if (self->myerrno) {
693 Py_INCREF(self->myerrno);
694 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
695 }
696 else {
697 Py_INCREF(Py_None);
698 PyTuple_SET_ITEM(tuple, 0, Py_None);
699 }
700 if (self->strerror) {
701 Py_INCREF(self->strerror);
702 PyTuple_SET_ITEM(tuple, 1, self->strerror);
703 }
704 else {
705 Py_INCREF(Py_None);
706 PyTuple_SET_ITEM(tuple, 1, Py_None);
707 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000708
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000709 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000710
711 Py_DECREF(fmt);
712 Py_DECREF(tuple);
713 }
714 else
715 rtnval = BaseException_str((PyBaseExceptionObject *)self);
716
717 return rtnval;
718}
719
720static PyMemberDef EnvironmentError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000721 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000722 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000723 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000724 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000725 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000726 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000727 {NULL} /* Sentinel */
728};
729
730
731static PyObject *
732EnvironmentError_reduce(PyEnvironmentErrorObject *self)
733{
734 PyObject *args = self->args;
735 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000736
Richard Jones7b9558d2006-05-27 12:29:24 +0000737 /* self->args is only the first two real arguments if there was a
738 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000739 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000740 args = PyTuple_New(3);
741 if (!args) return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000742
743 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000744 Py_INCREF(tmp);
745 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000746
747 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000748 Py_INCREF(tmp);
749 PyTuple_SET_ITEM(args, 1, tmp);
750
751 Py_INCREF(self->filename);
752 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000753 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000754 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000755
756 if (self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000757 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000758 else
Christian Heimese93237d2007-12-19 02:37:44 +0000759 res = PyTuple_Pack(2, Py_TYPE(self), args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000760 Py_DECREF(args);
761 return res;
762}
763
764
765static PyMethodDef EnvironmentError_methods[] = {
766 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
767 {NULL}
768};
769
770ComplexExtendsException(PyExc_StandardError, EnvironmentError,
771 EnvironmentError, EnvironmentError_dealloc,
772 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000773 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000774 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000775
776
777/*
778 * IOError extends EnvironmentError
779 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000780MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000781 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000782
783
784/*
785 * OSError extends EnvironmentError
786 */
787MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000788 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000789
790
791/*
792 * WindowsError extends OSError
793 */
794#ifdef MS_WINDOWS
795#include "errmap.h"
796
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000797static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000798WindowsError_clear(PyWindowsErrorObject *self)
799{
800 Py_CLEAR(self->myerrno);
801 Py_CLEAR(self->strerror);
802 Py_CLEAR(self->filename);
803 Py_CLEAR(self->winerror);
804 return BaseException_clear((PyBaseExceptionObject *)self);
805}
806
807static void
808WindowsError_dealloc(PyWindowsErrorObject *self)
809{
Georg Brandl38f62372006-09-06 06:50:05 +0000810 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000811 WindowsError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000812 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000813}
814
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000815static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000816WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
817{
818 Py_VISIT(self->myerrno);
819 Py_VISIT(self->strerror);
820 Py_VISIT(self->filename);
821 Py_VISIT(self->winerror);
822 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
823}
824
Richard Jones7b9558d2006-05-27 12:29:24 +0000825static int
826WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
827{
828 PyObject *o_errcode = NULL;
829 long errcode;
830 long posix_errno;
831
832 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
833 == -1)
834 return -1;
835
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000836 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000837 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000838
839 /* Set errno to the POSIX errno, and winerror to the Win32
840 error code. */
841 errcode = PyInt_AsLong(self->myerrno);
842 if (errcode == -1 && PyErr_Occurred())
843 return -1;
844 posix_errno = winerror_to_errno(errcode);
845
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000846 Py_CLEAR(self->winerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000847 self->winerror = self->myerrno;
848
849 o_errcode = PyInt_FromLong(posix_errno);
850 if (!o_errcode)
851 return -1;
852
853 self->myerrno = o_errcode;
854
855 return 0;
856}
857
858
859static PyObject *
860WindowsError_str(PyWindowsErrorObject *self)
861{
Richard Jones7b9558d2006-05-27 12:29:24 +0000862 PyObject *rtnval = NULL;
863
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000864 if (self->filename) {
865 PyObject *fmt;
866 PyObject *repr;
867 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000868
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000869 fmt = PyString_FromString("[Error %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000870 if (!fmt)
871 return NULL;
872
873 repr = PyObject_Repr(self->filename);
874 if (!repr) {
875 Py_DECREF(fmt);
876 return NULL;
877 }
878 tuple = PyTuple_New(3);
879 if (!tuple) {
880 Py_DECREF(repr);
881 Py_DECREF(fmt);
882 return NULL;
883 }
884
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000885 if (self->winerror) {
886 Py_INCREF(self->winerror);
887 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000888 }
889 else {
890 Py_INCREF(Py_None);
891 PyTuple_SET_ITEM(tuple, 0, Py_None);
892 }
893 if (self->strerror) {
894 Py_INCREF(self->strerror);
895 PyTuple_SET_ITEM(tuple, 1, self->strerror);
896 }
897 else {
898 Py_INCREF(Py_None);
899 PyTuple_SET_ITEM(tuple, 1, Py_None);
900 }
901
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000902 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000903
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000904 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000905
906 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000907 Py_DECREF(tuple);
908 }
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000909 else if (self->winerror && self->strerror) {
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000910 PyObject *fmt;
911 PyObject *tuple;
912
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000913 fmt = PyString_FromString("[Error %s] %s");
Richard Jones7b9558d2006-05-27 12:29:24 +0000914 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000915 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000916
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000917 tuple = PyTuple_New(2);
918 if (!tuple) {
919 Py_DECREF(fmt);
920 return NULL;
921 }
922
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000923 if (self->winerror) {
924 Py_INCREF(self->winerror);
925 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000926 }
927 else {
928 Py_INCREF(Py_None);
929 PyTuple_SET_ITEM(tuple, 0, Py_None);
930 }
931 if (self->strerror) {
932 Py_INCREF(self->strerror);
933 PyTuple_SET_ITEM(tuple, 1, self->strerror);
934 }
935 else {
936 Py_INCREF(Py_None);
937 PyTuple_SET_ITEM(tuple, 1, Py_None);
938 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000939
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000940 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000941
942 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000943 Py_DECREF(tuple);
944 }
945 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000946 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000947
Richard Jones7b9558d2006-05-27 12:29:24 +0000948 return rtnval;
949}
950
951static PyMemberDef WindowsError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000952 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000953 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000954 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000955 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000956 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000957 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000958 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000959 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000960 {NULL} /* Sentinel */
961};
962
Richard Jones2d555b32006-05-27 16:15:11 +0000963ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
964 WindowsError_dealloc, 0, WindowsError_members,
965 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000966
967#endif /* MS_WINDOWS */
968
969
970/*
971 * VMSError extends OSError (I think)
972 */
973#ifdef __VMS
974MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +0000975 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000976#endif
977
978
979/*
980 * EOFError extends StandardError
981 */
982SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +0000983 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000984
985
986/*
987 * RuntimeError extends StandardError
988 */
989SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000990 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000991
992
993/*
994 * NotImplementedError extends RuntimeError
995 */
996SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +0000997 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000998
999/*
1000 * NameError extends StandardError
1001 */
1002SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +00001003 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001004
1005/*
1006 * UnboundLocalError extends NameError
1007 */
1008SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +00001009 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001010
1011/*
1012 * AttributeError extends StandardError
1013 */
1014SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001015 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001016
1017
1018/*
1019 * SyntaxError extends StandardError
1020 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001021
1022static int
1023SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
1024{
1025 PyObject *info = NULL;
1026 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
1027
1028 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1029 return -1;
1030
1031 if (lenargs >= 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001032 Py_CLEAR(self->msg);
Richard Jones7b9558d2006-05-27 12:29:24 +00001033 self->msg = PyTuple_GET_ITEM(args, 0);
1034 Py_INCREF(self->msg);
1035 }
1036 if (lenargs == 2) {
1037 info = PyTuple_GET_ITEM(args, 1);
1038 info = PySequence_Tuple(info);
1039 if (!info) return -1;
1040
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001041 if (PyTuple_GET_SIZE(info) != 4) {
1042 /* not a very good error message, but it's what Python 2.4 gives */
1043 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
1044 Py_DECREF(info);
1045 return -1;
1046 }
1047
1048 Py_CLEAR(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001049 self->filename = PyTuple_GET_ITEM(info, 0);
1050 Py_INCREF(self->filename);
1051
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001052 Py_CLEAR(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001053 self->lineno = PyTuple_GET_ITEM(info, 1);
1054 Py_INCREF(self->lineno);
1055
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001056 Py_CLEAR(self->offset);
Richard Jones7b9558d2006-05-27 12:29:24 +00001057 self->offset = PyTuple_GET_ITEM(info, 2);
1058 Py_INCREF(self->offset);
1059
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001060 Py_CLEAR(self->text);
Richard Jones7b9558d2006-05-27 12:29:24 +00001061 self->text = PyTuple_GET_ITEM(info, 3);
1062 Py_INCREF(self->text);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001063
1064 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001065 }
1066 return 0;
1067}
1068
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001069static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001070SyntaxError_clear(PySyntaxErrorObject *self)
1071{
1072 Py_CLEAR(self->msg);
1073 Py_CLEAR(self->filename);
1074 Py_CLEAR(self->lineno);
1075 Py_CLEAR(self->offset);
1076 Py_CLEAR(self->text);
1077 Py_CLEAR(self->print_file_and_line);
1078 return BaseException_clear((PyBaseExceptionObject *)self);
1079}
1080
1081static void
1082SyntaxError_dealloc(PySyntaxErrorObject *self)
1083{
Georg Brandl38f62372006-09-06 06:50:05 +00001084 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001085 SyntaxError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001086 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001087}
1088
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001089static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001090SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1091{
1092 Py_VISIT(self->msg);
1093 Py_VISIT(self->filename);
1094 Py_VISIT(self->lineno);
1095 Py_VISIT(self->offset);
1096 Py_VISIT(self->text);
1097 Py_VISIT(self->print_file_and_line);
1098 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1099}
1100
1101/* This is called "my_basename" instead of just "basename" to avoid name
1102 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1103 defined, and Python does define that. */
1104static char *
1105my_basename(char *name)
1106{
1107 char *cp = name;
1108 char *result = name;
1109
1110 if (name == NULL)
1111 return "???";
1112 while (*cp != '\0') {
1113 if (*cp == SEP)
1114 result = cp + 1;
1115 ++cp;
1116 }
1117 return result;
1118}
1119
1120
1121static PyObject *
1122SyntaxError_str(PySyntaxErrorObject *self)
1123{
1124 PyObject *str;
1125 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001126 int have_filename = 0;
1127 int have_lineno = 0;
1128 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001129 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001130
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001131 if (self->msg)
1132 str = PyObject_Str(self->msg);
1133 else
1134 str = PyObject_Str(Py_None);
Georg Brandl43ab1002006-05-28 20:57:09 +00001135 if (!str) return NULL;
1136 /* Don't fiddle with non-string return (shouldn't happen anyway) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001137 if (!PyString_Check(str)) return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001138
1139 /* XXX -- do all the additional formatting with filename and
1140 lineno here */
1141
Georg Brandl43ab1002006-05-28 20:57:09 +00001142 have_filename = (self->filename != NULL) &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001143 PyString_Check(self->filename);
Georg Brandl43ab1002006-05-28 20:57:09 +00001144 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001145
Georg Brandl43ab1002006-05-28 20:57:09 +00001146 if (!have_filename && !have_lineno)
1147 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001148
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001149 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001150 if (have_filename)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001151 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001152
Georg Brandl43ab1002006-05-28 20:57:09 +00001153 buffer = PyMem_MALLOC(bufsize);
1154 if (buffer == NULL)
1155 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001156
Georg Brandl43ab1002006-05-28 20:57:09 +00001157 if (have_filename && have_lineno)
1158 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001159 PyString_AS_STRING(str),
1160 my_basename(PyString_AS_STRING(self->filename)),
Georg Brandl43ab1002006-05-28 20:57:09 +00001161 PyInt_AsLong(self->lineno));
1162 else if (have_filename)
1163 PyOS_snprintf(buffer, bufsize, "%s (%s)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001164 PyString_AS_STRING(str),
1165 my_basename(PyString_AS_STRING(self->filename)));
Georg Brandl43ab1002006-05-28 20:57:09 +00001166 else /* only have_lineno */
1167 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001168 PyString_AS_STRING(str),
Georg Brandl43ab1002006-05-28 20:57:09 +00001169 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001170
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001171 result = PyString_FromString(buffer);
Georg Brandl43ab1002006-05-28 20:57:09 +00001172 PyMem_FREE(buffer);
1173
1174 if (result == NULL)
1175 result = str;
1176 else
1177 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001178 return result;
1179}
1180
1181static PyMemberDef SyntaxError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001182 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1183 PyDoc_STR("exception msg")},
1184 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1185 PyDoc_STR("exception filename")},
1186 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1187 PyDoc_STR("exception lineno")},
1188 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1189 PyDoc_STR("exception offset")},
1190 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1191 PyDoc_STR("exception text")},
1192 {"print_file_and_line", T_OBJECT,
1193 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1194 PyDoc_STR("exception print_file_and_line")},
1195 {NULL} /* Sentinel */
1196};
1197
1198ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1199 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001200 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001201
1202
1203/*
1204 * IndentationError extends SyntaxError
1205 */
1206MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001207 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001208
1209
1210/*
1211 * TabError extends IndentationError
1212 */
1213MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001214 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001215
1216
1217/*
1218 * LookupError extends StandardError
1219 */
1220SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001221 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001222
1223
1224/*
1225 * IndexError extends LookupError
1226 */
1227SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001228 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001229
1230
1231/*
1232 * KeyError extends LookupError
1233 */
1234static PyObject *
1235KeyError_str(PyBaseExceptionObject *self)
1236{
1237 /* If args is a tuple of exactly one item, apply repr to args[0].
1238 This is done so that e.g. the exception raised by {}[''] prints
1239 KeyError: ''
1240 rather than the confusing
1241 KeyError
1242 alone. The downside is that if KeyError is raised with an explanatory
1243 string, that string will be displayed in quotes. Too bad.
1244 If args is anything else, use the default BaseException__str__().
1245 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001246 if (PyTuple_GET_SIZE(self->args) == 1) {
1247 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001248 }
1249 return BaseException_str(self);
1250}
1251
1252ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001253 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001254
1255
1256/*
1257 * ValueError extends StandardError
1258 */
1259SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001260 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001261
1262/*
1263 * UnicodeError extends ValueError
1264 */
1265
1266SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001267 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001268
1269#ifdef Py_USING_UNICODE
Richard Jones7b9558d2006-05-27 12:29:24 +00001270static PyObject *
1271get_string(PyObject *attr, const char *name)
1272{
1273 if (!attr) {
1274 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1275 return NULL;
1276 }
1277
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001278 if (!PyString_Check(attr)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001279 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1280 return NULL;
1281 }
1282 Py_INCREF(attr);
1283 return attr;
1284}
1285
1286
1287static int
1288set_string(PyObject **attr, const char *value)
1289{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001290 PyObject *obj = PyString_FromString(value);
Richard Jones7b9558d2006-05-27 12:29:24 +00001291 if (!obj)
1292 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001293 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001294 *attr = obj;
1295 return 0;
1296}
1297
1298
1299static PyObject *
1300get_unicode(PyObject *attr, const char *name)
1301{
1302 if (!attr) {
1303 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1304 return NULL;
1305 }
1306
1307 if (!PyUnicode_Check(attr)) {
1308 PyErr_Format(PyExc_TypeError,
1309 "%.200s attribute must be unicode", name);
1310 return NULL;
1311 }
1312 Py_INCREF(attr);
1313 return attr;
1314}
1315
1316PyObject *
1317PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1318{
1319 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1320}
1321
1322PyObject *
1323PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1324{
1325 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1326}
1327
1328PyObject *
1329PyUnicodeEncodeError_GetObject(PyObject *exc)
1330{
1331 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1332}
1333
1334PyObject *
1335PyUnicodeDecodeError_GetObject(PyObject *exc)
1336{
1337 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1338}
1339
1340PyObject *
1341PyUnicodeTranslateError_GetObject(PyObject *exc)
1342{
1343 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1344}
1345
1346int
1347PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1348{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001349 Py_ssize_t size;
1350 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1351 "object");
1352 if (!obj)
1353 return -1;
1354 *start = ((PyUnicodeErrorObject *)exc)->start;
1355 size = PyUnicode_GET_SIZE(obj);
1356 if (*start<0)
1357 *start = 0; /*XXX check for values <0*/
1358 if (*start>=size)
1359 *start = size-1;
1360 Py_DECREF(obj);
1361 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001362}
1363
1364
1365int
1366PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1367{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001368 Py_ssize_t size;
1369 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1370 "object");
1371 if (!obj)
1372 return -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001373 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001374 *start = ((PyUnicodeErrorObject *)exc)->start;
1375 if (*start<0)
1376 *start = 0;
1377 if (*start>=size)
1378 *start = size-1;
1379 Py_DECREF(obj);
1380 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001381}
1382
1383
1384int
1385PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1386{
1387 return PyUnicodeEncodeError_GetStart(exc, start);
1388}
1389
1390
1391int
1392PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1393{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001394 ((PyUnicodeErrorObject *)exc)->start = start;
1395 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001396}
1397
1398
1399int
1400PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1401{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001402 ((PyUnicodeErrorObject *)exc)->start = start;
1403 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001404}
1405
1406
1407int
1408PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1409{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001410 ((PyUnicodeErrorObject *)exc)->start = start;
1411 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001412}
1413
1414
1415int
1416PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1417{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001418 Py_ssize_t size;
1419 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1420 "object");
1421 if (!obj)
1422 return -1;
1423 *end = ((PyUnicodeErrorObject *)exc)->end;
1424 size = PyUnicode_GET_SIZE(obj);
1425 if (*end<1)
1426 *end = 1;
1427 if (*end>size)
1428 *end = size;
1429 Py_DECREF(obj);
1430 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001431}
1432
1433
1434int
1435PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1436{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001437 Py_ssize_t size;
1438 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1439 "object");
1440 if (!obj)
1441 return -1;
1442 *end = ((PyUnicodeErrorObject *)exc)->end;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001443 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001444 if (*end<1)
1445 *end = 1;
1446 if (*end>size)
1447 *end = size;
1448 Py_DECREF(obj);
1449 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001450}
1451
1452
1453int
1454PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1455{
1456 return PyUnicodeEncodeError_GetEnd(exc, start);
1457}
1458
1459
1460int
1461PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1462{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001463 ((PyUnicodeErrorObject *)exc)->end = end;
1464 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001465}
1466
1467
1468int
1469PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1470{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001471 ((PyUnicodeErrorObject *)exc)->end = end;
1472 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001473}
1474
1475
1476int
1477PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1478{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001479 ((PyUnicodeErrorObject *)exc)->end = end;
1480 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001481}
1482
1483PyObject *
1484PyUnicodeEncodeError_GetReason(PyObject *exc)
1485{
1486 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1487}
1488
1489
1490PyObject *
1491PyUnicodeDecodeError_GetReason(PyObject *exc)
1492{
1493 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1494}
1495
1496
1497PyObject *
1498PyUnicodeTranslateError_GetReason(PyObject *exc)
1499{
1500 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1501}
1502
1503
1504int
1505PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1506{
1507 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1508}
1509
1510
1511int
1512PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1513{
1514 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1515}
1516
1517
1518int
1519PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1520{
1521 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1522}
1523
1524
Richard Jones7b9558d2006-05-27 12:29:24 +00001525static int
1526UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1527 PyTypeObject *objecttype)
1528{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001529 Py_CLEAR(self->encoding);
1530 Py_CLEAR(self->object);
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001531 Py_CLEAR(self->reason);
1532
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001533 if (!PyArg_ParseTuple(args, "O!O!nnO!",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001534 &PyString_Type, &self->encoding,
Richard Jones7b9558d2006-05-27 12:29:24 +00001535 objecttype, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001536 &self->start,
1537 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001538 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001539 self->encoding = self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001540 return -1;
1541 }
1542
1543 Py_INCREF(self->encoding);
1544 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001545 Py_INCREF(self->reason);
1546
1547 return 0;
1548}
1549
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001550static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001551UnicodeError_clear(PyUnicodeErrorObject *self)
1552{
1553 Py_CLEAR(self->encoding);
1554 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001555 Py_CLEAR(self->reason);
1556 return BaseException_clear((PyBaseExceptionObject *)self);
1557}
1558
1559static void
1560UnicodeError_dealloc(PyUnicodeErrorObject *self)
1561{
Georg Brandl38f62372006-09-06 06:50:05 +00001562 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001563 UnicodeError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001564 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001565}
1566
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001567static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001568UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1569{
1570 Py_VISIT(self->encoding);
1571 Py_VISIT(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001572 Py_VISIT(self->reason);
1573 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1574}
1575
1576static PyMemberDef UnicodeError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001577 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1578 PyDoc_STR("exception encoding")},
1579 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1580 PyDoc_STR("exception object")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001581 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001582 PyDoc_STR("exception start")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001583 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001584 PyDoc_STR("exception end")},
1585 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1586 PyDoc_STR("exception reason")},
1587 {NULL} /* Sentinel */
1588};
1589
1590
1591/*
1592 * UnicodeEncodeError extends UnicodeError
1593 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001594
1595static int
1596UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1597{
1598 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1599 return -1;
1600 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1601 kwds, &PyUnicode_Type);
1602}
1603
1604static PyObject *
1605UnicodeEncodeError_str(PyObject *self)
1606{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001607 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001608
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001609 if (uself->end==uself->start+1) {
1610 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001611 char badchar_str[20];
1612 if (badchar <= 0xff)
1613 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1614 else if (badchar <= 0xffff)
1615 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1616 else
1617 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001618 return PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001619 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001620 PyString_AS_STRING(uself->encoding),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001621 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001622 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001623 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001624 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001625 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001626 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001627 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001628 PyString_AS_STRING(uself->encoding),
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001629 uself->start,
1630 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001631 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001632 );
1633}
1634
1635static PyTypeObject _PyExc_UnicodeEncodeError = {
1636 PyObject_HEAD_INIT(NULL)
1637 0,
Georg Brandl38f62372006-09-06 06:50:05 +00001638 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001639 sizeof(PyUnicodeErrorObject), 0,
1640 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1641 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1642 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001643 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1644 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001645 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001646 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001647};
1648PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1649
1650PyObject *
1651PyUnicodeEncodeError_Create(
1652 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1653 Py_ssize_t start, Py_ssize_t end, const char *reason)
1654{
1655 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001656 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001657}
1658
1659
1660/*
1661 * UnicodeDecodeError extends UnicodeError
1662 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001663
1664static int
1665UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1666{
1667 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1668 return -1;
1669 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001670 kwds, &PyString_Type);
Richard Jones7b9558d2006-05-27 12:29:24 +00001671}
1672
1673static PyObject *
1674UnicodeDecodeError_str(PyObject *self)
1675{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001676 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001677
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001678 if (uself->end==uself->start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001679 /* FromFormat does not support %02x, so format that separately */
1680 char byte[4];
1681 PyOS_snprintf(byte, sizeof(byte), "%02x",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001682 ((int)PyString_AS_STRING(uself->object)[uself->start])&0xff);
1683 return PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001684 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001685 PyString_AS_STRING(uself->encoding),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001686 byte,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001687 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001688 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001689 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001690 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001691 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001692 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001693 PyString_AS_STRING(uself->encoding),
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001694 uself->start,
1695 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001696 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001697 );
1698}
1699
1700static PyTypeObject _PyExc_UnicodeDecodeError = {
1701 PyObject_HEAD_INIT(NULL)
1702 0,
1703 EXC_MODULE_NAME "UnicodeDecodeError",
1704 sizeof(PyUnicodeErrorObject), 0,
1705 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1706 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1707 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001708 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1709 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001710 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001711 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001712};
1713PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1714
1715PyObject *
1716PyUnicodeDecodeError_Create(
1717 const char *encoding, const char *object, Py_ssize_t length,
1718 Py_ssize_t start, Py_ssize_t end, const char *reason)
1719{
1720 assert(length < INT_MAX);
1721 assert(start < INT_MAX);
1722 assert(end < INT_MAX);
1723 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001724 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001725}
1726
1727
1728/*
1729 * UnicodeTranslateError extends UnicodeError
1730 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001731
1732static int
1733UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1734 PyObject *kwds)
1735{
1736 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1737 return -1;
1738
1739 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001740 Py_CLEAR(self->reason);
1741
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001742 if (!PyArg_ParseTuple(args, "O!nnO!",
Richard Jones7b9558d2006-05-27 12:29:24 +00001743 &PyUnicode_Type, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001744 &self->start,
1745 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001746 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001747 self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001748 return -1;
1749 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001750
Richard Jones7b9558d2006-05-27 12:29:24 +00001751 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001752 Py_INCREF(self->reason);
1753
1754 return 0;
1755}
1756
1757
1758static PyObject *
1759UnicodeTranslateError_str(PyObject *self)
1760{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001761 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001762
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001763 if (uself->end==uself->start+1) {
1764 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001765 char badchar_str[20];
1766 if (badchar <= 0xff)
1767 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1768 else if (badchar <= 0xffff)
1769 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1770 else
1771 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001772 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001773 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001774 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001775 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001776 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001777 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001778 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001779 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001780 "can't translate characters in position %zd-%zd: %.400s",
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001781 uself->start,
1782 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001783 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001784 );
1785}
1786
1787static PyTypeObject _PyExc_UnicodeTranslateError = {
1788 PyObject_HEAD_INIT(NULL)
1789 0,
1790 EXC_MODULE_NAME "UnicodeTranslateError",
1791 sizeof(PyUnicodeErrorObject), 0,
1792 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1793 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1794 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001795 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001796 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1797 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001798 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001799};
1800PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1801
1802PyObject *
1803PyUnicodeTranslateError_Create(
1804 const Py_UNICODE *object, Py_ssize_t length,
1805 Py_ssize_t start, Py_ssize_t end, const char *reason)
1806{
1807 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001808 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001809}
1810#endif
1811
1812
1813/*
1814 * AssertionError extends StandardError
1815 */
1816SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001817 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001818
1819
1820/*
1821 * ArithmeticError extends StandardError
1822 */
1823SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001824 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001825
1826
1827/*
1828 * FloatingPointError extends ArithmeticError
1829 */
1830SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001831 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001832
1833
1834/*
1835 * OverflowError extends ArithmeticError
1836 */
1837SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001838 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001839
1840
1841/*
1842 * ZeroDivisionError extends ArithmeticError
1843 */
1844SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001845 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001846
1847
1848/*
1849 * SystemError extends StandardError
1850 */
1851SimpleExtendsException(PyExc_StandardError, SystemError,
1852 "Internal error in the Python interpreter.\n"
1853 "\n"
1854 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001855 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001856
1857
1858/*
1859 * ReferenceError extends StandardError
1860 */
1861SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001862 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001863
1864
1865/*
1866 * MemoryError extends StandardError
1867 */
Richard Jones2d555b32006-05-27 16:15:11 +00001868SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001869
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00001870/*
1871 * BufferError extends StandardError
1872 */
1873SimpleExtendsException(PyExc_StandardError, BufferError, "Buffer error.");
1874
Richard Jones7b9558d2006-05-27 12:29:24 +00001875
1876/* Warning category docstrings */
1877
1878/*
1879 * Warning extends Exception
1880 */
1881SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001882 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001883
1884
1885/*
1886 * UserWarning extends Warning
1887 */
1888SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001889 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001890
1891
1892/*
1893 * DeprecationWarning extends Warning
1894 */
1895SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001896 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001897
1898
1899/*
1900 * PendingDeprecationWarning extends Warning
1901 */
1902SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1903 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001904 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001905
1906
1907/*
1908 * SyntaxWarning extends Warning
1909 */
1910SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001911 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001912
1913
1914/*
1915 * RuntimeWarning extends Warning
1916 */
1917SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001918 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001919
1920
1921/*
1922 * FutureWarning extends Warning
1923 */
1924SimpleExtendsException(PyExc_Warning, FutureWarning,
1925 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001926 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001927
1928
1929/*
1930 * ImportWarning extends Warning
1931 */
1932SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001933 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00001934
1935
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001936/*
1937 * UnicodeWarning extends Warning
1938 */
1939SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1940 "Base class for warnings about Unicode related problems, mostly\n"
1941 "related to conversion problems.");
1942
Christian Heimes1a6387e2008-03-26 12:49:49 +00001943/*
1944 * BytesWarning extends Warning
1945 */
1946SimpleExtendsException(PyExc_Warning, BytesWarning,
1947 "Base class for warnings about bytes and buffer related problems, mostly\n"
1948 "related to conversion from str or comparing to str.");
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001949
Richard Jones7b9558d2006-05-27 12:29:24 +00001950/* Pre-computed MemoryError instance. Best to create this as early as
1951 * possible and not wait until a MemoryError is actually raised!
1952 */
1953PyObject *PyExc_MemoryErrorInst=NULL;
1954
Brett Cannon1e534b52007-09-07 04:18:30 +00001955/* Pre-computed RuntimeError instance for when recursion depth is reached.
1956 Meant to be used when normalizing the exception for exceeding the recursion
1957 depth will cause its own infinite recursion.
1958*/
1959PyObject *PyExc_RecursionErrorInst = NULL;
1960
Richard Jones7b9558d2006-05-27 12:29:24 +00001961/* module global functions */
1962static PyMethodDef functions[] = {
1963 /* Sentinel */
1964 {NULL, NULL}
1965};
1966
1967#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1968 Py_FatalError("exceptions bootstrapping error.");
1969
1970#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1971 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1972 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1973 Py_FatalError("Module dictionary insertion problem.");
1974
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00001975#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00001976/* crt variable checking in VisualStudio .NET 2005 */
1977#include <crtdbg.h>
1978
1979static int prevCrtReportMode;
1980static _invalid_parameter_handler prevCrtHandler;
1981
1982/* Invalid parameter handler. Sets a ValueError exception */
1983static void
1984InvalidParameterHandler(
1985 const wchar_t * expression,
1986 const wchar_t * function,
1987 const wchar_t * file,
1988 unsigned int line,
1989 uintptr_t pReserved)
1990{
1991 /* Do nothing, allow execution to continue. Usually this
1992 * means that the CRT will set errno to EINVAL
1993 */
1994}
1995#endif
1996
1997
Richard Jones7b9558d2006-05-27 12:29:24 +00001998PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001999_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00002000{
2001 PyObject *m, *bltinmod, *bdict;
2002
2003 PRE_INIT(BaseException)
2004 PRE_INIT(Exception)
2005 PRE_INIT(StandardError)
2006 PRE_INIT(TypeError)
2007 PRE_INIT(StopIteration)
2008 PRE_INIT(GeneratorExit)
2009 PRE_INIT(SystemExit)
2010 PRE_INIT(KeyboardInterrupt)
2011 PRE_INIT(ImportError)
2012 PRE_INIT(EnvironmentError)
2013 PRE_INIT(IOError)
2014 PRE_INIT(OSError)
2015#ifdef MS_WINDOWS
2016 PRE_INIT(WindowsError)
2017#endif
2018#ifdef __VMS
2019 PRE_INIT(VMSError)
2020#endif
2021 PRE_INIT(EOFError)
2022 PRE_INIT(RuntimeError)
2023 PRE_INIT(NotImplementedError)
2024 PRE_INIT(NameError)
2025 PRE_INIT(UnboundLocalError)
2026 PRE_INIT(AttributeError)
2027 PRE_INIT(SyntaxError)
2028 PRE_INIT(IndentationError)
2029 PRE_INIT(TabError)
2030 PRE_INIT(LookupError)
2031 PRE_INIT(IndexError)
2032 PRE_INIT(KeyError)
2033 PRE_INIT(ValueError)
2034 PRE_INIT(UnicodeError)
2035#ifdef Py_USING_UNICODE
2036 PRE_INIT(UnicodeEncodeError)
2037 PRE_INIT(UnicodeDecodeError)
2038 PRE_INIT(UnicodeTranslateError)
2039#endif
2040 PRE_INIT(AssertionError)
2041 PRE_INIT(ArithmeticError)
2042 PRE_INIT(FloatingPointError)
2043 PRE_INIT(OverflowError)
2044 PRE_INIT(ZeroDivisionError)
2045 PRE_INIT(SystemError)
2046 PRE_INIT(ReferenceError)
2047 PRE_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002048 PRE_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002049 PRE_INIT(Warning)
2050 PRE_INIT(UserWarning)
2051 PRE_INIT(DeprecationWarning)
2052 PRE_INIT(PendingDeprecationWarning)
2053 PRE_INIT(SyntaxWarning)
2054 PRE_INIT(RuntimeWarning)
2055 PRE_INIT(FutureWarning)
2056 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002057 PRE_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002058 PRE_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002059
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002060 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2061 (PyObject *)NULL, PYTHON_API_VERSION);
Richard Jones7b9558d2006-05-27 12:29:24 +00002062 if (m == NULL) return;
2063
2064 bltinmod = PyImport_ImportModule("__builtin__");
2065 if (bltinmod == NULL)
2066 Py_FatalError("exceptions bootstrapping error.");
2067 bdict = PyModule_GetDict(bltinmod);
2068 if (bdict == NULL)
2069 Py_FatalError("exceptions bootstrapping error.");
2070
2071 POST_INIT(BaseException)
2072 POST_INIT(Exception)
2073 POST_INIT(StandardError)
2074 POST_INIT(TypeError)
2075 POST_INIT(StopIteration)
2076 POST_INIT(GeneratorExit)
2077 POST_INIT(SystemExit)
2078 POST_INIT(KeyboardInterrupt)
2079 POST_INIT(ImportError)
2080 POST_INIT(EnvironmentError)
2081 POST_INIT(IOError)
2082 POST_INIT(OSError)
2083#ifdef MS_WINDOWS
2084 POST_INIT(WindowsError)
2085#endif
2086#ifdef __VMS
2087 POST_INIT(VMSError)
2088#endif
2089 POST_INIT(EOFError)
2090 POST_INIT(RuntimeError)
2091 POST_INIT(NotImplementedError)
2092 POST_INIT(NameError)
2093 POST_INIT(UnboundLocalError)
2094 POST_INIT(AttributeError)
2095 POST_INIT(SyntaxError)
2096 POST_INIT(IndentationError)
2097 POST_INIT(TabError)
2098 POST_INIT(LookupError)
2099 POST_INIT(IndexError)
2100 POST_INIT(KeyError)
2101 POST_INIT(ValueError)
2102 POST_INIT(UnicodeError)
2103#ifdef Py_USING_UNICODE
2104 POST_INIT(UnicodeEncodeError)
2105 POST_INIT(UnicodeDecodeError)
2106 POST_INIT(UnicodeTranslateError)
2107#endif
2108 POST_INIT(AssertionError)
2109 POST_INIT(ArithmeticError)
2110 POST_INIT(FloatingPointError)
2111 POST_INIT(OverflowError)
2112 POST_INIT(ZeroDivisionError)
2113 POST_INIT(SystemError)
2114 POST_INIT(ReferenceError)
2115 POST_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002116 POST_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002117 POST_INIT(Warning)
2118 POST_INIT(UserWarning)
2119 POST_INIT(DeprecationWarning)
2120 POST_INIT(PendingDeprecationWarning)
2121 POST_INIT(SyntaxWarning)
2122 POST_INIT(RuntimeWarning)
2123 POST_INIT(FutureWarning)
2124 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002125 POST_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002126 POST_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002127
2128 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2129 if (!PyExc_MemoryErrorInst)
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +00002130 Py_FatalError("Cannot pre-allocate MemoryError instance");
Richard Jones7b9558d2006-05-27 12:29:24 +00002131
Brett Cannon1e534b52007-09-07 04:18:30 +00002132 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2133 if (!PyExc_RecursionErrorInst)
2134 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2135 "recursion errors");
2136 else {
2137 PyBaseExceptionObject *err_inst =
2138 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2139 PyObject *args_tuple;
2140 PyObject *exc_message;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002141 exc_message = PyString_FromString("maximum recursion depth exceeded");
Brett Cannon1e534b52007-09-07 04:18:30 +00002142 if (!exc_message)
2143 Py_FatalError("cannot allocate argument for RuntimeError "
2144 "pre-allocation");
2145 args_tuple = PyTuple_Pack(1, exc_message);
2146 if (!args_tuple)
2147 Py_FatalError("cannot allocate tuple for RuntimeError "
2148 "pre-allocation");
2149 Py_DECREF(exc_message);
2150 if (BaseException_init(err_inst, args_tuple, NULL))
2151 Py_FatalError("init of pre-allocated RuntimeError failed");
2152 Py_DECREF(args_tuple);
2153 }
2154
Richard Jones7b9558d2006-05-27 12:29:24 +00002155 Py_DECREF(bltinmod);
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002156
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002157#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002158 /* Set CRT argument error handler */
2159 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
2160 /* turn off assertions in debug mode */
2161 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
2162#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002163}
2164
2165void
2166_PyExc_Fini(void)
2167{
2168 Py_XDECREF(PyExc_MemoryErrorInst);
2169 PyExc_MemoryErrorInst = NULL;
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002170#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002171 /* reset CRT error handling */
2172 _set_invalid_parameter_handler(prevCrtHandler);
2173 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
2174#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002175}