blob: b1d5d0b6d5097ffdb90a3202b80d38b25d73126c [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
Michael W. Hudson22a80e72006-05-28 15:51:40 +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:
107 out = PyString_FromString("");
108 break;
109 case 1:
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000110 out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +0000111 break;
Richard Jones7b9558d2006-05-27 12:29:24 +0000112 default:
113 out = PyObject_Str(self->args);
114 break;
115 }
116
117 return out;
118}
119
120static PyObject *
121BaseException_repr(PyBaseExceptionObject *self)
122{
Richard Jones7b9558d2006-05-27 12:29:24 +0000123 PyObject *repr_suffix;
124 PyObject *repr;
125 char *name;
126 char *dot;
127
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000128 repr_suffix = PyObject_Repr(self->args);
129 if (!repr_suffix)
Richard Jones7b9558d2006-05-27 12:29:24 +0000130 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000131
Christian Heimese93237d2007-12-19 02:37:44 +0000132 name = (char *)Py_TYPE(self)->tp_name;
Richard Jones7b9558d2006-05-27 12:29:24 +0000133 dot = strrchr(name, '.');
134 if (dot != NULL) name = dot+1;
135
136 repr = PyString_FromString(name);
137 if (!repr) {
138 Py_DECREF(repr_suffix);
139 return NULL;
140 }
141
142 PyString_ConcatAndDel(&repr, repr_suffix);
143 return repr;
144}
145
146/* Pickling support */
147static PyObject *
148BaseException_reduce(PyBaseExceptionObject *self)
149{
Georg Brandlddba4732006-05-30 07:04:55 +0000150 if (self->args && self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000151 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000152 else
Christian Heimese93237d2007-12-19 02:37:44 +0000153 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000154}
155
Georg Brandl85ac8502006-06-01 06:39:19 +0000156/*
157 * Needed for backward compatibility, since exceptions used to store
158 * all their attributes in the __dict__. Code is taken from cPickle's
159 * load_build function.
160 */
161static PyObject *
162BaseException_setstate(PyObject *self, PyObject *state)
163{
164 PyObject *d_key, *d_value;
165 Py_ssize_t i = 0;
166
167 if (state != Py_None) {
168 if (!PyDict_Check(state)) {
169 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
170 return NULL;
171 }
172 while (PyDict_Next(state, &i, &d_key, &d_value)) {
173 if (PyObject_SetAttr(self, d_key, d_value) < 0)
174 return NULL;
175 }
176 }
177 Py_RETURN_NONE;
178}
Richard Jones7b9558d2006-05-27 12:29:24 +0000179
Richard Jones7b9558d2006-05-27 12:29:24 +0000180
181static PyMethodDef BaseException_methods[] = {
182 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Georg Brandl85ac8502006-06-01 06:39:19 +0000183 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Richard Jones7b9558d2006-05-27 12:29:24 +0000184 {NULL, NULL, 0, NULL},
185};
186
187
188
189static PyObject *
190BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
191{
192 return PySequence_GetItem(self->args, index);
193}
194
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000195static PyObject *
196BaseException_getslice(PyBaseExceptionObject *self,
197 Py_ssize_t start, Py_ssize_t stop)
198{
199 return PySequence_GetSlice(self->args, start, stop);
200}
201
Richard Jones7b9558d2006-05-27 12:29:24 +0000202static PySequenceMethods BaseException_as_sequence = {
203 0, /* sq_length; */
204 0, /* sq_concat; */
205 0, /* sq_repeat; */
206 (ssizeargfunc)BaseException_getitem, /* sq_item; */
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000207 (ssizessizeargfunc)BaseException_getslice, /* sq_slice; */
Richard Jones7b9558d2006-05-27 12:29:24 +0000208 0, /* sq_ass_item; */
209 0, /* sq_ass_slice; */
210 0, /* sq_contains; */
211 0, /* sq_inplace_concat; */
212 0 /* sq_inplace_repeat; */
213};
214
Richard Jones7b9558d2006-05-27 12:29:24 +0000215static PyObject *
216BaseException_get_dict(PyBaseExceptionObject *self)
217{
218 if (self->dict == NULL) {
219 self->dict = PyDict_New();
220 if (!self->dict)
221 return NULL;
222 }
223 Py_INCREF(self->dict);
224 return self->dict;
225}
226
227static int
228BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
229{
230 if (val == NULL) {
231 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
232 return -1;
233 }
234 if (!PyDict_Check(val)) {
235 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
236 return -1;
237 }
238 Py_CLEAR(self->dict);
239 Py_INCREF(val);
240 self->dict = val;
241 return 0;
242}
243
244static PyObject *
245BaseException_get_args(PyBaseExceptionObject *self)
246{
247 if (self->args == NULL) {
248 Py_INCREF(Py_None);
249 return Py_None;
250 }
251 Py_INCREF(self->args);
252 return self->args;
253}
254
255static int
256BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
257{
258 PyObject *seq;
259 if (val == NULL) {
260 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
261 return -1;
262 }
263 seq = PySequence_Tuple(val);
264 if (!seq) return -1;
Georg Brandlc7c51142006-05-29 09:46:51 +0000265 Py_CLEAR(self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000266 self->args = seq;
267 return 0;
268}
269
Brett Cannon229cee22007-05-05 01:34:02 +0000270static PyObject *
271BaseException_get_message(PyBaseExceptionObject *self)
272{
273 int ret;
274 ret = PyErr_WarnEx(PyExc_DeprecationWarning,
275 "BaseException.message has been deprecated as "
276 "of Python 2.6",
277 1);
278 if (ret == -1)
279 return NULL;
280
281 Py_INCREF(self->message);
282 return self->message;
283}
284
285static int
286BaseException_set_message(PyBaseExceptionObject *self, PyObject *val)
287{
288 int ret;
289 ret = PyErr_WarnEx(PyExc_DeprecationWarning,
290 "BaseException.message has been deprecated as "
291 "of Python 2.6",
292 1);
293 if (ret == -1)
294 return -1;
295 Py_INCREF(val);
296 Py_DECREF(self->message);
297 self->message = val;
298 return 0;
299}
300
Richard Jones7b9558d2006-05-27 12:29:24 +0000301static PyGetSetDef BaseException_getset[] = {
302 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
303 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Brett Cannon229cee22007-05-05 01:34:02 +0000304 {"message", (getter)BaseException_get_message,
305 (setter)BaseException_set_message},
Richard Jones7b9558d2006-05-27 12:29:24 +0000306 {NULL},
307};
308
309
310static PyTypeObject _PyExc_BaseException = {
311 PyObject_HEAD_INIT(NULL)
312 0, /*ob_size*/
313 EXC_MODULE_NAME "BaseException", /*tp_name*/
314 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
315 0, /*tp_itemsize*/
316 (destructor)BaseException_dealloc, /*tp_dealloc*/
317 0, /*tp_print*/
318 0, /*tp_getattr*/
319 0, /*tp_setattr*/
320 0, /* tp_compare; */
321 (reprfunc)BaseException_repr, /*tp_repr*/
322 0, /*tp_as_number*/
323 &BaseException_as_sequence, /*tp_as_sequence*/
324 0, /*tp_as_mapping*/
325 0, /*tp_hash */
326 0, /*tp_call*/
327 (reprfunc)BaseException_str, /*tp_str*/
328 PyObject_GenericGetAttr, /*tp_getattro*/
329 PyObject_GenericSetAttr, /*tp_setattro*/
330 0, /*tp_as_buffer*/
Neal Norwitzee3a1b52007-02-25 19:44:48 +0000331 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
332 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Richard Jones7b9558d2006-05-27 12:29:24 +0000333 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
334 (traverseproc)BaseException_traverse, /* tp_traverse */
335 (inquiry)BaseException_clear, /* tp_clear */
336 0, /* tp_richcompare */
337 0, /* tp_weaklistoffset */
338 0, /* tp_iter */
339 0, /* tp_iternext */
340 BaseException_methods, /* tp_methods */
Brett Cannon229cee22007-05-05 01:34:02 +0000341 0, /* tp_members */
Richard Jones7b9558d2006-05-27 12:29:24 +0000342 BaseException_getset, /* tp_getset */
343 0, /* tp_base */
344 0, /* tp_dict */
345 0, /* tp_descr_get */
346 0, /* tp_descr_set */
347 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
348 (initproc)BaseException_init, /* tp_init */
349 0, /* tp_alloc */
350 BaseException_new, /* tp_new */
351};
352/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
353from the previous implmentation and also allowing Python objects to be used
354in the API */
355PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
356
Richard Jones2d555b32006-05-27 16:15:11 +0000357/* note these macros omit the last semicolon so the macro invocation may
358 * include it and not look strange.
359 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000360#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
361static PyTypeObject _PyExc_ ## EXCNAME = { \
362 PyObject_HEAD_INIT(NULL) \
363 0, \
364 EXC_MODULE_NAME # EXCNAME, \
365 sizeof(PyBaseExceptionObject), \
366 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
367 0, 0, 0, 0, 0, 0, 0, \
368 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
369 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
370 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
371 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
372 (initproc)BaseException_init, 0, BaseException_new,\
373}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000374PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000375
376#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
377static PyTypeObject _PyExc_ ## EXCNAME = { \
378 PyObject_HEAD_INIT(NULL) \
379 0, \
380 EXC_MODULE_NAME # EXCNAME, \
381 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000382 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000383 0, 0, 0, 0, 0, \
384 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000385 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
386 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000387 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000388 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000389}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000390PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000391
392#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
393static PyTypeObject _PyExc_ ## EXCNAME = { \
394 PyObject_HEAD_INIT(NULL) \
395 0, \
396 EXC_MODULE_NAME # EXCNAME, \
397 sizeof(Py ## EXCSTORE ## Object), 0, \
398 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
399 (reprfunc)EXCSTR, 0, 0, 0, \
400 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
401 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
402 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
403 EXCMEMBERS, 0, &_ ## EXCBASE, \
404 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000405 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000406}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000407PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000408
409
410/*
411 * Exception extends BaseException
412 */
413SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000414 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000415
416
417/*
418 * StandardError extends Exception
419 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000420SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000421 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000422 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000423
424
425/*
426 * TypeError extends StandardError
427 */
428SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000429 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000430
431
432/*
433 * StopIteration extends Exception
434 */
435SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000436 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000437
438
439/*
Christian Heimes44eeaec2007-12-03 20:01:02 +0000440 * GeneratorExit extends BaseException
Richard Jones7b9558d2006-05-27 12:29:24 +0000441 */
Christian Heimes44eeaec2007-12-03 20:01:02 +0000442SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000443 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000444
445
446/*
447 * SystemExit extends BaseException
448 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000449
450static int
451SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
452{
453 Py_ssize_t size = PyTuple_GET_SIZE(args);
454
455 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
456 return -1;
457
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000458 if (size == 0)
459 return 0;
460 Py_CLEAR(self->code);
Richard Jones7b9558d2006-05-27 12:29:24 +0000461 if (size == 1)
462 self->code = PyTuple_GET_ITEM(args, 0);
463 else if (size > 1)
464 self->code = args;
465 Py_INCREF(self->code);
466 return 0;
467}
468
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000469static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000470SystemExit_clear(PySystemExitObject *self)
471{
472 Py_CLEAR(self->code);
473 return BaseException_clear((PyBaseExceptionObject *)self);
474}
475
476static void
477SystemExit_dealloc(PySystemExitObject *self)
478{
Georg Brandl38f62372006-09-06 06:50:05 +0000479 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000480 SystemExit_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000481 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000482}
483
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000484static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000485SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
486{
487 Py_VISIT(self->code);
488 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
489}
490
491static PyMemberDef SystemExit_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000492 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
493 PyDoc_STR("exception code")},
494 {NULL} /* Sentinel */
495};
496
497ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
498 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000499 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000500
501/*
502 * KeyboardInterrupt extends BaseException
503 */
504SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000505 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000506
507
508/*
509 * ImportError extends StandardError
510 */
511SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000512 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000513
514
515/*
516 * EnvironmentError extends StandardError
517 */
518
Richard Jones7b9558d2006-05-27 12:29:24 +0000519/* Where a function has a single filename, such as open() or some
520 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
521 * called, giving a third argument which is the filename. But, so
522 * that old code using in-place unpacking doesn't break, e.g.:
523 *
524 * except IOError, (errno, strerror):
525 *
526 * we hack args so that it only contains two items. This also
527 * means we need our own __str__() which prints out the filename
528 * when it was supplied.
529 */
530static int
531EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
532 PyObject *kwds)
533{
534 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
535 PyObject *subslice = NULL;
536
537 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
538 return -1;
539
Georg Brandl3267d282006-09-30 09:03:42 +0000540 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000541 return 0;
542 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000543
544 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000545 &myerrno, &strerror, &filename)) {
546 return -1;
547 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000548 Py_CLEAR(self->myerrno); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000549 self->myerrno = myerrno;
550 Py_INCREF(self->myerrno);
551
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000552 Py_CLEAR(self->strerror); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000553 self->strerror = strerror;
554 Py_INCREF(self->strerror);
555
556 /* self->filename will remain Py_None otherwise */
557 if (filename != NULL) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000558 Py_CLEAR(self->filename); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000559 self->filename = filename;
560 Py_INCREF(self->filename);
561
562 subslice = PyTuple_GetSlice(args, 0, 2);
563 if (!subslice)
564 return -1;
565
566 Py_DECREF(self->args); /* replacing args */
567 self->args = subslice;
568 }
569 return 0;
570}
571
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000572static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000573EnvironmentError_clear(PyEnvironmentErrorObject *self)
574{
575 Py_CLEAR(self->myerrno);
576 Py_CLEAR(self->strerror);
577 Py_CLEAR(self->filename);
578 return BaseException_clear((PyBaseExceptionObject *)self);
579}
580
581static void
582EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
583{
Georg Brandl38f62372006-09-06 06:50:05 +0000584 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000585 EnvironmentError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000586 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000587}
588
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000589static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000590EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
591 void *arg)
592{
593 Py_VISIT(self->myerrno);
594 Py_VISIT(self->strerror);
595 Py_VISIT(self->filename);
596 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
597}
598
599static PyObject *
600EnvironmentError_str(PyEnvironmentErrorObject *self)
601{
602 PyObject *rtnval = NULL;
603
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000604 if (self->filename) {
605 PyObject *fmt;
606 PyObject *repr;
607 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000608
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000609 fmt = PyString_FromString("[Errno %s] %s: %s");
610 if (!fmt)
611 return NULL;
612
613 repr = PyObject_Repr(self->filename);
614 if (!repr) {
615 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000616 return NULL;
617 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000618 tuple = PyTuple_New(3);
619 if (!tuple) {
620 Py_DECREF(repr);
621 Py_DECREF(fmt);
622 return NULL;
623 }
624
625 if (self->myerrno) {
626 Py_INCREF(self->myerrno);
627 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
628 }
629 else {
630 Py_INCREF(Py_None);
631 PyTuple_SET_ITEM(tuple, 0, Py_None);
632 }
633 if (self->strerror) {
634 Py_INCREF(self->strerror);
635 PyTuple_SET_ITEM(tuple, 1, self->strerror);
636 }
637 else {
638 Py_INCREF(Py_None);
639 PyTuple_SET_ITEM(tuple, 1, Py_None);
640 }
641
Richard Jones7b9558d2006-05-27 12:29:24 +0000642 PyTuple_SET_ITEM(tuple, 2, repr);
643
644 rtnval = PyString_Format(fmt, tuple);
645
646 Py_DECREF(fmt);
647 Py_DECREF(tuple);
648 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000649 else if (self->myerrno && self->strerror) {
650 PyObject *fmt;
651 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000652
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000653 fmt = PyString_FromString("[Errno %s] %s");
654 if (!fmt)
655 return NULL;
656
657 tuple = PyTuple_New(2);
658 if (!tuple) {
659 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000660 return NULL;
661 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000662
663 if (self->myerrno) {
664 Py_INCREF(self->myerrno);
665 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
666 }
667 else {
668 Py_INCREF(Py_None);
669 PyTuple_SET_ITEM(tuple, 0, Py_None);
670 }
671 if (self->strerror) {
672 Py_INCREF(self->strerror);
673 PyTuple_SET_ITEM(tuple, 1, self->strerror);
674 }
675 else {
676 Py_INCREF(Py_None);
677 PyTuple_SET_ITEM(tuple, 1, Py_None);
678 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000679
680 rtnval = PyString_Format(fmt, tuple);
681
682 Py_DECREF(fmt);
683 Py_DECREF(tuple);
684 }
685 else
686 rtnval = BaseException_str((PyBaseExceptionObject *)self);
687
688 return rtnval;
689}
690
691static PyMemberDef EnvironmentError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000692 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000693 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000694 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000695 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000696 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000697 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000698 {NULL} /* Sentinel */
699};
700
701
702static PyObject *
703EnvironmentError_reduce(PyEnvironmentErrorObject *self)
704{
705 PyObject *args = self->args;
706 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000707
Richard Jones7b9558d2006-05-27 12:29:24 +0000708 /* self->args is only the first two real arguments if there was a
709 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000710 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000711 args = PyTuple_New(3);
712 if (!args) return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000713
714 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000715 Py_INCREF(tmp);
716 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000717
718 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000719 Py_INCREF(tmp);
720 PyTuple_SET_ITEM(args, 1, tmp);
721
722 Py_INCREF(self->filename);
723 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000724 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000725 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000726
727 if (self->dict)
Christian Heimese93237d2007-12-19 02:37:44 +0000728 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000729 else
Christian Heimese93237d2007-12-19 02:37:44 +0000730 res = PyTuple_Pack(2, Py_TYPE(self), args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000731 Py_DECREF(args);
732 return res;
733}
734
735
736static PyMethodDef EnvironmentError_methods[] = {
737 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
738 {NULL}
739};
740
741ComplexExtendsException(PyExc_StandardError, EnvironmentError,
742 EnvironmentError, EnvironmentError_dealloc,
743 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000744 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000745 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000746
747
748/*
749 * IOError extends EnvironmentError
750 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000751MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000752 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000753
754
755/*
756 * OSError extends EnvironmentError
757 */
758MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000759 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000760
761
762/*
763 * WindowsError extends OSError
764 */
765#ifdef MS_WINDOWS
766#include "errmap.h"
767
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000768static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000769WindowsError_clear(PyWindowsErrorObject *self)
770{
771 Py_CLEAR(self->myerrno);
772 Py_CLEAR(self->strerror);
773 Py_CLEAR(self->filename);
774 Py_CLEAR(self->winerror);
775 return BaseException_clear((PyBaseExceptionObject *)self);
776}
777
778static void
779WindowsError_dealloc(PyWindowsErrorObject *self)
780{
Georg Brandl38f62372006-09-06 06:50:05 +0000781 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000782 WindowsError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +0000783 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000784}
785
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000786static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000787WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
788{
789 Py_VISIT(self->myerrno);
790 Py_VISIT(self->strerror);
791 Py_VISIT(self->filename);
792 Py_VISIT(self->winerror);
793 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
794}
795
Richard Jones7b9558d2006-05-27 12:29:24 +0000796static int
797WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
798{
799 PyObject *o_errcode = NULL;
800 long errcode;
801 long posix_errno;
802
803 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
804 == -1)
805 return -1;
806
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000807 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000808 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000809
810 /* Set errno to the POSIX errno, and winerror to the Win32
811 error code. */
812 errcode = PyInt_AsLong(self->myerrno);
813 if (errcode == -1 && PyErr_Occurred())
814 return -1;
815 posix_errno = winerror_to_errno(errcode);
816
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000817 Py_CLEAR(self->winerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000818 self->winerror = self->myerrno;
819
820 o_errcode = PyInt_FromLong(posix_errno);
821 if (!o_errcode)
822 return -1;
823
824 self->myerrno = o_errcode;
825
826 return 0;
827}
828
829
830static PyObject *
831WindowsError_str(PyWindowsErrorObject *self)
832{
Richard Jones7b9558d2006-05-27 12:29:24 +0000833 PyObject *rtnval = NULL;
834
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000835 if (self->filename) {
836 PyObject *fmt;
837 PyObject *repr;
838 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000839
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000840 fmt = PyString_FromString("[Error %s] %s: %s");
841 if (!fmt)
842 return NULL;
843
844 repr = PyObject_Repr(self->filename);
845 if (!repr) {
846 Py_DECREF(fmt);
847 return NULL;
848 }
849 tuple = PyTuple_New(3);
850 if (!tuple) {
851 Py_DECREF(repr);
852 Py_DECREF(fmt);
853 return NULL;
854 }
855
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000856 if (self->winerror) {
857 Py_INCREF(self->winerror);
858 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000859 }
860 else {
861 Py_INCREF(Py_None);
862 PyTuple_SET_ITEM(tuple, 0, Py_None);
863 }
864 if (self->strerror) {
865 Py_INCREF(self->strerror);
866 PyTuple_SET_ITEM(tuple, 1, self->strerror);
867 }
868 else {
869 Py_INCREF(Py_None);
870 PyTuple_SET_ITEM(tuple, 1, Py_None);
871 }
872
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000873 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000874
875 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000876
877 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000878 Py_DECREF(tuple);
879 }
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000880 else if (self->winerror && self->strerror) {
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000881 PyObject *fmt;
882 PyObject *tuple;
883
Richard Jones7b9558d2006-05-27 12:29:24 +0000884 fmt = PyString_FromString("[Error %s] %s");
885 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000886 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000887
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000888 tuple = PyTuple_New(2);
889 if (!tuple) {
890 Py_DECREF(fmt);
891 return NULL;
892 }
893
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000894 if (self->winerror) {
895 Py_INCREF(self->winerror);
896 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000897 }
898 else {
899 Py_INCREF(Py_None);
900 PyTuple_SET_ITEM(tuple, 0, Py_None);
901 }
902 if (self->strerror) {
903 Py_INCREF(self->strerror);
904 PyTuple_SET_ITEM(tuple, 1, self->strerror);
905 }
906 else {
907 Py_INCREF(Py_None);
908 PyTuple_SET_ITEM(tuple, 1, Py_None);
909 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000910
911 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000912
913 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000914 Py_DECREF(tuple);
915 }
916 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000917 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000918
Richard Jones7b9558d2006-05-27 12:29:24 +0000919 return rtnval;
920}
921
922static PyMemberDef WindowsError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +0000923 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000924 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000925 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000926 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000927 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000928 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000929 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000930 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000931 {NULL} /* Sentinel */
932};
933
Richard Jones2d555b32006-05-27 16:15:11 +0000934ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
935 WindowsError_dealloc, 0, WindowsError_members,
936 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000937
938#endif /* MS_WINDOWS */
939
940
941/*
942 * VMSError extends OSError (I think)
943 */
944#ifdef __VMS
945MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +0000946 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000947#endif
948
949
950/*
951 * EOFError extends StandardError
952 */
953SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +0000954 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000955
956
957/*
958 * RuntimeError extends StandardError
959 */
960SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000961 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000962
963
964/*
965 * NotImplementedError extends RuntimeError
966 */
967SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +0000968 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000969
970/*
971 * NameError extends StandardError
972 */
973SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +0000974 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000975
976/*
977 * UnboundLocalError extends NameError
978 */
979SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +0000980 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000981
982/*
983 * AttributeError extends StandardError
984 */
985SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000986 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000987
988
989/*
990 * SyntaxError extends StandardError
991 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000992
993static int
994SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
995{
996 PyObject *info = NULL;
997 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
998
999 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1000 return -1;
1001
1002 if (lenargs >= 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001003 Py_CLEAR(self->msg);
Richard Jones7b9558d2006-05-27 12:29:24 +00001004 self->msg = PyTuple_GET_ITEM(args, 0);
1005 Py_INCREF(self->msg);
1006 }
1007 if (lenargs == 2) {
1008 info = PyTuple_GET_ITEM(args, 1);
1009 info = PySequence_Tuple(info);
1010 if (!info) return -1;
1011
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001012 if (PyTuple_GET_SIZE(info) != 4) {
1013 /* not a very good error message, but it's what Python 2.4 gives */
1014 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
1015 Py_DECREF(info);
1016 return -1;
1017 }
1018
1019 Py_CLEAR(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001020 self->filename = PyTuple_GET_ITEM(info, 0);
1021 Py_INCREF(self->filename);
1022
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001023 Py_CLEAR(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001024 self->lineno = PyTuple_GET_ITEM(info, 1);
1025 Py_INCREF(self->lineno);
1026
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001027 Py_CLEAR(self->offset);
Richard Jones7b9558d2006-05-27 12:29:24 +00001028 self->offset = PyTuple_GET_ITEM(info, 2);
1029 Py_INCREF(self->offset);
1030
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001031 Py_CLEAR(self->text);
Richard Jones7b9558d2006-05-27 12:29:24 +00001032 self->text = PyTuple_GET_ITEM(info, 3);
1033 Py_INCREF(self->text);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001034
1035 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001036 }
1037 return 0;
1038}
1039
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001040static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001041SyntaxError_clear(PySyntaxErrorObject *self)
1042{
1043 Py_CLEAR(self->msg);
1044 Py_CLEAR(self->filename);
1045 Py_CLEAR(self->lineno);
1046 Py_CLEAR(self->offset);
1047 Py_CLEAR(self->text);
1048 Py_CLEAR(self->print_file_and_line);
1049 return BaseException_clear((PyBaseExceptionObject *)self);
1050}
1051
1052static void
1053SyntaxError_dealloc(PySyntaxErrorObject *self)
1054{
Georg Brandl38f62372006-09-06 06:50:05 +00001055 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001056 SyntaxError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001057 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001058}
1059
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001060static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001061SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1062{
1063 Py_VISIT(self->msg);
1064 Py_VISIT(self->filename);
1065 Py_VISIT(self->lineno);
1066 Py_VISIT(self->offset);
1067 Py_VISIT(self->text);
1068 Py_VISIT(self->print_file_and_line);
1069 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1070}
1071
1072/* This is called "my_basename" instead of just "basename" to avoid name
1073 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1074 defined, and Python does define that. */
1075static char *
1076my_basename(char *name)
1077{
1078 char *cp = name;
1079 char *result = name;
1080
1081 if (name == NULL)
1082 return "???";
1083 while (*cp != '\0') {
1084 if (*cp == SEP)
1085 result = cp + 1;
1086 ++cp;
1087 }
1088 return result;
1089}
1090
1091
1092static PyObject *
1093SyntaxError_str(PySyntaxErrorObject *self)
1094{
1095 PyObject *str;
1096 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001097 int have_filename = 0;
1098 int have_lineno = 0;
1099 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001100 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001101
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001102 if (self->msg)
1103 str = PyObject_Str(self->msg);
1104 else
1105 str = PyObject_Str(Py_None);
Georg Brandl43ab1002006-05-28 20:57:09 +00001106 if (!str) return NULL;
1107 /* Don't fiddle with non-string return (shouldn't happen anyway) */
1108 if (!PyString_Check(str)) return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001109
1110 /* XXX -- do all the additional formatting with filename and
1111 lineno here */
1112
Georg Brandl43ab1002006-05-28 20:57:09 +00001113 have_filename = (self->filename != NULL) &&
1114 PyString_Check(self->filename);
1115 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001116
Georg Brandl43ab1002006-05-28 20:57:09 +00001117 if (!have_filename && !have_lineno)
1118 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001119
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001120 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001121 if (have_filename)
1122 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001123
Georg Brandl43ab1002006-05-28 20:57:09 +00001124 buffer = PyMem_MALLOC(bufsize);
1125 if (buffer == NULL)
1126 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001127
Georg Brandl43ab1002006-05-28 20:57:09 +00001128 if (have_filename && have_lineno)
1129 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
1130 PyString_AS_STRING(str),
1131 my_basename(PyString_AS_STRING(self->filename)),
1132 PyInt_AsLong(self->lineno));
1133 else if (have_filename)
1134 PyOS_snprintf(buffer, bufsize, "%s (%s)",
1135 PyString_AS_STRING(str),
1136 my_basename(PyString_AS_STRING(self->filename)));
1137 else /* only have_lineno */
1138 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
1139 PyString_AS_STRING(str),
1140 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001141
Georg Brandl43ab1002006-05-28 20:57:09 +00001142 result = PyString_FromString(buffer);
1143 PyMem_FREE(buffer);
1144
1145 if (result == NULL)
1146 result = str;
1147 else
1148 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001149 return result;
1150}
1151
1152static PyMemberDef SyntaxError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001153 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1154 PyDoc_STR("exception msg")},
1155 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1156 PyDoc_STR("exception filename")},
1157 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1158 PyDoc_STR("exception lineno")},
1159 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1160 PyDoc_STR("exception offset")},
1161 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1162 PyDoc_STR("exception text")},
1163 {"print_file_and_line", T_OBJECT,
1164 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1165 PyDoc_STR("exception print_file_and_line")},
1166 {NULL} /* Sentinel */
1167};
1168
1169ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1170 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001171 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001172
1173
1174/*
1175 * IndentationError extends SyntaxError
1176 */
1177MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001178 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001179
1180
1181/*
1182 * TabError extends IndentationError
1183 */
1184MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001185 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001186
1187
1188/*
1189 * LookupError extends StandardError
1190 */
1191SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001192 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001193
1194
1195/*
1196 * IndexError extends LookupError
1197 */
1198SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001199 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001200
1201
1202/*
1203 * KeyError extends LookupError
1204 */
1205static PyObject *
1206KeyError_str(PyBaseExceptionObject *self)
1207{
1208 /* If args is a tuple of exactly one item, apply repr to args[0].
1209 This is done so that e.g. the exception raised by {}[''] prints
1210 KeyError: ''
1211 rather than the confusing
1212 KeyError
1213 alone. The downside is that if KeyError is raised with an explanatory
1214 string, that string will be displayed in quotes. Too bad.
1215 If args is anything else, use the default BaseException__str__().
1216 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001217 if (PyTuple_GET_SIZE(self->args) == 1) {
1218 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001219 }
1220 return BaseException_str(self);
1221}
1222
1223ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001224 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001225
1226
1227/*
1228 * ValueError extends StandardError
1229 */
1230SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001231 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001232
1233/*
1234 * UnicodeError extends ValueError
1235 */
1236
1237SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001238 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001239
1240#ifdef Py_USING_UNICODE
Richard Jones7b9558d2006-05-27 12:29:24 +00001241static PyObject *
1242get_string(PyObject *attr, const char *name)
1243{
1244 if (!attr) {
1245 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1246 return NULL;
1247 }
1248
1249 if (!PyString_Check(attr)) {
1250 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1251 return NULL;
1252 }
1253 Py_INCREF(attr);
1254 return attr;
1255}
1256
1257
1258static int
1259set_string(PyObject **attr, const char *value)
1260{
1261 PyObject *obj = PyString_FromString(value);
1262 if (!obj)
1263 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001264 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001265 *attr = obj;
1266 return 0;
1267}
1268
1269
1270static PyObject *
1271get_unicode(PyObject *attr, const char *name)
1272{
1273 if (!attr) {
1274 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1275 return NULL;
1276 }
1277
1278 if (!PyUnicode_Check(attr)) {
1279 PyErr_Format(PyExc_TypeError,
1280 "%.200s attribute must be unicode", name);
1281 return NULL;
1282 }
1283 Py_INCREF(attr);
1284 return attr;
1285}
1286
1287PyObject *
1288PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1289{
1290 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1291}
1292
1293PyObject *
1294PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1295{
1296 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1297}
1298
1299PyObject *
1300PyUnicodeEncodeError_GetObject(PyObject *exc)
1301{
1302 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1303}
1304
1305PyObject *
1306PyUnicodeDecodeError_GetObject(PyObject *exc)
1307{
1308 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1309}
1310
1311PyObject *
1312PyUnicodeTranslateError_GetObject(PyObject *exc)
1313{
1314 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1315}
1316
1317int
1318PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1319{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001320 Py_ssize_t size;
1321 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1322 "object");
1323 if (!obj)
1324 return -1;
1325 *start = ((PyUnicodeErrorObject *)exc)->start;
1326 size = PyUnicode_GET_SIZE(obj);
1327 if (*start<0)
1328 *start = 0; /*XXX check for values <0*/
1329 if (*start>=size)
1330 *start = size-1;
1331 Py_DECREF(obj);
1332 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001333}
1334
1335
1336int
1337PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1338{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001339 Py_ssize_t size;
1340 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1341 "object");
1342 if (!obj)
1343 return -1;
1344 size = PyString_GET_SIZE(obj);
1345 *start = ((PyUnicodeErrorObject *)exc)->start;
1346 if (*start<0)
1347 *start = 0;
1348 if (*start>=size)
1349 *start = size-1;
1350 Py_DECREF(obj);
1351 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001352}
1353
1354
1355int
1356PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1357{
1358 return PyUnicodeEncodeError_GetStart(exc, start);
1359}
1360
1361
1362int
1363PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1364{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001365 ((PyUnicodeErrorObject *)exc)->start = start;
1366 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001367}
1368
1369
1370int
1371PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1372{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001373 ((PyUnicodeErrorObject *)exc)->start = start;
1374 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001375}
1376
1377
1378int
1379PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1380{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001381 ((PyUnicodeErrorObject *)exc)->start = start;
1382 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001383}
1384
1385
1386int
1387PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1388{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001389 Py_ssize_t size;
1390 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1391 "object");
1392 if (!obj)
1393 return -1;
1394 *end = ((PyUnicodeErrorObject *)exc)->end;
1395 size = PyUnicode_GET_SIZE(obj);
1396 if (*end<1)
1397 *end = 1;
1398 if (*end>size)
1399 *end = size;
1400 Py_DECREF(obj);
1401 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001402}
1403
1404
1405int
1406PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1407{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001408 Py_ssize_t size;
1409 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1410 "object");
1411 if (!obj)
1412 return -1;
1413 *end = ((PyUnicodeErrorObject *)exc)->end;
1414 size = PyString_GET_SIZE(obj);
1415 if (*end<1)
1416 *end = 1;
1417 if (*end>size)
1418 *end = size;
1419 Py_DECREF(obj);
1420 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001421}
1422
1423
1424int
1425PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1426{
1427 return PyUnicodeEncodeError_GetEnd(exc, start);
1428}
1429
1430
1431int
1432PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1433{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001434 ((PyUnicodeErrorObject *)exc)->end = end;
1435 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001436}
1437
1438
1439int
1440PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1441{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001442 ((PyUnicodeErrorObject *)exc)->end = end;
1443 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001444}
1445
1446
1447int
1448PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1449{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001450 ((PyUnicodeErrorObject *)exc)->end = end;
1451 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001452}
1453
1454PyObject *
1455PyUnicodeEncodeError_GetReason(PyObject *exc)
1456{
1457 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1458}
1459
1460
1461PyObject *
1462PyUnicodeDecodeError_GetReason(PyObject *exc)
1463{
1464 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1465}
1466
1467
1468PyObject *
1469PyUnicodeTranslateError_GetReason(PyObject *exc)
1470{
1471 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1472}
1473
1474
1475int
1476PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1477{
1478 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1479}
1480
1481
1482int
1483PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1484{
1485 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1486}
1487
1488
1489int
1490PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1491{
1492 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1493}
1494
1495
Richard Jones7b9558d2006-05-27 12:29:24 +00001496static int
1497UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1498 PyTypeObject *objecttype)
1499{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001500 Py_CLEAR(self->encoding);
1501 Py_CLEAR(self->object);
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001502 Py_CLEAR(self->reason);
1503
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001504 if (!PyArg_ParseTuple(args, "O!O!nnO!",
Richard Jones7b9558d2006-05-27 12:29:24 +00001505 &PyString_Type, &self->encoding,
1506 objecttype, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001507 &self->start,
1508 &self->end,
Richard Jones7b9558d2006-05-27 12:29:24 +00001509 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001510 self->encoding = self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001511 return -1;
1512 }
1513
1514 Py_INCREF(self->encoding);
1515 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001516 Py_INCREF(self->reason);
1517
1518 return 0;
1519}
1520
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001521static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001522UnicodeError_clear(PyUnicodeErrorObject *self)
1523{
1524 Py_CLEAR(self->encoding);
1525 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001526 Py_CLEAR(self->reason);
1527 return BaseException_clear((PyBaseExceptionObject *)self);
1528}
1529
1530static void
1531UnicodeError_dealloc(PyUnicodeErrorObject *self)
1532{
Georg Brandl38f62372006-09-06 06:50:05 +00001533 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001534 UnicodeError_clear(self);
Christian Heimese93237d2007-12-19 02:37:44 +00001535 Py_TYPE(self)->tp_free((PyObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001536}
1537
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001538static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001539UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1540{
1541 Py_VISIT(self->encoding);
1542 Py_VISIT(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001543 Py_VISIT(self->reason);
1544 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1545}
1546
1547static PyMemberDef UnicodeError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001548 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1549 PyDoc_STR("exception encoding")},
1550 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1551 PyDoc_STR("exception object")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001552 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001553 PyDoc_STR("exception start")},
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001554 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Richard Jones7b9558d2006-05-27 12:29:24 +00001555 PyDoc_STR("exception end")},
1556 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1557 PyDoc_STR("exception reason")},
1558 {NULL} /* Sentinel */
1559};
1560
1561
1562/*
1563 * UnicodeEncodeError extends UnicodeError
1564 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001565
1566static int
1567UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1568{
1569 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1570 return -1;
1571 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1572 kwds, &PyUnicode_Type);
1573}
1574
1575static PyObject *
1576UnicodeEncodeError_str(PyObject *self)
1577{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001578 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001579
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001580 if (uself->end==uself->start+1) {
1581 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001582 char badchar_str[20];
1583 if (badchar <= 0xff)
1584 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1585 else if (badchar <= 0xffff)
1586 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1587 else
1588 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1589 return PyString_FromFormat(
1590 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001591 PyString_AS_STRING(uself->encoding),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001592 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001593 uself->start,
1594 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001595 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001596 }
1597 return PyString_FromFormat(
1598 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001599 PyString_AS_STRING(uself->encoding),
1600 uself->start,
1601 uself->end-1,
1602 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001603 );
1604}
1605
1606static PyTypeObject _PyExc_UnicodeEncodeError = {
1607 PyObject_HEAD_INIT(NULL)
1608 0,
Georg Brandl38f62372006-09-06 06:50:05 +00001609 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001610 sizeof(PyUnicodeErrorObject), 0,
1611 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1612 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1613 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001614 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1615 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001616 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001617 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001618};
1619PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1620
1621PyObject *
1622PyUnicodeEncodeError_Create(
1623 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1624 Py_ssize_t start, Py_ssize_t end, const char *reason)
1625{
1626 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001627 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001628}
1629
1630
1631/*
1632 * UnicodeDecodeError extends UnicodeError
1633 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001634
1635static int
1636UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1637{
1638 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1639 return -1;
1640 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1641 kwds, &PyString_Type);
1642}
1643
1644static PyObject *
1645UnicodeDecodeError_str(PyObject *self)
1646{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001647 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001648
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001649 if (uself->end==uself->start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001650 /* FromFormat does not support %02x, so format that separately */
1651 char byte[4];
1652 PyOS_snprintf(byte, sizeof(byte), "%02x",
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001653 ((int)PyString_AS_STRING(uself->object)[uself->start])&0xff);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001654 return PyString_FromFormat(
1655 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001656 PyString_AS_STRING(uself->encoding),
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001657 byte,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001658 uself->start,
1659 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001660 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001661 }
1662 return PyString_FromFormat(
1663 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001664 PyString_AS_STRING(uself->encoding),
1665 uself->start,
1666 uself->end-1,
1667 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001668 );
1669}
1670
1671static PyTypeObject _PyExc_UnicodeDecodeError = {
1672 PyObject_HEAD_INIT(NULL)
1673 0,
1674 EXC_MODULE_NAME "UnicodeDecodeError",
1675 sizeof(PyUnicodeErrorObject), 0,
1676 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1677 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1678 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001679 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1680 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001681 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001682 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001683};
1684PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1685
1686PyObject *
1687PyUnicodeDecodeError_Create(
1688 const char *encoding, const char *object, Py_ssize_t length,
1689 Py_ssize_t start, Py_ssize_t end, const char *reason)
1690{
1691 assert(length < INT_MAX);
1692 assert(start < INT_MAX);
1693 assert(end < INT_MAX);
1694 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001695 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001696}
1697
1698
1699/*
1700 * UnicodeTranslateError extends UnicodeError
1701 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001702
1703static int
1704UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1705 PyObject *kwds)
1706{
1707 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1708 return -1;
1709
1710 Py_CLEAR(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001711 Py_CLEAR(self->reason);
1712
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001713 if (!PyArg_ParseTuple(args, "O!nnO!",
Richard Jones7b9558d2006-05-27 12:29:24 +00001714 &PyUnicode_Type, &self->object,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001715 &self->start,
1716 &self->end,
Richard Jones7b9558d2006-05-27 12:29:24 +00001717 &PyString_Type, &self->reason)) {
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001718 self->object = self->reason = NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001719 return -1;
1720 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001721
Richard Jones7b9558d2006-05-27 12:29:24 +00001722 Py_INCREF(self->object);
Richard Jones7b9558d2006-05-27 12:29:24 +00001723 Py_INCREF(self->reason);
1724
1725 return 0;
1726}
1727
1728
1729static PyObject *
1730UnicodeTranslateError_str(PyObject *self)
1731{
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001732 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Richard Jones7b9558d2006-05-27 12:29:24 +00001733
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001734 if (uself->end==uself->start+1) {
1735 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001736 char badchar_str[20];
1737 if (badchar <= 0xff)
1738 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1739 else if (badchar <= 0xffff)
1740 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1741 else
1742 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1743 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001744 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001745 badchar_str,
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001746 uself->start,
1747 PyString_AS_STRING(uself->reason)
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001748 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001749 }
1750 return PyString_FromFormat(
1751 "can't translate characters in position %zd-%zd: %.400s",
Walter Dörwald84a3efe2007-06-13 16:57:12 +00001752 uself->start,
1753 uself->end-1,
1754 PyString_AS_STRING(uself->reason)
Richard Jones7b9558d2006-05-27 12:29:24 +00001755 );
1756}
1757
1758static PyTypeObject _PyExc_UnicodeTranslateError = {
1759 PyObject_HEAD_INIT(NULL)
1760 0,
1761 EXC_MODULE_NAME "UnicodeTranslateError",
1762 sizeof(PyUnicodeErrorObject), 0,
1763 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1764 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1765 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001766 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001767 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1768 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001769 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001770};
1771PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1772
1773PyObject *
1774PyUnicodeTranslateError_Create(
1775 const Py_UNICODE *object, Py_ssize_t length,
1776 Py_ssize_t start, Py_ssize_t end, const char *reason)
1777{
1778 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001779 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001780}
1781#endif
1782
1783
1784/*
1785 * AssertionError extends StandardError
1786 */
1787SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001788 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001789
1790
1791/*
1792 * ArithmeticError extends StandardError
1793 */
1794SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001795 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001796
1797
1798/*
1799 * FloatingPointError extends ArithmeticError
1800 */
1801SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001802 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001803
1804
1805/*
1806 * OverflowError extends ArithmeticError
1807 */
1808SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001809 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001810
1811
1812/*
1813 * ZeroDivisionError extends ArithmeticError
1814 */
1815SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001816 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001817
1818
1819/*
1820 * SystemError extends StandardError
1821 */
1822SimpleExtendsException(PyExc_StandardError, SystemError,
1823 "Internal error in the Python interpreter.\n"
1824 "\n"
1825 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001826 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001827
1828
1829/*
1830 * ReferenceError extends StandardError
1831 */
1832SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001833 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001834
1835
1836/*
1837 * MemoryError extends StandardError
1838 */
Richard Jones2d555b32006-05-27 16:15:11 +00001839SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001840
1841
1842/* Warning category docstrings */
1843
1844/*
1845 * Warning extends Exception
1846 */
1847SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001848 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001849
1850
1851/*
1852 * UserWarning extends Warning
1853 */
1854SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001855 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001856
1857
1858/*
1859 * DeprecationWarning extends Warning
1860 */
1861SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001862 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001863
1864
1865/*
1866 * PendingDeprecationWarning extends Warning
1867 */
1868SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1869 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001870 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001871
1872
1873/*
1874 * SyntaxWarning extends Warning
1875 */
1876SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001877 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001878
1879
1880/*
1881 * RuntimeWarning extends Warning
1882 */
1883SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001884 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001885
1886
1887/*
1888 * FutureWarning extends Warning
1889 */
1890SimpleExtendsException(PyExc_Warning, FutureWarning,
1891 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001892 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001893
1894
1895/*
1896 * ImportWarning extends Warning
1897 */
1898SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001899 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00001900
1901
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001902/*
1903 * UnicodeWarning extends Warning
1904 */
1905SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1906 "Base class for warnings about Unicode related problems, mostly\n"
1907 "related to conversion problems.");
1908
1909
Richard Jones7b9558d2006-05-27 12:29:24 +00001910/* Pre-computed MemoryError instance. Best to create this as early as
1911 * possible and not wait until a MemoryError is actually raised!
1912 */
1913PyObject *PyExc_MemoryErrorInst=NULL;
1914
Brett Cannon1e534b52007-09-07 04:18:30 +00001915/* Pre-computed RuntimeError instance for when recursion depth is reached.
1916 Meant to be used when normalizing the exception for exceeding the recursion
1917 depth will cause its own infinite recursion.
1918*/
1919PyObject *PyExc_RecursionErrorInst = NULL;
1920
Richard Jones7b9558d2006-05-27 12:29:24 +00001921/* module global functions */
1922static PyMethodDef functions[] = {
1923 /* Sentinel */
1924 {NULL, NULL}
1925};
1926
1927#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1928 Py_FatalError("exceptions bootstrapping error.");
1929
1930#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1931 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1932 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1933 Py_FatalError("Module dictionary insertion problem.");
1934
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00001935#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00001936/* crt variable checking in VisualStudio .NET 2005 */
1937#include <crtdbg.h>
1938
1939static int prevCrtReportMode;
1940static _invalid_parameter_handler prevCrtHandler;
1941
1942/* Invalid parameter handler. Sets a ValueError exception */
1943static void
1944InvalidParameterHandler(
1945 const wchar_t * expression,
1946 const wchar_t * function,
1947 const wchar_t * file,
1948 unsigned int line,
1949 uintptr_t pReserved)
1950{
1951 /* Do nothing, allow execution to continue. Usually this
1952 * means that the CRT will set errno to EINVAL
1953 */
1954}
1955#endif
1956
1957
Richard Jones7b9558d2006-05-27 12:29:24 +00001958PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001959_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00001960{
1961 PyObject *m, *bltinmod, *bdict;
1962
1963 PRE_INIT(BaseException)
1964 PRE_INIT(Exception)
1965 PRE_INIT(StandardError)
1966 PRE_INIT(TypeError)
1967 PRE_INIT(StopIteration)
1968 PRE_INIT(GeneratorExit)
1969 PRE_INIT(SystemExit)
1970 PRE_INIT(KeyboardInterrupt)
1971 PRE_INIT(ImportError)
1972 PRE_INIT(EnvironmentError)
1973 PRE_INIT(IOError)
1974 PRE_INIT(OSError)
1975#ifdef MS_WINDOWS
1976 PRE_INIT(WindowsError)
1977#endif
1978#ifdef __VMS
1979 PRE_INIT(VMSError)
1980#endif
1981 PRE_INIT(EOFError)
1982 PRE_INIT(RuntimeError)
1983 PRE_INIT(NotImplementedError)
1984 PRE_INIT(NameError)
1985 PRE_INIT(UnboundLocalError)
1986 PRE_INIT(AttributeError)
1987 PRE_INIT(SyntaxError)
1988 PRE_INIT(IndentationError)
1989 PRE_INIT(TabError)
1990 PRE_INIT(LookupError)
1991 PRE_INIT(IndexError)
1992 PRE_INIT(KeyError)
1993 PRE_INIT(ValueError)
1994 PRE_INIT(UnicodeError)
1995#ifdef Py_USING_UNICODE
1996 PRE_INIT(UnicodeEncodeError)
1997 PRE_INIT(UnicodeDecodeError)
1998 PRE_INIT(UnicodeTranslateError)
1999#endif
2000 PRE_INIT(AssertionError)
2001 PRE_INIT(ArithmeticError)
2002 PRE_INIT(FloatingPointError)
2003 PRE_INIT(OverflowError)
2004 PRE_INIT(ZeroDivisionError)
2005 PRE_INIT(SystemError)
2006 PRE_INIT(ReferenceError)
2007 PRE_INIT(MemoryError)
2008 PRE_INIT(Warning)
2009 PRE_INIT(UserWarning)
2010 PRE_INIT(DeprecationWarning)
2011 PRE_INIT(PendingDeprecationWarning)
2012 PRE_INIT(SyntaxWarning)
2013 PRE_INIT(RuntimeWarning)
2014 PRE_INIT(FutureWarning)
2015 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002016 PRE_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002017
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002018 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2019 (PyObject *)NULL, PYTHON_API_VERSION);
Richard Jones7b9558d2006-05-27 12:29:24 +00002020 if (m == NULL) return;
2021
2022 bltinmod = PyImport_ImportModule("__builtin__");
2023 if (bltinmod == NULL)
2024 Py_FatalError("exceptions bootstrapping error.");
2025 bdict = PyModule_GetDict(bltinmod);
2026 if (bdict == NULL)
2027 Py_FatalError("exceptions bootstrapping error.");
2028
2029 POST_INIT(BaseException)
2030 POST_INIT(Exception)
2031 POST_INIT(StandardError)
2032 POST_INIT(TypeError)
2033 POST_INIT(StopIteration)
2034 POST_INIT(GeneratorExit)
2035 POST_INIT(SystemExit)
2036 POST_INIT(KeyboardInterrupt)
2037 POST_INIT(ImportError)
2038 POST_INIT(EnvironmentError)
2039 POST_INIT(IOError)
2040 POST_INIT(OSError)
2041#ifdef MS_WINDOWS
2042 POST_INIT(WindowsError)
2043#endif
2044#ifdef __VMS
2045 POST_INIT(VMSError)
2046#endif
2047 POST_INIT(EOFError)
2048 POST_INIT(RuntimeError)
2049 POST_INIT(NotImplementedError)
2050 POST_INIT(NameError)
2051 POST_INIT(UnboundLocalError)
2052 POST_INIT(AttributeError)
2053 POST_INIT(SyntaxError)
2054 POST_INIT(IndentationError)
2055 POST_INIT(TabError)
2056 POST_INIT(LookupError)
2057 POST_INIT(IndexError)
2058 POST_INIT(KeyError)
2059 POST_INIT(ValueError)
2060 POST_INIT(UnicodeError)
2061#ifdef Py_USING_UNICODE
2062 POST_INIT(UnicodeEncodeError)
2063 POST_INIT(UnicodeDecodeError)
2064 POST_INIT(UnicodeTranslateError)
2065#endif
2066 POST_INIT(AssertionError)
2067 POST_INIT(ArithmeticError)
2068 POST_INIT(FloatingPointError)
2069 POST_INIT(OverflowError)
2070 POST_INIT(ZeroDivisionError)
2071 POST_INIT(SystemError)
2072 POST_INIT(ReferenceError)
2073 POST_INIT(MemoryError)
2074 POST_INIT(Warning)
2075 POST_INIT(UserWarning)
2076 POST_INIT(DeprecationWarning)
2077 POST_INIT(PendingDeprecationWarning)
2078 POST_INIT(SyntaxWarning)
2079 POST_INIT(RuntimeWarning)
2080 POST_INIT(FutureWarning)
2081 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002082 POST_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002083
2084 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2085 if (!PyExc_MemoryErrorInst)
2086 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
2087
Brett Cannon1e534b52007-09-07 04:18:30 +00002088 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2089 if (!PyExc_RecursionErrorInst)
2090 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2091 "recursion errors");
2092 else {
2093 PyBaseExceptionObject *err_inst =
2094 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2095 PyObject *args_tuple;
2096 PyObject *exc_message;
2097 exc_message = PyString_FromString("maximum recursion depth exceeded");
2098 if (!exc_message)
2099 Py_FatalError("cannot allocate argument for RuntimeError "
2100 "pre-allocation");
2101 args_tuple = PyTuple_Pack(1, exc_message);
2102 if (!args_tuple)
2103 Py_FatalError("cannot allocate tuple for RuntimeError "
2104 "pre-allocation");
2105 Py_DECREF(exc_message);
2106 if (BaseException_init(err_inst, args_tuple, NULL))
2107 Py_FatalError("init of pre-allocated RuntimeError failed");
2108 Py_DECREF(args_tuple);
2109 }
2110
Richard Jones7b9558d2006-05-27 12:29:24 +00002111 Py_DECREF(bltinmod);
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002112
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002113#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002114 /* Set CRT argument error handler */
2115 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
2116 /* turn off assertions in debug mode */
2117 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
2118#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002119}
2120
2121void
2122_PyExc_Fini(void)
2123{
2124 Py_XDECREF(PyExc_MemoryErrorInst);
2125 PyExc_MemoryErrorInst = NULL;
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002126#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002127 /* reset CRT error handling */
2128 _set_invalid_parameter_handler(prevCrtHandler);
2129 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
2130#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002131}