blob: 0a39a6bee95b796a61639e14158799bf3fa8fa2b [file] [log] [blame]
Georg Brandl43ab1002006-05-28 20:57:09 +00001/*
2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
3 *
4 * Thanks go to Tim Peters and Michael Hudson for debugging.
5 */
6
Richard Jones7b9558d2006-05-27 12:29:24 +00007#define PY_SSIZE_T_CLEAN
8#include <Python.h>
9#include "structmember.h"
10#include "osdefs.h"
11
Richard Jones7b9558d2006-05-27 12:29:24 +000012#define EXC_MODULE_NAME "exceptions."
13
Richard Jonesc5b2a2e2006-05-27 16:07:28 +000014/* NOTE: If the exception class hierarchy changes, don't forget to update
15 * Lib/test/exception_hierarchy.txt
16 */
17
18PyDoc_STRVAR(exceptions_doc, "Python's standard exception class hierarchy.\n\
19\n\
20Exceptions found here are defined both in the exceptions module and the\n\
21built-in namespace. It is recommended that user-defined exceptions\n\
22inherit from Exception. See the documentation for the exception\n\
23inheritance hierarchy.\n\
24");
25
Richard Jones7b9558d2006-05-27 12:29:24 +000026/*
27 * BaseException
28 */
29static PyObject *
30BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
31{
32 PyBaseExceptionObject *self;
33
34 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
Georg Brandlc02e1312007-04-11 17:16:24 +000035 if (!self)
36 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +000037 /* the dict is created on the fly in PyObject_GenericSetAttr */
38 self->message = self->dict = NULL;
39
Georg Brandld7e9f602007-08-21 06:03:43 +000040 self->args = PyTuple_New(0);
41 if (!self->args) {
Richard Jones7b9558d2006-05-27 12:29:24 +000042 Py_DECREF(self);
43 return NULL;
44 }
Georg Brandld7e9f602007-08-21 06:03:43 +000045
Gregory P. Smithdd96db62008-06-09 04:58:54 +000046 self->message = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +000047 if (!self->message) {
48 Py_DECREF(self);
49 return NULL;
50 }
Georg Brandld7e9f602007-08-21 06:03:43 +000051
Richard Jones7b9558d2006-05-27 12:29:24 +000052 return (PyObject *)self;
53}
54
55static int
56BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
57{
Christian Heimese93237d2007-12-19 02:37:44 +000058 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
Georg Brandlb0432bc2006-05-30 08:17:00 +000059 return -1;
60
Richard Jones7b9558d2006-05-27 12:29:24 +000061 Py_DECREF(self->args);
62 self->args = args;
63 Py_INCREF(self->args);
64
65 if (PyTuple_GET_SIZE(self->args) == 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +000066 Py_CLEAR(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000067 self->message = PyTuple_GET_ITEM(self->args, 0);
Michael W. Hudson22a80e72006-05-28 15:51:40 +000068 Py_INCREF(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000069 }
70 return 0;
71}
72
Michael W. Hudson96495ee2006-05-28 17:40:29 +000073static int
Richard Jones7b9558d2006-05-27 12:29:24 +000074BaseException_clear(PyBaseExceptionObject *self)
75{
76 Py_CLEAR(self->dict);
77 Py_CLEAR(self->args);
78 Py_CLEAR(self->message);
79 return 0;
80}
81
82static void
83BaseException_dealloc(PyBaseExceptionObject *self)
84{
Georg Brandl38f62372006-09-06 06:50:05 +000085 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +000086 BaseException_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +000087 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +000088}
89
Michael W. Hudson96495ee2006-05-28 17:40:29 +000090static int
Richard Jones7b9558d2006-05-27 12:29:24 +000091BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
92{
Michael W. Hudson22a80e72006-05-28 15:51:40 +000093 Py_VISIT(self->dict);
Richard Jones7b9558d2006-05-27 12:29:24 +000094 Py_VISIT(self->args);
95 Py_VISIT(self->message);
96 return 0;
97}
98
99static PyObject *
100BaseException_str(PyBaseExceptionObject *self)
101{
102 PyObject *out;
103
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000104 switch (PyTuple_GET_SIZE(self->args)) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000105 case 0:
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000106 out = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +0000107 break;
108 case 1:
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000109 out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +0000110 break;
Richard Jones7b9558d2006-05-27 12:29:24 +0000111 default:
112 out = PyObject_Str(self->args);
113 break;
114 }
115
116 return out;
117}
118
Nick Coghlan524b7772008-07-08 14:08:04 +0000119#ifdef Py_USING_UNICODE
120static PyObject *
121BaseException_unicode(PyBaseExceptionObject *self)
122{
123 PyObject *out;
124
125 switch (PyTuple_GET_SIZE(self->args)) {
126 case 0:
127 out = PyUnicode_FromString("");
128 break;
129 case 1:
130 out = PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));
131 break;
132 default:
133 out = PyObject_Unicode(self->args);
134 break;
135 }
136
137 return out;
138}
139#endif
140
Richard Jones7b9558d2006-05-27 12:29:24 +0000141static PyObject *
142BaseException_repr(PyBaseExceptionObject *self)
143{
Richard Jones7b9558d2006-05-27 12:29:24 +0000144 PyObject *repr_suffix;
145 PyObject *repr;
146 char *name;
147 char *dot;
148
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000149 repr_suffix = PyObject_Repr(self->args);
150 if (!repr_suffix)
Richard Jones7b9558d2006-05-27 12:29:24 +0000151 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000152
Christian Heimese93237d2007-12-19 02:37:44 +0000153 name = (char *)Py_TYPE(self)->tp_name;
Richard Jones7b9558d2006-05-27 12:29:24 +0000154 dot = strrchr(name, '.');
155 if (dot != NULL) name = dot+1;
156
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000157 repr = PyString_FromString(name);
Richard Jones7b9558d2006-05-27 12:29:24 +0000158 if (!repr) {
159 Py_DECREF(repr_suffix);
160 return NULL;
161 }
162
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000163 PyString_ConcatAndDel(&repr, repr_suffix);
Richard Jones7b9558d2006-05-27 12:29:24 +0000164 return repr;
165}
166
167/* Pickling support */
168static PyObject *
169BaseException_reduce(PyBaseExceptionObject *self)
170{
Georg Brandlddba4732006-05-30 07:04:55 +0000171 if (self->args && self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000172 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000173 else
Christian Heimese93237d2007-12-19 02:37:44 +0000174 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000175}
176
Georg Brandl85ac8502006-06-01 06:39:19 +0000177/*
178 * Needed for backward compatibility, since exceptions used to store
179 * all their attributes in the __dict__. Code is taken from cPickle's
180 * load_build function.
181 */
182static PyObject *
183BaseException_setstate(PyObject *self, PyObject *state)
184{
185 PyObject *d_key, *d_value;
186 Py_ssize_t i = 0;
187
188 if (state != Py_None) {
189 if (!PyDict_Check(state)) {
190 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
191 return NULL;
192 }
193 while (PyDict_Next(state, &i, &d_key, &d_value)) {
194 if (PyObject_SetAttr(self, d_key, d_value) < 0)
195 return NULL;
196 }
197 }
198 Py_RETURN_NONE;
199}
Richard Jones7b9558d2006-05-27 12:29:24 +0000200
Richard Jones7b9558d2006-05-27 12:29:24 +0000201
202static PyMethodDef BaseException_methods[] = {
203 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Georg Brandl85ac8502006-06-01 06:39:19 +0000204 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Nick Coghlan524b7772008-07-08 14:08:04 +0000205#ifdef Py_USING_UNICODE
206 {"__unicode__", (PyCFunction)BaseException_unicode, METH_NOARGS },
207#endif
Richard Jones7b9558d2006-05-27 12:29:24 +0000208 {NULL, NULL, 0, NULL},
209};
210
211
212
213static PyObject *
214BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
215{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000216 if (PyErr_WarnPy3k("__getitem__ not supported for exception "
217 "classes in 3.x; use args attribute", 1) < 0)
218 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000219 return PySequence_GetItem(self->args, index);
220}
221
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000222static PyObject *
223BaseException_getslice(PyBaseExceptionObject *self,
224 Py_ssize_t start, Py_ssize_t stop)
225{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000226 if (PyErr_WarnPy3k("__getslice__ not supported for exception "
227 "classes in 3.x; use args attribute", 1) < 0)
228 return NULL;
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000229 return PySequence_GetSlice(self->args, start, stop);
230}
231
Richard Jones7b9558d2006-05-27 12:29:24 +0000232static PySequenceMethods BaseException_as_sequence = {
233 0, /* sq_length; */
234 0, /* sq_concat; */
235 0, /* sq_repeat; */
236 (ssizeargfunc)BaseException_getitem, /* sq_item; */
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000237 (ssizessizeargfunc)BaseException_getslice, /* sq_slice; */
Richard Jones7b9558d2006-05-27 12:29:24 +0000238 0, /* sq_ass_item; */
239 0, /* sq_ass_slice; */
240 0, /* sq_contains; */
241 0, /* sq_inplace_concat; */
242 0 /* sq_inplace_repeat; */
243};
244
Richard Jones7b9558d2006-05-27 12:29:24 +0000245static PyObject *
246BaseException_get_dict(PyBaseExceptionObject *self)
247{
248 if (self->dict == NULL) {
249 self->dict = PyDict_New();
250 if (!self->dict)
251 return NULL;
252 }
253 Py_INCREF(self->dict);
254 return self->dict;
255}
256
257static int
258BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
259{
260 if (val == NULL) {
261 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
262 return -1;
263 }
264 if (!PyDict_Check(val)) {
265 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
266 return -1;
267 }
268 Py_CLEAR(self->dict);
269 Py_INCREF(val);
270 self->dict = val;
271 return 0;
272}
273
274static PyObject *
275BaseException_get_args(PyBaseExceptionObject *self)
276{
277 if (self->args == NULL) {
278 Py_INCREF(Py_None);
279 return Py_None;
280 }
281 Py_INCREF(self->args);
282 return self->args;
283}
284
285static int
286BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
287{
288 PyObject *seq;
289 if (val == NULL) {
290 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
291 return -1;
292 }
293 seq = PySequence_Tuple(val);
294 if (!seq) return -1;
Georg Brandlc7c51142006-05-29 09:46:51 +0000295 Py_CLEAR(self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000296 self->args = seq;
297 return 0;
298}
299
Brett Cannon229cee22007-05-05 01:34:02 +0000300static PyObject *
301BaseException_get_message(PyBaseExceptionObject *self)
302{
Georg Brandl0674d3f2009-09-16 20:30:09 +0000303 PyObject *msg;
304
305 /* if "message" is in self->dict, accessing a user-set message attribute */
306 if (self->dict &&
307 (msg = PyDict_GetItemString(self->dict, "message"))) {
308 Py_INCREF(msg);
309 return msg;
310 }
Brett Cannon229cee22007-05-05 01:34:02 +0000311
Georg Brandl0674d3f2009-09-16 20:30:09 +0000312 if (self->message == NULL) {
313 PyErr_SetString(PyExc_AttributeError, "message attribute was deleted");
314 return NULL;
315 }
316
317 /* accessing the deprecated "builtin" message attribute of Exception */
318 if (PyErr_WarnEx(PyExc_DeprecationWarning,
319 "BaseException.message has been deprecated as "
320 "of Python 2.6", 1) < 0)
321 return NULL;
322
323 Py_INCREF(self->message);
324 return self->message;
Brett Cannon229cee22007-05-05 01:34:02 +0000325}
326
327static int
328BaseException_set_message(PyBaseExceptionObject *self, PyObject *val)
329{
Georg Brandl0674d3f2009-09-16 20:30:09 +0000330 /* if val is NULL, delete the message attribute */
331 if (val == NULL) {
332 if (self->dict && PyDict_GetItemString(self->dict, "message")) {
333 if (PyDict_DelItemString(self->dict, "message") < 0)
334 return -1;
335 }
336 Py_XDECREF(self->message);
337 self->message = NULL;
338 return 0;
339 }
340
341 /* else set it in __dict__, but may need to create the dict first */
342 if (self->dict == NULL) {
343 self->dict = PyDict_New();
344 if (!self->dict)
345 return -1;
346 }
347 return PyDict_SetItemString(self->dict, "message", val);
Brett Cannon229cee22007-05-05 01:34:02 +0000348}
349
Richard Jones7b9558d2006-05-27 12:29:24 +0000350static PyGetSetDef BaseException_getset[] = {
351 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
352 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Brett Cannon229cee22007-05-05 01:34:02 +0000353 {"message", (getter)BaseException_get_message,
354 (setter)BaseException_set_message},
Richard Jones7b9558d2006-05-27 12:29:24 +0000355 {NULL},
356};
357
358
359static PyTypeObject _PyExc_BaseException = {
360 PyObject_HEAD_INIT(NULL)
361 0, /*ob_size*/
362 EXC_MODULE_NAME "BaseException", /*tp_name*/
363 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
364 0, /*tp_itemsize*/
365 (destructor)BaseException_dealloc, /*tp_dealloc*/
366 0, /*tp_print*/
367 0, /*tp_getattr*/
368 0, /*tp_setattr*/
369 0, /* tp_compare; */
370 (reprfunc)BaseException_repr, /*tp_repr*/
371 0, /*tp_as_number*/
372 &BaseException_as_sequence, /*tp_as_sequence*/
373 0, /*tp_as_mapping*/
374 0, /*tp_hash */
375 0, /*tp_call*/
376 (reprfunc)BaseException_str, /*tp_str*/
377 PyObject_GenericGetAttr, /*tp_getattro*/
378 PyObject_GenericSetAttr, /*tp_setattro*/
379 0, /*tp_as_buffer*/
Neal Norwitzee3a1b52007-02-25 19:44:48 +0000380 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
381 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Richard Jones7b9558d2006-05-27 12:29:24 +0000382 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
383 (traverseproc)BaseException_traverse, /* tp_traverse */
384 (inquiry)BaseException_clear, /* tp_clear */
385 0, /* tp_richcompare */
386 0, /* tp_weaklistoffset */
387 0, /* tp_iter */
388 0, /* tp_iternext */
389 BaseException_methods, /* tp_methods */
Brett Cannon229cee22007-05-05 01:34:02 +0000390 0, /* tp_members */
Richard Jones7b9558d2006-05-27 12:29:24 +0000391 BaseException_getset, /* tp_getset */
392 0, /* tp_base */
393 0, /* tp_dict */
394 0, /* tp_descr_get */
395 0, /* tp_descr_set */
396 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
397 (initproc)BaseException_init, /* tp_init */
398 0, /* tp_alloc */
399 BaseException_new, /* tp_new */
400};
401/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
402from the previous implmentation and also allowing Python objects to be used
403in the API */
404PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
405
Richard Jones2d555b32006-05-27 16:15:11 +0000406/* note these macros omit the last semicolon so the macro invocation may
407 * include it and not look strange.
408 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000409#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
410static PyTypeObject _PyExc_ ## EXCNAME = { \
411 PyObject_HEAD_INIT(NULL) \
412 0, \
413 EXC_MODULE_NAME # EXCNAME, \
414 sizeof(PyBaseExceptionObject), \
415 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
416 0, 0, 0, 0, 0, 0, 0, \
417 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
418 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
419 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
420 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
421 (initproc)BaseException_init, 0, BaseException_new,\
422}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000423PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000424
425#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
426static PyTypeObject _PyExc_ ## EXCNAME = { \
427 PyObject_HEAD_INIT(NULL) \
428 0, \
429 EXC_MODULE_NAME # EXCNAME, \
430 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000431 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000432 0, 0, 0, 0, 0, \
433 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000434 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
435 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000436 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000437 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000438}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000439PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000440
441#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
442static PyTypeObject _PyExc_ ## EXCNAME = { \
443 PyObject_HEAD_INIT(NULL) \
444 0, \
445 EXC_MODULE_NAME # EXCNAME, \
446 sizeof(Py ## EXCSTORE ## Object), 0, \
447 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
448 (reprfunc)EXCSTR, 0, 0, 0, \
449 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
450 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
451 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
452 EXCMEMBERS, 0, &_ ## EXCBASE, \
453 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000454 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000455}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000456PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000457
458
459/*
460 * Exception extends BaseException
461 */
462SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000463 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000464
465
466/*
467 * StandardError extends Exception
468 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000469SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000470 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000471 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000472
473
474/*
475 * TypeError extends StandardError
476 */
477SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000478 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000479
480
481/*
482 * StopIteration extends Exception
483 */
484SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000485 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000486
487
488/*
Christian Heimes44eeaec2007-12-03 20:01:02 +0000489 * GeneratorExit extends BaseException
Richard Jones7b9558d2006-05-27 12:29:24 +0000490 */
Christian Heimes44eeaec2007-12-03 20:01:02 +0000491SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000492 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000493
494
495/*
496 * SystemExit extends BaseException
497 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000498
499static int
500SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
501{
502 Py_ssize_t size = PyTuple_GET_SIZE(args);
503
504 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
505 return -1;
506
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000507 if (size == 0)
508 return 0;
509 Py_CLEAR(self->code);
Richard Jones7b9558d2006-05-27 12:29:24 +0000510 if (size == 1)
511 self->code = PyTuple_GET_ITEM(args, 0);
512 else if (size > 1)
513 self->code = args;
514 Py_INCREF(self->code);
515 return 0;
516}
517
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000518static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000519SystemExit_clear(PySystemExitObject *self)
520{
521 Py_CLEAR(self->code);
522 return BaseException_clear((PyBaseExceptionObject *)self);
523}
524
525static void
526SystemExit_dealloc(PySystemExitObject *self)
527{
Georg Brandl38f62372006-09-06 06:50:05 +0000528 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000529 SystemExit_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000530 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000531}
532
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000533static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000534SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
535{
536 Py_VISIT(self->code);
537 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
538}
539
540static PyMemberDef SystemExit_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000541 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
542 PyDoc_STR("exception code")},
543 {NULL} /* Sentinel */
544};
545
546ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
547 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000548 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000549
550/*
551 * KeyboardInterrupt extends BaseException
552 */
553SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000554 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000555
556
557/*
558 * ImportError extends StandardError
559 */
560SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000561 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000562
563
564/*
565 * EnvironmentError extends StandardError
566 */
567
Richard Jones7b9558d2006-05-27 12:29:24 +0000568/* Where a function has a single filename, such as open() or some
569 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
570 * called, giving a third argument which is the filename. But, so
571 * that old code using in-place unpacking doesn't break, e.g.:
572 *
573 * except IOError, (errno, strerror):
574 *
575 * we hack args so that it only contains two items. This also
576 * means we need our own __str__() which prints out the filename
577 * when it was supplied.
578 */
579static int
580EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
581 PyObject *kwds)
582{
583 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
584 PyObject *subslice = NULL;
585
586 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
587 return -1;
588
Georg Brandl3267d282006-09-30 09:03:42 +0000589 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000590 return 0;
591 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000592
593 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000594 &myerrno, &strerror, &filename)) {
595 return -1;
596 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000597 Py_CLEAR(self->myerrno); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000598 self->myerrno = myerrno;
599 Py_INCREF(self->myerrno);
600
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000601 Py_CLEAR(self->strerror); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000602 self->strerror = strerror;
603 Py_INCREF(self->strerror);
604
605 /* self->filename will remain Py_None otherwise */
606 if (filename != NULL) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000607 Py_CLEAR(self->filename); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000608 self->filename = filename;
609 Py_INCREF(self->filename);
610
611 subslice = PyTuple_GetSlice(args, 0, 2);
612 if (!subslice)
613 return -1;
614
615 Py_DECREF(self->args); /* replacing args */
616 self->args = subslice;
617 }
618 return 0;
619}
620
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000621static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000622EnvironmentError_clear(PyEnvironmentErrorObject *self)
623{
624 Py_CLEAR(self->myerrno);
625 Py_CLEAR(self->strerror);
626 Py_CLEAR(self->filename);
627 return BaseException_clear((PyBaseExceptionObject *)self);
628}
629
630static void
631EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
632{
Georg Brandl38f62372006-09-06 06:50:05 +0000633 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000634 EnvironmentError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000635 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000636}
637
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000638static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000639EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
640 void *arg)
641{
642 Py_VISIT(self->myerrno);
643 Py_VISIT(self->strerror);
644 Py_VISIT(self->filename);
645 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
646}
647
648static PyObject *
649EnvironmentError_str(PyEnvironmentErrorObject *self)
650{
651 PyObject *rtnval = NULL;
652
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000653 if (self->filename) {
654 PyObject *fmt;
655 PyObject *repr;
656 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000657
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000658 fmt = PyString_FromString("[Errno %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000659 if (!fmt)
660 return NULL;
661
662 repr = PyObject_Repr(self->filename);
663 if (!repr) {
664 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000665 return NULL;
666 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000667 tuple = PyTuple_New(3);
668 if (!tuple) {
669 Py_DECREF(repr);
670 Py_DECREF(fmt);
671 return NULL;
672 }
673
674 if (self->myerrno) {
675 Py_INCREF(self->myerrno);
676 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
677 }
678 else {
679 Py_INCREF(Py_None);
680 PyTuple_SET_ITEM(tuple, 0, Py_None);
681 }
682 if (self->strerror) {
683 Py_INCREF(self->strerror);
684 PyTuple_SET_ITEM(tuple, 1, self->strerror);
685 }
686 else {
687 Py_INCREF(Py_None);
688 PyTuple_SET_ITEM(tuple, 1, Py_None);
689 }
690
Richard Jones7b9558d2006-05-27 12:29:24 +0000691 PyTuple_SET_ITEM(tuple, 2, repr);
692
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000693 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000694
695 Py_DECREF(fmt);
696 Py_DECREF(tuple);
697 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000698 else if (self->myerrno && self->strerror) {
699 PyObject *fmt;
700 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000701
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000702 fmt = PyString_FromString("[Errno %s] %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000703 if (!fmt)
704 return NULL;
705
706 tuple = PyTuple_New(2);
707 if (!tuple) {
708 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000709 return NULL;
710 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000711
712 if (self->myerrno) {
713 Py_INCREF(self->myerrno);
714 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
715 }
716 else {
717 Py_INCREF(Py_None);
718 PyTuple_SET_ITEM(tuple, 0, Py_None);
719 }
720 if (self->strerror) {
721 Py_INCREF(self->strerror);
722 PyTuple_SET_ITEM(tuple, 1, self->strerror);
723 }
724 else {
725 Py_INCREF(Py_None);
726 PyTuple_SET_ITEM(tuple, 1, Py_None);
727 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000728
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000729 rtnval = PyString_Format(fmt, tuple);
Richard Jones7b9558d2006-05-27 12:29:24 +0000730
731 Py_DECREF(fmt);
732 Py_DECREF(tuple);
733 }
734 else
735 rtnval = BaseException_str((PyBaseExceptionObject *)self);
736
737 return rtnval;
738}
739
740static PyMemberDef EnvironmentError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000741 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000742 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000743 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000744 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000745 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000746 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000747 {NULL} /* Sentinel */
748};
749
750
751static PyObject *
752EnvironmentError_reduce(PyEnvironmentErrorObject *self)
753{
754 PyObject *args = self->args;
755 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000756
Richard Jones7b9558d2006-05-27 12:29:24 +0000757 /* self->args is only the first two real arguments if there was a
758 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000759 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000760 args = PyTuple_New(3);
761 if (!args) return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000762
763 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000764 Py_INCREF(tmp);
765 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000766
767 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000768 Py_INCREF(tmp);
769 PyTuple_SET_ITEM(args, 1, tmp);
770
771 Py_INCREF(self->filename);
772 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000773 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000774 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000775
776 if (self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000777 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000778 else
Christian Heimese93237d2007-12-19 02:37:44 +0000779 res = PyTuple_Pack(2, Py_TYPE(self), args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000780 Py_DECREF(args);
781 return res;
782}
783
784
785static PyMethodDef EnvironmentError_methods[] = {
786 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
787 {NULL}
788};
789
790ComplexExtendsException(PyExc_StandardError, EnvironmentError,
791 EnvironmentError, EnvironmentError_dealloc,
792 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000793 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000794 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000795
796
797/*
798 * IOError extends EnvironmentError
799 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000800MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000801 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000802
803
804/*
805 * OSError extends EnvironmentError
806 */
807MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000808 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000809
810
811/*
812 * WindowsError extends OSError
813 */
814#ifdef MS_WINDOWS
815#include "errmap.h"
816
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000817static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000818WindowsError_clear(PyWindowsErrorObject *self)
819{
820 Py_CLEAR(self->myerrno);
821 Py_CLEAR(self->strerror);
822 Py_CLEAR(self->filename);
823 Py_CLEAR(self->winerror);
824 return BaseException_clear((PyBaseExceptionObject *)self);
825}
826
827static void
828WindowsError_dealloc(PyWindowsErrorObject *self)
829{
Georg Brandl38f62372006-09-06 06:50:05 +0000830 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000831 WindowsError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000832 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000833}
834
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000835static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000836WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
837{
838 Py_VISIT(self->myerrno);
839 Py_VISIT(self->strerror);
840 Py_VISIT(self->filename);
841 Py_VISIT(self->winerror);
842 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
843}
844
Richard Jones7b9558d2006-05-27 12:29:24 +0000845static int
846WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
847{
848 PyObject *o_errcode = NULL;
849 long errcode;
850 long posix_errno;
851
852 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
853 == -1)
854 return -1;
855
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000856 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000857 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000858
859 /* Set errno to the POSIX errno, and winerror to the Win32
860 error code. */
861 errcode = PyInt_AsLong(self->myerrno);
862 if (errcode == -1 && PyErr_Occurred())
863 return -1;
864 posix_errno = winerror_to_errno(errcode);
865
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000866 Py_CLEAR(self->winerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000867 self->winerror = self->myerrno;
868
869 o_errcode = PyInt_FromLong(posix_errno);
870 if (!o_errcode)
871 return -1;
872
873 self->myerrno = o_errcode;
874
875 return 0;
876}
877
878
879static PyObject *
880WindowsError_str(PyWindowsErrorObject *self)
881{
Richard Jones7b9558d2006-05-27 12:29:24 +0000882 PyObject *rtnval = NULL;
883
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000884 if (self->filename) {
885 PyObject *fmt;
886 PyObject *repr;
887 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000888
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000889 fmt = PyString_FromString("[Error %s] %s: %s");
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000890 if (!fmt)
891 return NULL;
892
893 repr = PyObject_Repr(self->filename);
894 if (!repr) {
895 Py_DECREF(fmt);
896 return NULL;
897 }
898 tuple = PyTuple_New(3);
899 if (!tuple) {
900 Py_DECREF(repr);
901 Py_DECREF(fmt);
902 return NULL;
903 }
904
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000905 if (self->winerror) {
906 Py_INCREF(self->winerror);
907 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000908 }
909 else {
910 Py_INCREF(Py_None);
911 PyTuple_SET_ITEM(tuple, 0, Py_None);
912 }
913 if (self->strerror) {
914 Py_INCREF(self->strerror);
915 PyTuple_SET_ITEM(tuple, 1, self->strerror);
916 }
917 else {
918 Py_INCREF(Py_None);
919 PyTuple_SET_ITEM(tuple, 1, Py_None);
920 }
921
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000922 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000923
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000924 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000925
926 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000927 Py_DECREF(tuple);
928 }
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000929 else if (self->winerror && self->strerror) {
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000930 PyObject *fmt;
931 PyObject *tuple;
932
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000933 fmt = PyString_FromString("[Error %s] %s");
Richard Jones7b9558d2006-05-27 12:29:24 +0000934 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000935 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000936
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000937 tuple = PyTuple_New(2);
938 if (!tuple) {
939 Py_DECREF(fmt);
940 return NULL;
941 }
942
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000943 if (self->winerror) {
944 Py_INCREF(self->winerror);
945 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000946 }
947 else {
948 Py_INCREF(Py_None);
949 PyTuple_SET_ITEM(tuple, 0, Py_None);
950 }
951 if (self->strerror) {
952 Py_INCREF(self->strerror);
953 PyTuple_SET_ITEM(tuple, 1, self->strerror);
954 }
955 else {
956 Py_INCREF(Py_None);
957 PyTuple_SET_ITEM(tuple, 1, Py_None);
958 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000959
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000960 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000961
962 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000963 Py_DECREF(tuple);
964 }
965 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000966 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000967
Richard Jones7b9558d2006-05-27 12:29:24 +0000968 return rtnval;
969}
970
971static PyMemberDef WindowsError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000972 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000973 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000974 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000975 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000976 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000977 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000978 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000979 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000980 {NULL} /* Sentinel */
981};
982
Richard Jones2d555b32006-05-27 16:15:11 +0000983ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
984 WindowsError_dealloc, 0, WindowsError_members,
985 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000986
987#endif /* MS_WINDOWS */
988
989
990/*
991 * VMSError extends OSError (I think)
992 */
993#ifdef __VMS
994MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +0000995 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000996#endif
997
998
999/*
1000 * EOFError extends StandardError
1001 */
1002SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +00001003 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001004
1005
1006/*
1007 * RuntimeError extends StandardError
1008 */
1009SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001010 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001011
1012
1013/*
1014 * NotImplementedError extends RuntimeError
1015 */
1016SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +00001017 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001018
1019/*
1020 * NameError extends StandardError
1021 */
1022SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +00001023 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001024
1025/*
1026 * UnboundLocalError extends NameError
1027 */
1028SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +00001029 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001030
1031/*
1032 * AttributeError extends StandardError
1033 */
1034SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001035 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001036
1037
1038/*
1039 * SyntaxError extends StandardError
1040 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001041
1042static int
1043SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
1044{
1045 PyObject *info = NULL;
1046 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
1047
1048 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1049 return -1;
1050
1051 if (lenargs >= 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001052 Py_CLEAR(self->msg);
Richard Jones7b9558d2006-05-27 12:29:24 +00001053 self->msg = PyTuple_GET_ITEM(args, 0);
1054 Py_INCREF(self->msg);
1055 }
1056 if (lenargs == 2) {
1057 info = PyTuple_GET_ITEM(args, 1);
1058 info = PySequence_Tuple(info);
1059 if (!info) return -1;
1060
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001061 if (PyTuple_GET_SIZE(info) != 4) {
1062 /* not a very good error message, but it's what Python 2.4 gives */
1063 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
1064 Py_DECREF(info);
1065 return -1;
1066 }
1067
1068 Py_CLEAR(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001069 self->filename = PyTuple_GET_ITEM(info, 0);
1070 Py_INCREF(self->filename);
1071
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001072 Py_CLEAR(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001073 self->lineno = PyTuple_GET_ITEM(info, 1);
1074 Py_INCREF(self->lineno);
1075
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001076 Py_CLEAR(self->offset);
Richard Jones7b9558d2006-05-27 12:29:24 +00001077 self->offset = PyTuple_GET_ITEM(info, 2);
1078 Py_INCREF(self->offset);
1079
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001080 Py_CLEAR(self->text);
Richard Jones7b9558d2006-05-27 12:29:24 +00001081 self->text = PyTuple_GET_ITEM(info, 3);
1082 Py_INCREF(self->text);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001083
1084 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001085 }
1086 return 0;
1087}
1088
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001089static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001090SyntaxError_clear(PySyntaxErrorObject *self)
1091{
1092 Py_CLEAR(self->msg);
1093 Py_CLEAR(self->filename);
1094 Py_CLEAR(self->lineno);
1095 Py_CLEAR(self->offset);
1096 Py_CLEAR(self->text);
1097 Py_CLEAR(self->print_file_and_line);
1098 return BaseException_clear((PyBaseExceptionObject *)self);
1099}
1100
1101static void
1102SyntaxError_dealloc(PySyntaxErrorObject *self)
1103{
Georg Brandl38f62372006-09-06 06:50:05 +00001104 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001105 SyntaxError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001106 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001107}
1108
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001109static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001110SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1111{
1112 Py_VISIT(self->msg);
1113 Py_VISIT(self->filename);
1114 Py_VISIT(self->lineno);
1115 Py_VISIT(self->offset);
1116 Py_VISIT(self->text);
1117 Py_VISIT(self->print_file_and_line);
1118 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1119}
1120
1121/* This is called "my_basename" instead of just "basename" to avoid name
1122 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1123 defined, and Python does define that. */
1124static char *
1125my_basename(char *name)
1126{
1127 char *cp = name;
1128 char *result = name;
1129
1130 if (name == NULL)
1131 return "???";
1132 while (*cp != '\0') {
1133 if (*cp == SEP)
1134 result = cp + 1;
1135 ++cp;
1136 }
1137 return result;
1138}
1139
1140
1141static PyObject *
1142SyntaxError_str(PySyntaxErrorObject *self)
1143{
1144 PyObject *str;
1145 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001146 int have_filename = 0;
1147 int have_lineno = 0;
1148 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001149 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001150
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001151 if (self->msg)
1152 str = PyObject_Str(self->msg);
1153 else
1154 str = PyObject_Str(Py_None);
Georg Brandl43ab1002006-05-28 20:57:09 +00001155 if (!str) return NULL;
1156 /* Don't fiddle with non-string return (shouldn't happen anyway) */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001157 if (!PyString_Check(str)) return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001158
1159 /* XXX -- do all the additional formatting with filename and
1160 lineno here */
1161
Georg Brandl43ab1002006-05-28 20:57:09 +00001162 have_filename = (self->filename != NULL) &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001163 PyString_Check(self->filename);
Georg Brandl43ab1002006-05-28 20:57:09 +00001164 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001165
Georg Brandl43ab1002006-05-28 20:57:09 +00001166 if (!have_filename && !have_lineno)
1167 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001168
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001169 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001170 if (have_filename)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001171 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001172
Georg Brandl43ab1002006-05-28 20:57:09 +00001173 buffer = PyMem_MALLOC(bufsize);
1174 if (buffer == NULL)
1175 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001176
Georg Brandl43ab1002006-05-28 20:57:09 +00001177 if (have_filename && have_lineno)
1178 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001179 PyString_AS_STRING(str),
1180 my_basename(PyString_AS_STRING(self->filename)),
Georg Brandl43ab1002006-05-28 20:57:09 +00001181 PyInt_AsLong(self->lineno));
1182 else if (have_filename)
1183 PyOS_snprintf(buffer, bufsize, "%s (%s)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001184 PyString_AS_STRING(str),
1185 my_basename(PyString_AS_STRING(self->filename)));
Georg Brandl43ab1002006-05-28 20:57:09 +00001186 else /* only have_lineno */
1187 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001188 PyString_AS_STRING(str),
Georg Brandl43ab1002006-05-28 20:57:09 +00001189 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001190
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001191 result = PyString_FromString(buffer);
Georg Brandl43ab1002006-05-28 20:57:09 +00001192 PyMem_FREE(buffer);
1193
1194 if (result == NULL)
1195 result = str;
1196 else
1197 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001198 return result;
1199}
1200
1201static PyMemberDef SyntaxError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001202 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1203 PyDoc_STR("exception msg")},
1204 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1205 PyDoc_STR("exception filename")},
1206 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1207 PyDoc_STR("exception lineno")},
1208 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1209 PyDoc_STR("exception offset")},
1210 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1211 PyDoc_STR("exception text")},
1212 {"print_file_and_line", T_OBJECT,
1213 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1214 PyDoc_STR("exception print_file_and_line")},
1215 {NULL} /* Sentinel */
1216};
1217
1218ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1219 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001220 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001221
1222
1223/*
1224 * IndentationError extends SyntaxError
1225 */
1226MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001227 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001228
1229
1230/*
1231 * TabError extends IndentationError
1232 */
1233MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001234 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001235
1236
1237/*
1238 * LookupError extends StandardError
1239 */
1240SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001241 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001242
1243
1244/*
1245 * IndexError extends LookupError
1246 */
1247SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001248 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001249
1250
1251/*
1252 * KeyError extends LookupError
1253 */
1254static PyObject *
1255KeyError_str(PyBaseExceptionObject *self)
1256{
1257 /* If args is a tuple of exactly one item, apply repr to args[0].
1258 This is done so that e.g. the exception raised by {}[''] prints
1259 KeyError: ''
1260 rather than the confusing
1261 KeyError
1262 alone. The downside is that if KeyError is raised with an explanatory
1263 string, that string will be displayed in quotes. Too bad.
1264 If args is anything else, use the default BaseException__str__().
1265 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001266 if (PyTuple_GET_SIZE(self->args) == 1) {
1267 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001268 }
1269 return BaseException_str(self);
1270}
1271
1272ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001273 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001274
1275
1276/*
1277 * ValueError extends StandardError
1278 */
1279SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001280 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001281
1282/*
1283 * UnicodeError extends ValueError
1284 */
1285
1286SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001287 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001288
1289#ifdef Py_USING_UNICODE
Richard Jones7b9558d2006-05-27 12:29:24 +00001290static PyObject *
1291get_string(PyObject *attr, const char *name)
1292{
1293 if (!attr) {
1294 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1295 return NULL;
1296 }
1297
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001298 if (!PyString_Check(attr)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001299 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1300 return NULL;
1301 }
1302 Py_INCREF(attr);
1303 return attr;
1304}
1305
1306
1307static int
1308set_string(PyObject **attr, const char *value)
1309{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001310 PyObject *obj = PyString_FromString(value);
Richard Jones7b9558d2006-05-27 12:29:24 +00001311 if (!obj)
1312 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001313 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001314 *attr = obj;
1315 return 0;
1316}
1317
1318
1319static PyObject *
1320get_unicode(PyObject *attr, const char *name)
1321{
1322 if (!attr) {
1323 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1324 return NULL;
1325 }
1326
1327 if (!PyUnicode_Check(attr)) {
1328 PyErr_Format(PyExc_TypeError,
1329 "%.200s attribute must be unicode", name);
1330 return NULL;
1331 }
1332 Py_INCREF(attr);
1333 return attr;
1334}
1335
1336PyObject *
1337PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1338{
1339 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1340}
1341
1342PyObject *
1343PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1344{
1345 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1346}
1347
1348PyObject *
1349PyUnicodeEncodeError_GetObject(PyObject *exc)
1350{
1351 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1352}
1353
1354PyObject *
1355PyUnicodeDecodeError_GetObject(PyObject *exc)
1356{
1357 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1358}
1359
1360PyObject *
1361PyUnicodeTranslateError_GetObject(PyObject *exc)
1362{
1363 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1364}
1365
1366int
1367PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1368{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001369 Py_ssize_t size;
1370 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1371 "object");
1372 if (!obj)
1373 return -1;
1374 *start = ((PyUnicodeErrorObject *)exc)->start;
1375 size = PyUnicode_GET_SIZE(obj);
1376 if (*start<0)
1377 *start = 0; /*XXX check for values <0*/
1378 if (*start>=size)
1379 *start = size-1;
1380 Py_DECREF(obj);
1381 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001382}
1383
1384
1385int
1386PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1387{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001388 Py_ssize_t size;
1389 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1390 "object");
1391 if (!obj)
1392 return -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001393 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001394 *start = ((PyUnicodeErrorObject *)exc)->start;
1395 if (*start<0)
1396 *start = 0;
1397 if (*start>=size)
1398 *start = size-1;
1399 Py_DECREF(obj);
1400 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001401}
1402
1403
1404int
1405PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1406{
1407 return PyUnicodeEncodeError_GetStart(exc, start);
1408}
1409
1410
1411int
1412PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1413{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001414 ((PyUnicodeErrorObject *)exc)->start = start;
1415 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001416}
1417
1418
1419int
1420PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1421{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001422 ((PyUnicodeErrorObject *)exc)->start = start;
1423 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001424}
1425
1426
1427int
1428PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1429{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001430 ((PyUnicodeErrorObject *)exc)->start = start;
1431 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001432}
1433
1434
1435int
1436PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1437{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001438 Py_ssize_t size;
1439 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1440 "object");
1441 if (!obj)
1442 return -1;
1443 *end = ((PyUnicodeErrorObject *)exc)->end;
1444 size = PyUnicode_GET_SIZE(obj);
1445 if (*end<1)
1446 *end = 1;
1447 if (*end>size)
1448 *end = size;
1449 Py_DECREF(obj);
1450 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001451}
1452
1453
1454int
1455PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1456{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001457 Py_ssize_t size;
1458 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1459 "object");
1460 if (!obj)
1461 return -1;
1462 *end = ((PyUnicodeErrorObject *)exc)->end;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001463 size = PyString_GET_SIZE(obj);
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001464 if (*end<1)
1465 *end = 1;
1466 if (*end>size)
1467 *end = size;
1468 Py_DECREF(obj);
1469 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001470}
1471
1472
1473int
1474PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1475{
1476 return PyUnicodeEncodeError_GetEnd(exc, start);
1477}
1478
1479
1480int
1481PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1482{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001483 ((PyUnicodeErrorObject *)exc)->end = end;
1484 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001485}
1486
1487
1488int
1489PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1490{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001491 ((PyUnicodeErrorObject *)exc)->end = end;
1492 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001493}
1494
1495
1496int
1497PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1498{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001499 ((PyUnicodeErrorObject *)exc)->end = end;
1500 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001501}
1502
1503PyObject *
1504PyUnicodeEncodeError_GetReason(PyObject *exc)
1505{
1506 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1507}
1508
1509
1510PyObject *
1511PyUnicodeDecodeError_GetReason(PyObject *exc)
1512{
1513 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1514}
1515
1516
1517PyObject *
1518PyUnicodeTranslateError_GetReason(PyObject *exc)
1519{
1520 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1521}
1522
1523
1524int
1525PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1526{
1527 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1528}
1529
1530
1531int
1532PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1533{
1534 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1535}
1536
1537
1538int
1539PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1540{
1541 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1542}
1543
1544
Richard Jones7b9558d2006-05-27 12:29:24 +00001545static int
1546UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1547 PyTypeObject *objecttype)
1548{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001549 Py_CLEAR(self->encoding);
1550 Py_CLEAR(self->object);
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001551 Py_CLEAR(self->reason);
1552
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001553 if (!PyArg_ParseTuple(args, "O!O!nnO!",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001554 &PyString_Type, &self->encoding,
Richard Jones7b9558d2006-05-27 12:29:24 +00001555 objecttype, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001556 &self->start,
1557 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001558 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001559 self->encoding = self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001560 return -1;
1561 }
1562
1563 Py_INCREF(self->encoding);
1564 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001565 Py_INCREF(self->reason);
1566
1567 return 0;
1568}
1569
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001570static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001571UnicodeError_clear(PyUnicodeErrorObject *self)
1572{
1573 Py_CLEAR(self->encoding);
1574 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001575 Py_CLEAR(self->reason);
1576 return BaseException_clear((PyBaseExceptionObject *)self);
1577}
1578
1579static void
1580UnicodeError_dealloc(PyUnicodeErrorObject *self)
1581{
Georg Brandl38f62372006-09-06 06:50:05 +00001582 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001583 UnicodeError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001584 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001585}
1586
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001587static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001588UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1589{
1590 Py_VISIT(self->encoding);
1591 Py_VISIT(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001592 Py_VISIT(self->reason);
1593 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1594}
1595
1596static PyMemberDef UnicodeError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001597 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1598 PyDoc_STR("exception encoding")},
1599 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1600 PyDoc_STR("exception object")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001601 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001602 PyDoc_STR("exception start")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001603 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001604 PyDoc_STR("exception end")},
1605 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1606 PyDoc_STR("exception reason")},
1607 {NULL} /* Sentinel */
1608};
1609
1610
1611/*
1612 * UnicodeEncodeError extends UnicodeError
1613 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001614
1615static int
1616UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1617{
1618 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1619 return -1;
1620 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1621 kwds, &PyUnicode_Type);
1622}
1623
1624static PyObject *
1625UnicodeEncodeError_str(PyObject *self)
1626{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001627 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001628
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001629 if (uself->end==uself->start+1) {
1630 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001631 char badchar_str[20];
1632 if (badchar <= 0xff)
1633 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1634 else if (badchar <= 0xffff)
1635 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1636 else
1637 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001638 return PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001639 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001640 PyString_AS_STRING(uself->encoding),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001641 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001642 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001643 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001644 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001645 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001646 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001647 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001648 PyString_AS_STRING(uself->encoding),
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001649 uself->start,
1650 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001651 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001652 );
1653}
1654
1655static PyTypeObject _PyExc_UnicodeEncodeError = {
1656 PyObject_HEAD_INIT(NULL)
1657 0,
Georg Brandl38f62372006-09-06 06:50:05 +00001658 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001659 sizeof(PyUnicodeErrorObject), 0,
1660 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1661 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1662 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001663 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1664 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001665 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001666 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001667};
1668PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1669
1670PyObject *
1671PyUnicodeEncodeError_Create(
1672 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1673 Py_ssize_t start, Py_ssize_t end, const char *reason)
1674{
1675 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001676 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001677}
1678
1679
1680/*
1681 * UnicodeDecodeError extends UnicodeError
1682 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001683
1684static int
1685UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1686{
1687 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1688 return -1;
1689 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001690 kwds, &PyString_Type);
Richard Jones7b9558d2006-05-27 12:29:24 +00001691}
1692
1693static PyObject *
1694UnicodeDecodeError_str(PyObject *self)
1695{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001696 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001697
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001698 if (uself->end==uself->start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001699 /* FromFormat does not support %02x, so format that separately */
1700 char byte[4];
1701 PyOS_snprintf(byte, sizeof(byte), "%02x",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001702 ((int)PyString_AS_STRING(uself->object)[uself->start])&0xff);
1703 return PyString_FromFormat(
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001704 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001705 PyString_AS_STRING(uself->encoding),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001706 byte,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001707 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001708 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001709 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001710 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001711 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001712 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001713 PyString_AS_STRING(uself->encoding),
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001714 uself->start,
1715 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001716 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001717 );
1718}
1719
1720static PyTypeObject _PyExc_UnicodeDecodeError = {
1721 PyObject_HEAD_INIT(NULL)
1722 0,
1723 EXC_MODULE_NAME "UnicodeDecodeError",
1724 sizeof(PyUnicodeErrorObject), 0,
1725 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1726 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1727 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001728 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1729 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001730 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001731 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001732};
1733PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1734
1735PyObject *
1736PyUnicodeDecodeError_Create(
1737 const char *encoding, const char *object, Py_ssize_t length,
1738 Py_ssize_t start, Py_ssize_t end, const char *reason)
1739{
1740 assert(length < INT_MAX);
1741 assert(start < INT_MAX);
1742 assert(end < INT_MAX);
1743 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001744 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001745}
1746
1747
1748/*
1749 * UnicodeTranslateError extends UnicodeError
1750 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001751
1752static int
1753UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1754 PyObject *kwds)
1755{
1756 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1757 return -1;
1758
1759 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001760 Py_CLEAR(self->reason);
1761
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001762 if (!PyArg_ParseTuple(args, "O!nnO!",
Richard Jones7b9558d2006-05-27 12:29:24 +00001763 &PyUnicode_Type, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001764 &self->start,
1765 &self->end,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001766 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001767 self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001768 return -1;
1769 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001770
Richard Jones7b9558d2006-05-27 12:29:24 +00001771 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001772 Py_INCREF(self->reason);
1773
1774 return 0;
1775}
1776
1777
1778static PyObject *
1779UnicodeTranslateError_str(PyObject *self)
1780{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001781 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001782
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001783 if (uself->end==uself->start+1) {
1784 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001785 char badchar_str[20];
1786 if (badchar <= 0xff)
1787 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1788 else if (badchar <= 0xffff)
1789 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1790 else
1791 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001792 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001793 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001794 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001795 uself->start,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001796 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001797 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001798 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001799 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001800 "can't translate characters in position %zd-%zd: %.400s",
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001801 uself->start,
1802 uself->end-1,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001803 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001804 );
1805}
1806
1807static PyTypeObject _PyExc_UnicodeTranslateError = {
1808 PyObject_HEAD_INIT(NULL)
1809 0,
1810 EXC_MODULE_NAME "UnicodeTranslateError",
1811 sizeof(PyUnicodeErrorObject), 0,
1812 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1813 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1814 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001815 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001816 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1817 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001818 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001819};
1820PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1821
1822PyObject *
1823PyUnicodeTranslateError_Create(
1824 const Py_UNICODE *object, Py_ssize_t length,
1825 Py_ssize_t start, Py_ssize_t end, const char *reason)
1826{
1827 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001828 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001829}
1830#endif
1831
1832
1833/*
1834 * AssertionError extends StandardError
1835 */
1836SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001837 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001838
1839
1840/*
1841 * ArithmeticError extends StandardError
1842 */
1843SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001844 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001845
1846
1847/*
1848 * FloatingPointError extends ArithmeticError
1849 */
1850SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001851 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001852
1853
1854/*
1855 * OverflowError extends ArithmeticError
1856 */
1857SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001858 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001859
1860
1861/*
1862 * ZeroDivisionError extends ArithmeticError
1863 */
1864SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001865 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001866
1867
1868/*
1869 * SystemError extends StandardError
1870 */
1871SimpleExtendsException(PyExc_StandardError, SystemError,
1872 "Internal error in the Python interpreter.\n"
1873 "\n"
1874 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001875 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001876
1877
1878/*
1879 * ReferenceError extends StandardError
1880 */
1881SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001882 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001883
1884
1885/*
1886 * MemoryError extends StandardError
1887 */
Richard Jones2d555b32006-05-27 16:15:11 +00001888SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001889
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00001890/*
1891 * BufferError extends StandardError
1892 */
1893SimpleExtendsException(PyExc_StandardError, BufferError, "Buffer error.");
1894
Richard Jones7b9558d2006-05-27 12:29:24 +00001895
1896/* Warning category docstrings */
1897
1898/*
1899 * Warning extends Exception
1900 */
1901SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001902 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001903
1904
1905/*
1906 * UserWarning extends Warning
1907 */
1908SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001909 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001910
1911
1912/*
1913 * DeprecationWarning extends Warning
1914 */
1915SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001916 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001917
1918
1919/*
1920 * PendingDeprecationWarning extends Warning
1921 */
1922SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1923 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001924 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001925
1926
1927/*
1928 * SyntaxWarning extends Warning
1929 */
1930SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001931 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001932
1933
1934/*
1935 * RuntimeWarning extends Warning
1936 */
1937SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001938 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001939
1940
1941/*
1942 * FutureWarning extends Warning
1943 */
1944SimpleExtendsException(PyExc_Warning, FutureWarning,
1945 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001946 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001947
1948
1949/*
1950 * ImportWarning extends Warning
1951 */
1952SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001953 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00001954
1955
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001956/*
1957 * UnicodeWarning extends Warning
1958 */
1959SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1960 "Base class for warnings about Unicode related problems, mostly\n"
1961 "related to conversion problems.");
1962
Christian Heimes1a6387e2008-03-26 12:49:49 +00001963/*
1964 * BytesWarning extends Warning
1965 */
1966SimpleExtendsException(PyExc_Warning, BytesWarning,
1967 "Base class for warnings about bytes and buffer related problems, mostly\n"
1968 "related to conversion from str or comparing to str.");
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001969
Richard Jones7b9558d2006-05-27 12:29:24 +00001970/* Pre-computed MemoryError instance. Best to create this as early as
1971 * possible and not wait until a MemoryError is actually raised!
1972 */
1973PyObject *PyExc_MemoryErrorInst=NULL;
1974
Brett Cannon1e534b52007-09-07 04:18:30 +00001975/* Pre-computed RuntimeError instance for when recursion depth is reached.
1976 Meant to be used when normalizing the exception for exceeding the recursion
1977 depth will cause its own infinite recursion.
1978*/
1979PyObject *PyExc_RecursionErrorInst = NULL;
1980
Richard Jones7b9558d2006-05-27 12:29:24 +00001981/* module global functions */
1982static PyMethodDef functions[] = {
1983 /* Sentinel */
1984 {NULL, NULL}
1985};
1986
1987#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1988 Py_FatalError("exceptions bootstrapping error.");
1989
1990#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1991 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1992 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1993 Py_FatalError("Module dictionary insertion problem.");
1994
KristjĂ¡n Valur JĂ³nssonf6083172006-06-12 15:45:12 +00001995
Richard Jones7b9558d2006-05-27 12:29:24 +00001996PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001997_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00001998{
1999 PyObject *m, *bltinmod, *bdict;
2000
2001 PRE_INIT(BaseException)
2002 PRE_INIT(Exception)
2003 PRE_INIT(StandardError)
2004 PRE_INIT(TypeError)
2005 PRE_INIT(StopIteration)
2006 PRE_INIT(GeneratorExit)
2007 PRE_INIT(SystemExit)
2008 PRE_INIT(KeyboardInterrupt)
2009 PRE_INIT(ImportError)
2010 PRE_INIT(EnvironmentError)
2011 PRE_INIT(IOError)
2012 PRE_INIT(OSError)
2013#ifdef MS_WINDOWS
2014 PRE_INIT(WindowsError)
2015#endif
2016#ifdef __VMS
2017 PRE_INIT(VMSError)
2018#endif
2019 PRE_INIT(EOFError)
2020 PRE_INIT(RuntimeError)
2021 PRE_INIT(NotImplementedError)
2022 PRE_INIT(NameError)
2023 PRE_INIT(UnboundLocalError)
2024 PRE_INIT(AttributeError)
2025 PRE_INIT(SyntaxError)
2026 PRE_INIT(IndentationError)
2027 PRE_INIT(TabError)
2028 PRE_INIT(LookupError)
2029 PRE_INIT(IndexError)
2030 PRE_INIT(KeyError)
2031 PRE_INIT(ValueError)
2032 PRE_INIT(UnicodeError)
2033#ifdef Py_USING_UNICODE
2034 PRE_INIT(UnicodeEncodeError)
2035 PRE_INIT(UnicodeDecodeError)
2036 PRE_INIT(UnicodeTranslateError)
2037#endif
2038 PRE_INIT(AssertionError)
2039 PRE_INIT(ArithmeticError)
2040 PRE_INIT(FloatingPointError)
2041 PRE_INIT(OverflowError)
2042 PRE_INIT(ZeroDivisionError)
2043 PRE_INIT(SystemError)
2044 PRE_INIT(ReferenceError)
2045 PRE_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002046 PRE_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002047 PRE_INIT(Warning)
2048 PRE_INIT(UserWarning)
2049 PRE_INIT(DeprecationWarning)
2050 PRE_INIT(PendingDeprecationWarning)
2051 PRE_INIT(SyntaxWarning)
2052 PRE_INIT(RuntimeWarning)
2053 PRE_INIT(FutureWarning)
2054 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002055 PRE_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002056 PRE_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002057
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002058 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2059 (PyObject *)NULL, PYTHON_API_VERSION);
Richard Jones7b9558d2006-05-27 12:29:24 +00002060 if (m == NULL) return;
2061
2062 bltinmod = PyImport_ImportModule("__builtin__");
2063 if (bltinmod == NULL)
2064 Py_FatalError("exceptions bootstrapping error.");
2065 bdict = PyModule_GetDict(bltinmod);
2066 if (bdict == NULL)
2067 Py_FatalError("exceptions bootstrapping error.");
2068
2069 POST_INIT(BaseException)
2070 POST_INIT(Exception)
2071 POST_INIT(StandardError)
2072 POST_INIT(TypeError)
2073 POST_INIT(StopIteration)
2074 POST_INIT(GeneratorExit)
2075 POST_INIT(SystemExit)
2076 POST_INIT(KeyboardInterrupt)
2077 POST_INIT(ImportError)
2078 POST_INIT(EnvironmentError)
2079 POST_INIT(IOError)
2080 POST_INIT(OSError)
2081#ifdef MS_WINDOWS
2082 POST_INIT(WindowsError)
2083#endif
2084#ifdef __VMS
2085 POST_INIT(VMSError)
2086#endif
2087 POST_INIT(EOFError)
2088 POST_INIT(RuntimeError)
2089 POST_INIT(NotImplementedError)
2090 POST_INIT(NameError)
2091 POST_INIT(UnboundLocalError)
2092 POST_INIT(AttributeError)
2093 POST_INIT(SyntaxError)
2094 POST_INIT(IndentationError)
2095 POST_INIT(TabError)
2096 POST_INIT(LookupError)
2097 POST_INIT(IndexError)
2098 POST_INIT(KeyError)
2099 POST_INIT(ValueError)
2100 POST_INIT(UnicodeError)
2101#ifdef Py_USING_UNICODE
2102 POST_INIT(UnicodeEncodeError)
2103 POST_INIT(UnicodeDecodeError)
2104 POST_INIT(UnicodeTranslateError)
2105#endif
2106 POST_INIT(AssertionError)
2107 POST_INIT(ArithmeticError)
2108 POST_INIT(FloatingPointError)
2109 POST_INIT(OverflowError)
2110 POST_INIT(ZeroDivisionError)
2111 POST_INIT(SystemError)
2112 POST_INIT(ReferenceError)
2113 POST_INIT(MemoryError)
Benjamin Petersonc0bf76d2008-07-30 17:45:10 +00002114 POST_INIT(BufferError)
Richard Jones7b9558d2006-05-27 12:29:24 +00002115 POST_INIT(Warning)
2116 POST_INIT(UserWarning)
2117 POST_INIT(DeprecationWarning)
2118 POST_INIT(PendingDeprecationWarning)
2119 POST_INIT(SyntaxWarning)
2120 POST_INIT(RuntimeWarning)
2121 POST_INIT(FutureWarning)
2122 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002123 POST_INIT(UnicodeWarning)
Christian Heimes1a6387e2008-03-26 12:49:49 +00002124 POST_INIT(BytesWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002125
2126 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2127 if (!PyExc_MemoryErrorInst)
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +00002128 Py_FatalError("Cannot pre-allocate MemoryError instance");
Richard Jones7b9558d2006-05-27 12:29:24 +00002129
Brett Cannon1e534b52007-09-07 04:18:30 +00002130 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2131 if (!PyExc_RecursionErrorInst)
2132 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2133 "recursion errors");
2134 else {
2135 PyBaseExceptionObject *err_inst =
2136 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2137 PyObject *args_tuple;
2138 PyObject *exc_message;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002139 exc_message = PyString_FromString("maximum recursion depth exceeded");
Brett Cannon1e534b52007-09-07 04:18:30 +00002140 if (!exc_message)
2141 Py_FatalError("cannot allocate argument for RuntimeError "
2142 "pre-allocation");
2143 args_tuple = PyTuple_Pack(1, exc_message);
2144 if (!args_tuple)
2145 Py_FatalError("cannot allocate tuple for RuntimeError "
2146 "pre-allocation");
2147 Py_DECREF(exc_message);
2148 if (BaseException_init(err_inst, args_tuple, NULL))
2149 Py_FatalError("init of pre-allocated RuntimeError failed");
2150 Py_DECREF(args_tuple);
2151 }
2152
Richard Jones7b9558d2006-05-27 12:29:24 +00002153 Py_DECREF(bltinmod);
2154}
2155
2156void
2157_PyExc_Fini(void)
2158{
Alexandre Vassalotti55bd1ef2009-06-12 18:56:57 +00002159 Py_CLEAR(PyExc_MemoryErrorInst);
2160 Py_CLEAR(PyExc_RecursionErrorInst);
Richard Jones7b9558d2006-05-27 12:29:24 +00002161}