blob: 0a5282023a7282ef20de94058a5bf98811177976 [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{
Georg Brandl89971032009-09-16 20:34:51 +0000304 PyObject *msg;
305
306 /* if "message" is in self->dict, accessing a user-set message attribute */
307 if (self->dict &&
308 (msg = PyDict_GetItemString(self->dict, "message"))) {
309 Py_INCREF(msg);
310 return msg;
311 }
Brett Cannon229cee22007-05-05 01:34:02 +0000312
Georg Brandl89971032009-09-16 20:34:51 +0000313 if (self->message == NULL) {
314 PyErr_SetString(PyExc_AttributeError, "message attribute was deleted");
315 return NULL;
316 }
317
318 /* accessing the deprecated "builtin" message attribute of Exception */
319 if (PyErr_WarnEx(PyExc_DeprecationWarning,
320 "BaseException.message has been deprecated as "
321 "of Python 2.6", 1) < 0)
322 return NULL;
323
324 Py_INCREF(self->message);
325 return self->message;
Brett Cannon229cee22007-05-05 01:34:02 +0000326}
327
328static int
329BaseException_set_message(PyBaseExceptionObject *self, PyObject *val)
330{
Georg Brandl89971032009-09-16 20:34:51 +0000331 /* if val is NULL, delete the message attribute */
332 if (val == NULL) {
333 if (self->dict && PyDict_GetItemString(self->dict, "message")) {
334 if (PyDict_DelItemString(self->dict, "message") < 0)
335 return -1;
336 }
337 Py_XDECREF(self->message);
338 self->message = NULL;
339 return 0;
340 }
341
342 /* else set it in __dict__, but may need to create the dict first */
343 if (self->dict == NULL) {
344 self->dict = PyDict_New();
345 if (!self->dict)
346 return -1;
347 }
348 return PyDict_SetItemString(self->dict, "message", val);
Brett Cannon229cee22007-05-05 01:34:02 +0000349}
350
Richard Jones7b9558d2006-05-27 12:29:24 +0000351static PyGetSetDef BaseException_getset[] = {
352 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
353 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Brett Cannon229cee22007-05-05 01:34:02 +0000354 {"message", (getter)BaseException_get_message,
355 (setter)BaseException_set_message},
Richard Jones7b9558d2006-05-27 12:29:24 +0000356 {NULL},
357};
358
359
360static PyTypeObject _PyExc_BaseException = {
361 PyObject_HEAD_INIT(NULL)
362 0, /*ob_size*/
363 EXC_MODULE_NAME "BaseException", /*tp_name*/
364 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
365 0, /*tp_itemsize*/
366 (destructor)BaseException_dealloc, /*tp_dealloc*/
367 0, /*tp_print*/
368 0, /*tp_getattr*/
369 0, /*tp_setattr*/
370 0, /* tp_compare; */
371 (reprfunc)BaseException_repr, /*tp_repr*/
372 0, /*tp_as_number*/
373 &BaseException_as_sequence, /*tp_as_sequence*/
374 0, /*tp_as_mapping*/
375 0, /*tp_hash */
376 0, /*tp_call*/
377 (reprfunc)BaseException_str, /*tp_str*/
378 PyObject_GenericGetAttr, /*tp_getattro*/
379 PyObject_GenericSetAttr, /*tp_setattro*/
380 0, /*tp_as_buffer*/
Neal Norwitzee3a1b52007-02-25 19:44:48 +0000381 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
382 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Richard Jones7b9558d2006-05-27 12:29:24 +0000383 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
384 (traverseproc)BaseException_traverse, /* tp_traverse */
385 (inquiry)BaseException_clear, /* tp_clear */
386 0, /* tp_richcompare */
387 0, /* tp_weaklistoffset */
388 0, /* tp_iter */
389 0, /* tp_iternext */
390 BaseException_methods, /* tp_methods */
Brett Cannon229cee22007-05-05 01:34:02 +0000391 0, /* tp_members */
Richard Jones7b9558d2006-05-27 12:29:24 +0000392 BaseException_getset, /* tp_getset */
393 0, /* tp_base */
394 0, /* tp_dict */
395 0, /* tp_descr_get */
396 0, /* tp_descr_set */
397 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
398 (initproc)BaseException_init, /* tp_init */
399 0, /* tp_alloc */
400 BaseException_new, /* tp_new */
401};
402/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
403from the previous implmentation and also allowing Python objects to be used
404in the API */
405PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
406
Richard Jones2d555b32006-05-27 16:15:11 +0000407/* note these macros omit the last semicolon so the macro invocation may
408 * include it and not look strange.
409 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000410#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
411static PyTypeObject _PyExc_ ## EXCNAME = { \
412 PyObject_HEAD_INIT(NULL) \
413 0, \
414 EXC_MODULE_NAME # EXCNAME, \
415 sizeof(PyBaseExceptionObject), \
416 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
417 0, 0, 0, 0, 0, 0, 0, \
418 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
419 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
420 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
421 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
422 (initproc)BaseException_init, 0, BaseException_new,\
423}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000424PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000425
426#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
427static PyTypeObject _PyExc_ ## EXCNAME = { \
428 PyObject_HEAD_INIT(NULL) \
429 0, \
430 EXC_MODULE_NAME # EXCNAME, \
431 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000432 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000433 0, 0, 0, 0, 0, \
434 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000435 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
436 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000437 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000438 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000439}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000440PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000441
442#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
443static PyTypeObject _PyExc_ ## EXCNAME = { \
444 PyObject_HEAD_INIT(NULL) \
445 0, \
446 EXC_MODULE_NAME # EXCNAME, \
447 sizeof(Py ## EXCSTORE ## Object), 0, \
448 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
449 (reprfunc)EXCSTR, 0, 0, 0, \
450 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
451 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
452 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
453 EXCMEMBERS, 0, &_ ## EXCBASE, \
454 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000455 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000456}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000457PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000458
459
460/*
461 * Exception extends BaseException
462 */
463SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000464 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000465
466
467/*
468 * StandardError extends Exception
469 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000470SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000471 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000472 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000473
474
475/*
476 * TypeError extends StandardError
477 */
478SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000479 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000480
481
482/*
483 * StopIteration extends Exception
484 */
485SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000486 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000487
488
489/*
Christian Heimes44eeaec2007-12-03 20:01:02 +0000490 * GeneratorExit extends BaseException
Richard Jones7b9558d2006-05-27 12:29:24 +0000491 */
Christian Heimes44eeaec2007-12-03 20:01:02 +0000492SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000493 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000494
495
496/*
497 * SystemExit extends BaseException
498 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000499
500static int
501SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
502{
503 Py_ssize_t size = PyTuple_GET_SIZE(args);
504
505 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
506 return -1;
507
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000508 if (size == 0)
509 return 0;
510 Py_CLEAR(self->code);
Richard Jones7b9558d2006-05-27 12:29:24 +0000511 if (size == 1)
512 self->code = PyTuple_GET_ITEM(args, 0);
513 else if (size > 1)
514 self->code = args;
515 Py_INCREF(self->code);
516 return 0;
517}
518
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000519static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000520SystemExit_clear(PySystemExitObject *self)
521{
522 Py_CLEAR(self->code);
523 return BaseException_clear((PyBaseExceptionObject *)self);
524}
525
526static void
527SystemExit_dealloc(PySystemExitObject *self)
528{
Georg Brandl38f62372006-09-06 06:50:05 +0000529 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000530 SystemExit_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000531 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000532}
533
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000534static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000535SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
536{
537 Py_VISIT(self->code);
538 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
539}
540
541static PyMemberDef SystemExit_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000542 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
543 PyDoc_STR("exception code")},
544 {NULL} /* Sentinel */
545};
546
547ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
548 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000549 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000550
551/*
552 * KeyboardInterrupt extends BaseException
553 */
554SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000555 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000556
557
558/*
559 * ImportError extends StandardError
560 */
561SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000562 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000563
564
565/*
566 * EnvironmentError extends StandardError
567 */
568
Richard Jones7b9558d2006-05-27 12:29:24 +0000569/* Where a function has a single filename, such as open() or some
570 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
571 * called, giving a third argument which is the filename. But, so
572 * that old code using in-place unpacking doesn't break, e.g.:
573 *
574 * except IOError, (errno, strerror):
575 *
576 * we hack args so that it only contains two items. This also
577 * means we need our own __str__() which prints out the filename
578 * when it was supplied.
579 */
580static int
581EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
582 PyObject *kwds)
583{
584 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
585 PyObject *subslice = NULL;
586
587 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
588 return -1;
589
Georg Brandl3267d282006-09-30 09:03:42 +0000590 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000591 return 0;
592 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000593
594 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000595 &myerrno, &strerror, &filename)) {
596 return -1;
597 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000598 Py_CLEAR(self->myerrno); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000599 self->myerrno = myerrno;
600 Py_INCREF(self->myerrno);
601
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000602 Py_CLEAR(self->strerror); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000603 self->strerror = strerror;
604 Py_INCREF(self->strerror);
605
606 /* self->filename will remain Py_None otherwise */
607 if (filename != NULL) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000608 Py_CLEAR(self->filename); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000609 self->filename = filename;
610 Py_INCREF(self->filename);
611
612 subslice = PyTuple_GetSlice(args, 0, 2);
613 if (!subslice)
614 return -1;
615
616 Py_DECREF(self->args); /* replacing args */
617 self->args = subslice;
618 }
619 return 0;
620}
621
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000622static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000623EnvironmentError_clear(PyEnvironmentErrorObject *self)
624{
625 Py_CLEAR(self->myerrno);
626 Py_CLEAR(self->strerror);
627 Py_CLEAR(self->filename);
628 return BaseException_clear((PyBaseExceptionObject *)self);
629}
630
631static void
632EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
633{
Georg Brandl38f62372006-09-06 06:50:05 +0000634 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000635 EnvironmentError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000636 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000637}
638
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000639static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000640EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
641 void *arg)
642{
643 Py_VISIT(self->myerrno);
644 Py_VISIT(self->strerror);
645 Py_VISIT(self->filename);
646 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
647}
648
649static PyObject *
650EnvironmentError_str(PyEnvironmentErrorObject *self)
651{
652 PyObject *rtnval = NULL;
653
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000654 if (self->filename) {
655 PyObject *fmt;
656 PyObject *repr;
657 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000658
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000659 fmt = PyString_FromString("[Errno %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000660 if (!fmt)
661 return NULL;
662
663 repr = PyObject_Repr(self->filename);
664 if (!repr) {
665 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000666 return NULL;
667 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000668 tuple = PyTuple_New(3);
669 if (!tuple) {
670 Py_DECREF(repr);
671 Py_DECREF(fmt);
672 return NULL;
673 }
674
675 if (self->myerrno) {
676 Py_INCREF(self->myerrno);
677 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
678 }
679 else {
680 Py_INCREF(Py_None);
681 PyTuple_SET_ITEM(tuple, 0, Py_None);
682 }
683 if (self->strerror) {
684 Py_INCREF(self->strerror);
685 PyTuple_SET_ITEM(tuple, 1, self->strerror);
686 }
687 else {
688 Py_INCREF(Py_None);
689 PyTuple_SET_ITEM(tuple, 1, Py_None);
690 }
691
Richard Jones7b9558d2006-05-27 12:29:24 +0000692 PyTuple_SET_ITEM(tuple, 2, repr);
693
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000694 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000695
696 Py_DECREF(fmt);
697 Py_DECREF(tuple);
698 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000699 else if (self->myerrno && self->strerror) {
700 PyObject *fmt;
701 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000702
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000703 fmt = PyString_FromString("[Errno %s] %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000704 if (!fmt)
705 return NULL;
706
707 tuple = PyTuple_New(2);
708 if (!tuple) {
709 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000710 return NULL;
711 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000712
713 if (self->myerrno) {
714 Py_INCREF(self->myerrno);
715 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
716 }
717 else {
718 Py_INCREF(Py_None);
719 PyTuple_SET_ITEM(tuple, 0, Py_None);
720 }
721 if (self->strerror) {
722 Py_INCREF(self->strerror);
723 PyTuple_SET_ITEM(tuple, 1, self->strerror);
724 }
725 else {
726 Py_INCREF(Py_None);
727 PyTuple_SET_ITEM(tuple, 1, Py_None);
728 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000729
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000730 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000731
732 Py_DECREF(fmt);
733 Py_DECREF(tuple);
734 }
735 else
736 rtnval = BaseException_str((PyBaseExceptionObject *)self);
737
738 return rtnval;
739}
740
741static PyMemberDef EnvironmentError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000742 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000743 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000744 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000745 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000746 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000747 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000748 {NULL} /* Sentinel */
749};
750
751
752static PyObject *
753EnvironmentError_reduce(PyEnvironmentErrorObject *self)
754{
755 PyObject *args = self->args;
756 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000757
Richard Jones7b9558d2006-05-27 12:29:24 +0000758 /* self->args is only the first two real arguments if there was a
759 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000760 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000761 args = PyTuple_New(3);
762 if (!args) return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000763
764 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000765 Py_INCREF(tmp);
766 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000767
768 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000769 Py_INCREF(tmp);
770 PyTuple_SET_ITEM(args, 1, tmp);
771
772 Py_INCREF(self->filename);
773 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000774 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000775 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000776
777 if (self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000778 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000779 else
Christian Heimese93237d2007-12-19 02:37:44 +0000780 res = PyTuple_Pack(2, Py_TYPE(self), args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000781 Py_DECREF(args);
782 return res;
783}
784
785
786static PyMethodDef EnvironmentError_methods[] = {
787 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
788 {NULL}
789};
790
791ComplexExtendsException(PyExc_StandardError, EnvironmentError,
792 EnvironmentError, EnvironmentError_dealloc,
793 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000794 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000795 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000796
797
798/*
799 * IOError extends EnvironmentError
800 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000801MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000802 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000803
804
805/*
806 * OSError extends EnvironmentError
807 */
808MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000809 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000810
811
812/*
813 * WindowsError extends OSError
814 */
815#ifdef MS_WINDOWS
816#include "errmap.h"
817
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000818static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000819WindowsError_clear(PyWindowsErrorObject *self)
820{
821 Py_CLEAR(self->myerrno);
822 Py_CLEAR(self->strerror);
823 Py_CLEAR(self->filename);
824 Py_CLEAR(self->winerror);
825 return BaseException_clear((PyBaseExceptionObject *)self);
826}
827
828static void
829WindowsError_dealloc(PyWindowsErrorObject *self)
830{
Georg Brandl38f62372006-09-06 06:50:05 +0000831 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000832 WindowsError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000833 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000834}
835
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000836static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000837WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
838{
839 Py_VISIT(self->myerrno);
840 Py_VISIT(self->strerror);
841 Py_VISIT(self->filename);
842 Py_VISIT(self->winerror);
843 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
844}
845
Richard Jones7b9558d2006-05-27 12:29:24 +0000846static int
847WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
848{
849 PyObject *o_errcode = NULL;
850 long errcode;
851 long posix_errno;
852
853 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
854 == -1)
855 return -1;
856
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000857 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000858 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000859
860 /* Set errno to the POSIX errno, and winerror to the Win32
861 error code. */
862 errcode = PyInt_AsLong(self->myerrno);
863 if (errcode == -1 && PyErr_Occurred())
864 return -1;
865 posix_errno = winerror_to_errno(errcode);
866
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000867 Py_CLEAR(self->winerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000868 self->winerror = self->myerrno;
869
870 o_errcode = PyInt_FromLong(posix_errno);
871 if (!o_errcode)
872 return -1;
873
874 self->myerrno = o_errcode;
875
876 return 0;
877}
878
879
880static PyObject *
881WindowsError_str(PyWindowsErrorObject *self)
882{
Richard Jones7b9558d2006-05-27 12:29:24 +0000883 PyObject *rtnval = NULL;
884
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000885 if (self->filename) {
886 PyObject *fmt;
887 PyObject *repr;
888 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000889
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000890 fmt = PyString_FromString("[Error %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000891 if (!fmt)
892 return NULL;
893
894 repr = PyObject_Repr(self->filename);
895 if (!repr) {
896 Py_DECREF(fmt);
897 return NULL;
898 }
899 tuple = PyTuple_New(3);
900 if (!tuple) {
901 Py_DECREF(repr);
902 Py_DECREF(fmt);
903 return NULL;
904 }
905
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000906 if (self->winerror) {
907 Py_INCREF(self->winerror);
908 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000909 }
910 else {
911 Py_INCREF(Py_None);
912 PyTuple_SET_ITEM(tuple, 0, Py_None);
913 }
914 if (self->strerror) {
915 Py_INCREF(self->strerror);
916 PyTuple_SET_ITEM(tuple, 1, self->strerror);
917 }
918 else {
919 Py_INCREF(Py_None);
920 PyTuple_SET_ITEM(tuple, 1, Py_None);
921 }
922
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000923 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000924
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000925 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000926
927 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000928 Py_DECREF(tuple);
929 }
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000930 else if (self->winerror && self->strerror) {
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000931 PyObject *fmt;
932 PyObject *tuple;
933
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000934 fmt = PyString_FromString("[Error %s] %s");
Richard Jones7b9558d2006-05-27 12:29:24 +0000935 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000936 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000937
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000938 tuple = PyTuple_New(2);
939 if (!tuple) {
940 Py_DECREF(fmt);
941 return NULL;
942 }
943
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000944 if (self->winerror) {
945 Py_INCREF(self->winerror);
946 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000947 }
948 else {
949 Py_INCREF(Py_None);
950 PyTuple_SET_ITEM(tuple, 0, Py_None);
951 }
952 if (self->strerror) {
953 Py_INCREF(self->strerror);
954 PyTuple_SET_ITEM(tuple, 1, self->strerror);
955 }
956 else {
957 Py_INCREF(Py_None);
958 PyTuple_SET_ITEM(tuple, 1, Py_None);
959 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000960
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000961 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000962
963 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000964 Py_DECREF(tuple);
965 }
966 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000967 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000968
Richard Jones7b9558d2006-05-27 12:29:24 +0000969 return rtnval;
970}
971
972static PyMemberDef WindowsError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000973 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000974 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000975 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000976 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000977 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000978 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000979 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000980 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000981 {NULL} /* Sentinel */
982};
983
Richard Jones2d555b32006-05-27 16:15:11 +0000984ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
985 WindowsError_dealloc, 0, WindowsError_members,
986 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000987
988#endif /* MS_WINDOWS */
989
990
991/*
992 * VMSError extends OSError (I think)
993 */
994#ifdef __VMS
995MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +0000996 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000997#endif
998
999
1000/*
1001 * EOFError extends StandardError
1002 */
1003SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +00001004 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001005
1006
1007/*
1008 * RuntimeError extends StandardError
1009 */
1010SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001011 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001012
1013
1014/*
1015 * NotImplementedError extends RuntimeError
1016 */
1017SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +00001018 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001019
1020/*
1021 * NameError extends StandardError
1022 */
1023SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +00001024 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001025
1026/*
1027 * UnboundLocalError extends NameError
1028 */
1029SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +00001030 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001031
1032/*
1033 * AttributeError extends StandardError
1034 */
1035SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001036 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001037
1038
1039/*
1040 * SyntaxError extends StandardError
1041 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001042
1043static int
1044SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
1045{
1046 PyObject *info = NULL;
1047 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
1048
1049 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1050 return -1;
1051
1052 if (lenargs >= 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001053 Py_CLEAR(self->msg);
Richard Jones7b9558d2006-05-27 12:29:24 +00001054 self->msg = PyTuple_GET_ITEM(args, 0);
1055 Py_INCREF(self->msg);
1056 }
1057 if (lenargs == 2) {
1058 info = PyTuple_GET_ITEM(args, 1);
1059 info = PySequence_Tuple(info);
1060 if (!info) return -1;
1061
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001062 if (PyTuple_GET_SIZE(info) != 4) {
1063 /* not a very good error message, but it's what Python 2.4 gives */
1064 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
1065 Py_DECREF(info);
1066 return -1;
1067 }
1068
1069 Py_CLEAR(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001070 self->filename = PyTuple_GET_ITEM(info, 0);
1071 Py_INCREF(self->filename);
1072
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001073 Py_CLEAR(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001074 self->lineno = PyTuple_GET_ITEM(info, 1);
1075 Py_INCREF(self->lineno);
1076
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001077 Py_CLEAR(self->offset);
Richard Jones7b9558d2006-05-27 12:29:24 +00001078 self->offset = PyTuple_GET_ITEM(info, 2);
1079 Py_INCREF(self->offset);
1080
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001081 Py_CLEAR(self->text);
Richard Jones7b9558d2006-05-27 12:29:24 +00001082 self->text = PyTuple_GET_ITEM(info, 3);
1083 Py_INCREF(self->text);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001084
1085 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001086 }
1087 return 0;
1088}
1089
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001090static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001091SyntaxError_clear(PySyntaxErrorObject *self)
1092{
1093 Py_CLEAR(self->msg);
1094 Py_CLEAR(self->filename);
1095 Py_CLEAR(self->lineno);
1096 Py_CLEAR(self->offset);
1097 Py_CLEAR(self->text);
1098 Py_CLEAR(self->print_file_and_line);
1099 return BaseException_clear((PyBaseExceptionObject *)self);
1100}
1101
1102static void
1103SyntaxError_dealloc(PySyntaxErrorObject *self)
1104{
Georg Brandl38f62372006-09-06 06:50:05 +00001105 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001106 SyntaxError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001107 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001108}
1109
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001110static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001111SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1112{
1113 Py_VISIT(self->msg);
1114 Py_VISIT(self->filename);
1115 Py_VISIT(self->lineno);
1116 Py_VISIT(self->offset);
1117 Py_VISIT(self->text);
1118 Py_VISIT(self->print_file_and_line);
1119 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1120}
1121
1122/* This is called "my_basename" instead of just "basename" to avoid name
1123 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1124 defined, and Python does define that. */
1125static char *
1126my_basename(char *name)
1127{
1128 char *cp = name;
1129 char *result = name;
1130
1131 if (name == NULL)
1132 return "???";
1133 while (*cp != '\0') {
1134 if (*cp == SEP)
1135 result = cp + 1;
1136 ++cp;
1137 }
1138 return result;
1139}
1140
1141
1142static PyObject *
1143SyntaxError_str(PySyntaxErrorObject *self)
1144{
1145 PyObject *str;
1146 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001147 int have_filename = 0;
1148 int have_lineno = 0;
1149 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001150 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001151
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001152 if (self->msg)
1153 str = PyObject_Str(self->msg);
1154 else
1155 str = PyObject_Str(Py_None);
Georg Brandl43ab1002006-05-28 20:57:09 +00001156 if (!str) return NULL;
1157 /* Don't fiddle with non-string return (shouldn't happen anyway) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001158 if (!PyString_Check(str)) return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001159
1160 /* XXX -- do all the additional formatting with filename and
1161 lineno here */
1162
Georg Brandl43ab1002006-05-28 20:57:09 +00001163 have_filename = (self->filename != NULL) &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001164 PyString_Check(self->filename);
Georg Brandl43ab1002006-05-28 20:57:09 +00001165 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001166
Georg Brandl43ab1002006-05-28 20:57:09 +00001167 if (!have_filename && !have_lineno)
1168 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001169
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001170 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001171 if (have_filename)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001172 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001173
Georg Brandl43ab1002006-05-28 20:57:09 +00001174 buffer = PyMem_MALLOC(bufsize);
1175 if (buffer == NULL)
1176 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001177
Georg Brandl43ab1002006-05-28 20:57:09 +00001178 if (have_filename && have_lineno)
1179 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001180 PyString_AS_STRING(str),
1181 my_basename(PyString_AS_STRING(self->filename)),
Georg Brandl43ab1002006-05-28 20:57:09 +00001182 PyInt_AsLong(self->lineno));
1183 else if (have_filename)
1184 PyOS_snprintf(buffer, bufsize, "%s (%s)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001185 PyString_AS_STRING(str),
1186 my_basename(PyString_AS_STRING(self->filename)));
Georg Brandl43ab1002006-05-28 20:57:09 +00001187 else /* only have_lineno */
1188 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001189 PyString_AS_STRING(str),
Georg Brandl43ab1002006-05-28 20:57:09 +00001190 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001191
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001192 result = PyString_FromString(buffer);
Georg Brandl43ab1002006-05-28 20:57:09 +00001193 PyMem_FREE(buffer);
1194
1195 if (result == NULL)
1196 result = str;
1197 else
1198 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001199 return result;
1200}
1201
1202static PyMemberDef SyntaxError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001203 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1204 PyDoc_STR("exception msg")},
1205 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1206 PyDoc_STR("exception filename")},
1207 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1208 PyDoc_STR("exception lineno")},
1209 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1210 PyDoc_STR("exception offset")},
1211 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1212 PyDoc_STR("exception text")},
1213 {"print_file_and_line", T_OBJECT,
1214 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1215 PyDoc_STR("exception print_file_and_line")},
1216 {NULL} /* Sentinel */
1217};
1218
1219ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1220 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001221 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001222
1223
1224/*
1225 * IndentationError extends SyntaxError
1226 */
1227MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001228 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001229
1230
1231/*
1232 * TabError extends IndentationError
1233 */
1234MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001235 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001236
1237
1238/*
1239 * LookupError extends StandardError
1240 */
1241SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001242 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001243
1244
1245/*
1246 * IndexError extends LookupError
1247 */
1248SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001249 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001250
1251
1252/*
1253 * KeyError extends LookupError
1254 */
1255static PyObject *
1256KeyError_str(PyBaseExceptionObject *self)
1257{
1258 /* If args is a tuple of exactly one item, apply repr to args[0].
1259 This is done so that e.g. the exception raised by {}[''] prints
1260 KeyError: ''
1261 rather than the confusing
1262 KeyError
1263 alone. The downside is that if KeyError is raised with an explanatory
1264 string, that string will be displayed in quotes. Too bad.
1265 If args is anything else, use the default BaseException__str__().
1266 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001267 if (PyTuple_GET_SIZE(self->args) == 1) {
1268 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001269 }
1270 return BaseException_str(self);
1271}
1272
1273ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001274 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001275
1276
1277/*
1278 * ValueError extends StandardError
1279 */
1280SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001281 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001282
1283/*
1284 * UnicodeError extends ValueError
1285 */
1286
1287SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001288 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001289
1290#ifdef Py_USING_UNICODE
Richard Jones7b9558d2006-05-27 12:29:24 +00001291static PyObject *
1292get_string(PyObject *attr, const char *name)
1293{
1294 if (!attr) {
1295 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1296 return NULL;
1297 }
1298
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001299 if (!PyString_Check(attr)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001300 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1301 return NULL;
1302 }
1303 Py_INCREF(attr);
1304 return attr;
1305}
1306
1307
1308static int
1309set_string(PyObject **attr, const char *value)
1310{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001311 PyObject *obj = PyString_FromString(value);
Richard Jones7b9558d2006-05-27 12:29:24 +00001312 if (!obj)
1313 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001314 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001315 *attr = obj;
1316 return 0;
1317}
1318
1319
1320static PyObject *
1321get_unicode(PyObject *attr, const char *name)
1322{
1323 if (!attr) {
1324 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1325 return NULL;
1326 }
1327
1328 if (!PyUnicode_Check(attr)) {
1329 PyErr_Format(PyExc_TypeError,
1330 "%.200s attribute must be unicode", name);
1331 return NULL;
1332 }
1333 Py_INCREF(attr);
1334 return attr;
1335}
1336
1337PyObject *
1338PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1339{
1340 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1341}
1342
1343PyObject *
1344PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1345{
1346 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1347}
1348
1349PyObject *
1350PyUnicodeEncodeError_GetObject(PyObject *exc)
1351{
1352 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1353}
1354
1355PyObject *
1356PyUnicodeDecodeError_GetObject(PyObject *exc)
1357{
1358 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1359}
1360
1361PyObject *
1362PyUnicodeTranslateError_GetObject(PyObject *exc)
1363{
1364 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1365}
1366
1367int
1368PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1369{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001370 Py_ssize_t size;
1371 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1372 "object");
1373 if (!obj)
1374 return -1;
1375 *start = ((PyUnicodeErrorObject *)exc)->start;
1376 size = PyUnicode_GET_SIZE(obj);
1377 if (*start<0)
1378 *start = 0; /*XXX check for values <0*/
1379 if (*start>=size)
1380 *start = size-1;
1381 Py_DECREF(obj);
1382 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001383}
1384
1385
1386int
1387PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1388{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001389 Py_ssize_t size;
1390 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1391 "object");
1392 if (!obj)
1393 return -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001394 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001395 *start = ((PyUnicodeErrorObject *)exc)->start;
1396 if (*start<0)
1397 *start = 0;
1398 if (*start>=size)
1399 *start = size-1;
1400 Py_DECREF(obj);
1401 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001402}
1403
1404
1405int
1406PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1407{
1408 return PyUnicodeEncodeError_GetStart(exc, start);
1409}
1410
1411
1412int
1413PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1414{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001415 ((PyUnicodeErrorObject *)exc)->start = start;
1416 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001417}
1418
1419
1420int
1421PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1422{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001423 ((PyUnicodeErrorObject *)exc)->start = start;
1424 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001425}
1426
1427
1428int
1429PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1430{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001431 ((PyUnicodeErrorObject *)exc)->start = start;
1432 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001433}
1434
1435
1436int
1437PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1438{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001439 Py_ssize_t size;
1440 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1441 "object");
1442 if (!obj)
1443 return -1;
1444 *end = ((PyUnicodeErrorObject *)exc)->end;
1445 size = PyUnicode_GET_SIZE(obj);
1446 if (*end<1)
1447 *end = 1;
1448 if (*end>size)
1449 *end = size;
1450 Py_DECREF(obj);
1451 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001452}
1453
1454
1455int
1456PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1457{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001458 Py_ssize_t size;
1459 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1460 "object");
1461 if (!obj)
1462 return -1;
1463 *end = ((PyUnicodeErrorObject *)exc)->end;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001464 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001465 if (*end<1)
1466 *end = 1;
1467 if (*end>size)
1468 *end = size;
1469 Py_DECREF(obj);
1470 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001471}
1472
1473
1474int
1475PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1476{
1477 return PyUnicodeEncodeError_GetEnd(exc, start);
1478}
1479
1480
1481int
1482PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1483{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001484 ((PyUnicodeErrorObject *)exc)->end = end;
1485 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001486}
1487
1488
1489int
1490PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1491{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001492 ((PyUnicodeErrorObject *)exc)->end = end;
1493 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001494}
1495
1496
1497int
1498PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1499{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001500 ((PyUnicodeErrorObject *)exc)->end = end;
1501 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001502}
1503
1504PyObject *
1505PyUnicodeEncodeError_GetReason(PyObject *exc)
1506{
1507 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1508}
1509
1510
1511PyObject *
1512PyUnicodeDecodeError_GetReason(PyObject *exc)
1513{
1514 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1515}
1516
1517
1518PyObject *
1519PyUnicodeTranslateError_GetReason(PyObject *exc)
1520{
1521 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1522}
1523
1524
1525int
1526PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1527{
1528 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1529}
1530
1531
1532int
1533PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1534{
1535 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1536}
1537
1538
1539int
1540PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1541{
1542 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1543}
1544
1545
Richard Jones7b9558d2006-05-27 12:29:24 +00001546static int
1547UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1548 PyTypeObject *objecttype)
1549{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001550 Py_CLEAR(self->encoding);
1551 Py_CLEAR(self->object);
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001552 Py_CLEAR(self->reason);
1553
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001554 if (!PyArg_ParseTuple(args, "O!O!nnO!",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001555 &PyString_Type, &self->encoding,
Richard Jones7b9558d2006-05-27 12:29:24 +00001556 objecttype, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001557 &self->start,
1558 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001559 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001560 self->encoding = self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001561 return -1;
1562 }
1563
1564 Py_INCREF(self->encoding);
1565 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001566 Py_INCREF(self->reason);
1567
1568 return 0;
1569}
1570
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001571static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001572UnicodeError_clear(PyUnicodeErrorObject *self)
1573{
1574 Py_CLEAR(self->encoding);
1575 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001576 Py_CLEAR(self->reason);
1577 return BaseException_clear((PyBaseExceptionObject *)self);
1578}
1579
1580static void
1581UnicodeError_dealloc(PyUnicodeErrorObject *self)
1582{
Georg Brandl38f62372006-09-06 06:50:05 +00001583 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001584 UnicodeError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001585 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001586}
1587
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001588static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001589UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1590{
1591 Py_VISIT(self->encoding);
1592 Py_VISIT(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001593 Py_VISIT(self->reason);
1594 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1595}
1596
1597static PyMemberDef UnicodeError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001598 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1599 PyDoc_STR("exception encoding")},
1600 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1601 PyDoc_STR("exception object")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001602 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001603 PyDoc_STR("exception start")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001604 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001605 PyDoc_STR("exception end")},
1606 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1607 PyDoc_STR("exception reason")},
1608 {NULL} /* Sentinel */
1609};
1610
1611
1612/*
1613 * UnicodeEncodeError extends UnicodeError
1614 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001615
1616static int
1617UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1618{
1619 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1620 return -1;
1621 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1622 kwds, &PyUnicode_Type);
1623}
1624
1625static PyObject *
1626UnicodeEncodeError_str(PyObject *self)
1627{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001628 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001629
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001630 if (uself->end==uself->start+1) {
1631 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001632 char badchar_str[20];
1633 if (badchar <= 0xff)
1634 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1635 else if (badchar <= 0xffff)
1636 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1637 else
1638 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001639 return PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001640 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001641 PyString_AS_STRING(uself->encoding),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001642 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001643 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001644 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001645 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001646 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001647 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001648 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001649 PyString_AS_STRING(uself->encoding),
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001650 uself->start,
1651 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001652 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001653 );
1654}
1655
1656static PyTypeObject _PyExc_UnicodeEncodeError = {
1657 PyObject_HEAD_INIT(NULL)
1658 0,
Georg Brandl38f62372006-09-06 06:50:05 +00001659 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001660 sizeof(PyUnicodeErrorObject), 0,
1661 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1662 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1663 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001664 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1665 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001666 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001667 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001668};
1669PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1670
1671PyObject *
1672PyUnicodeEncodeError_Create(
1673 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1674 Py_ssize_t start, Py_ssize_t end, const char *reason)
1675{
1676 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001677 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001678}
1679
1680
1681/*
1682 * UnicodeDecodeError extends UnicodeError
1683 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001684
1685static int
1686UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1687{
1688 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1689 return -1;
1690 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001691 kwds, &PyString_Type);
Richard Jones7b9558d2006-05-27 12:29:24 +00001692}
1693
1694static PyObject *
1695UnicodeDecodeError_str(PyObject *self)
1696{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001697 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001698
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001699 if (uself->end==uself->start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001700 /* FromFormat does not support %02x, so format that separately */
1701 char byte[4];
1702 PyOS_snprintf(byte, sizeof(byte), "%02x",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001703 ((int)PyString_AS_STRING(uself->object)[uself->start])&0xff);
1704 return PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001705 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001706 PyString_AS_STRING(uself->encoding),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001707 byte,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001708 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001709 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001710 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001711 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001712 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001713 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001714 PyString_AS_STRING(uself->encoding),
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001715 uself->start,
1716 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001717 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001718 );
1719}
1720
1721static PyTypeObject _PyExc_UnicodeDecodeError = {
1722 PyObject_HEAD_INIT(NULL)
1723 0,
1724 EXC_MODULE_NAME "UnicodeDecodeError",
1725 sizeof(PyUnicodeErrorObject), 0,
1726 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1727 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1728 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001729 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1730 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001731 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001732 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001733};
1734PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1735
1736PyObject *
1737PyUnicodeDecodeError_Create(
1738 const char *encoding, const char *object, Py_ssize_t length,
1739 Py_ssize_t start, Py_ssize_t end, const char *reason)
1740{
1741 assert(length < INT_MAX);
1742 assert(start < INT_MAX);
1743 assert(end < INT_MAX);
1744 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001745 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001746}
1747
1748
1749/*
1750 * UnicodeTranslateError extends UnicodeError
1751 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001752
1753static int
1754UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1755 PyObject *kwds)
1756{
1757 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1758 return -1;
1759
1760 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001761 Py_CLEAR(self->reason);
1762
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001763 if (!PyArg_ParseTuple(args, "O!nnO!",
Richard Jones7b9558d2006-05-27 12:29:24 +00001764 &PyUnicode_Type, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001765 &self->start,
1766 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001767 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001768 self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001769 return -1;
1770 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001771
Richard Jones7b9558d2006-05-27 12:29:24 +00001772 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001773 Py_INCREF(self->reason);
1774
1775 return 0;
1776}
1777
1778
1779static PyObject *
1780UnicodeTranslateError_str(PyObject *self)
1781{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001782 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001783
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001784 if (uself->end==uself->start+1) {
1785 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001786 char badchar_str[20];
1787 if (badchar <= 0xff)
1788 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1789 else if (badchar <= 0xffff)
1790 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1791 else
1792 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001793 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001794 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001795 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001796 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001797 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001798 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001799 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001800 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001801 "can't translate characters in position %zd-%zd: %.400s",
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001802 uself->start,
1803 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001804 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001805 );
1806}
1807
1808static PyTypeObject _PyExc_UnicodeTranslateError = {
1809 PyObject_HEAD_INIT(NULL)
1810 0,
1811 EXC_MODULE_NAME "UnicodeTranslateError",
1812 sizeof(PyUnicodeErrorObject), 0,
1813 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1814 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1815 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001816 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001817 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1818 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001819 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001820};
1821PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1822
1823PyObject *
1824PyUnicodeTranslateError_Create(
1825 const Py_UNICODE *object, Py_ssize_t length,
1826 Py_ssize_t start, Py_ssize_t end, const char *reason)
1827{
1828 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001829 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001830}
1831#endif
1832
1833
1834/*
1835 * AssertionError extends StandardError
1836 */
1837SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001838 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001839
1840
1841/*
1842 * ArithmeticError extends StandardError
1843 */
1844SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001845 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001846
1847
1848/*
1849 * FloatingPointError extends ArithmeticError
1850 */
1851SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001852 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001853
1854
1855/*
1856 * OverflowError extends ArithmeticError
1857 */
1858SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001859 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001860
1861
1862/*
1863 * ZeroDivisionError extends ArithmeticError
1864 */
1865SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001866 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001867
1868
1869/*
1870 * SystemError extends StandardError
1871 */
1872SimpleExtendsException(PyExc_StandardError, SystemError,
1873 "Internal error in the Python interpreter.\n"
1874 "\n"
1875 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001876 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001877
1878
1879/*
1880 * ReferenceError extends StandardError
1881 */
1882SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001883 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001884
1885
1886/*
1887 * MemoryError extends StandardError
1888 */
Richard Jones2d555b32006-05-27 16:15:11 +00001889SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001890
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00001891/*
1892 * BufferError extends StandardError
1893 */
1894SimpleExtendsException(PyExc_StandardError, BufferError, "Buffer error.");
1895
Richard Jones7b9558d2006-05-27 12:29:24 +00001896
1897/* Warning category docstrings */
1898
1899/*
1900 * Warning extends Exception
1901 */
1902SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001903 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001904
1905
1906/*
1907 * UserWarning extends Warning
1908 */
1909SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001910 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001911
1912
1913/*
1914 * DeprecationWarning extends Warning
1915 */
1916SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001917 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001918
1919
1920/*
1921 * PendingDeprecationWarning extends Warning
1922 */
1923SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1924 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001925 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001926
1927
1928/*
1929 * SyntaxWarning extends Warning
1930 */
1931SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001932 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001933
1934
1935/*
1936 * RuntimeWarning extends Warning
1937 */
1938SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001939 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001940
1941
1942/*
1943 * FutureWarning extends Warning
1944 */
1945SimpleExtendsException(PyExc_Warning, FutureWarning,
1946 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001947 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001948
1949
1950/*
1951 * ImportWarning extends Warning
1952 */
1953SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001954 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00001955
1956
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001957/*
1958 * UnicodeWarning extends Warning
1959 */
1960SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1961 "Base class for warnings about Unicode related problems, mostly\n"
1962 "related to conversion problems.");
1963
Christian Heimes1a6387e2008-03-26 12:49:49 +00001964/*
1965 * BytesWarning extends Warning
1966 */
1967SimpleExtendsException(PyExc_Warning, BytesWarning,
1968 "Base class for warnings about bytes and buffer related problems, mostly\n"
1969 "related to conversion from str or comparing to str.");
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001970
Richard Jones7b9558d2006-05-27 12:29:24 +00001971/* Pre-computed MemoryError instance. Best to create this as early as
1972 * possible and not wait until a MemoryError is actually raised!
1973 */
1974PyObject *PyExc_MemoryErrorInst=NULL;
1975
Brett Cannon1e534b52007-09-07 04:18:30 +00001976/* Pre-computed RuntimeError instance for when recursion depth is reached.
1977 Meant to be used when normalizing the exception for exceeding the recursion
1978 depth will cause its own infinite recursion.
1979*/
1980PyObject *PyExc_RecursionErrorInst = NULL;
1981
Richard Jones7b9558d2006-05-27 12:29:24 +00001982/* module global functions */
1983static PyMethodDef functions[] = {
1984 /* Sentinel */
1985 {NULL, NULL}
1986};
1987
1988#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1989 Py_FatalError("exceptions bootstrapping error.");
1990
1991#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1992 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1993 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1994 Py_FatalError("Module dictionary insertion problem.");
1995
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00001996#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00001997/* crt variable checking in VisualStudio .NET 2005 */
1998#include <crtdbg.h>
1999
2000static int prevCrtReportMode;
2001static _invalid_parameter_handler prevCrtHandler;
2002
2003/* Invalid parameter handler. Sets a ValueError exception */
2004static void
2005InvalidParameterHandler(
2006 const wchar_t * expression,
2007 const wchar_t * function,
2008 const wchar_t * file,
2009 unsigned int line,
2010 uintptr_t pReserved)
2011{
2012 /* Do nothing, allow execution to continue. Usually this
2013 * means that the CRT will set errno to EINVAL
2014 */
2015}
2016#endif
2017
2018
Richard Jones7b9558d2006-05-27 12:29:24 +00002019PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00002020_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00002021{
2022 PyObject *m, *bltinmod, *bdict;
2023
2024 PRE_INIT(BaseException)
2025 PRE_INIT(Exception)
2026 PRE_INIT(StandardError)
2027 PRE_INIT(TypeError)
2028 PRE_INIT(StopIteration)
2029 PRE_INIT(GeneratorExit)
2030 PRE_INIT(SystemExit)
2031 PRE_INIT(KeyboardInterrupt)
2032 PRE_INIT(ImportError)
2033 PRE_INIT(EnvironmentError)
2034 PRE_INIT(IOError)
2035 PRE_INIT(OSError)
2036#ifdef MS_WINDOWS
2037 PRE_INIT(WindowsError)
2038#endif
2039#ifdef __VMS
2040 PRE_INIT(VMSError)
2041#endif
2042 PRE_INIT(EOFError)
2043 PRE_INIT(RuntimeError)
2044 PRE_INIT(NotImplementedError)
2045 PRE_INIT(NameError)
2046 PRE_INIT(UnboundLocalError)
2047 PRE_INIT(AttributeError)
2048 PRE_INIT(SyntaxError)
2049 PRE_INIT(IndentationError)
2050 PRE_INIT(TabError)
2051 PRE_INIT(LookupError)
2052 PRE_INIT(IndexError)
2053 PRE_INIT(KeyError)
2054 PRE_INIT(ValueError)
2055 PRE_INIT(UnicodeError)
2056#ifdef Py_USING_UNICODE
2057 PRE_INIT(UnicodeEncodeError)
2058 PRE_INIT(UnicodeDecodeError)
2059 PRE_INIT(UnicodeTranslateError)
2060#endif
2061 PRE_INIT(AssertionError)
2062 PRE_INIT(ArithmeticError)
2063 PRE_INIT(FloatingPointError)
2064 PRE_INIT(OverflowError)
2065 PRE_INIT(ZeroDivisionError)
2066 PRE_INIT(SystemError)
2067 PRE_INIT(ReferenceError)
2068 PRE_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002069 PRE_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002070 PRE_INIT(Warning)
2071 PRE_INIT(UserWarning)
2072 PRE_INIT(DeprecationWarning)
2073 PRE_INIT(PendingDeprecationWarning)
2074 PRE_INIT(SyntaxWarning)
2075 PRE_INIT(RuntimeWarning)
2076 PRE_INIT(FutureWarning)
2077 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002078 PRE_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002079 PRE_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002080
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002081 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2082 (PyObject *)NULL, PYTHON_API_VERSION);
Richard Jones7b9558d2006-05-27 12:29:24 +00002083 if (m == NULL) return;
2084
2085 bltinmod = PyImport_ImportModule("__builtin__");
2086 if (bltinmod == NULL)
2087 Py_FatalError("exceptions bootstrapping error.");
2088 bdict = PyModule_GetDict(bltinmod);
2089 if (bdict == NULL)
2090 Py_FatalError("exceptions bootstrapping error.");
2091
2092 POST_INIT(BaseException)
2093 POST_INIT(Exception)
2094 POST_INIT(StandardError)
2095 POST_INIT(TypeError)
2096 POST_INIT(StopIteration)
2097 POST_INIT(GeneratorExit)
2098 POST_INIT(SystemExit)
2099 POST_INIT(KeyboardInterrupt)
2100 POST_INIT(ImportError)
2101 POST_INIT(EnvironmentError)
2102 POST_INIT(IOError)
2103 POST_INIT(OSError)
2104#ifdef MS_WINDOWS
2105 POST_INIT(WindowsError)
2106#endif
2107#ifdef __VMS
2108 POST_INIT(VMSError)
2109#endif
2110 POST_INIT(EOFError)
2111 POST_INIT(RuntimeError)
2112 POST_INIT(NotImplementedError)
2113 POST_INIT(NameError)
2114 POST_INIT(UnboundLocalError)
2115 POST_INIT(AttributeError)
2116 POST_INIT(SyntaxError)
2117 POST_INIT(IndentationError)
2118 POST_INIT(TabError)
2119 POST_INIT(LookupError)
2120 POST_INIT(IndexError)
2121 POST_INIT(KeyError)
2122 POST_INIT(ValueError)
2123 POST_INIT(UnicodeError)
2124#ifdef Py_USING_UNICODE
2125 POST_INIT(UnicodeEncodeError)
2126 POST_INIT(UnicodeDecodeError)
2127 POST_INIT(UnicodeTranslateError)
2128#endif
2129 POST_INIT(AssertionError)
2130 POST_INIT(ArithmeticError)
2131 POST_INIT(FloatingPointError)
2132 POST_INIT(OverflowError)
2133 POST_INIT(ZeroDivisionError)
2134 POST_INIT(SystemError)
2135 POST_INIT(ReferenceError)
2136 POST_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002137 POST_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002138 POST_INIT(Warning)
2139 POST_INIT(UserWarning)
2140 POST_INIT(DeprecationWarning)
2141 POST_INIT(PendingDeprecationWarning)
2142 POST_INIT(SyntaxWarning)
2143 POST_INIT(RuntimeWarning)
2144 POST_INIT(FutureWarning)
2145 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002146 POST_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002147 POST_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002148
2149 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2150 if (!PyExc_MemoryErrorInst)
2151 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
2152
Brett Cannon1e534b52007-09-07 04:18:30 +00002153 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2154 if (!PyExc_RecursionErrorInst)
2155 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2156 "recursion errors");
2157 else {
2158 PyBaseExceptionObject *err_inst =
2159 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2160 PyObject *args_tuple;
2161 PyObject *exc_message;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002162 exc_message = PyString_FromString("maximum recursion depth exceeded");
Brett Cannon1e534b52007-09-07 04:18:30 +00002163 if (!exc_message)
2164 Py_FatalError("cannot allocate argument for RuntimeError "
2165 "pre-allocation");
2166 args_tuple = PyTuple_Pack(1, exc_message);
2167 if (!args_tuple)
2168 Py_FatalError("cannot allocate tuple for RuntimeError "
2169 "pre-allocation");
2170 Py_DECREF(exc_message);
2171 if (BaseException_init(err_inst, args_tuple, NULL))
2172 Py_FatalError("init of pre-allocated RuntimeError failed");
2173 Py_DECREF(args_tuple);
2174 }
2175
Richard Jones7b9558d2006-05-27 12:29:24 +00002176 Py_DECREF(bltinmod);
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002177
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002178#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002179 /* Set CRT argument error handler */
2180 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
2181 /* turn off assertions in debug mode */
2182 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
2183#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002184}
2185
2186void
2187_PyExc_Fini(void)
2188{
2189 Py_XDECREF(PyExc_MemoryErrorInst);
2190 PyExc_MemoryErrorInst = NULL;
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002191#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002192 /* reset CRT error handling */
2193 _set_invalid_parameter_handler(prevCrtHandler);
2194 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
2195#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002196}