blob: c6ea6a40c4fd82124dfc8b127428d291bfccdc04 [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);
36 /* the dict is created on the fly in PyObject_GenericSetAttr */
37 self->message = self->dict = NULL;
38
39 self->args = PyTuple_New(0);
40 if (!self->args) {
41 Py_DECREF(self);
42 return NULL;
43 }
44
Michael W. Hudson22a80e72006-05-28 15:51:40 +000045 self->message = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +000046 if (!self->message) {
47 Py_DECREF(self);
48 return NULL;
49 }
50
51 return (PyObject *)self;
52}
53
54static int
55BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
56{
Georg Brandlb0432bc2006-05-30 08:17:00 +000057 if (!_PyArg_NoKeywords(self->ob_type->tp_name, kwds))
58 return -1;
59
Richard Jones7b9558d2006-05-27 12:29:24 +000060 Py_DECREF(self->args);
61 self->args = args;
62 Py_INCREF(self->args);
63
64 if (PyTuple_GET_SIZE(self->args) == 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +000065 Py_CLEAR(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000066 self->message = PyTuple_GET_ITEM(self->args, 0);
Michael W. Hudson22a80e72006-05-28 15:51:40 +000067 Py_INCREF(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000068 }
69 return 0;
70}
71
Michael W. Hudson96495ee2006-05-28 17:40:29 +000072static int
Richard Jones7b9558d2006-05-27 12:29:24 +000073BaseException_clear(PyBaseExceptionObject *self)
74{
75 Py_CLEAR(self->dict);
76 Py_CLEAR(self->args);
77 Py_CLEAR(self->message);
78 return 0;
79}
80
81static void
82BaseException_dealloc(PyBaseExceptionObject *self)
83{
Georg Brandl38f62372006-09-06 06:50:05 +000084 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +000085 BaseException_clear(self);
86 self->ob_type->tp_free((PyObject *)self);
87}
88
Michael W. Hudson96495ee2006-05-28 17:40:29 +000089static int
Richard Jones7b9558d2006-05-27 12:29:24 +000090BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
91{
Michael W. Hudson22a80e72006-05-28 15:51:40 +000092 Py_VISIT(self->dict);
Richard Jones7b9558d2006-05-27 12:29:24 +000093 Py_VISIT(self->args);
94 Py_VISIT(self->message);
95 return 0;
96}
97
98static PyObject *
99BaseException_str(PyBaseExceptionObject *self)
100{
101 PyObject *out;
102
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000103 switch (PyTuple_GET_SIZE(self->args)) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000104 case 0:
105 out = PyString_FromString("");
106 break;
107 case 1:
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000108 out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +0000109 break;
Richard Jones7b9558d2006-05-27 12:29:24 +0000110 default:
111 out = PyObject_Str(self->args);
112 break;
113 }
114
115 return out;
116}
117
118static PyObject *
119BaseException_repr(PyBaseExceptionObject *self)
120{
Richard Jones7b9558d2006-05-27 12:29:24 +0000121 PyObject *repr_suffix;
122 PyObject *repr;
123 char *name;
124 char *dot;
125
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000126 repr_suffix = PyObject_Repr(self->args);
127 if (!repr_suffix)
Richard Jones7b9558d2006-05-27 12:29:24 +0000128 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000129
130 name = (char *)self->ob_type->tp_name;
131 dot = strrchr(name, '.');
132 if (dot != NULL) name = dot+1;
133
134 repr = PyString_FromString(name);
135 if (!repr) {
136 Py_DECREF(repr_suffix);
137 return NULL;
138 }
139
140 PyString_ConcatAndDel(&repr, repr_suffix);
141 return repr;
142}
143
144/* Pickling support */
145static PyObject *
146BaseException_reduce(PyBaseExceptionObject *self)
147{
Georg Brandlddba4732006-05-30 07:04:55 +0000148 if (self->args && self->dict)
149 return PyTuple_Pack(3, self->ob_type, self->args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000150 else
Georg Brandlddba4732006-05-30 07:04:55 +0000151 return PyTuple_Pack(2, self->ob_type, self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000152}
153
Georg Brandl85ac8502006-06-01 06:39:19 +0000154/*
155 * Needed for backward compatibility, since exceptions used to store
156 * all their attributes in the __dict__. Code is taken from cPickle's
157 * load_build function.
158 */
159static PyObject *
160BaseException_setstate(PyObject *self, PyObject *state)
161{
162 PyObject *d_key, *d_value;
163 Py_ssize_t i = 0;
164
165 if (state != Py_None) {
166 if (!PyDict_Check(state)) {
167 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
168 return NULL;
169 }
170 while (PyDict_Next(state, &i, &d_key, &d_value)) {
171 if (PyObject_SetAttr(self, d_key, d_value) < 0)
172 return NULL;
173 }
174 }
175 Py_RETURN_NONE;
176}
Richard Jones7b9558d2006-05-27 12:29:24 +0000177
Richard Jones7b9558d2006-05-27 12:29:24 +0000178
179static PyMethodDef BaseException_methods[] = {
180 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Georg Brandl85ac8502006-06-01 06:39:19 +0000181 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Richard Jones7b9558d2006-05-27 12:29:24 +0000182 {NULL, NULL, 0, NULL},
183};
184
185
186
187static PyObject *
188BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
189{
190 return PySequence_GetItem(self->args, index);
191}
192
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000193static PyObject *
194BaseException_getslice(PyBaseExceptionObject *self,
195 Py_ssize_t start, Py_ssize_t stop)
196{
197 return PySequence_GetSlice(self->args, start, stop);
198}
199
Richard Jones7b9558d2006-05-27 12:29:24 +0000200static PySequenceMethods BaseException_as_sequence = {
201 0, /* sq_length; */
202 0, /* sq_concat; */
203 0, /* sq_repeat; */
204 (ssizeargfunc)BaseException_getitem, /* sq_item; */
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000205 (ssizessizeargfunc)BaseException_getslice, /* sq_slice; */
Richard Jones7b9558d2006-05-27 12:29:24 +0000206 0, /* sq_ass_item; */
207 0, /* sq_ass_slice; */
208 0, /* sq_contains; */
209 0, /* sq_inplace_concat; */
210 0 /* sq_inplace_repeat; */
211};
212
213static PyMemberDef BaseException_members[] = {
214 {"message", T_OBJECT, offsetof(PyBaseExceptionObject, message), 0,
215 PyDoc_STR("exception message")},
216 {NULL} /* Sentinel */
217};
218
219
220static PyObject *
221BaseException_get_dict(PyBaseExceptionObject *self)
222{
223 if (self->dict == NULL) {
224 self->dict = PyDict_New();
225 if (!self->dict)
226 return NULL;
227 }
228 Py_INCREF(self->dict);
229 return self->dict;
230}
231
232static int
233BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
234{
235 if (val == NULL) {
236 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
237 return -1;
238 }
239 if (!PyDict_Check(val)) {
240 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
241 return -1;
242 }
243 Py_CLEAR(self->dict);
244 Py_INCREF(val);
245 self->dict = val;
246 return 0;
247}
248
249static PyObject *
250BaseException_get_args(PyBaseExceptionObject *self)
251{
252 if (self->args == NULL) {
253 Py_INCREF(Py_None);
254 return Py_None;
255 }
256 Py_INCREF(self->args);
257 return self->args;
258}
259
260static int
261BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
262{
263 PyObject *seq;
264 if (val == NULL) {
265 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
266 return -1;
267 }
268 seq = PySequence_Tuple(val);
269 if (!seq) return -1;
Georg Brandlc7c51142006-05-29 09:46:51 +0000270 Py_CLEAR(self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000271 self->args = seq;
272 return 0;
273}
274
275static PyGetSetDef BaseException_getset[] = {
276 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
277 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
278 {NULL},
279};
280
281
282static PyTypeObject _PyExc_BaseException = {
283 PyObject_HEAD_INIT(NULL)
284 0, /*ob_size*/
285 EXC_MODULE_NAME "BaseException", /*tp_name*/
286 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
287 0, /*tp_itemsize*/
288 (destructor)BaseException_dealloc, /*tp_dealloc*/
289 0, /*tp_print*/
290 0, /*tp_getattr*/
291 0, /*tp_setattr*/
292 0, /* tp_compare; */
293 (reprfunc)BaseException_repr, /*tp_repr*/
294 0, /*tp_as_number*/
295 &BaseException_as_sequence, /*tp_as_sequence*/
296 0, /*tp_as_mapping*/
297 0, /*tp_hash */
298 0, /*tp_call*/
299 (reprfunc)BaseException_str, /*tp_str*/
300 PyObject_GenericGetAttr, /*tp_getattro*/
301 PyObject_GenericSetAttr, /*tp_setattro*/
302 0, /*tp_as_buffer*/
Neal Norwitzee3a1b52007-02-25 19:44:48 +0000303 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
304 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Richard Jones7b9558d2006-05-27 12:29:24 +0000305 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
306 (traverseproc)BaseException_traverse, /* tp_traverse */
307 (inquiry)BaseException_clear, /* tp_clear */
308 0, /* tp_richcompare */
309 0, /* tp_weaklistoffset */
310 0, /* tp_iter */
311 0, /* tp_iternext */
312 BaseException_methods, /* tp_methods */
313 BaseException_members, /* tp_members */
314 BaseException_getset, /* tp_getset */
315 0, /* tp_base */
316 0, /* tp_dict */
317 0, /* tp_descr_get */
318 0, /* tp_descr_set */
319 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
320 (initproc)BaseException_init, /* tp_init */
321 0, /* tp_alloc */
322 BaseException_new, /* tp_new */
323};
324/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
325from the previous implmentation and also allowing Python objects to be used
326in the API */
327PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
328
Richard Jones2d555b32006-05-27 16:15:11 +0000329/* note these macros omit the last semicolon so the macro invocation may
330 * include it and not look strange.
331 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000332#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
333static PyTypeObject _PyExc_ ## EXCNAME = { \
334 PyObject_HEAD_INIT(NULL) \
335 0, \
336 EXC_MODULE_NAME # EXCNAME, \
337 sizeof(PyBaseExceptionObject), \
338 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
339 0, 0, 0, 0, 0, 0, 0, \
340 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
341 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
342 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
343 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
344 (initproc)BaseException_init, 0, BaseException_new,\
345}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000346PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000347
348#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
349static PyTypeObject _PyExc_ ## EXCNAME = { \
350 PyObject_HEAD_INIT(NULL) \
351 0, \
352 EXC_MODULE_NAME # EXCNAME, \
353 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000354 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000355 0, 0, 0, 0, 0, \
356 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000357 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
358 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000359 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000360 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000361}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000362PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000363
364#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
365static PyTypeObject _PyExc_ ## EXCNAME = { \
366 PyObject_HEAD_INIT(NULL) \
367 0, \
368 EXC_MODULE_NAME # EXCNAME, \
369 sizeof(Py ## EXCSTORE ## Object), 0, \
370 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
371 (reprfunc)EXCSTR, 0, 0, 0, \
372 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
373 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
374 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
375 EXCMEMBERS, 0, &_ ## EXCBASE, \
376 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000377 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000378}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000379PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000380
381
382/*
383 * Exception extends BaseException
384 */
385SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000386 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000387
388
389/*
390 * StandardError extends Exception
391 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000392SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000393 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000394 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000395
396
397/*
398 * TypeError extends StandardError
399 */
400SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000401 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000402
403
404/*
405 * StopIteration extends Exception
406 */
407SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000408 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000409
410
411/*
412 * GeneratorExit extends Exception
413 */
414SimpleExtendsException(PyExc_Exception, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000415 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000416
417
418/*
419 * SystemExit extends BaseException
420 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000421
422static int
423SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
424{
425 Py_ssize_t size = PyTuple_GET_SIZE(args);
426
427 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
428 return -1;
429
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000430 if (size == 0)
431 return 0;
432 Py_CLEAR(self->code);
Richard Jones7b9558d2006-05-27 12:29:24 +0000433 if (size == 1)
434 self->code = PyTuple_GET_ITEM(args, 0);
435 else if (size > 1)
436 self->code = args;
437 Py_INCREF(self->code);
438 return 0;
439}
440
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000441static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000442SystemExit_clear(PySystemExitObject *self)
443{
444 Py_CLEAR(self->code);
445 return BaseException_clear((PyBaseExceptionObject *)self);
446}
447
448static void
449SystemExit_dealloc(PySystemExitObject *self)
450{
Georg Brandl38f62372006-09-06 06:50:05 +0000451 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000452 SystemExit_clear(self);
453 self->ob_type->tp_free((PyObject *)self);
454}
455
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000456static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000457SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
458{
459 Py_VISIT(self->code);
460 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
461}
462
463static PyMemberDef SystemExit_members[] = {
464 {"message", T_OBJECT, offsetof(PySystemExitObject, message), 0,
465 PyDoc_STR("exception message")},
466 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
467 PyDoc_STR("exception code")},
468 {NULL} /* Sentinel */
469};
470
471ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
472 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000473 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000474
475/*
476 * KeyboardInterrupt extends BaseException
477 */
478SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000479 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000480
481
482/*
483 * ImportError extends StandardError
484 */
485SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000486 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000487
488
489/*
490 * EnvironmentError extends StandardError
491 */
492
Richard Jones7b9558d2006-05-27 12:29:24 +0000493/* Where a function has a single filename, such as open() or some
494 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
495 * called, giving a third argument which is the filename. But, so
496 * that old code using in-place unpacking doesn't break, e.g.:
497 *
498 * except IOError, (errno, strerror):
499 *
500 * we hack args so that it only contains two items. This also
501 * means we need our own __str__() which prints out the filename
502 * when it was supplied.
503 */
504static int
505EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
506 PyObject *kwds)
507{
508 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
509 PyObject *subslice = NULL;
510
511 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
512 return -1;
513
Georg Brandl3267d282006-09-30 09:03:42 +0000514 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000515 return 0;
516 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000517
518 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000519 &myerrno, &strerror, &filename)) {
520 return -1;
521 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000522 Py_CLEAR(self->myerrno); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000523 self->myerrno = myerrno;
524 Py_INCREF(self->myerrno);
525
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000526 Py_CLEAR(self->strerror); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000527 self->strerror = strerror;
528 Py_INCREF(self->strerror);
529
530 /* self->filename will remain Py_None otherwise */
531 if (filename != NULL) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000532 Py_CLEAR(self->filename); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000533 self->filename = filename;
534 Py_INCREF(self->filename);
535
536 subslice = PyTuple_GetSlice(args, 0, 2);
537 if (!subslice)
538 return -1;
539
540 Py_DECREF(self->args); /* replacing args */
541 self->args = subslice;
542 }
543 return 0;
544}
545
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000546static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000547EnvironmentError_clear(PyEnvironmentErrorObject *self)
548{
549 Py_CLEAR(self->myerrno);
550 Py_CLEAR(self->strerror);
551 Py_CLEAR(self->filename);
552 return BaseException_clear((PyBaseExceptionObject *)self);
553}
554
555static void
556EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
557{
Georg Brandl38f62372006-09-06 06:50:05 +0000558 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000559 EnvironmentError_clear(self);
560 self->ob_type->tp_free((PyObject *)self);
561}
562
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000563static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000564EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
565 void *arg)
566{
567 Py_VISIT(self->myerrno);
568 Py_VISIT(self->strerror);
569 Py_VISIT(self->filename);
570 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
571}
572
573static PyObject *
574EnvironmentError_str(PyEnvironmentErrorObject *self)
575{
576 PyObject *rtnval = NULL;
577
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000578 if (self->filename) {
579 PyObject *fmt;
580 PyObject *repr;
581 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000582
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000583 fmt = PyString_FromString("[Errno %s] %s: %s");
584 if (!fmt)
585 return NULL;
586
587 repr = PyObject_Repr(self->filename);
588 if (!repr) {
589 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000590 return NULL;
591 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000592 tuple = PyTuple_New(3);
593 if (!tuple) {
594 Py_DECREF(repr);
595 Py_DECREF(fmt);
596 return NULL;
597 }
598
599 if (self->myerrno) {
600 Py_INCREF(self->myerrno);
601 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
602 }
603 else {
604 Py_INCREF(Py_None);
605 PyTuple_SET_ITEM(tuple, 0, Py_None);
606 }
607 if (self->strerror) {
608 Py_INCREF(self->strerror);
609 PyTuple_SET_ITEM(tuple, 1, self->strerror);
610 }
611 else {
612 Py_INCREF(Py_None);
613 PyTuple_SET_ITEM(tuple, 1, Py_None);
614 }
615
Richard Jones7b9558d2006-05-27 12:29:24 +0000616 PyTuple_SET_ITEM(tuple, 2, repr);
617
618 rtnval = PyString_Format(fmt, tuple);
619
620 Py_DECREF(fmt);
621 Py_DECREF(tuple);
622 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000623 else if (self->myerrno && self->strerror) {
624 PyObject *fmt;
625 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000626
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000627 fmt = PyString_FromString("[Errno %s] %s");
628 if (!fmt)
629 return NULL;
630
631 tuple = PyTuple_New(2);
632 if (!tuple) {
633 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000634 return NULL;
635 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000636
637 if (self->myerrno) {
638 Py_INCREF(self->myerrno);
639 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
640 }
641 else {
642 Py_INCREF(Py_None);
643 PyTuple_SET_ITEM(tuple, 0, Py_None);
644 }
645 if (self->strerror) {
646 Py_INCREF(self->strerror);
647 PyTuple_SET_ITEM(tuple, 1, self->strerror);
648 }
649 else {
650 Py_INCREF(Py_None);
651 PyTuple_SET_ITEM(tuple, 1, Py_None);
652 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000653
654 rtnval = PyString_Format(fmt, tuple);
655
656 Py_DECREF(fmt);
657 Py_DECREF(tuple);
658 }
659 else
660 rtnval = BaseException_str((PyBaseExceptionObject *)self);
661
662 return rtnval;
663}
664
665static PyMemberDef EnvironmentError_members[] = {
666 {"message", T_OBJECT, offsetof(PyEnvironmentErrorObject, message), 0,
667 PyDoc_STR("exception message")},
668 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000669 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000670 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000671 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000672 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000673 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000674 {NULL} /* Sentinel */
675};
676
677
678static PyObject *
679EnvironmentError_reduce(PyEnvironmentErrorObject *self)
680{
681 PyObject *args = self->args;
682 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000683
Richard Jones7b9558d2006-05-27 12:29:24 +0000684 /* self->args is only the first two real arguments if there was a
685 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000686 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000687 args = PyTuple_New(3);
688 if (!args) return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000689
690 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000691 Py_INCREF(tmp);
692 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000693
694 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000695 Py_INCREF(tmp);
696 PyTuple_SET_ITEM(args, 1, tmp);
697
698 Py_INCREF(self->filename);
699 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000700 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000701 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000702
703 if (self->dict)
704 res = PyTuple_Pack(3, self->ob_type, args, self->dict);
705 else
706 res = PyTuple_Pack(2, self->ob_type, args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000707 Py_DECREF(args);
708 return res;
709}
710
711
712static PyMethodDef EnvironmentError_methods[] = {
713 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
714 {NULL}
715};
716
717ComplexExtendsException(PyExc_StandardError, EnvironmentError,
718 EnvironmentError, EnvironmentError_dealloc,
719 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000720 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000721 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000722
723
724/*
725 * IOError extends EnvironmentError
726 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000727MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000728 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000729
730
731/*
732 * OSError extends EnvironmentError
733 */
734MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000735 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000736
737
738/*
739 * WindowsError extends OSError
740 */
741#ifdef MS_WINDOWS
742#include "errmap.h"
743
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000744static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000745WindowsError_clear(PyWindowsErrorObject *self)
746{
747 Py_CLEAR(self->myerrno);
748 Py_CLEAR(self->strerror);
749 Py_CLEAR(self->filename);
750 Py_CLEAR(self->winerror);
751 return BaseException_clear((PyBaseExceptionObject *)self);
752}
753
754static void
755WindowsError_dealloc(PyWindowsErrorObject *self)
756{
Georg Brandl38f62372006-09-06 06:50:05 +0000757 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000758 WindowsError_clear(self);
759 self->ob_type->tp_free((PyObject *)self);
760}
761
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000762static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000763WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
764{
765 Py_VISIT(self->myerrno);
766 Py_VISIT(self->strerror);
767 Py_VISIT(self->filename);
768 Py_VISIT(self->winerror);
769 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
770}
771
Richard Jones7b9558d2006-05-27 12:29:24 +0000772static int
773WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
774{
775 PyObject *o_errcode = NULL;
776 long errcode;
777 long posix_errno;
778
779 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
780 == -1)
781 return -1;
782
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000783 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000784 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000785
786 /* Set errno to the POSIX errno, and winerror to the Win32
787 error code. */
788 errcode = PyInt_AsLong(self->myerrno);
789 if (errcode == -1 && PyErr_Occurred())
790 return -1;
791 posix_errno = winerror_to_errno(errcode);
792
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000793 Py_CLEAR(self->winerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000794 self->winerror = self->myerrno;
795
796 o_errcode = PyInt_FromLong(posix_errno);
797 if (!o_errcode)
798 return -1;
799
800 self->myerrno = o_errcode;
801
802 return 0;
803}
804
805
806static PyObject *
807WindowsError_str(PyWindowsErrorObject *self)
808{
Richard Jones7b9558d2006-05-27 12:29:24 +0000809 PyObject *rtnval = NULL;
810
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000811 if (self->filename) {
812 PyObject *fmt;
813 PyObject *repr;
814 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000815
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000816 fmt = PyString_FromString("[Error %s] %s: %s");
817 if (!fmt)
818 return NULL;
819
820 repr = PyObject_Repr(self->filename);
821 if (!repr) {
822 Py_DECREF(fmt);
823 return NULL;
824 }
825 tuple = PyTuple_New(3);
826 if (!tuple) {
827 Py_DECREF(repr);
828 Py_DECREF(fmt);
829 return NULL;
830 }
831
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000832 if (self->winerror) {
833 Py_INCREF(self->winerror);
834 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000835 }
836 else {
837 Py_INCREF(Py_None);
838 PyTuple_SET_ITEM(tuple, 0, Py_None);
839 }
840 if (self->strerror) {
841 Py_INCREF(self->strerror);
842 PyTuple_SET_ITEM(tuple, 1, self->strerror);
843 }
844 else {
845 Py_INCREF(Py_None);
846 PyTuple_SET_ITEM(tuple, 1, Py_None);
847 }
848
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000849 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000850
851 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000852
853 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000854 Py_DECREF(tuple);
855 }
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000856 else if (self->winerror && self->strerror) {
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000857 PyObject *fmt;
858 PyObject *tuple;
859
Richard Jones7b9558d2006-05-27 12:29:24 +0000860 fmt = PyString_FromString("[Error %s] %s");
861 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000862 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000863
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000864 tuple = PyTuple_New(2);
865 if (!tuple) {
866 Py_DECREF(fmt);
867 return NULL;
868 }
869
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000870 if (self->winerror) {
871 Py_INCREF(self->winerror);
872 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000873 }
874 else {
875 Py_INCREF(Py_None);
876 PyTuple_SET_ITEM(tuple, 0, Py_None);
877 }
878 if (self->strerror) {
879 Py_INCREF(self->strerror);
880 PyTuple_SET_ITEM(tuple, 1, self->strerror);
881 }
882 else {
883 Py_INCREF(Py_None);
884 PyTuple_SET_ITEM(tuple, 1, Py_None);
885 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000886
887 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000888
889 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000890 Py_DECREF(tuple);
891 }
892 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000893 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000894
Richard Jones7b9558d2006-05-27 12:29:24 +0000895 return rtnval;
896}
897
898static PyMemberDef WindowsError_members[] = {
899 {"message", T_OBJECT, offsetof(PyWindowsErrorObject, message), 0,
900 PyDoc_STR("exception message")},
901 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000902 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000903 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000904 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000905 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000906 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000907 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000908 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000909 {NULL} /* Sentinel */
910};
911
Richard Jones2d555b32006-05-27 16:15:11 +0000912ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
913 WindowsError_dealloc, 0, WindowsError_members,
914 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000915
916#endif /* MS_WINDOWS */
917
918
919/*
920 * VMSError extends OSError (I think)
921 */
922#ifdef __VMS
923MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +0000924 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000925#endif
926
927
928/*
929 * EOFError extends StandardError
930 */
931SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +0000932 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000933
934
935/*
936 * RuntimeError extends StandardError
937 */
938SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000939 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000940
941
942/*
943 * NotImplementedError extends RuntimeError
944 */
945SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +0000946 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000947
948/*
949 * NameError extends StandardError
950 */
951SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +0000952 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000953
954/*
955 * UnboundLocalError extends NameError
956 */
957SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +0000958 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000959
960/*
961 * AttributeError extends StandardError
962 */
963SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000964 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000965
966
967/*
968 * SyntaxError extends StandardError
969 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000970
971static int
972SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
973{
974 PyObject *info = NULL;
975 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
976
977 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
978 return -1;
979
980 if (lenargs >= 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000981 Py_CLEAR(self->msg);
Richard Jones7b9558d2006-05-27 12:29:24 +0000982 self->msg = PyTuple_GET_ITEM(args, 0);
983 Py_INCREF(self->msg);
984 }
985 if (lenargs == 2) {
986 info = PyTuple_GET_ITEM(args, 1);
987 info = PySequence_Tuple(info);
988 if (!info) return -1;
989
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000990 if (PyTuple_GET_SIZE(info) != 4) {
991 /* not a very good error message, but it's what Python 2.4 gives */
992 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
993 Py_DECREF(info);
994 return -1;
995 }
996
997 Py_CLEAR(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +0000998 self->filename = PyTuple_GET_ITEM(info, 0);
999 Py_INCREF(self->filename);
1000
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001001 Py_CLEAR(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001002 self->lineno = PyTuple_GET_ITEM(info, 1);
1003 Py_INCREF(self->lineno);
1004
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001005 Py_CLEAR(self->offset);
Richard Jones7b9558d2006-05-27 12:29:24 +00001006 self->offset = PyTuple_GET_ITEM(info, 2);
1007 Py_INCREF(self->offset);
1008
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001009 Py_CLEAR(self->text);
Richard Jones7b9558d2006-05-27 12:29:24 +00001010 self->text = PyTuple_GET_ITEM(info, 3);
1011 Py_INCREF(self->text);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001012
1013 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001014 }
1015 return 0;
1016}
1017
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001018static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001019SyntaxError_clear(PySyntaxErrorObject *self)
1020{
1021 Py_CLEAR(self->msg);
1022 Py_CLEAR(self->filename);
1023 Py_CLEAR(self->lineno);
1024 Py_CLEAR(self->offset);
1025 Py_CLEAR(self->text);
1026 Py_CLEAR(self->print_file_and_line);
1027 return BaseException_clear((PyBaseExceptionObject *)self);
1028}
1029
1030static void
1031SyntaxError_dealloc(PySyntaxErrorObject *self)
1032{
Georg Brandl38f62372006-09-06 06:50:05 +00001033 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001034 SyntaxError_clear(self);
1035 self->ob_type->tp_free((PyObject *)self);
1036}
1037
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001038static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001039SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1040{
1041 Py_VISIT(self->msg);
1042 Py_VISIT(self->filename);
1043 Py_VISIT(self->lineno);
1044 Py_VISIT(self->offset);
1045 Py_VISIT(self->text);
1046 Py_VISIT(self->print_file_and_line);
1047 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1048}
1049
1050/* This is called "my_basename" instead of just "basename" to avoid name
1051 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1052 defined, and Python does define that. */
1053static char *
1054my_basename(char *name)
1055{
1056 char *cp = name;
1057 char *result = name;
1058
1059 if (name == NULL)
1060 return "???";
1061 while (*cp != '\0') {
1062 if (*cp == SEP)
1063 result = cp + 1;
1064 ++cp;
1065 }
1066 return result;
1067}
1068
1069
1070static PyObject *
1071SyntaxError_str(PySyntaxErrorObject *self)
1072{
1073 PyObject *str;
1074 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001075 int have_filename = 0;
1076 int have_lineno = 0;
1077 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001078 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001079
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001080 if (self->msg)
1081 str = PyObject_Str(self->msg);
1082 else
1083 str = PyObject_Str(Py_None);
Georg Brandl43ab1002006-05-28 20:57:09 +00001084 if (!str) return NULL;
1085 /* Don't fiddle with non-string return (shouldn't happen anyway) */
1086 if (!PyString_Check(str)) return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001087
1088 /* XXX -- do all the additional formatting with filename and
1089 lineno here */
1090
Georg Brandl43ab1002006-05-28 20:57:09 +00001091 have_filename = (self->filename != NULL) &&
1092 PyString_Check(self->filename);
1093 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001094
Georg Brandl43ab1002006-05-28 20:57:09 +00001095 if (!have_filename && !have_lineno)
1096 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001097
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001098 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001099 if (have_filename)
1100 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001101
Georg Brandl43ab1002006-05-28 20:57:09 +00001102 buffer = PyMem_MALLOC(bufsize);
1103 if (buffer == NULL)
1104 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001105
Georg Brandl43ab1002006-05-28 20:57:09 +00001106 if (have_filename && have_lineno)
1107 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
1108 PyString_AS_STRING(str),
1109 my_basename(PyString_AS_STRING(self->filename)),
1110 PyInt_AsLong(self->lineno));
1111 else if (have_filename)
1112 PyOS_snprintf(buffer, bufsize, "%s (%s)",
1113 PyString_AS_STRING(str),
1114 my_basename(PyString_AS_STRING(self->filename)));
1115 else /* only have_lineno */
1116 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
1117 PyString_AS_STRING(str),
1118 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001119
Georg Brandl43ab1002006-05-28 20:57:09 +00001120 result = PyString_FromString(buffer);
1121 PyMem_FREE(buffer);
1122
1123 if (result == NULL)
1124 result = str;
1125 else
1126 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001127 return result;
1128}
1129
1130static PyMemberDef SyntaxError_members[] = {
1131 {"message", T_OBJECT, offsetof(PySyntaxErrorObject, message), 0,
1132 PyDoc_STR("exception message")},
1133 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1134 PyDoc_STR("exception msg")},
1135 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1136 PyDoc_STR("exception filename")},
1137 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1138 PyDoc_STR("exception lineno")},
1139 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1140 PyDoc_STR("exception offset")},
1141 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1142 PyDoc_STR("exception text")},
1143 {"print_file_and_line", T_OBJECT,
1144 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1145 PyDoc_STR("exception print_file_and_line")},
1146 {NULL} /* Sentinel */
1147};
1148
1149ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1150 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001151 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001152
1153
1154/*
1155 * IndentationError extends SyntaxError
1156 */
1157MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001158 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001159
1160
1161/*
1162 * TabError extends IndentationError
1163 */
1164MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001165 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001166
1167
1168/*
1169 * LookupError extends StandardError
1170 */
1171SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001172 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001173
1174
1175/*
1176 * IndexError extends LookupError
1177 */
1178SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001179 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001180
1181
1182/*
1183 * KeyError extends LookupError
1184 */
1185static PyObject *
1186KeyError_str(PyBaseExceptionObject *self)
1187{
1188 /* If args is a tuple of exactly one item, apply repr to args[0].
1189 This is done so that e.g. the exception raised by {}[''] prints
1190 KeyError: ''
1191 rather than the confusing
1192 KeyError
1193 alone. The downside is that if KeyError is raised with an explanatory
1194 string, that string will be displayed in quotes. Too bad.
1195 If args is anything else, use the default BaseException__str__().
1196 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001197 if (PyTuple_GET_SIZE(self->args) == 1) {
1198 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001199 }
1200 return BaseException_str(self);
1201}
1202
1203ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001204 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001205
1206
1207/*
1208 * ValueError extends StandardError
1209 */
1210SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001211 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001212
1213/*
1214 * UnicodeError extends ValueError
1215 */
1216
1217SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001218 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001219
1220#ifdef Py_USING_UNICODE
1221static int
1222get_int(PyObject *attr, Py_ssize_t *value, const char *name)
1223{
1224 if (!attr) {
1225 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1226 return -1;
1227 }
1228
1229 if (PyInt_Check(attr)) {
1230 *value = PyInt_AS_LONG(attr);
1231 } else if (PyLong_Check(attr)) {
1232 *value = _PyLong_AsSsize_t(attr);
1233 if (*value == -1 && PyErr_Occurred())
1234 return -1;
1235 } else {
1236 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
1237 return -1;
1238 }
1239 return 0;
1240}
1241
1242static int
1243set_ssize_t(PyObject **attr, Py_ssize_t value)
1244{
1245 PyObject *obj = PyInt_FromSsize_t(value);
1246 if (!obj)
1247 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001248 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001249 *attr = obj;
1250 return 0;
1251}
1252
1253static PyObject *
1254get_string(PyObject *attr, const char *name)
1255{
1256 if (!attr) {
1257 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1258 return NULL;
1259 }
1260
1261 if (!PyString_Check(attr)) {
1262 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1263 return NULL;
1264 }
1265 Py_INCREF(attr);
1266 return attr;
1267}
1268
1269
1270static int
1271set_string(PyObject **attr, const char *value)
1272{
1273 PyObject *obj = PyString_FromString(value);
1274 if (!obj)
1275 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001276 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001277 *attr = obj;
1278 return 0;
1279}
1280
1281
1282static PyObject *
1283get_unicode(PyObject *attr, const char *name)
1284{
1285 if (!attr) {
1286 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1287 return NULL;
1288 }
1289
1290 if (!PyUnicode_Check(attr)) {
1291 PyErr_Format(PyExc_TypeError,
1292 "%.200s attribute must be unicode", name);
1293 return NULL;
1294 }
1295 Py_INCREF(attr);
1296 return attr;
1297}
1298
1299PyObject *
1300PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1301{
1302 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1303}
1304
1305PyObject *
1306PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1307{
1308 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1309}
1310
1311PyObject *
1312PyUnicodeEncodeError_GetObject(PyObject *exc)
1313{
1314 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1315}
1316
1317PyObject *
1318PyUnicodeDecodeError_GetObject(PyObject *exc)
1319{
1320 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1321}
1322
1323PyObject *
1324PyUnicodeTranslateError_GetObject(PyObject *exc)
1325{
1326 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1327}
1328
1329int
1330PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1331{
1332 if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
1333 Py_ssize_t size;
1334 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1335 "object");
1336 if (!obj) return -1;
1337 size = PyUnicode_GET_SIZE(obj);
1338 if (*start<0)
1339 *start = 0; /*XXX check for values <0*/
1340 if (*start>=size)
1341 *start = size-1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001342 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001343 return 0;
1344 }
1345 return -1;
1346}
1347
1348
1349int
1350PyUnicodeDecodeError_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_string(((PyUnicodeErrorObject *)exc)->object,
1355 "object");
1356 if (!obj) return -1;
1357 size = PyString_GET_SIZE(obj);
1358 if (*start<0)
1359 *start = 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
1370PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1371{
1372 return PyUnicodeEncodeError_GetStart(exc, start);
1373}
1374
1375
1376int
1377PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1378{
1379 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1380}
1381
1382
1383int
1384PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1385{
1386 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1387}
1388
1389
1390int
1391PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1392{
1393 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1394}
1395
1396
1397int
1398PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1399{
1400 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1401 Py_ssize_t size;
1402 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1403 "object");
1404 if (!obj) return -1;
1405 size = PyUnicode_GET_SIZE(obj);
1406 if (*end<1)
1407 *end = 1;
1408 if (*end>size)
1409 *end = size;
Georg Brandl43ab1002006-05-28 20:57:09 +00001410 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001411 return 0;
1412 }
1413 return -1;
1414}
1415
1416
1417int
1418PyUnicodeDecodeError_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_string(((PyUnicodeErrorObject *)exc)->object,
1423 "object");
1424 if (!obj) return -1;
1425 size = PyString_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
1438PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1439{
1440 return PyUnicodeEncodeError_GetEnd(exc, start);
1441}
1442
1443
1444int
1445PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1446{
1447 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1448}
1449
1450
1451int
1452PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1453{
1454 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1455}
1456
1457
1458int
1459PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1460{
1461 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1462}
1463
1464PyObject *
1465PyUnicodeEncodeError_GetReason(PyObject *exc)
1466{
1467 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1468}
1469
1470
1471PyObject *
1472PyUnicodeDecodeError_GetReason(PyObject *exc)
1473{
1474 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1475}
1476
1477
1478PyObject *
1479PyUnicodeTranslateError_GetReason(PyObject *exc)
1480{
1481 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1482}
1483
1484
1485int
1486PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1487{
1488 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1489}
1490
1491
1492int
1493PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1494{
1495 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1496}
1497
1498
1499int
1500PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1501{
1502 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1503}
1504
1505
Richard Jones7b9558d2006-05-27 12:29:24 +00001506static int
1507UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1508 PyTypeObject *objecttype)
1509{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001510 Py_CLEAR(self->encoding);
1511 Py_CLEAR(self->object);
1512 Py_CLEAR(self->start);
1513 Py_CLEAR(self->end);
1514 Py_CLEAR(self->reason);
1515
Richard Jones7b9558d2006-05-27 12:29:24 +00001516 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1517 &PyString_Type, &self->encoding,
1518 objecttype, &self->object,
1519 &PyInt_Type, &self->start,
1520 &PyInt_Type, &self->end,
1521 &PyString_Type, &self->reason)) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001522 self->encoding = self->object = self->start = self->end =
Richard Jones7b9558d2006-05-27 12:29:24 +00001523 self->reason = NULL;
1524 return -1;
1525 }
1526
1527 Py_INCREF(self->encoding);
1528 Py_INCREF(self->object);
1529 Py_INCREF(self->start);
1530 Py_INCREF(self->end);
1531 Py_INCREF(self->reason);
1532
1533 return 0;
1534}
1535
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001536static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001537UnicodeError_clear(PyUnicodeErrorObject *self)
1538{
1539 Py_CLEAR(self->encoding);
1540 Py_CLEAR(self->object);
1541 Py_CLEAR(self->start);
1542 Py_CLEAR(self->end);
1543 Py_CLEAR(self->reason);
1544 return BaseException_clear((PyBaseExceptionObject *)self);
1545}
1546
1547static void
1548UnicodeError_dealloc(PyUnicodeErrorObject *self)
1549{
Georg Brandl38f62372006-09-06 06:50:05 +00001550 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001551 UnicodeError_clear(self);
1552 self->ob_type->tp_free((PyObject *)self);
1553}
1554
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001555static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001556UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1557{
1558 Py_VISIT(self->encoding);
1559 Py_VISIT(self->object);
1560 Py_VISIT(self->start);
1561 Py_VISIT(self->end);
1562 Py_VISIT(self->reason);
1563 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1564}
1565
1566static PyMemberDef UnicodeError_members[] = {
1567 {"message", T_OBJECT, offsetof(PyUnicodeErrorObject, message), 0,
1568 PyDoc_STR("exception message")},
1569 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1570 PyDoc_STR("exception encoding")},
1571 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1572 PyDoc_STR("exception object")},
1573 {"start", T_OBJECT, offsetof(PyUnicodeErrorObject, start), 0,
1574 PyDoc_STR("exception start")},
1575 {"end", T_OBJECT, offsetof(PyUnicodeErrorObject, end), 0,
1576 PyDoc_STR("exception end")},
1577 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1578 PyDoc_STR("exception reason")},
1579 {NULL} /* Sentinel */
1580};
1581
1582
1583/*
1584 * UnicodeEncodeError extends UnicodeError
1585 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001586
1587static int
1588UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1589{
1590 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1591 return -1;
1592 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1593 kwds, &PyUnicode_Type);
1594}
1595
1596static PyObject *
1597UnicodeEncodeError_str(PyObject *self)
1598{
1599 Py_ssize_t start;
1600 Py_ssize_t end;
1601
1602 if (PyUnicodeEncodeError_GetStart(self, &start))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001603 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001604
1605 if (PyUnicodeEncodeError_GetEnd(self, &end))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001606 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001607
1608 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001609 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1610 char badchar_str[20];
1611 if (badchar <= 0xff)
1612 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1613 else if (badchar <= 0xffff)
1614 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1615 else
1616 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1617 return PyString_FromFormat(
1618 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
1619 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1620 badchar_str,
1621 start,
1622 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1623 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001624 }
1625 return PyString_FromFormat(
1626 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
1627 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1628 start,
1629 (end-1),
1630 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1631 );
1632}
1633
1634static PyTypeObject _PyExc_UnicodeEncodeError = {
1635 PyObject_HEAD_INIT(NULL)
1636 0,
Georg Brandl38f62372006-09-06 06:50:05 +00001637 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001638 sizeof(PyUnicodeErrorObject), 0,
1639 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1640 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1641 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001642 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1643 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001644 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001645 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001646};
1647PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1648
1649PyObject *
1650PyUnicodeEncodeError_Create(
1651 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1652 Py_ssize_t start, Py_ssize_t end, const char *reason)
1653{
1654 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001655 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001656}
1657
1658
1659/*
1660 * UnicodeDecodeError extends UnicodeError
1661 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001662
1663static int
1664UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1665{
1666 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1667 return -1;
1668 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1669 kwds, &PyString_Type);
1670}
1671
1672static PyObject *
1673UnicodeDecodeError_str(PyObject *self)
1674{
Georg Brandl43ab1002006-05-28 20:57:09 +00001675 Py_ssize_t start = 0;
1676 Py_ssize_t end = 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001677
1678 if (PyUnicodeDecodeError_GetStart(self, &start))
1679 return NULL;
1680
1681 if (PyUnicodeDecodeError_GetEnd(self, &end))
1682 return NULL;
1683
1684 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001685 /* FromFormat does not support %02x, so format that separately */
1686 char byte[4];
1687 PyOS_snprintf(byte, sizeof(byte), "%02x",
1688 ((int)PyString_AS_STRING(((PyUnicodeErrorObject *)self)->object)[start])&0xff);
1689 return PyString_FromFormat(
1690 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
1691 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1692 byte,
1693 start,
1694 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1695 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001696 }
1697 return PyString_FromFormat(
1698 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
1699 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1700 start,
1701 (end-1),
1702 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1703 );
1704}
1705
1706static PyTypeObject _PyExc_UnicodeDecodeError = {
1707 PyObject_HEAD_INIT(NULL)
1708 0,
1709 EXC_MODULE_NAME "UnicodeDecodeError",
1710 sizeof(PyUnicodeErrorObject), 0,
1711 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1712 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1713 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001714 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1715 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001716 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001717 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001718};
1719PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1720
1721PyObject *
1722PyUnicodeDecodeError_Create(
1723 const char *encoding, const char *object, Py_ssize_t length,
1724 Py_ssize_t start, Py_ssize_t end, const char *reason)
1725{
1726 assert(length < INT_MAX);
1727 assert(start < INT_MAX);
1728 assert(end < INT_MAX);
1729 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001730 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001731}
1732
1733
1734/*
1735 * UnicodeTranslateError extends UnicodeError
1736 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001737
1738static int
1739UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1740 PyObject *kwds)
1741{
1742 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1743 return -1;
1744
1745 Py_CLEAR(self->object);
1746 Py_CLEAR(self->start);
1747 Py_CLEAR(self->end);
1748 Py_CLEAR(self->reason);
1749
1750 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1751 &PyUnicode_Type, &self->object,
1752 &PyInt_Type, &self->start,
1753 &PyInt_Type, &self->end,
1754 &PyString_Type, &self->reason)) {
1755 self->object = self->start = self->end = self->reason = NULL;
1756 return -1;
1757 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001758
Richard Jones7b9558d2006-05-27 12:29:24 +00001759 Py_INCREF(self->object);
1760 Py_INCREF(self->start);
1761 Py_INCREF(self->end);
1762 Py_INCREF(self->reason);
1763
1764 return 0;
1765}
1766
1767
1768static PyObject *
1769UnicodeTranslateError_str(PyObject *self)
1770{
1771 Py_ssize_t start;
1772 Py_ssize_t end;
1773
1774 if (PyUnicodeTranslateError_GetStart(self, &start))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001775 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001776
1777 if (PyUnicodeTranslateError_GetEnd(self, &end))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001778 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001779
1780 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001781 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1782 char badchar_str[20];
1783 if (badchar <= 0xff)
1784 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1785 else if (badchar <= 0xffff)
1786 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1787 else
1788 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1789 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001790 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001791 badchar_str,
1792 start,
1793 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1794 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001795 }
1796 return PyString_FromFormat(
1797 "can't translate characters in position %zd-%zd: %.400s",
1798 start,
1799 (end-1),
1800 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1801 );
1802}
1803
1804static PyTypeObject _PyExc_UnicodeTranslateError = {
1805 PyObject_HEAD_INIT(NULL)
1806 0,
1807 EXC_MODULE_NAME "UnicodeTranslateError",
1808 sizeof(PyUnicodeErrorObject), 0,
1809 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1810 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1811 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001812 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001813 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1814 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001815 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001816};
1817PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1818
1819PyObject *
1820PyUnicodeTranslateError_Create(
1821 const Py_UNICODE *object, Py_ssize_t length,
1822 Py_ssize_t start, Py_ssize_t end, const char *reason)
1823{
1824 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001825 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001826}
1827#endif
1828
1829
1830/*
1831 * AssertionError extends StandardError
1832 */
1833SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001834 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001835
1836
1837/*
1838 * ArithmeticError extends StandardError
1839 */
1840SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001841 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001842
1843
1844/*
1845 * FloatingPointError extends ArithmeticError
1846 */
1847SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001848 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001849
1850
1851/*
1852 * OverflowError extends ArithmeticError
1853 */
1854SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001855 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001856
1857
1858/*
1859 * ZeroDivisionError extends ArithmeticError
1860 */
1861SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001862 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001863
1864
1865/*
1866 * SystemError extends StandardError
1867 */
1868SimpleExtendsException(PyExc_StandardError, SystemError,
1869 "Internal error in the Python interpreter.\n"
1870 "\n"
1871 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001872 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001873
1874
1875/*
1876 * ReferenceError extends StandardError
1877 */
1878SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001879 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001880
1881
1882/*
1883 * MemoryError extends StandardError
1884 */
Richard Jones2d555b32006-05-27 16:15:11 +00001885SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001886
1887
1888/* Warning category docstrings */
1889
1890/*
1891 * Warning extends Exception
1892 */
1893SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001894 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001895
1896
1897/*
1898 * UserWarning extends Warning
1899 */
1900SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001901 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001902
1903
1904/*
1905 * DeprecationWarning extends Warning
1906 */
1907SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001908 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001909
1910
1911/*
1912 * PendingDeprecationWarning extends Warning
1913 */
1914SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1915 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001916 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001917
1918
1919/*
1920 * SyntaxWarning extends Warning
1921 */
1922SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001923 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001924
1925
1926/*
1927 * RuntimeWarning extends Warning
1928 */
1929SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001930 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001931
1932
1933/*
1934 * FutureWarning extends Warning
1935 */
1936SimpleExtendsException(PyExc_Warning, FutureWarning,
1937 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001938 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001939
1940
1941/*
1942 * ImportWarning extends Warning
1943 */
1944SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001945 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00001946
1947
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001948/*
1949 * UnicodeWarning extends Warning
1950 */
1951SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1952 "Base class for warnings about Unicode related problems, mostly\n"
1953 "related to conversion problems.");
1954
1955
Richard Jones7b9558d2006-05-27 12:29:24 +00001956/* Pre-computed MemoryError instance. Best to create this as early as
1957 * possible and not wait until a MemoryError is actually raised!
1958 */
1959PyObject *PyExc_MemoryErrorInst=NULL;
1960
1961/* module global functions */
1962static PyMethodDef functions[] = {
1963 /* Sentinel */
1964 {NULL, NULL}
1965};
1966
1967#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1968 Py_FatalError("exceptions bootstrapping error.");
1969
1970#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1971 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1972 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1973 Py_FatalError("Module dictionary insertion problem.");
1974
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00001975#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00001976/* crt variable checking in VisualStudio .NET 2005 */
1977#include <crtdbg.h>
1978
1979static int prevCrtReportMode;
1980static _invalid_parameter_handler prevCrtHandler;
1981
1982/* Invalid parameter handler. Sets a ValueError exception */
1983static void
1984InvalidParameterHandler(
1985 const wchar_t * expression,
1986 const wchar_t * function,
1987 const wchar_t * file,
1988 unsigned int line,
1989 uintptr_t pReserved)
1990{
1991 /* Do nothing, allow execution to continue. Usually this
1992 * means that the CRT will set errno to EINVAL
1993 */
1994}
1995#endif
1996
1997
Richard Jones7b9558d2006-05-27 12:29:24 +00001998PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001999_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00002000{
2001 PyObject *m, *bltinmod, *bdict;
2002
2003 PRE_INIT(BaseException)
2004 PRE_INIT(Exception)
2005 PRE_INIT(StandardError)
2006 PRE_INIT(TypeError)
2007 PRE_INIT(StopIteration)
2008 PRE_INIT(GeneratorExit)
2009 PRE_INIT(SystemExit)
2010 PRE_INIT(KeyboardInterrupt)
2011 PRE_INIT(ImportError)
2012 PRE_INIT(EnvironmentError)
2013 PRE_INIT(IOError)
2014 PRE_INIT(OSError)
2015#ifdef MS_WINDOWS
2016 PRE_INIT(WindowsError)
2017#endif
2018#ifdef __VMS
2019 PRE_INIT(VMSError)
2020#endif
2021 PRE_INIT(EOFError)
2022 PRE_INIT(RuntimeError)
2023 PRE_INIT(NotImplementedError)
2024 PRE_INIT(NameError)
2025 PRE_INIT(UnboundLocalError)
2026 PRE_INIT(AttributeError)
2027 PRE_INIT(SyntaxError)
2028 PRE_INIT(IndentationError)
2029 PRE_INIT(TabError)
2030 PRE_INIT(LookupError)
2031 PRE_INIT(IndexError)
2032 PRE_INIT(KeyError)
2033 PRE_INIT(ValueError)
2034 PRE_INIT(UnicodeError)
2035#ifdef Py_USING_UNICODE
2036 PRE_INIT(UnicodeEncodeError)
2037 PRE_INIT(UnicodeDecodeError)
2038 PRE_INIT(UnicodeTranslateError)
2039#endif
2040 PRE_INIT(AssertionError)
2041 PRE_INIT(ArithmeticError)
2042 PRE_INIT(FloatingPointError)
2043 PRE_INIT(OverflowError)
2044 PRE_INIT(ZeroDivisionError)
2045 PRE_INIT(SystemError)
2046 PRE_INIT(ReferenceError)
2047 PRE_INIT(MemoryError)
2048 PRE_INIT(Warning)
2049 PRE_INIT(UserWarning)
2050 PRE_INIT(DeprecationWarning)
2051 PRE_INIT(PendingDeprecationWarning)
2052 PRE_INIT(SyntaxWarning)
2053 PRE_INIT(RuntimeWarning)
2054 PRE_INIT(FutureWarning)
2055 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002056 PRE_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002057
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002058 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2059 (PyObject *)NULL, PYTHON_API_VERSION);
Richard Jones7b9558d2006-05-27 12:29:24 +00002060 if (m == NULL) return;
2061
2062 bltinmod = PyImport_ImportModule("__builtin__");
2063 if (bltinmod == NULL)
2064 Py_FatalError("exceptions bootstrapping error.");
2065 bdict = PyModule_GetDict(bltinmod);
2066 if (bdict == NULL)
2067 Py_FatalError("exceptions bootstrapping error.");
2068
2069 POST_INIT(BaseException)
2070 POST_INIT(Exception)
2071 POST_INIT(StandardError)
2072 POST_INIT(TypeError)
2073 POST_INIT(StopIteration)
2074 POST_INIT(GeneratorExit)
2075 POST_INIT(SystemExit)
2076 POST_INIT(KeyboardInterrupt)
2077 POST_INIT(ImportError)
2078 POST_INIT(EnvironmentError)
2079 POST_INIT(IOError)
2080 POST_INIT(OSError)
2081#ifdef MS_WINDOWS
2082 POST_INIT(WindowsError)
2083#endif
2084#ifdef __VMS
2085 POST_INIT(VMSError)
2086#endif
2087 POST_INIT(EOFError)
2088 POST_INIT(RuntimeError)
2089 POST_INIT(NotImplementedError)
2090 POST_INIT(NameError)
2091 POST_INIT(UnboundLocalError)
2092 POST_INIT(AttributeError)
2093 POST_INIT(SyntaxError)
2094 POST_INIT(IndentationError)
2095 POST_INIT(TabError)
2096 POST_INIT(LookupError)
2097 POST_INIT(IndexError)
2098 POST_INIT(KeyError)
2099 POST_INIT(ValueError)
2100 POST_INIT(UnicodeError)
2101#ifdef Py_USING_UNICODE
2102 POST_INIT(UnicodeEncodeError)
2103 POST_INIT(UnicodeDecodeError)
2104 POST_INIT(UnicodeTranslateError)
2105#endif
2106 POST_INIT(AssertionError)
2107 POST_INIT(ArithmeticError)
2108 POST_INIT(FloatingPointError)
2109 POST_INIT(OverflowError)
2110 POST_INIT(ZeroDivisionError)
2111 POST_INIT(SystemError)
2112 POST_INIT(ReferenceError)
2113 POST_INIT(MemoryError)
2114 POST_INIT(Warning)
2115 POST_INIT(UserWarning)
2116 POST_INIT(DeprecationWarning)
2117 POST_INIT(PendingDeprecationWarning)
2118 POST_INIT(SyntaxWarning)
2119 POST_INIT(RuntimeWarning)
2120 POST_INIT(FutureWarning)
2121 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002122 POST_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002123
2124 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2125 if (!PyExc_MemoryErrorInst)
2126 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
2127
2128 Py_DECREF(bltinmod);
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002129
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002130#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002131 /* Set CRT argument error handler */
2132 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
2133 /* turn off assertions in debug mode */
2134 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
2135#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002136}
2137
2138void
2139_PyExc_Fini(void)
2140{
2141 Py_XDECREF(PyExc_MemoryErrorInst);
2142 PyExc_MemoryErrorInst = NULL;
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002143#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002144 /* reset CRT error handling */
2145 _set_invalid_parameter_handler(prevCrtHandler);
2146 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
2147#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002148}