blob: cdf2609bc8c250b7cc6d5ee39782c2ae95ccbe1b [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 Brandlecab6232006-09-06 06:47:02 +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
178#ifdef Py_USING_UNICODE
179/* while this method generates fairly uninspired output, it a least
180 * guarantees that we can display exceptions that have unicode attributes
181 */
182static PyObject *
183BaseException_unicode(PyBaseExceptionObject *self)
184{
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000185 if (PyTuple_GET_SIZE(self->args) == 0)
Richard Jones7b9558d2006-05-27 12:29:24 +0000186 return PyUnicode_FromUnicode(NULL, 0);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000187 if (PyTuple_GET_SIZE(self->args) == 1)
188 return PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +0000189 return PyObject_Unicode(self->args);
190}
191#endif /* Py_USING_UNICODE */
192
193static PyMethodDef BaseException_methods[] = {
194 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Georg Brandl85ac8502006-06-01 06:39:19 +0000195 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Richard Jones7b9558d2006-05-27 12:29:24 +0000196#ifdef Py_USING_UNICODE
197 {"__unicode__", (PyCFunction)BaseException_unicode, METH_NOARGS },
198#endif
199 {NULL, NULL, 0, NULL},
200};
201
202
203
204static PyObject *
205BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
206{
207 return PySequence_GetItem(self->args, index);
208}
209
210static PySequenceMethods BaseException_as_sequence = {
211 0, /* sq_length; */
212 0, /* sq_concat; */
213 0, /* sq_repeat; */
214 (ssizeargfunc)BaseException_getitem, /* sq_item; */
215 0, /* sq_slice; */
216 0, /* sq_ass_item; */
217 0, /* sq_ass_slice; */
218 0, /* sq_contains; */
219 0, /* sq_inplace_concat; */
220 0 /* sq_inplace_repeat; */
221};
222
223static PyMemberDef BaseException_members[] = {
224 {"message", T_OBJECT, offsetof(PyBaseExceptionObject, message), 0,
225 PyDoc_STR("exception message")},
226 {NULL} /* Sentinel */
227};
228
229
230static PyObject *
231BaseException_get_dict(PyBaseExceptionObject *self)
232{
233 if (self->dict == NULL) {
234 self->dict = PyDict_New();
235 if (!self->dict)
236 return NULL;
237 }
238 Py_INCREF(self->dict);
239 return self->dict;
240}
241
242static int
243BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
244{
245 if (val == NULL) {
246 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
247 return -1;
248 }
249 if (!PyDict_Check(val)) {
250 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
251 return -1;
252 }
253 Py_CLEAR(self->dict);
254 Py_INCREF(val);
255 self->dict = val;
256 return 0;
257}
258
259static PyObject *
260BaseException_get_args(PyBaseExceptionObject *self)
261{
262 if (self->args == NULL) {
263 Py_INCREF(Py_None);
264 return Py_None;
265 }
266 Py_INCREF(self->args);
267 return self->args;
268}
269
270static int
271BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
272{
273 PyObject *seq;
274 if (val == NULL) {
275 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
276 return -1;
277 }
278 seq = PySequence_Tuple(val);
279 if (!seq) return -1;
Georg Brandlc7c51142006-05-29 09:46:51 +0000280 Py_CLEAR(self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000281 self->args = seq;
282 return 0;
283}
284
285static PyGetSetDef BaseException_getset[] = {
286 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
287 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
288 {NULL},
289};
290
291
292static PyTypeObject _PyExc_BaseException = {
293 PyObject_HEAD_INIT(NULL)
294 0, /*ob_size*/
295 EXC_MODULE_NAME "BaseException", /*tp_name*/
296 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
297 0, /*tp_itemsize*/
298 (destructor)BaseException_dealloc, /*tp_dealloc*/
299 0, /*tp_print*/
300 0, /*tp_getattr*/
301 0, /*tp_setattr*/
302 0, /* tp_compare; */
303 (reprfunc)BaseException_repr, /*tp_repr*/
304 0, /*tp_as_number*/
305 &BaseException_as_sequence, /*tp_as_sequence*/
306 0, /*tp_as_mapping*/
307 0, /*tp_hash */
308 0, /*tp_call*/
309 (reprfunc)BaseException_str, /*tp_str*/
310 PyObject_GenericGetAttr, /*tp_getattro*/
311 PyObject_GenericSetAttr, /*tp_setattro*/
312 0, /*tp_as_buffer*/
313 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
314 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
315 (traverseproc)BaseException_traverse, /* tp_traverse */
316 (inquiry)BaseException_clear, /* tp_clear */
317 0, /* tp_richcompare */
318 0, /* tp_weaklistoffset */
319 0, /* tp_iter */
320 0, /* tp_iternext */
321 BaseException_methods, /* tp_methods */
322 BaseException_members, /* tp_members */
323 BaseException_getset, /* tp_getset */
324 0, /* tp_base */
325 0, /* tp_dict */
326 0, /* tp_descr_get */
327 0, /* tp_descr_set */
328 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
329 (initproc)BaseException_init, /* tp_init */
330 0, /* tp_alloc */
331 BaseException_new, /* tp_new */
332};
333/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
334from the previous implmentation and also allowing Python objects to be used
335in the API */
336PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
337
Richard Jones2d555b32006-05-27 16:15:11 +0000338/* note these macros omit the last semicolon so the macro invocation may
339 * include it and not look strange.
340 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000341#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
342static PyTypeObject _PyExc_ ## EXCNAME = { \
343 PyObject_HEAD_INIT(NULL) \
344 0, \
345 EXC_MODULE_NAME # EXCNAME, \
346 sizeof(PyBaseExceptionObject), \
347 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
348 0, 0, 0, 0, 0, 0, 0, \
349 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
350 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
351 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
352 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
353 (initproc)BaseException_init, 0, BaseException_new,\
354}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000355PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000356
357#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
358static PyTypeObject _PyExc_ ## EXCNAME = { \
359 PyObject_HEAD_INIT(NULL) \
360 0, \
361 EXC_MODULE_NAME # EXCNAME, \
362 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000363 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000364 0, 0, 0, 0, 0, \
365 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000366 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
367 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000368 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000369 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000370}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000371PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000372
373#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
374static PyTypeObject _PyExc_ ## EXCNAME = { \
375 PyObject_HEAD_INIT(NULL) \
376 0, \
377 EXC_MODULE_NAME # EXCNAME, \
378 sizeof(Py ## EXCSTORE ## Object), 0, \
379 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
380 (reprfunc)EXCSTR, 0, 0, 0, \
381 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
382 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
383 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
384 EXCMEMBERS, 0, &_ ## EXCBASE, \
385 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000386 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000387}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000388PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000389
390
391/*
392 * Exception extends BaseException
393 */
394SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000395 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000396
397
398/*
399 * StandardError extends Exception
400 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000401SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000402 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000403 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000404
405
406/*
407 * TypeError extends StandardError
408 */
409SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000410 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000411
412
413/*
414 * StopIteration extends Exception
415 */
416SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000417 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000418
419
420/*
421 * GeneratorExit extends Exception
422 */
423SimpleExtendsException(PyExc_Exception, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000424 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000425
426
427/*
428 * SystemExit extends BaseException
429 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000430
431static int
432SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
433{
434 Py_ssize_t size = PyTuple_GET_SIZE(args);
435
436 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
437 return -1;
438
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000439 if (size == 0)
440 return 0;
441 Py_CLEAR(self->code);
Richard Jones7b9558d2006-05-27 12:29:24 +0000442 if (size == 1)
443 self->code = PyTuple_GET_ITEM(args, 0);
444 else if (size > 1)
445 self->code = args;
446 Py_INCREF(self->code);
447 return 0;
448}
449
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000450static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000451SystemExit_clear(PySystemExitObject *self)
452{
453 Py_CLEAR(self->code);
454 return BaseException_clear((PyBaseExceptionObject *)self);
455}
456
457static void
458SystemExit_dealloc(PySystemExitObject *self)
459{
Georg Brandlecab6232006-09-06 06:47:02 +0000460 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000461 SystemExit_clear(self);
462 self->ob_type->tp_free((PyObject *)self);
463}
464
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000465static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000466SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
467{
468 Py_VISIT(self->code);
469 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
470}
471
472static PyMemberDef SystemExit_members[] = {
473 {"message", T_OBJECT, offsetof(PySystemExitObject, message), 0,
474 PyDoc_STR("exception message")},
475 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
476 PyDoc_STR("exception code")},
477 {NULL} /* Sentinel */
478};
479
480ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
481 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000482 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000483
484/*
485 * KeyboardInterrupt extends BaseException
486 */
487SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000488 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000489
490
491/*
492 * ImportError extends StandardError
493 */
494SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000495 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000496
497
498/*
499 * EnvironmentError extends StandardError
500 */
501
Richard Jones7b9558d2006-05-27 12:29:24 +0000502/* Where a function has a single filename, such as open() or some
503 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
504 * called, giving a third argument which is the filename. But, so
505 * that old code using in-place unpacking doesn't break, e.g.:
506 *
507 * except IOError, (errno, strerror):
508 *
509 * we hack args so that it only contains two items. This also
510 * means we need our own __str__() which prints out the filename
511 * when it was supplied.
512 */
513static int
514EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
515 PyObject *kwds)
516{
517 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
518 PyObject *subslice = NULL;
519
520 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
521 return -1;
522
523 if (PyTuple_GET_SIZE(args) <= 1) {
524 return 0;
525 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000526
527 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000528 &myerrno, &strerror, &filename)) {
529 return -1;
530 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000531 Py_CLEAR(self->myerrno); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000532 self->myerrno = myerrno;
533 Py_INCREF(self->myerrno);
534
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000535 Py_CLEAR(self->strerror); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000536 self->strerror = strerror;
537 Py_INCREF(self->strerror);
538
539 /* self->filename will remain Py_None otherwise */
540 if (filename != NULL) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000541 Py_CLEAR(self->filename); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000542 self->filename = filename;
543 Py_INCREF(self->filename);
544
545 subslice = PyTuple_GetSlice(args, 0, 2);
546 if (!subslice)
547 return -1;
548
549 Py_DECREF(self->args); /* replacing args */
550 self->args = subslice;
551 }
552 return 0;
553}
554
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000555static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000556EnvironmentError_clear(PyEnvironmentErrorObject *self)
557{
558 Py_CLEAR(self->myerrno);
559 Py_CLEAR(self->strerror);
560 Py_CLEAR(self->filename);
561 return BaseException_clear((PyBaseExceptionObject *)self);
562}
563
564static void
565EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
566{
Georg Brandlecab6232006-09-06 06:47:02 +0000567 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000568 EnvironmentError_clear(self);
569 self->ob_type->tp_free((PyObject *)self);
570}
571
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000572static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000573EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
574 void *arg)
575{
576 Py_VISIT(self->myerrno);
577 Py_VISIT(self->strerror);
578 Py_VISIT(self->filename);
579 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
580}
581
582static PyObject *
583EnvironmentError_str(PyEnvironmentErrorObject *self)
584{
585 PyObject *rtnval = NULL;
586
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000587 if (self->filename) {
588 PyObject *fmt;
589 PyObject *repr;
590 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000591
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000592 fmt = PyString_FromString("[Errno %s] %s: %s");
593 if (!fmt)
594 return NULL;
595
596 repr = PyObject_Repr(self->filename);
597 if (!repr) {
598 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000599 return NULL;
600 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000601 tuple = PyTuple_New(3);
602 if (!tuple) {
603 Py_DECREF(repr);
604 Py_DECREF(fmt);
605 return NULL;
606 }
607
608 if (self->myerrno) {
609 Py_INCREF(self->myerrno);
610 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
611 }
612 else {
613 Py_INCREF(Py_None);
614 PyTuple_SET_ITEM(tuple, 0, Py_None);
615 }
616 if (self->strerror) {
617 Py_INCREF(self->strerror);
618 PyTuple_SET_ITEM(tuple, 1, self->strerror);
619 }
620 else {
621 Py_INCREF(Py_None);
622 PyTuple_SET_ITEM(tuple, 1, Py_None);
623 }
624
Richard Jones7b9558d2006-05-27 12:29:24 +0000625 PyTuple_SET_ITEM(tuple, 2, repr);
626
627 rtnval = PyString_Format(fmt, tuple);
628
629 Py_DECREF(fmt);
630 Py_DECREF(tuple);
631 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000632 else if (self->myerrno && self->strerror) {
633 PyObject *fmt;
634 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000635
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000636 fmt = PyString_FromString("[Errno %s] %s");
637 if (!fmt)
638 return NULL;
639
640 tuple = PyTuple_New(2);
641 if (!tuple) {
642 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000643 return NULL;
644 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000645
646 if (self->myerrno) {
647 Py_INCREF(self->myerrno);
648 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
649 }
650 else {
651 Py_INCREF(Py_None);
652 PyTuple_SET_ITEM(tuple, 0, Py_None);
653 }
654 if (self->strerror) {
655 Py_INCREF(self->strerror);
656 PyTuple_SET_ITEM(tuple, 1, self->strerror);
657 }
658 else {
659 Py_INCREF(Py_None);
660 PyTuple_SET_ITEM(tuple, 1, Py_None);
661 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000662
663 rtnval = PyString_Format(fmt, tuple);
664
665 Py_DECREF(fmt);
666 Py_DECREF(tuple);
667 }
668 else
669 rtnval = BaseException_str((PyBaseExceptionObject *)self);
670
671 return rtnval;
672}
673
674static PyMemberDef EnvironmentError_members[] = {
675 {"message", T_OBJECT, offsetof(PyEnvironmentErrorObject, message), 0,
676 PyDoc_STR("exception message")},
677 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000678 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000679 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000680 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000681 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000682 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000683 {NULL} /* Sentinel */
684};
685
686
687static PyObject *
688EnvironmentError_reduce(PyEnvironmentErrorObject *self)
689{
690 PyObject *args = self->args;
691 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000692
Richard Jones7b9558d2006-05-27 12:29:24 +0000693 /* self->args is only the first two real arguments if there was a
694 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000695 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000696 args = PyTuple_New(3);
697 if (!args) return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000698
699 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000700 Py_INCREF(tmp);
701 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000702
703 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000704 Py_INCREF(tmp);
705 PyTuple_SET_ITEM(args, 1, tmp);
706
707 Py_INCREF(self->filename);
708 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000709 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000710 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000711
712 if (self->dict)
713 res = PyTuple_Pack(3, self->ob_type, args, self->dict);
714 else
715 res = PyTuple_Pack(2, self->ob_type, args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000716 Py_DECREF(args);
717 return res;
718}
719
720
721static PyMethodDef EnvironmentError_methods[] = {
722 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
723 {NULL}
724};
725
726ComplexExtendsException(PyExc_StandardError, EnvironmentError,
727 EnvironmentError, EnvironmentError_dealloc,
728 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000729 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000730 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000731
732
733/*
734 * IOError extends EnvironmentError
735 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000736MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000737 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000738
739
740/*
741 * OSError extends EnvironmentError
742 */
743MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000744 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000745
746
747/*
748 * WindowsError extends OSError
749 */
750#ifdef MS_WINDOWS
751#include "errmap.h"
752
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000753static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000754WindowsError_clear(PyWindowsErrorObject *self)
755{
756 Py_CLEAR(self->myerrno);
757 Py_CLEAR(self->strerror);
758 Py_CLEAR(self->filename);
759 Py_CLEAR(self->winerror);
760 return BaseException_clear((PyBaseExceptionObject *)self);
761}
762
763static void
764WindowsError_dealloc(PyWindowsErrorObject *self)
765{
Georg Brandlecab6232006-09-06 06:47:02 +0000766 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000767 WindowsError_clear(self);
768 self->ob_type->tp_free((PyObject *)self);
769}
770
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000771static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000772WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
773{
774 Py_VISIT(self->myerrno);
775 Py_VISIT(self->strerror);
776 Py_VISIT(self->filename);
777 Py_VISIT(self->winerror);
778 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
779}
780
Richard Jones7b9558d2006-05-27 12:29:24 +0000781static int
782WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
783{
784 PyObject *o_errcode = NULL;
785 long errcode;
786 long posix_errno;
787
788 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
789 == -1)
790 return -1;
791
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000792 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000793 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000794
795 /* Set errno to the POSIX errno, and winerror to the Win32
796 error code. */
797 errcode = PyInt_AsLong(self->myerrno);
798 if (errcode == -1 && PyErr_Occurred())
799 return -1;
800 posix_errno = winerror_to_errno(errcode);
801
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000802 Py_CLEAR(self->winerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000803 self->winerror = self->myerrno;
804
805 o_errcode = PyInt_FromLong(posix_errno);
806 if (!o_errcode)
807 return -1;
808
809 self->myerrno = o_errcode;
810
811 return 0;
812}
813
814
815static PyObject *
816WindowsError_str(PyWindowsErrorObject *self)
817{
Richard Jones7b9558d2006-05-27 12:29:24 +0000818 PyObject *rtnval = NULL;
819
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000820 if (self->filename) {
821 PyObject *fmt;
822 PyObject *repr;
823 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000824
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000825 fmt = PyString_FromString("[Error %s] %s: %s");
826 if (!fmt)
827 return NULL;
828
829 repr = PyObject_Repr(self->filename);
830 if (!repr) {
831 Py_DECREF(fmt);
832 return NULL;
833 }
834 tuple = PyTuple_New(3);
835 if (!tuple) {
836 Py_DECREF(repr);
837 Py_DECREF(fmt);
838 return NULL;
839 }
840
841 if (self->myerrno) {
842 Py_INCREF(self->myerrno);
843 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
844 }
845 else {
846 Py_INCREF(Py_None);
847 PyTuple_SET_ITEM(tuple, 0, Py_None);
848 }
849 if (self->strerror) {
850 Py_INCREF(self->strerror);
851 PyTuple_SET_ITEM(tuple, 1, self->strerror);
852 }
853 else {
854 Py_INCREF(Py_None);
855 PyTuple_SET_ITEM(tuple, 1, Py_None);
856 }
857
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000858 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000859
860 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000861
862 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000863 Py_DECREF(tuple);
864 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000865 else if (self->myerrno && self->strerror) {
866 PyObject *fmt;
867 PyObject *tuple;
868
Richard Jones7b9558d2006-05-27 12:29:24 +0000869 fmt = PyString_FromString("[Error %s] %s");
870 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000871 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000872
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000873 tuple = PyTuple_New(2);
874 if (!tuple) {
875 Py_DECREF(fmt);
876 return NULL;
877 }
878
879 if (self->myerrno) {
880 Py_INCREF(self->myerrno);
881 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
882 }
883 else {
884 Py_INCREF(Py_None);
885 PyTuple_SET_ITEM(tuple, 0, Py_None);
886 }
887 if (self->strerror) {
888 Py_INCREF(self->strerror);
889 PyTuple_SET_ITEM(tuple, 1, self->strerror);
890 }
891 else {
892 Py_INCREF(Py_None);
893 PyTuple_SET_ITEM(tuple, 1, Py_None);
894 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000895
896 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000897
898 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000899 Py_DECREF(tuple);
900 }
901 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000902 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000903
Richard Jones7b9558d2006-05-27 12:29:24 +0000904 return rtnval;
905}
906
907static PyMemberDef WindowsError_members[] = {
908 {"message", T_OBJECT, offsetof(PyWindowsErrorObject, message), 0,
909 PyDoc_STR("exception message")},
910 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000911 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000912 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000913 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000914 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000915 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000916 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000917 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000918 {NULL} /* Sentinel */
919};
920
Richard Jones2d555b32006-05-27 16:15:11 +0000921ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
922 WindowsError_dealloc, 0, WindowsError_members,
923 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000924
925#endif /* MS_WINDOWS */
926
927
928/*
929 * VMSError extends OSError (I think)
930 */
931#ifdef __VMS
932MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +0000933 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000934#endif
935
936
937/*
938 * EOFError extends StandardError
939 */
940SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +0000941 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000942
943
944/*
945 * RuntimeError extends StandardError
946 */
947SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000948 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000949
950
951/*
952 * NotImplementedError extends RuntimeError
953 */
954SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +0000955 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000956
957/*
958 * NameError extends StandardError
959 */
960SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +0000961 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000962
963/*
964 * UnboundLocalError extends NameError
965 */
966SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +0000967 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000968
969/*
970 * AttributeError extends StandardError
971 */
972SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000973 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000974
975
976/*
977 * SyntaxError extends StandardError
978 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000979
980static int
981SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
982{
983 PyObject *info = NULL;
984 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
985
986 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
987 return -1;
988
989 if (lenargs >= 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000990 Py_CLEAR(self->msg);
Richard Jones7b9558d2006-05-27 12:29:24 +0000991 self->msg = PyTuple_GET_ITEM(args, 0);
992 Py_INCREF(self->msg);
993 }
994 if (lenargs == 2) {
995 info = PyTuple_GET_ITEM(args, 1);
996 info = PySequence_Tuple(info);
997 if (!info) return -1;
998
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000999 if (PyTuple_GET_SIZE(info) != 4) {
1000 /* not a very good error message, but it's what Python 2.4 gives */
1001 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
1002 Py_DECREF(info);
1003 return -1;
1004 }
1005
1006 Py_CLEAR(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001007 self->filename = PyTuple_GET_ITEM(info, 0);
1008 Py_INCREF(self->filename);
1009
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001010 Py_CLEAR(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001011 self->lineno = PyTuple_GET_ITEM(info, 1);
1012 Py_INCREF(self->lineno);
1013
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001014 Py_CLEAR(self->offset);
Richard Jones7b9558d2006-05-27 12:29:24 +00001015 self->offset = PyTuple_GET_ITEM(info, 2);
1016 Py_INCREF(self->offset);
1017
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001018 Py_CLEAR(self->text);
Richard Jones7b9558d2006-05-27 12:29:24 +00001019 self->text = PyTuple_GET_ITEM(info, 3);
1020 Py_INCREF(self->text);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001021
1022 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001023 }
1024 return 0;
1025}
1026
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001027static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001028SyntaxError_clear(PySyntaxErrorObject *self)
1029{
1030 Py_CLEAR(self->msg);
1031 Py_CLEAR(self->filename);
1032 Py_CLEAR(self->lineno);
1033 Py_CLEAR(self->offset);
1034 Py_CLEAR(self->text);
1035 Py_CLEAR(self->print_file_and_line);
1036 return BaseException_clear((PyBaseExceptionObject *)self);
1037}
1038
1039static void
1040SyntaxError_dealloc(PySyntaxErrorObject *self)
1041{
Georg Brandlecab6232006-09-06 06:47:02 +00001042 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001043 SyntaxError_clear(self);
1044 self->ob_type->tp_free((PyObject *)self);
1045}
1046
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001047static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001048SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1049{
1050 Py_VISIT(self->msg);
1051 Py_VISIT(self->filename);
1052 Py_VISIT(self->lineno);
1053 Py_VISIT(self->offset);
1054 Py_VISIT(self->text);
1055 Py_VISIT(self->print_file_and_line);
1056 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1057}
1058
1059/* This is called "my_basename" instead of just "basename" to avoid name
1060 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1061 defined, and Python does define that. */
1062static char *
1063my_basename(char *name)
1064{
1065 char *cp = name;
1066 char *result = name;
1067
1068 if (name == NULL)
1069 return "???";
1070 while (*cp != '\0') {
1071 if (*cp == SEP)
1072 result = cp + 1;
1073 ++cp;
1074 }
1075 return result;
1076}
1077
1078
1079static PyObject *
1080SyntaxError_str(PySyntaxErrorObject *self)
1081{
1082 PyObject *str;
1083 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001084 int have_filename = 0;
1085 int have_lineno = 0;
1086 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001087 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001088
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001089 if (self->msg)
1090 str = PyObject_Str(self->msg);
1091 else
1092 str = PyObject_Str(Py_None);
Georg Brandl43ab1002006-05-28 20:57:09 +00001093 if (!str) return NULL;
1094 /* Don't fiddle with non-string return (shouldn't happen anyway) */
1095 if (!PyString_Check(str)) return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001096
1097 /* XXX -- do all the additional formatting with filename and
1098 lineno here */
1099
Georg Brandl43ab1002006-05-28 20:57:09 +00001100 have_filename = (self->filename != NULL) &&
1101 PyString_Check(self->filename);
1102 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001103
Georg Brandl43ab1002006-05-28 20:57:09 +00001104 if (!have_filename && !have_lineno)
1105 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001106
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001107 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001108 if (have_filename)
1109 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001110
Georg Brandl43ab1002006-05-28 20:57:09 +00001111 buffer = PyMem_MALLOC(bufsize);
1112 if (buffer == NULL)
1113 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001114
Georg Brandl43ab1002006-05-28 20:57:09 +00001115 if (have_filename && have_lineno)
1116 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
1117 PyString_AS_STRING(str),
1118 my_basename(PyString_AS_STRING(self->filename)),
1119 PyInt_AsLong(self->lineno));
1120 else if (have_filename)
1121 PyOS_snprintf(buffer, bufsize, "%s (%s)",
1122 PyString_AS_STRING(str),
1123 my_basename(PyString_AS_STRING(self->filename)));
1124 else /* only have_lineno */
1125 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
1126 PyString_AS_STRING(str),
1127 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001128
Georg Brandl43ab1002006-05-28 20:57:09 +00001129 result = PyString_FromString(buffer);
1130 PyMem_FREE(buffer);
1131
1132 if (result == NULL)
1133 result = str;
1134 else
1135 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001136 return result;
1137}
1138
1139static PyMemberDef SyntaxError_members[] = {
1140 {"message", T_OBJECT, offsetof(PySyntaxErrorObject, message), 0,
1141 PyDoc_STR("exception message")},
1142 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1143 PyDoc_STR("exception msg")},
1144 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1145 PyDoc_STR("exception filename")},
1146 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1147 PyDoc_STR("exception lineno")},
1148 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1149 PyDoc_STR("exception offset")},
1150 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1151 PyDoc_STR("exception text")},
1152 {"print_file_and_line", T_OBJECT,
1153 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1154 PyDoc_STR("exception print_file_and_line")},
1155 {NULL} /* Sentinel */
1156};
1157
1158ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1159 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001160 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001161
1162
1163/*
1164 * IndentationError extends SyntaxError
1165 */
1166MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001167 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001168
1169
1170/*
1171 * TabError extends IndentationError
1172 */
1173MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001174 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001175
1176
1177/*
1178 * LookupError extends StandardError
1179 */
1180SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001181 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001182
1183
1184/*
1185 * IndexError extends LookupError
1186 */
1187SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001188 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001189
1190
1191/*
1192 * KeyError extends LookupError
1193 */
1194static PyObject *
1195KeyError_str(PyBaseExceptionObject *self)
1196{
1197 /* If args is a tuple of exactly one item, apply repr to args[0].
1198 This is done so that e.g. the exception raised by {}[''] prints
1199 KeyError: ''
1200 rather than the confusing
1201 KeyError
1202 alone. The downside is that if KeyError is raised with an explanatory
1203 string, that string will be displayed in quotes. Too bad.
1204 If args is anything else, use the default BaseException__str__().
1205 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001206 if (PyTuple_GET_SIZE(self->args) == 1) {
1207 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001208 }
1209 return BaseException_str(self);
1210}
1211
1212ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001213 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001214
1215
1216/*
1217 * ValueError extends StandardError
1218 */
1219SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001220 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001221
1222/*
1223 * UnicodeError extends ValueError
1224 */
1225
1226SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001227 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001228
1229#ifdef Py_USING_UNICODE
1230static int
1231get_int(PyObject *attr, Py_ssize_t *value, const char *name)
1232{
1233 if (!attr) {
1234 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1235 return -1;
1236 }
1237
1238 if (PyInt_Check(attr)) {
1239 *value = PyInt_AS_LONG(attr);
1240 } else if (PyLong_Check(attr)) {
1241 *value = _PyLong_AsSsize_t(attr);
1242 if (*value == -1 && PyErr_Occurred())
1243 return -1;
1244 } else {
1245 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
1246 return -1;
1247 }
1248 return 0;
1249}
1250
1251static int
1252set_ssize_t(PyObject **attr, Py_ssize_t value)
1253{
1254 PyObject *obj = PyInt_FromSsize_t(value);
1255 if (!obj)
1256 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001257 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001258 *attr = obj;
1259 return 0;
1260}
1261
1262static PyObject *
1263get_string(PyObject *attr, const char *name)
1264{
1265 if (!attr) {
1266 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1267 return NULL;
1268 }
1269
1270 if (!PyString_Check(attr)) {
1271 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1272 return NULL;
1273 }
1274 Py_INCREF(attr);
1275 return attr;
1276}
1277
1278
1279static int
1280set_string(PyObject **attr, const char *value)
1281{
1282 PyObject *obj = PyString_FromString(value);
1283 if (!obj)
1284 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001285 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001286 *attr = obj;
1287 return 0;
1288}
1289
1290
1291static PyObject *
1292get_unicode(PyObject *attr, const char *name)
1293{
1294 if (!attr) {
1295 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1296 return NULL;
1297 }
1298
1299 if (!PyUnicode_Check(attr)) {
1300 PyErr_Format(PyExc_TypeError,
1301 "%.200s attribute must be unicode", name);
1302 return NULL;
1303 }
1304 Py_INCREF(attr);
1305 return attr;
1306}
1307
1308PyObject *
1309PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1310{
1311 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1312}
1313
1314PyObject *
1315PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1316{
1317 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1318}
1319
1320PyObject *
1321PyUnicodeEncodeError_GetObject(PyObject *exc)
1322{
1323 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1324}
1325
1326PyObject *
1327PyUnicodeDecodeError_GetObject(PyObject *exc)
1328{
1329 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1330}
1331
1332PyObject *
1333PyUnicodeTranslateError_GetObject(PyObject *exc)
1334{
1335 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1336}
1337
1338int
1339PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1340{
1341 if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
1342 Py_ssize_t size;
1343 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1344 "object");
1345 if (!obj) return -1;
1346 size = PyUnicode_GET_SIZE(obj);
1347 if (*start<0)
1348 *start = 0; /*XXX check for values <0*/
1349 if (*start>=size)
1350 *start = size-1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001351 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001352 return 0;
1353 }
1354 return -1;
1355}
1356
1357
1358int
1359PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1360{
1361 if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
1362 Py_ssize_t size;
1363 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1364 "object");
1365 if (!obj) return -1;
1366 size = PyString_GET_SIZE(obj);
1367 if (*start<0)
1368 *start = 0;
1369 if (*start>=size)
1370 *start = size-1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001371 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001372 return 0;
1373 }
1374 return -1;
1375}
1376
1377
1378int
1379PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1380{
1381 return PyUnicodeEncodeError_GetStart(exc, start);
1382}
1383
1384
1385int
1386PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1387{
1388 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1389}
1390
1391
1392int
1393PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1394{
1395 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1396}
1397
1398
1399int
1400PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1401{
1402 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1403}
1404
1405
1406int
1407PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1408{
1409 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1410 Py_ssize_t size;
1411 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1412 "object");
1413 if (!obj) return -1;
1414 size = PyUnicode_GET_SIZE(obj);
1415 if (*end<1)
1416 *end = 1;
1417 if (*end>size)
1418 *end = size;
Georg Brandl43ab1002006-05-28 20:57:09 +00001419 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001420 return 0;
1421 }
1422 return -1;
1423}
1424
1425
1426int
1427PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1428{
1429 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1430 Py_ssize_t size;
1431 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1432 "object");
1433 if (!obj) return -1;
1434 size = PyString_GET_SIZE(obj);
1435 if (*end<1)
1436 *end = 1;
1437 if (*end>size)
1438 *end = size;
Georg Brandl43ab1002006-05-28 20:57:09 +00001439 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001440 return 0;
1441 }
1442 return -1;
1443}
1444
1445
1446int
1447PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1448{
1449 return PyUnicodeEncodeError_GetEnd(exc, start);
1450}
1451
1452
1453int
1454PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1455{
1456 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1457}
1458
1459
1460int
1461PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1462{
1463 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1464}
1465
1466
1467int
1468PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1469{
1470 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1471}
1472
1473PyObject *
1474PyUnicodeEncodeError_GetReason(PyObject *exc)
1475{
1476 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1477}
1478
1479
1480PyObject *
1481PyUnicodeDecodeError_GetReason(PyObject *exc)
1482{
1483 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1484}
1485
1486
1487PyObject *
1488PyUnicodeTranslateError_GetReason(PyObject *exc)
1489{
1490 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1491}
1492
1493
1494int
1495PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1496{
1497 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1498}
1499
1500
1501int
1502PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1503{
1504 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1505}
1506
1507
1508int
1509PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1510{
1511 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1512}
1513
1514
Richard Jones7b9558d2006-05-27 12:29:24 +00001515static int
1516UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1517 PyTypeObject *objecttype)
1518{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001519 Py_CLEAR(self->encoding);
1520 Py_CLEAR(self->object);
1521 Py_CLEAR(self->start);
1522 Py_CLEAR(self->end);
1523 Py_CLEAR(self->reason);
1524
Richard Jones7b9558d2006-05-27 12:29:24 +00001525 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1526 &PyString_Type, &self->encoding,
1527 objecttype, &self->object,
1528 &PyInt_Type, &self->start,
1529 &PyInt_Type, &self->end,
1530 &PyString_Type, &self->reason)) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001531 self->encoding = self->object = self->start = self->end =
Richard Jones7b9558d2006-05-27 12:29:24 +00001532 self->reason = NULL;
1533 return -1;
1534 }
1535
1536 Py_INCREF(self->encoding);
1537 Py_INCREF(self->object);
1538 Py_INCREF(self->start);
1539 Py_INCREF(self->end);
1540 Py_INCREF(self->reason);
1541
1542 return 0;
1543}
1544
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001545static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001546UnicodeError_clear(PyUnicodeErrorObject *self)
1547{
1548 Py_CLEAR(self->encoding);
1549 Py_CLEAR(self->object);
1550 Py_CLEAR(self->start);
1551 Py_CLEAR(self->end);
1552 Py_CLEAR(self->reason);
1553 return BaseException_clear((PyBaseExceptionObject *)self);
1554}
1555
1556static void
1557UnicodeError_dealloc(PyUnicodeErrorObject *self)
1558{
Georg Brandlecab6232006-09-06 06:47:02 +00001559 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001560 UnicodeError_clear(self);
1561 self->ob_type->tp_free((PyObject *)self);
1562}
1563
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001564static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001565UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1566{
1567 Py_VISIT(self->encoding);
1568 Py_VISIT(self->object);
1569 Py_VISIT(self->start);
1570 Py_VISIT(self->end);
1571 Py_VISIT(self->reason);
1572 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1573}
1574
1575static PyMemberDef UnicodeError_members[] = {
1576 {"message", T_OBJECT, offsetof(PyUnicodeErrorObject, message), 0,
1577 PyDoc_STR("exception message")},
1578 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1579 PyDoc_STR("exception encoding")},
1580 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1581 PyDoc_STR("exception object")},
1582 {"start", T_OBJECT, offsetof(PyUnicodeErrorObject, start), 0,
1583 PyDoc_STR("exception start")},
1584 {"end", T_OBJECT, offsetof(PyUnicodeErrorObject, end), 0,
1585 PyDoc_STR("exception end")},
1586 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1587 PyDoc_STR("exception reason")},
1588 {NULL} /* Sentinel */
1589};
1590
1591
1592/*
1593 * UnicodeEncodeError extends UnicodeError
1594 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001595
1596static int
1597UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1598{
1599 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1600 return -1;
1601 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1602 kwds, &PyUnicode_Type);
1603}
1604
1605static PyObject *
1606UnicodeEncodeError_str(PyObject *self)
1607{
1608 Py_ssize_t start;
1609 Py_ssize_t end;
1610
1611 if (PyUnicodeEncodeError_GetStart(self, &start))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001612 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001613
1614 if (PyUnicodeEncodeError_GetEnd(self, &end))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001615 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001616
1617 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001618 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1619 char badchar_str[20];
1620 if (badchar <= 0xff)
1621 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1622 else if (badchar <= 0xffff)
1623 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1624 else
1625 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1626 return PyString_FromFormat(
1627 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
1628 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1629 badchar_str,
1630 start,
1631 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1632 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001633 }
1634 return PyString_FromFormat(
1635 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
1636 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1637 start,
1638 (end-1),
1639 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1640 );
1641}
1642
1643static PyTypeObject _PyExc_UnicodeEncodeError = {
1644 PyObject_HEAD_INIT(NULL)
1645 0,
Georg Brandlecab6232006-09-06 06:47:02 +00001646 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001647 sizeof(PyUnicodeErrorObject), 0,
1648 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1649 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1650 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001651 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1652 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001653 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001654 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001655};
1656PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1657
1658PyObject *
1659PyUnicodeEncodeError_Create(
1660 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1661 Py_ssize_t start, Py_ssize_t end, const char *reason)
1662{
1663 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001664 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001665}
1666
1667
1668/*
1669 * UnicodeDecodeError extends UnicodeError
1670 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001671
1672static int
1673UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1674{
1675 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1676 return -1;
1677 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1678 kwds, &PyString_Type);
1679}
1680
1681static PyObject *
1682UnicodeDecodeError_str(PyObject *self)
1683{
Georg Brandl43ab1002006-05-28 20:57:09 +00001684 Py_ssize_t start = 0;
1685 Py_ssize_t end = 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001686
1687 if (PyUnicodeDecodeError_GetStart(self, &start))
1688 return NULL;
1689
1690 if (PyUnicodeDecodeError_GetEnd(self, &end))
1691 return NULL;
1692
1693 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001694 /* FromFormat does not support %02x, so format that separately */
1695 char byte[4];
1696 PyOS_snprintf(byte, sizeof(byte), "%02x",
1697 ((int)PyString_AS_STRING(((PyUnicodeErrorObject *)self)->object)[start])&0xff);
1698 return PyString_FromFormat(
1699 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
1700 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1701 byte,
1702 start,
1703 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1704 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001705 }
1706 return PyString_FromFormat(
1707 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
1708 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1709 start,
1710 (end-1),
1711 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1712 );
1713}
1714
1715static PyTypeObject _PyExc_UnicodeDecodeError = {
1716 PyObject_HEAD_INIT(NULL)
1717 0,
1718 EXC_MODULE_NAME "UnicodeDecodeError",
1719 sizeof(PyUnicodeErrorObject), 0,
1720 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1721 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1722 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001723 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1724 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001725 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001726 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001727};
1728PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1729
1730PyObject *
1731PyUnicodeDecodeError_Create(
1732 const char *encoding, const char *object, Py_ssize_t length,
1733 Py_ssize_t start, Py_ssize_t end, const char *reason)
1734{
1735 assert(length < INT_MAX);
1736 assert(start < INT_MAX);
1737 assert(end < INT_MAX);
1738 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001739 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001740}
1741
1742
1743/*
1744 * UnicodeTranslateError extends UnicodeError
1745 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001746
1747static int
1748UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1749 PyObject *kwds)
1750{
1751 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1752 return -1;
1753
1754 Py_CLEAR(self->object);
1755 Py_CLEAR(self->start);
1756 Py_CLEAR(self->end);
1757 Py_CLEAR(self->reason);
1758
1759 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1760 &PyUnicode_Type, &self->object,
1761 &PyInt_Type, &self->start,
1762 &PyInt_Type, &self->end,
1763 &PyString_Type, &self->reason)) {
1764 self->object = self->start = self->end = self->reason = NULL;
1765 return -1;
1766 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001767
Richard Jones7b9558d2006-05-27 12:29:24 +00001768 Py_INCREF(self->object);
1769 Py_INCREF(self->start);
1770 Py_INCREF(self->end);
1771 Py_INCREF(self->reason);
1772
1773 return 0;
1774}
1775
1776
1777static PyObject *
1778UnicodeTranslateError_str(PyObject *self)
1779{
1780 Py_ssize_t start;
1781 Py_ssize_t end;
1782
1783 if (PyUnicodeTranslateError_GetStart(self, &start))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001784 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001785
1786 if (PyUnicodeTranslateError_GetEnd(self, &end))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001787 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001788
1789 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001790 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1791 char badchar_str[20];
1792 if (badchar <= 0xff)
1793 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1794 else if (badchar <= 0xffff)
1795 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1796 else
1797 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1798 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001799 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001800 badchar_str,
1801 start,
1802 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1803 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001804 }
1805 return PyString_FromFormat(
1806 "can't translate characters in position %zd-%zd: %.400s",
1807 start,
1808 (end-1),
1809 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1810 );
1811}
1812
1813static PyTypeObject _PyExc_UnicodeTranslateError = {
1814 PyObject_HEAD_INIT(NULL)
1815 0,
1816 EXC_MODULE_NAME "UnicodeTranslateError",
1817 sizeof(PyUnicodeErrorObject), 0,
1818 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1819 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1820 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandlecab6232006-09-06 06:47:02 +00001821 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001822 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1823 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001824 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001825};
1826PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1827
1828PyObject *
1829PyUnicodeTranslateError_Create(
1830 const Py_UNICODE *object, Py_ssize_t length,
1831 Py_ssize_t start, Py_ssize_t end, const char *reason)
1832{
1833 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001834 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001835}
1836#endif
1837
1838
1839/*
1840 * AssertionError extends StandardError
1841 */
1842SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001843 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001844
1845
1846/*
1847 * ArithmeticError extends StandardError
1848 */
1849SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001850 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001851
1852
1853/*
1854 * FloatingPointError extends ArithmeticError
1855 */
1856SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001857 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001858
1859
1860/*
1861 * OverflowError extends ArithmeticError
1862 */
1863SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001864 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001865
1866
1867/*
1868 * ZeroDivisionError extends ArithmeticError
1869 */
1870SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001871 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001872
1873
1874/*
1875 * SystemError extends StandardError
1876 */
1877SimpleExtendsException(PyExc_StandardError, SystemError,
1878 "Internal error in the Python interpreter.\n"
1879 "\n"
1880 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001881 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001882
1883
1884/*
1885 * ReferenceError extends StandardError
1886 */
1887SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001888 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001889
1890
1891/*
1892 * MemoryError extends StandardError
1893 */
Richard Jones2d555b32006-05-27 16:15:11 +00001894SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001895
1896
1897/* Warning category docstrings */
1898
1899/*
1900 * Warning extends Exception
1901 */
1902SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001903 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001904
1905
1906/*
1907 * UserWarning extends Warning
1908 */
1909SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001910 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001911
1912
1913/*
1914 * DeprecationWarning extends Warning
1915 */
1916SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001917 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001918
1919
1920/*
1921 * PendingDeprecationWarning extends Warning
1922 */
1923SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1924 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001925 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001926
1927
1928/*
1929 * SyntaxWarning extends Warning
1930 */
1931SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001932 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001933
1934
1935/*
1936 * RuntimeWarning extends Warning
1937 */
1938SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001939 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001940
1941
1942/*
1943 * FutureWarning extends Warning
1944 */
1945SimpleExtendsException(PyExc_Warning, FutureWarning,
1946 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001947 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001948
1949
1950/*
1951 * ImportWarning extends Warning
1952 */
1953SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001954 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00001955
1956
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001957/*
1958 * UnicodeWarning extends Warning
1959 */
1960SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1961 "Base class for warnings about Unicode related problems, mostly\n"
1962 "related to conversion problems.");
1963
1964
Richard Jones7b9558d2006-05-27 12:29:24 +00001965/* Pre-computed MemoryError instance. Best to create this as early as
1966 * possible and not wait until a MemoryError is actually raised!
1967 */
1968PyObject *PyExc_MemoryErrorInst=NULL;
1969
1970/* module global functions */
1971static PyMethodDef functions[] = {
1972 /* Sentinel */
1973 {NULL, NULL}
1974};
1975
1976#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1977 Py_FatalError("exceptions bootstrapping error.");
1978
1979#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1980 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1981 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1982 Py_FatalError("Module dictionary insertion problem.");
1983
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00001984#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00001985/* crt variable checking in VisualStudio .NET 2005 */
1986#include <crtdbg.h>
1987
1988static int prevCrtReportMode;
1989static _invalid_parameter_handler prevCrtHandler;
1990
1991/* Invalid parameter handler. Sets a ValueError exception */
1992static void
1993InvalidParameterHandler(
1994 const wchar_t * expression,
1995 const wchar_t * function,
1996 const wchar_t * file,
1997 unsigned int line,
1998 uintptr_t pReserved)
1999{
2000 /* Do nothing, allow execution to continue. Usually this
2001 * means that the CRT will set errno to EINVAL
2002 */
2003}
2004#endif
2005
2006
Richard Jones7b9558d2006-05-27 12:29:24 +00002007PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00002008_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00002009{
2010 PyObject *m, *bltinmod, *bdict;
2011
2012 PRE_INIT(BaseException)
2013 PRE_INIT(Exception)
2014 PRE_INIT(StandardError)
2015 PRE_INIT(TypeError)
2016 PRE_INIT(StopIteration)
2017 PRE_INIT(GeneratorExit)
2018 PRE_INIT(SystemExit)
2019 PRE_INIT(KeyboardInterrupt)
2020 PRE_INIT(ImportError)
2021 PRE_INIT(EnvironmentError)
2022 PRE_INIT(IOError)
2023 PRE_INIT(OSError)
2024#ifdef MS_WINDOWS
2025 PRE_INIT(WindowsError)
2026#endif
2027#ifdef __VMS
2028 PRE_INIT(VMSError)
2029#endif
2030 PRE_INIT(EOFError)
2031 PRE_INIT(RuntimeError)
2032 PRE_INIT(NotImplementedError)
2033 PRE_INIT(NameError)
2034 PRE_INIT(UnboundLocalError)
2035 PRE_INIT(AttributeError)
2036 PRE_INIT(SyntaxError)
2037 PRE_INIT(IndentationError)
2038 PRE_INIT(TabError)
2039 PRE_INIT(LookupError)
2040 PRE_INIT(IndexError)
2041 PRE_INIT(KeyError)
2042 PRE_INIT(ValueError)
2043 PRE_INIT(UnicodeError)
2044#ifdef Py_USING_UNICODE
2045 PRE_INIT(UnicodeEncodeError)
2046 PRE_INIT(UnicodeDecodeError)
2047 PRE_INIT(UnicodeTranslateError)
2048#endif
2049 PRE_INIT(AssertionError)
2050 PRE_INIT(ArithmeticError)
2051 PRE_INIT(FloatingPointError)
2052 PRE_INIT(OverflowError)
2053 PRE_INIT(ZeroDivisionError)
2054 PRE_INIT(SystemError)
2055 PRE_INIT(ReferenceError)
2056 PRE_INIT(MemoryError)
2057 PRE_INIT(Warning)
2058 PRE_INIT(UserWarning)
2059 PRE_INIT(DeprecationWarning)
2060 PRE_INIT(PendingDeprecationWarning)
2061 PRE_INIT(SyntaxWarning)
2062 PRE_INIT(RuntimeWarning)
2063 PRE_INIT(FutureWarning)
2064 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002065 PRE_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002066
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002067 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2068 (PyObject *)NULL, PYTHON_API_VERSION);
Richard Jones7b9558d2006-05-27 12:29:24 +00002069 if (m == NULL) return;
2070
2071 bltinmod = PyImport_ImportModule("__builtin__");
2072 if (bltinmod == NULL)
2073 Py_FatalError("exceptions bootstrapping error.");
2074 bdict = PyModule_GetDict(bltinmod);
2075 if (bdict == NULL)
2076 Py_FatalError("exceptions bootstrapping error.");
2077
2078 POST_INIT(BaseException)
2079 POST_INIT(Exception)
2080 POST_INIT(StandardError)
2081 POST_INIT(TypeError)
2082 POST_INIT(StopIteration)
2083 POST_INIT(GeneratorExit)
2084 POST_INIT(SystemExit)
2085 POST_INIT(KeyboardInterrupt)
2086 POST_INIT(ImportError)
2087 POST_INIT(EnvironmentError)
2088 POST_INIT(IOError)
2089 POST_INIT(OSError)
2090#ifdef MS_WINDOWS
2091 POST_INIT(WindowsError)
2092#endif
2093#ifdef __VMS
2094 POST_INIT(VMSError)
2095#endif
2096 POST_INIT(EOFError)
2097 POST_INIT(RuntimeError)
2098 POST_INIT(NotImplementedError)
2099 POST_INIT(NameError)
2100 POST_INIT(UnboundLocalError)
2101 POST_INIT(AttributeError)
2102 POST_INIT(SyntaxError)
2103 POST_INIT(IndentationError)
2104 POST_INIT(TabError)
2105 POST_INIT(LookupError)
2106 POST_INIT(IndexError)
2107 POST_INIT(KeyError)
2108 POST_INIT(ValueError)
2109 POST_INIT(UnicodeError)
2110#ifdef Py_USING_UNICODE
2111 POST_INIT(UnicodeEncodeError)
2112 POST_INIT(UnicodeDecodeError)
2113 POST_INIT(UnicodeTranslateError)
2114#endif
2115 POST_INIT(AssertionError)
2116 POST_INIT(ArithmeticError)
2117 POST_INIT(FloatingPointError)
2118 POST_INIT(OverflowError)
2119 POST_INIT(ZeroDivisionError)
2120 POST_INIT(SystemError)
2121 POST_INIT(ReferenceError)
2122 POST_INIT(MemoryError)
2123 POST_INIT(Warning)
2124 POST_INIT(UserWarning)
2125 POST_INIT(DeprecationWarning)
2126 POST_INIT(PendingDeprecationWarning)
2127 POST_INIT(SyntaxWarning)
2128 POST_INIT(RuntimeWarning)
2129 POST_INIT(FutureWarning)
2130 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002131 POST_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002132
2133 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2134 if (!PyExc_MemoryErrorInst)
2135 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
2136
2137 Py_DECREF(bltinmod);
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002138
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002139#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002140 /* Set CRT argument error handler */
2141 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
2142 /* turn off assertions in debug mode */
2143 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
2144#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002145}
2146
2147void
2148_PyExc_Fini(void)
2149{
2150 Py_XDECREF(PyExc_MemoryErrorInst);
2151 PyExc_MemoryErrorInst = NULL;
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002152#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002153 /* reset CRT error handling */
2154 _set_invalid_parameter_handler(prevCrtHandler);
2155 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
2156#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002157}