blob: 9faba6ae694e08c6086882ce1641bea093b0bcbd [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
41 self->args = PyTuple_New(0);
42 if (!self->args) {
43 Py_DECREF(self);
44 return NULL;
45 }
46
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 }
52
53 return (PyObject *)self;
54}
55
56static int
57BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
58{
Georg Brandlb0432bc2006-05-30 08:17:00 +000059 if (!_PyArg_NoKeywords(self->ob_type->tp_name, kwds))
60 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);
88 self->ob_type->tp_free((PyObject *)self);
89}
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
132 name = (char *)self->ob_type->tp_name;
133 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)
151 return PyTuple_Pack(3, self->ob_type, self->args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000152 else
Georg Brandlddba4732006-05-30 07:04:55 +0000153 return PyTuple_Pack(2, self->ob_type, 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/*
440 * GeneratorExit extends Exception
441 */
442SimpleExtendsException(PyExc_Exception, 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);
481 self->ob_type->tp_free((PyObject *)self);
482}
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);
586 self->ob_type->tp_free((PyObject *)self);
587}
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)
728 res = PyTuple_Pack(3, self->ob_type, args, self->dict);
729 else
730 res = PyTuple_Pack(2, self->ob_type, 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);
783 self->ob_type->tp_free((PyObject *)self);
784}
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);
1057 self->ob_type->tp_free((PyObject *)self);
1058}
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
1241static int
1242get_int(PyObject *attr, Py_ssize_t *value, const char *name)
1243{
1244 if (!attr) {
1245 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1246 return -1;
1247 }
1248
1249 if (PyInt_Check(attr)) {
1250 *value = PyInt_AS_LONG(attr);
1251 } else if (PyLong_Check(attr)) {
1252 *value = _PyLong_AsSsize_t(attr);
1253 if (*value == -1 && PyErr_Occurred())
1254 return -1;
1255 } else {
1256 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
1257 return -1;
1258 }
1259 return 0;
1260}
1261
1262static int
1263set_ssize_t(PyObject **attr, Py_ssize_t value)
1264{
1265 PyObject *obj = PyInt_FromSsize_t(value);
1266 if (!obj)
1267 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001268 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001269 *attr = obj;
1270 return 0;
1271}
1272
1273static PyObject *
1274get_string(PyObject *attr, const char *name)
1275{
1276 if (!attr) {
1277 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1278 return NULL;
1279 }
1280
1281 if (!PyString_Check(attr)) {
1282 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1283 return NULL;
1284 }
1285 Py_INCREF(attr);
1286 return attr;
1287}
1288
1289
1290static int
1291set_string(PyObject **attr, const char *value)
1292{
1293 PyObject *obj = PyString_FromString(value);
1294 if (!obj)
1295 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001296 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001297 *attr = obj;
1298 return 0;
1299}
1300
1301
1302static PyObject *
1303get_unicode(PyObject *attr, const char *name)
1304{
1305 if (!attr) {
1306 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1307 return NULL;
1308 }
1309
1310 if (!PyUnicode_Check(attr)) {
1311 PyErr_Format(PyExc_TypeError,
1312 "%.200s attribute must be unicode", name);
1313 return NULL;
1314 }
1315 Py_INCREF(attr);
1316 return attr;
1317}
1318
1319PyObject *
1320PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1321{
1322 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1323}
1324
1325PyObject *
1326PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1327{
1328 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1329}
1330
1331PyObject *
1332PyUnicodeEncodeError_GetObject(PyObject *exc)
1333{
1334 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1335}
1336
1337PyObject *
1338PyUnicodeDecodeError_GetObject(PyObject *exc)
1339{
1340 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1341}
1342
1343PyObject *
1344PyUnicodeTranslateError_GetObject(PyObject *exc)
1345{
1346 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1347}
1348
1349int
1350PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1351{
1352 if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
1353 Py_ssize_t size;
1354 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1355 "object");
1356 if (!obj) return -1;
1357 size = PyUnicode_GET_SIZE(obj);
1358 if (*start<0)
1359 *start = 0; /*XXX check for values <0*/
1360 if (*start>=size)
1361 *start = size-1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001362 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001363 return 0;
1364 }
1365 return -1;
1366}
1367
1368
1369int
1370PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1371{
1372 if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
1373 Py_ssize_t size;
1374 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1375 "object");
1376 if (!obj) return -1;
1377 size = PyString_GET_SIZE(obj);
1378 if (*start<0)
1379 *start = 0;
1380 if (*start>=size)
1381 *start = size-1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001382 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001383 return 0;
1384 }
1385 return -1;
1386}
1387
1388
1389int
1390PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1391{
1392 return PyUnicodeEncodeError_GetStart(exc, start);
1393}
1394
1395
1396int
1397PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1398{
1399 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1400}
1401
1402
1403int
1404PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1405{
1406 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1407}
1408
1409
1410int
1411PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1412{
1413 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1414}
1415
1416
1417int
1418PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1419{
1420 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1421 Py_ssize_t size;
1422 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1423 "object");
1424 if (!obj) return -1;
1425 size = PyUnicode_GET_SIZE(obj);
1426 if (*end<1)
1427 *end = 1;
1428 if (*end>size)
1429 *end = size;
Georg Brandl43ab1002006-05-28 20:57:09 +00001430 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001431 return 0;
1432 }
1433 return -1;
1434}
1435
1436
1437int
1438PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1439{
1440 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1441 Py_ssize_t size;
1442 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1443 "object");
1444 if (!obj) return -1;
1445 size = PyString_GET_SIZE(obj);
1446 if (*end<1)
1447 *end = 1;
1448 if (*end>size)
1449 *end = size;
Georg Brandl43ab1002006-05-28 20:57:09 +00001450 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001451 return 0;
1452 }
1453 return -1;
1454}
1455
1456
1457int
1458PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1459{
1460 return PyUnicodeEncodeError_GetEnd(exc, start);
1461}
1462
1463
1464int
1465PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1466{
1467 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1468}
1469
1470
1471int
1472PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1473{
1474 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1475}
1476
1477
1478int
1479PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1480{
1481 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1482}
1483
1484PyObject *
1485PyUnicodeEncodeError_GetReason(PyObject *exc)
1486{
1487 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1488}
1489
1490
1491PyObject *
1492PyUnicodeDecodeError_GetReason(PyObject *exc)
1493{
1494 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1495}
1496
1497
1498PyObject *
1499PyUnicodeTranslateError_GetReason(PyObject *exc)
1500{
1501 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1502}
1503
1504
1505int
1506PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1507{
1508 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1509}
1510
1511
1512int
1513PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1514{
1515 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1516}
1517
1518
1519int
1520PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1521{
1522 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1523}
1524
1525
Richard Jones7b9558d2006-05-27 12:29:24 +00001526static int
1527UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1528 PyTypeObject *objecttype)
1529{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001530 Py_CLEAR(self->encoding);
1531 Py_CLEAR(self->object);
1532 Py_CLEAR(self->start);
1533 Py_CLEAR(self->end);
1534 Py_CLEAR(self->reason);
1535
Richard Jones7b9558d2006-05-27 12:29:24 +00001536 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1537 &PyString_Type, &self->encoding,
1538 objecttype, &self->object,
1539 &PyInt_Type, &self->start,
1540 &PyInt_Type, &self->end,
1541 &PyString_Type, &self->reason)) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001542 self->encoding = self->object = self->start = self->end =
Richard Jones7b9558d2006-05-27 12:29:24 +00001543 self->reason = NULL;
1544 return -1;
1545 }
1546
1547 Py_INCREF(self->encoding);
1548 Py_INCREF(self->object);
1549 Py_INCREF(self->start);
1550 Py_INCREF(self->end);
1551 Py_INCREF(self->reason);
1552
1553 return 0;
1554}
1555
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001556static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001557UnicodeError_clear(PyUnicodeErrorObject *self)
1558{
1559 Py_CLEAR(self->encoding);
1560 Py_CLEAR(self->object);
1561 Py_CLEAR(self->start);
1562 Py_CLEAR(self->end);
1563 Py_CLEAR(self->reason);
1564 return BaseException_clear((PyBaseExceptionObject *)self);
1565}
1566
1567static void
1568UnicodeError_dealloc(PyUnicodeErrorObject *self)
1569{
Georg Brandl38f62372006-09-06 06:50:05 +00001570 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001571 UnicodeError_clear(self);
1572 self->ob_type->tp_free((PyObject *)self);
1573}
1574
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001575static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001576UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1577{
1578 Py_VISIT(self->encoding);
1579 Py_VISIT(self->object);
1580 Py_VISIT(self->start);
1581 Py_VISIT(self->end);
1582 Py_VISIT(self->reason);
1583 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1584}
1585
1586static PyMemberDef UnicodeError_members[] = {
Richard Jones7b9558d2006-05-27 12:29:24 +00001587 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1588 PyDoc_STR("exception encoding")},
1589 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1590 PyDoc_STR("exception object")},
1591 {"start", T_OBJECT, offsetof(PyUnicodeErrorObject, start), 0,
1592 PyDoc_STR("exception start")},
1593 {"end", T_OBJECT, offsetof(PyUnicodeErrorObject, end), 0,
1594 PyDoc_STR("exception end")},
1595 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1596 PyDoc_STR("exception reason")},
1597 {NULL} /* Sentinel */
1598};
1599
1600
1601/*
1602 * UnicodeEncodeError extends UnicodeError
1603 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001604
1605static int
1606UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1607{
1608 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1609 return -1;
1610 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1611 kwds, &PyUnicode_Type);
1612}
1613
1614static PyObject *
1615UnicodeEncodeError_str(PyObject *self)
1616{
1617 Py_ssize_t start;
1618 Py_ssize_t end;
1619
1620 if (PyUnicodeEncodeError_GetStart(self, &start))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001621 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001622
1623 if (PyUnicodeEncodeError_GetEnd(self, &end))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001624 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001625
1626 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001627 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1628 char badchar_str[20];
1629 if (badchar <= 0xff)
1630 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1631 else if (badchar <= 0xffff)
1632 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1633 else
1634 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1635 return PyString_FromFormat(
1636 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
1637 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1638 badchar_str,
1639 start,
1640 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1641 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001642 }
1643 return PyString_FromFormat(
1644 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
1645 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1646 start,
1647 (end-1),
1648 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1649 );
1650}
1651
1652static PyTypeObject _PyExc_UnicodeEncodeError = {
1653 PyObject_HEAD_INIT(NULL)
1654 0,
Georg Brandl38f62372006-09-06 06:50:05 +00001655 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001656 sizeof(PyUnicodeErrorObject), 0,
1657 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1658 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1659 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001660 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1661 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001662 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001663 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001664};
1665PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1666
1667PyObject *
1668PyUnicodeEncodeError_Create(
1669 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1670 Py_ssize_t start, Py_ssize_t end, const char *reason)
1671{
1672 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001673 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001674}
1675
1676
1677/*
1678 * UnicodeDecodeError extends UnicodeError
1679 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001680
1681static int
1682UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1683{
1684 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1685 return -1;
1686 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1687 kwds, &PyString_Type);
1688}
1689
1690static PyObject *
1691UnicodeDecodeError_str(PyObject *self)
1692{
Georg Brandl43ab1002006-05-28 20:57:09 +00001693 Py_ssize_t start = 0;
1694 Py_ssize_t end = 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001695
1696 if (PyUnicodeDecodeError_GetStart(self, &start))
1697 return NULL;
1698
1699 if (PyUnicodeDecodeError_GetEnd(self, &end))
1700 return NULL;
1701
1702 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001703 /* FromFormat does not support %02x, so format that separately */
1704 char byte[4];
1705 PyOS_snprintf(byte, sizeof(byte), "%02x",
1706 ((int)PyString_AS_STRING(((PyUnicodeErrorObject *)self)->object)[start])&0xff);
1707 return PyString_FromFormat(
1708 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
1709 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1710 byte,
1711 start,
1712 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1713 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001714 }
1715 return PyString_FromFormat(
1716 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
1717 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1718 start,
1719 (end-1),
1720 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1721 );
1722}
1723
1724static PyTypeObject _PyExc_UnicodeDecodeError = {
1725 PyObject_HEAD_INIT(NULL)
1726 0,
1727 EXC_MODULE_NAME "UnicodeDecodeError",
1728 sizeof(PyUnicodeErrorObject), 0,
1729 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1730 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1731 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001732 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1733 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001734 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001735 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001736};
1737PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1738
1739PyObject *
1740PyUnicodeDecodeError_Create(
1741 const char *encoding, const char *object, Py_ssize_t length,
1742 Py_ssize_t start, Py_ssize_t end, const char *reason)
1743{
1744 assert(length < INT_MAX);
1745 assert(start < INT_MAX);
1746 assert(end < INT_MAX);
1747 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001748 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001749}
1750
1751
1752/*
1753 * UnicodeTranslateError extends UnicodeError
1754 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001755
1756static int
1757UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1758 PyObject *kwds)
1759{
1760 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1761 return -1;
1762
1763 Py_CLEAR(self->object);
1764 Py_CLEAR(self->start);
1765 Py_CLEAR(self->end);
1766 Py_CLEAR(self->reason);
1767
1768 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1769 &PyUnicode_Type, &self->object,
1770 &PyInt_Type, &self->start,
1771 &PyInt_Type, &self->end,
1772 &PyString_Type, &self->reason)) {
1773 self->object = self->start = self->end = self->reason = NULL;
1774 return -1;
1775 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001776
Richard Jones7b9558d2006-05-27 12:29:24 +00001777 Py_INCREF(self->object);
1778 Py_INCREF(self->start);
1779 Py_INCREF(self->end);
1780 Py_INCREF(self->reason);
1781
1782 return 0;
1783}
1784
1785
1786static PyObject *
1787UnicodeTranslateError_str(PyObject *self)
1788{
1789 Py_ssize_t start;
1790 Py_ssize_t end;
1791
1792 if (PyUnicodeTranslateError_GetStart(self, &start))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001793 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001794
1795 if (PyUnicodeTranslateError_GetEnd(self, &end))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001796 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001797
1798 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001799 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1800 char badchar_str[20];
1801 if (badchar <= 0xff)
1802 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1803 else if (badchar <= 0xffff)
1804 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1805 else
1806 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1807 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001808 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001809 badchar_str,
1810 start,
1811 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1812 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001813 }
1814 return PyString_FromFormat(
1815 "can't translate characters in position %zd-%zd: %.400s",
1816 start,
1817 (end-1),
1818 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1819 );
1820}
1821
1822static PyTypeObject _PyExc_UnicodeTranslateError = {
1823 PyObject_HEAD_INIT(NULL)
1824 0,
1825 EXC_MODULE_NAME "UnicodeTranslateError",
1826 sizeof(PyUnicodeErrorObject), 0,
1827 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1828 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1829 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001830 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001831 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1832 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001833 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001834};
1835PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1836
1837PyObject *
1838PyUnicodeTranslateError_Create(
1839 const Py_UNICODE *object, Py_ssize_t length,
1840 Py_ssize_t start, Py_ssize_t end, const char *reason)
1841{
1842 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001843 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001844}
1845#endif
1846
1847
1848/*
1849 * AssertionError extends StandardError
1850 */
1851SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001852 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001853
1854
1855/*
1856 * ArithmeticError extends StandardError
1857 */
1858SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001859 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001860
1861
1862/*
1863 * FloatingPointError extends ArithmeticError
1864 */
1865SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001866 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001867
1868
1869/*
1870 * OverflowError extends ArithmeticError
1871 */
1872SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001873 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001874
1875
1876/*
1877 * ZeroDivisionError extends ArithmeticError
1878 */
1879SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001880 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001881
1882
1883/*
1884 * SystemError extends StandardError
1885 */
1886SimpleExtendsException(PyExc_StandardError, SystemError,
1887 "Internal error in the Python interpreter.\n"
1888 "\n"
1889 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001890 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001891
1892
1893/*
1894 * ReferenceError extends StandardError
1895 */
1896SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001897 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001898
1899
1900/*
1901 * MemoryError extends StandardError
1902 */
Richard Jones2d555b32006-05-27 16:15:11 +00001903SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001904
1905
1906/* Warning category docstrings */
1907
1908/*
1909 * Warning extends Exception
1910 */
1911SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001912 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001913
1914
1915/*
1916 * UserWarning extends Warning
1917 */
1918SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001919 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001920
1921
1922/*
1923 * DeprecationWarning extends Warning
1924 */
1925SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001926 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001927
1928
1929/*
1930 * PendingDeprecationWarning extends Warning
1931 */
1932SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1933 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001934 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001935
1936
1937/*
1938 * SyntaxWarning extends Warning
1939 */
1940SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001941 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001942
1943
1944/*
1945 * RuntimeWarning extends Warning
1946 */
1947SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001948 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001949
1950
1951/*
1952 * FutureWarning extends Warning
1953 */
1954SimpleExtendsException(PyExc_Warning, FutureWarning,
1955 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001956 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001957
1958
1959/*
1960 * ImportWarning extends Warning
1961 */
1962SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001963 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00001964
1965
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001966/*
1967 * UnicodeWarning extends Warning
1968 */
1969SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1970 "Base class for warnings about Unicode related problems, mostly\n"
1971 "related to conversion problems.");
1972
1973
Richard Jones7b9558d2006-05-27 12:29:24 +00001974/* Pre-computed MemoryError instance. Best to create this as early as
1975 * possible and not wait until a MemoryError is actually raised!
1976 */
1977PyObject *PyExc_MemoryErrorInst=NULL;
1978
1979/* module global functions */
1980static PyMethodDef functions[] = {
1981 /* Sentinel */
1982 {NULL, NULL}
1983};
1984
1985#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1986 Py_FatalError("exceptions bootstrapping error.");
1987
1988#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1989 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1990 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1991 Py_FatalError("Module dictionary insertion problem.");
1992
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00001993#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00001994/* crt variable checking in VisualStudio .NET 2005 */
1995#include <crtdbg.h>
1996
1997static int prevCrtReportMode;
1998static _invalid_parameter_handler prevCrtHandler;
1999
2000/* Invalid parameter handler. Sets a ValueError exception */
2001static void
2002InvalidParameterHandler(
2003 const wchar_t * expression,
2004 const wchar_t * function,
2005 const wchar_t * file,
2006 unsigned int line,
2007 uintptr_t pReserved)
2008{
2009 /* Do nothing, allow execution to continue. Usually this
2010 * means that the CRT will set errno to EINVAL
2011 */
2012}
2013#endif
2014
2015
Richard Jones7b9558d2006-05-27 12:29:24 +00002016PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00002017_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00002018{
2019 PyObject *m, *bltinmod, *bdict;
2020
2021 PRE_INIT(BaseException)
2022 PRE_INIT(Exception)
2023 PRE_INIT(StandardError)
2024 PRE_INIT(TypeError)
2025 PRE_INIT(StopIteration)
2026 PRE_INIT(GeneratorExit)
2027 PRE_INIT(SystemExit)
2028 PRE_INIT(KeyboardInterrupt)
2029 PRE_INIT(ImportError)
2030 PRE_INIT(EnvironmentError)
2031 PRE_INIT(IOError)
2032 PRE_INIT(OSError)
2033#ifdef MS_WINDOWS
2034 PRE_INIT(WindowsError)
2035#endif
2036#ifdef __VMS
2037 PRE_INIT(VMSError)
2038#endif
2039 PRE_INIT(EOFError)
2040 PRE_INIT(RuntimeError)
2041 PRE_INIT(NotImplementedError)
2042 PRE_INIT(NameError)
2043 PRE_INIT(UnboundLocalError)
2044 PRE_INIT(AttributeError)
2045 PRE_INIT(SyntaxError)
2046 PRE_INIT(IndentationError)
2047 PRE_INIT(TabError)
2048 PRE_INIT(LookupError)
2049 PRE_INIT(IndexError)
2050 PRE_INIT(KeyError)
2051 PRE_INIT(ValueError)
2052 PRE_INIT(UnicodeError)
2053#ifdef Py_USING_UNICODE
2054 PRE_INIT(UnicodeEncodeError)
2055 PRE_INIT(UnicodeDecodeError)
2056 PRE_INIT(UnicodeTranslateError)
2057#endif
2058 PRE_INIT(AssertionError)
2059 PRE_INIT(ArithmeticError)
2060 PRE_INIT(FloatingPointError)
2061 PRE_INIT(OverflowError)
2062 PRE_INIT(ZeroDivisionError)
2063 PRE_INIT(SystemError)
2064 PRE_INIT(ReferenceError)
2065 PRE_INIT(MemoryError)
2066 PRE_INIT(Warning)
2067 PRE_INIT(UserWarning)
2068 PRE_INIT(DeprecationWarning)
2069 PRE_INIT(PendingDeprecationWarning)
2070 PRE_INIT(SyntaxWarning)
2071 PRE_INIT(RuntimeWarning)
2072 PRE_INIT(FutureWarning)
2073 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002074 PRE_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002075
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002076 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2077 (PyObject *)NULL, PYTHON_API_VERSION);
Richard Jones7b9558d2006-05-27 12:29:24 +00002078 if (m == NULL) return;
2079
2080 bltinmod = PyImport_ImportModule("__builtin__");
2081 if (bltinmod == NULL)
2082 Py_FatalError("exceptions bootstrapping error.");
2083 bdict = PyModule_GetDict(bltinmod);
2084 if (bdict == NULL)
2085 Py_FatalError("exceptions bootstrapping error.");
2086
2087 POST_INIT(BaseException)
2088 POST_INIT(Exception)
2089 POST_INIT(StandardError)
2090 POST_INIT(TypeError)
2091 POST_INIT(StopIteration)
2092 POST_INIT(GeneratorExit)
2093 POST_INIT(SystemExit)
2094 POST_INIT(KeyboardInterrupt)
2095 POST_INIT(ImportError)
2096 POST_INIT(EnvironmentError)
2097 POST_INIT(IOError)
2098 POST_INIT(OSError)
2099#ifdef MS_WINDOWS
2100 POST_INIT(WindowsError)
2101#endif
2102#ifdef __VMS
2103 POST_INIT(VMSError)
2104#endif
2105 POST_INIT(EOFError)
2106 POST_INIT(RuntimeError)
2107 POST_INIT(NotImplementedError)
2108 POST_INIT(NameError)
2109 POST_INIT(UnboundLocalError)
2110 POST_INIT(AttributeError)
2111 POST_INIT(SyntaxError)
2112 POST_INIT(IndentationError)
2113 POST_INIT(TabError)
2114 POST_INIT(LookupError)
2115 POST_INIT(IndexError)
2116 POST_INIT(KeyError)
2117 POST_INIT(ValueError)
2118 POST_INIT(UnicodeError)
2119#ifdef Py_USING_UNICODE
2120 POST_INIT(UnicodeEncodeError)
2121 POST_INIT(UnicodeDecodeError)
2122 POST_INIT(UnicodeTranslateError)
2123#endif
2124 POST_INIT(AssertionError)
2125 POST_INIT(ArithmeticError)
2126 POST_INIT(FloatingPointError)
2127 POST_INIT(OverflowError)
2128 POST_INIT(ZeroDivisionError)
2129 POST_INIT(SystemError)
2130 POST_INIT(ReferenceError)
2131 POST_INIT(MemoryError)
2132 POST_INIT(Warning)
2133 POST_INIT(UserWarning)
2134 POST_INIT(DeprecationWarning)
2135 POST_INIT(PendingDeprecationWarning)
2136 POST_INIT(SyntaxWarning)
2137 POST_INIT(RuntimeWarning)
2138 POST_INIT(FutureWarning)
2139 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002140 POST_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002141
2142 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2143 if (!PyExc_MemoryErrorInst)
2144 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
2145
2146 Py_DECREF(bltinmod);
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002147
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002148#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002149 /* Set CRT argument error handler */
2150 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
2151 /* turn off assertions in debug mode */
2152 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
2153#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002154}
2155
2156void
2157_PyExc_Fini(void)
2158{
2159 Py_XDECREF(PyExc_MemoryErrorInst);
2160 PyExc_MemoryErrorInst = NULL;
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002161#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002162 /* reset CRT error handling */
2163 _set_invalid_parameter_handler(prevCrtHandler);
2164 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
2165#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002166}