blob: b73a3f0b7e10aa586be397d929184ce1f8edcb25 [file] [log] [blame]
Georg Brandl43ab1002006-05-28 20:57:09 +00001/*
2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
3 *
4 * Thanks go to Tim Peters and Michael Hudson for debugging.
5 */
6
Richard Jones7b9558d2006-05-27 12:29:24 +00007#define PY_SSIZE_T_CLEAN
8#include <Python.h>
9#include "structmember.h"
10#include "osdefs.h"
11
12#define MAKE_IT_NONE(x) (x) = Py_None; Py_INCREF(Py_None);
13#define EXC_MODULE_NAME "exceptions."
14
Richard Jonesc5b2a2e2006-05-27 16:07:28 +000015/* NOTE: If the exception class hierarchy changes, don't forget to update
16 * Lib/test/exception_hierarchy.txt
17 */
18
19PyDoc_STRVAR(exceptions_doc, "Python's standard exception class hierarchy.\n\
20\n\
21Exceptions found here are defined both in the exceptions module and the\n\
22built-in namespace. It is recommended that user-defined exceptions\n\
23inherit from Exception. See the documentation for the exception\n\
24inheritance hierarchy.\n\
25");
26
Richard Jones7b9558d2006-05-27 12:29:24 +000027/*
28 * BaseException
29 */
30static PyObject *
31BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
32{
33 PyBaseExceptionObject *self;
34
35 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
Georg Brandl1dfa8ac2007-04-21 07:22:57 +000036 if (!self)
37 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +000038 /* the dict is created on the fly in PyObject_GenericSetAttr */
39 self->message = self->dict = NULL;
40
41 self->args = PyTuple_New(0);
42 if (!self->args) {
43 Py_DECREF(self);
44 return NULL;
45 }
46
Michael W. Hudson22a80e72006-05-28 15:51:40 +000047 self->message = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +000048 if (!self->message) {
49 Py_DECREF(self);
50 return NULL;
51 }
52
53 return (PyObject *)self;
54}
55
56static int
57BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
58{
Georg Brandlb0432bc2006-05-30 08:17:00 +000059 if (!_PyArg_NoKeywords(self->ob_type->tp_name, kwds))
60 return -1;
61
Richard Jones7b9558d2006-05-27 12:29:24 +000062 Py_DECREF(self->args);
63 self->args = args;
64 Py_INCREF(self->args);
65
66 if (PyTuple_GET_SIZE(self->args) == 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +000067 Py_CLEAR(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000068 self->message = PyTuple_GET_ITEM(self->args, 0);
Michael W. Hudson22a80e72006-05-28 15:51:40 +000069 Py_INCREF(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000070 }
71 return 0;
72}
73
Michael W. Hudson96495ee2006-05-28 17:40:29 +000074static int
Richard Jones7b9558d2006-05-27 12:29:24 +000075BaseException_clear(PyBaseExceptionObject *self)
76{
77 Py_CLEAR(self->dict);
78 Py_CLEAR(self->args);
79 Py_CLEAR(self->message);
80 return 0;
81}
82
83static void
84BaseException_dealloc(PyBaseExceptionObject *self)
85{
Georg Brandlecab6232006-09-06 06:47:02 +000086 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +000087 BaseException_clear(self);
88 self->ob_type->tp_free((PyObject *)self);
89}
90
Michael W. Hudson96495ee2006-05-28 17:40:29 +000091static int
Richard Jones7b9558d2006-05-27 12:29:24 +000092BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
93{
Michael W. Hudson22a80e72006-05-28 15:51:40 +000094 Py_VISIT(self->dict);
Richard Jones7b9558d2006-05-27 12:29:24 +000095 Py_VISIT(self->args);
96 Py_VISIT(self->message);
97 return 0;
98}
99
100static PyObject *
101BaseException_str(PyBaseExceptionObject *self)
102{
103 PyObject *out;
104
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000105 switch (PyTuple_GET_SIZE(self->args)) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000106 case 0:
107 out = PyString_FromString("");
108 break;
109 case 1:
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000110 out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +0000111 break;
Richard Jones7b9558d2006-05-27 12:29:24 +0000112 default:
113 out = PyObject_Str(self->args);
114 break;
115 }
116
117 return out;
118}
119
120static PyObject *
121BaseException_repr(PyBaseExceptionObject *self)
122{
Richard Jones7b9558d2006-05-27 12:29:24 +0000123 PyObject *repr_suffix;
124 PyObject *repr;
125 char *name;
126 char *dot;
127
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000128 repr_suffix = PyObject_Repr(self->args);
129 if (!repr_suffix)
Richard Jones7b9558d2006-05-27 12:29:24 +0000130 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000131
132 name = (char *)self->ob_type->tp_name;
133 dot = strrchr(name, '.');
134 if (dot != NULL) name = dot+1;
135
136 repr = PyString_FromString(name);
137 if (!repr) {
138 Py_DECREF(repr_suffix);
139 return NULL;
140 }
141
142 PyString_ConcatAndDel(&repr, repr_suffix);
143 return repr;
144}
145
146/* Pickling support */
147static PyObject *
148BaseException_reduce(PyBaseExceptionObject *self)
149{
Georg Brandlddba4732006-05-30 07:04:55 +0000150 if (self->args && self->dict)
151 return PyTuple_Pack(3, self->ob_type, self->args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000152 else
Georg Brandlddba4732006-05-30 07:04:55 +0000153 return PyTuple_Pack(2, self->ob_type, self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000154}
155
Georg Brandl85ac8502006-06-01 06:39:19 +0000156/*
157 * Needed for backward compatibility, since exceptions used to store
158 * all their attributes in the __dict__. Code is taken from cPickle's
159 * load_build function.
160 */
161static PyObject *
162BaseException_setstate(PyObject *self, PyObject *state)
163{
164 PyObject *d_key, *d_value;
165 Py_ssize_t i = 0;
166
167 if (state != Py_None) {
168 if (!PyDict_Check(state)) {
169 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
170 return NULL;
171 }
172 while (PyDict_Next(state, &i, &d_key, &d_value)) {
173 if (PyObject_SetAttr(self, d_key, d_value) < 0)
174 return NULL;
175 }
176 }
177 Py_RETURN_NONE;
178}
Richard Jones7b9558d2006-05-27 12:29:24 +0000179
Richard Jones7b9558d2006-05-27 12:29:24 +0000180
181static PyMethodDef BaseException_methods[] = {
182 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Georg Brandl85ac8502006-06-01 06:39:19 +0000183 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Richard Jones7b9558d2006-05-27 12:29:24 +0000184 {NULL, NULL, 0, NULL},
185};
186
187
188
189static PyObject *
190BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
191{
192 return PySequence_GetItem(self->args, index);
193}
194
Brett Cannonc70e0032006-09-21 18:12:15 +0000195static PyObject *
196BaseException_getslice(PyBaseExceptionObject *self,
197 Py_ssize_t start, Py_ssize_t stop)
198{
199 return PySequence_GetSlice(self->args, start, stop);
200}
201
Richard Jones7b9558d2006-05-27 12:29:24 +0000202static PySequenceMethods BaseException_as_sequence = {
203 0, /* sq_length; */
204 0, /* sq_concat; */
205 0, /* sq_repeat; */
206 (ssizeargfunc)BaseException_getitem, /* sq_item; */
Brett Cannonc70e0032006-09-21 18:12:15 +0000207 (ssizessizeargfunc)BaseException_getslice, /* sq_slice; */
Richard Jones7b9558d2006-05-27 12:29:24 +0000208 0, /* sq_ass_item; */
209 0, /* sq_ass_slice; */
210 0, /* sq_contains; */
211 0, /* sq_inplace_concat; */
212 0 /* sq_inplace_repeat; */
213};
214
215static PyMemberDef BaseException_members[] = {
216 {"message", T_OBJECT, offsetof(PyBaseExceptionObject, message), 0,
217 PyDoc_STR("exception message")},
218 {NULL} /* Sentinel */
219};
220
221
222static PyObject *
223BaseException_get_dict(PyBaseExceptionObject *self)
224{
225 if (self->dict == NULL) {
226 self->dict = PyDict_New();
227 if (!self->dict)
228 return NULL;
229 }
230 Py_INCREF(self->dict);
231 return self->dict;
232}
233
234static int
235BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
236{
237 if (val == NULL) {
238 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
239 return -1;
240 }
241 if (!PyDict_Check(val)) {
242 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
243 return -1;
244 }
245 Py_CLEAR(self->dict);
246 Py_INCREF(val);
247 self->dict = val;
248 return 0;
249}
250
251static PyObject *
252BaseException_get_args(PyBaseExceptionObject *self)
253{
254 if (self->args == NULL) {
255 Py_INCREF(Py_None);
256 return Py_None;
257 }
258 Py_INCREF(self->args);
259 return self->args;
260}
261
262static int
263BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
264{
265 PyObject *seq;
266 if (val == NULL) {
267 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
268 return -1;
269 }
270 seq = PySequence_Tuple(val);
271 if (!seq) return -1;
Georg Brandlc7c51142006-05-29 09:46:51 +0000272 Py_CLEAR(self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000273 self->args = seq;
274 return 0;
275}
276
277static PyGetSetDef BaseException_getset[] = {
278 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
279 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
280 {NULL},
281};
282
283
284static PyTypeObject _PyExc_BaseException = {
285 PyObject_HEAD_INIT(NULL)
286 0, /*ob_size*/
287 EXC_MODULE_NAME "BaseException", /*tp_name*/
288 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
289 0, /*tp_itemsize*/
290 (destructor)BaseException_dealloc, /*tp_dealloc*/
291 0, /*tp_print*/
292 0, /*tp_getattr*/
293 0, /*tp_setattr*/
294 0, /* tp_compare; */
295 (reprfunc)BaseException_repr, /*tp_repr*/
296 0, /*tp_as_number*/
297 &BaseException_as_sequence, /*tp_as_sequence*/
298 0, /*tp_as_mapping*/
299 0, /*tp_hash */
300 0, /*tp_call*/
301 (reprfunc)BaseException_str, /*tp_str*/
302 PyObject_GenericGetAttr, /*tp_getattro*/
303 PyObject_GenericSetAttr, /*tp_setattro*/
304 0, /*tp_as_buffer*/
305 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
306 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
307 (traverseproc)BaseException_traverse, /* tp_traverse */
308 (inquiry)BaseException_clear, /* tp_clear */
309 0, /* tp_richcompare */
310 0, /* tp_weaklistoffset */
311 0, /* tp_iter */
312 0, /* tp_iternext */
313 BaseException_methods, /* tp_methods */
314 BaseException_members, /* tp_members */
315 BaseException_getset, /* tp_getset */
316 0, /* tp_base */
317 0, /* tp_dict */
318 0, /* tp_descr_get */
319 0, /* tp_descr_set */
320 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
321 (initproc)BaseException_init, /* tp_init */
322 0, /* tp_alloc */
323 BaseException_new, /* tp_new */
324};
325/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
326from the previous implmentation and also allowing Python objects to be used
327in the API */
328PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
329
Richard Jones2d555b32006-05-27 16:15:11 +0000330/* note these macros omit the last semicolon so the macro invocation may
331 * include it and not look strange.
332 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000333#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
334static PyTypeObject _PyExc_ ## EXCNAME = { \
335 PyObject_HEAD_INIT(NULL) \
336 0, \
337 EXC_MODULE_NAME # EXCNAME, \
338 sizeof(PyBaseExceptionObject), \
339 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
340 0, 0, 0, 0, 0, 0, 0, \
341 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
342 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
343 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
344 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
345 (initproc)BaseException_init, 0, BaseException_new,\
346}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000347PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000348
349#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
350static PyTypeObject _PyExc_ ## EXCNAME = { \
351 PyObject_HEAD_INIT(NULL) \
352 0, \
353 EXC_MODULE_NAME # EXCNAME, \
354 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000355 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000356 0, 0, 0, 0, 0, \
357 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000358 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
359 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000360 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000361 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000362}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000363PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000364
365#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
366static PyTypeObject _PyExc_ ## EXCNAME = { \
367 PyObject_HEAD_INIT(NULL) \
368 0, \
369 EXC_MODULE_NAME # EXCNAME, \
370 sizeof(Py ## EXCSTORE ## Object), 0, \
371 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
372 (reprfunc)EXCSTR, 0, 0, 0, \
373 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
374 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
375 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
376 EXCMEMBERS, 0, &_ ## EXCBASE, \
377 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000378 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000379}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000380PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000381
382
383/*
384 * Exception extends BaseException
385 */
386SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000387 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000388
389
390/*
391 * StandardError extends Exception
392 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000393SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000394 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000395 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000396
397
398/*
399 * TypeError extends StandardError
400 */
401SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000402 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000403
404
405/*
406 * StopIteration extends Exception
407 */
408SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000409 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000410
411
412/*
413 * GeneratorExit extends Exception
414 */
415SimpleExtendsException(PyExc_Exception, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000416 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000417
418
419/*
420 * SystemExit extends BaseException
421 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000422
423static int
424SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
425{
426 Py_ssize_t size = PyTuple_GET_SIZE(args);
427
428 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
429 return -1;
430
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000431 if (size == 0)
432 return 0;
433 Py_CLEAR(self->code);
Richard Jones7b9558d2006-05-27 12:29:24 +0000434 if (size == 1)
435 self->code = PyTuple_GET_ITEM(args, 0);
436 else if (size > 1)
437 self->code = args;
438 Py_INCREF(self->code);
439 return 0;
440}
441
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000442static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000443SystemExit_clear(PySystemExitObject *self)
444{
445 Py_CLEAR(self->code);
446 return BaseException_clear((PyBaseExceptionObject *)self);
447}
448
449static void
450SystemExit_dealloc(PySystemExitObject *self)
451{
Georg Brandlecab6232006-09-06 06:47:02 +0000452 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000453 SystemExit_clear(self);
454 self->ob_type->tp_free((PyObject *)self);
455}
456
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000457static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000458SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
459{
460 Py_VISIT(self->code);
461 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
462}
463
464static PyMemberDef SystemExit_members[] = {
465 {"message", T_OBJECT, offsetof(PySystemExitObject, message), 0,
466 PyDoc_STR("exception message")},
467 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
468 PyDoc_STR("exception code")},
469 {NULL} /* Sentinel */
470};
471
472ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
473 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000474 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000475
476/*
477 * KeyboardInterrupt extends BaseException
478 */
479SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000480 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000481
482
483/*
484 * ImportError extends StandardError
485 */
486SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000487 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000488
489
490/*
491 * EnvironmentError extends StandardError
492 */
493
Richard Jones7b9558d2006-05-27 12:29:24 +0000494/* Where a function has a single filename, such as open() or some
495 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
496 * called, giving a third argument which is the filename. But, so
497 * that old code using in-place unpacking doesn't break, e.g.:
498 *
499 * except IOError, (errno, strerror):
500 *
501 * we hack args so that it only contains two items. This also
502 * means we need our own __str__() which prints out the filename
503 * when it was supplied.
504 */
505static int
506EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
507 PyObject *kwds)
508{
509 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
510 PyObject *subslice = NULL;
511
512 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
513 return -1;
514
Georg Brandl506cc182006-09-30 09:03:45 +0000515 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000516 return 0;
517 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000518
519 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000520 &myerrno, &strerror, &filename)) {
521 return -1;
522 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000523 Py_CLEAR(self->myerrno); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000524 self->myerrno = myerrno;
525 Py_INCREF(self->myerrno);
526
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000527 Py_CLEAR(self->strerror); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000528 self->strerror = strerror;
529 Py_INCREF(self->strerror);
530
531 /* self->filename will remain Py_None otherwise */
532 if (filename != NULL) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000533 Py_CLEAR(self->filename); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000534 self->filename = filename;
535 Py_INCREF(self->filename);
536
537 subslice = PyTuple_GetSlice(args, 0, 2);
538 if (!subslice)
539 return -1;
540
541 Py_DECREF(self->args); /* replacing args */
542 self->args = subslice;
543 }
544 return 0;
545}
546
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000547static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000548EnvironmentError_clear(PyEnvironmentErrorObject *self)
549{
550 Py_CLEAR(self->myerrno);
551 Py_CLEAR(self->strerror);
552 Py_CLEAR(self->filename);
553 return BaseException_clear((PyBaseExceptionObject *)self);
554}
555
556static void
557EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
558{
Georg Brandlecab6232006-09-06 06:47:02 +0000559 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000560 EnvironmentError_clear(self);
561 self->ob_type->tp_free((PyObject *)self);
562}
563
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000564static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000565EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
566 void *arg)
567{
568 Py_VISIT(self->myerrno);
569 Py_VISIT(self->strerror);
570 Py_VISIT(self->filename);
571 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
572}
573
574static PyObject *
575EnvironmentError_str(PyEnvironmentErrorObject *self)
576{
577 PyObject *rtnval = NULL;
578
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000579 if (self->filename) {
580 PyObject *fmt;
581 PyObject *repr;
582 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000583
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000584 fmt = PyString_FromString("[Errno %s] %s: %s");
585 if (!fmt)
586 return NULL;
587
588 repr = PyObject_Repr(self->filename);
589 if (!repr) {
590 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000591 return NULL;
592 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000593 tuple = PyTuple_New(3);
594 if (!tuple) {
595 Py_DECREF(repr);
596 Py_DECREF(fmt);
597 return NULL;
598 }
599
600 if (self->myerrno) {
601 Py_INCREF(self->myerrno);
602 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
603 }
604 else {
605 Py_INCREF(Py_None);
606 PyTuple_SET_ITEM(tuple, 0, Py_None);
607 }
608 if (self->strerror) {
609 Py_INCREF(self->strerror);
610 PyTuple_SET_ITEM(tuple, 1, self->strerror);
611 }
612 else {
613 Py_INCREF(Py_None);
614 PyTuple_SET_ITEM(tuple, 1, Py_None);
615 }
616
Richard Jones7b9558d2006-05-27 12:29:24 +0000617 PyTuple_SET_ITEM(tuple, 2, repr);
618
619 rtnval = PyString_Format(fmt, tuple);
620
621 Py_DECREF(fmt);
622 Py_DECREF(tuple);
623 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000624 else if (self->myerrno && self->strerror) {
625 PyObject *fmt;
626 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000627
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000628 fmt = PyString_FromString("[Errno %s] %s");
629 if (!fmt)
630 return NULL;
631
632 tuple = PyTuple_New(2);
633 if (!tuple) {
634 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000635 return NULL;
636 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000637
638 if (self->myerrno) {
639 Py_INCREF(self->myerrno);
640 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
641 }
642 else {
643 Py_INCREF(Py_None);
644 PyTuple_SET_ITEM(tuple, 0, Py_None);
645 }
646 if (self->strerror) {
647 Py_INCREF(self->strerror);
648 PyTuple_SET_ITEM(tuple, 1, self->strerror);
649 }
650 else {
651 Py_INCREF(Py_None);
652 PyTuple_SET_ITEM(tuple, 1, Py_None);
653 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000654
655 rtnval = PyString_Format(fmt, tuple);
656
657 Py_DECREF(fmt);
658 Py_DECREF(tuple);
659 }
660 else
661 rtnval = BaseException_str((PyBaseExceptionObject *)self);
662
663 return rtnval;
664}
665
666static PyMemberDef EnvironmentError_members[] = {
667 {"message", T_OBJECT, offsetof(PyEnvironmentErrorObject, message), 0,
668 PyDoc_STR("exception message")},
669 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000670 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000671 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000672 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000673 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000674 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000675 {NULL} /* Sentinel */
676};
677
678
679static PyObject *
680EnvironmentError_reduce(PyEnvironmentErrorObject *self)
681{
682 PyObject *args = self->args;
683 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000684
Richard Jones7b9558d2006-05-27 12:29:24 +0000685 /* self->args is only the first two real arguments if there was a
686 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000687 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000688 args = PyTuple_New(3);
689 if (!args) return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000690
691 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000692 Py_INCREF(tmp);
693 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000694
695 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000696 Py_INCREF(tmp);
697 PyTuple_SET_ITEM(args, 1, tmp);
698
699 Py_INCREF(self->filename);
700 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000701 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000702 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000703
704 if (self->dict)
705 res = PyTuple_Pack(3, self->ob_type, args, self->dict);
706 else
707 res = PyTuple_Pack(2, self->ob_type, args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000708 Py_DECREF(args);
709 return res;
710}
711
712
713static PyMethodDef EnvironmentError_methods[] = {
714 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
715 {NULL}
716};
717
718ComplexExtendsException(PyExc_StandardError, EnvironmentError,
719 EnvironmentError, EnvironmentError_dealloc,
720 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000721 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000722 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000723
724
725/*
726 * IOError extends EnvironmentError
727 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000728MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000729 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000730
731
732/*
733 * OSError extends EnvironmentError
734 */
735MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000736 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000737
738
739/*
740 * WindowsError extends OSError
741 */
742#ifdef MS_WINDOWS
743#include "errmap.h"
744
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000745static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000746WindowsError_clear(PyWindowsErrorObject *self)
747{
748 Py_CLEAR(self->myerrno);
749 Py_CLEAR(self->strerror);
750 Py_CLEAR(self->filename);
751 Py_CLEAR(self->winerror);
752 return BaseException_clear((PyBaseExceptionObject *)self);
753}
754
755static void
756WindowsError_dealloc(PyWindowsErrorObject *self)
757{
Georg Brandlecab6232006-09-06 06:47:02 +0000758 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000759 WindowsError_clear(self);
760 self->ob_type->tp_free((PyObject *)self);
761}
762
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000763static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000764WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
765{
766 Py_VISIT(self->myerrno);
767 Py_VISIT(self->strerror);
768 Py_VISIT(self->filename);
769 Py_VISIT(self->winerror);
770 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
771}
772
Richard Jones7b9558d2006-05-27 12:29:24 +0000773static int
774WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
775{
776 PyObject *o_errcode = NULL;
777 long errcode;
778 long posix_errno;
779
780 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
781 == -1)
782 return -1;
783
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000784 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000785 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000786
787 /* Set errno to the POSIX errno, and winerror to the Win32
788 error code. */
789 errcode = PyInt_AsLong(self->myerrno);
790 if (errcode == -1 && PyErr_Occurred())
791 return -1;
792 posix_errno = winerror_to_errno(errcode);
793
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000794 Py_CLEAR(self->winerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000795 self->winerror = self->myerrno;
796
797 o_errcode = PyInt_FromLong(posix_errno);
798 if (!o_errcode)
799 return -1;
800
801 self->myerrno = o_errcode;
802
803 return 0;
804}
805
806
807static PyObject *
808WindowsError_str(PyWindowsErrorObject *self)
809{
Richard Jones7b9558d2006-05-27 12:29:24 +0000810 PyObject *rtnval = NULL;
811
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000812 if (self->filename) {
813 PyObject *fmt;
814 PyObject *repr;
815 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000816
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000817 fmt = PyString_FromString("[Error %s] %s: %s");
818 if (!fmt)
819 return NULL;
820
821 repr = PyObject_Repr(self->filename);
822 if (!repr) {
823 Py_DECREF(fmt);
824 return NULL;
825 }
826 tuple = PyTuple_New(3);
827 if (!tuple) {
828 Py_DECREF(repr);
829 Py_DECREF(fmt);
830 return NULL;
831 }
832
Thomas Hellera0a50fe2006-10-27 18:47:29 +0000833 if (self->winerror) {
834 Py_INCREF(self->winerror);
835 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000836 }
837 else {
838 Py_INCREF(Py_None);
839 PyTuple_SET_ITEM(tuple, 0, Py_None);
840 }
841 if (self->strerror) {
842 Py_INCREF(self->strerror);
843 PyTuple_SET_ITEM(tuple, 1, self->strerror);
844 }
845 else {
846 Py_INCREF(Py_None);
847 PyTuple_SET_ITEM(tuple, 1, Py_None);
848 }
849
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000850 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000851
852 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000853
854 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000855 Py_DECREF(tuple);
856 }
Thomas Hellera0a50fe2006-10-27 18:47:29 +0000857 else if (self->winerror && self->strerror) {
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000858 PyObject *fmt;
859 PyObject *tuple;
860
Richard Jones7b9558d2006-05-27 12:29:24 +0000861 fmt = PyString_FromString("[Error %s] %s");
862 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000863 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000864
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000865 tuple = PyTuple_New(2);
866 if (!tuple) {
867 Py_DECREF(fmt);
868 return NULL;
869 }
870
Thomas Hellera0a50fe2006-10-27 18:47:29 +0000871 if (self->winerror) {
872 Py_INCREF(self->winerror);
873 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000874 }
875 else {
876 Py_INCREF(Py_None);
877 PyTuple_SET_ITEM(tuple, 0, Py_None);
878 }
879 if (self->strerror) {
880 Py_INCREF(self->strerror);
881 PyTuple_SET_ITEM(tuple, 1, self->strerror);
882 }
883 else {
884 Py_INCREF(Py_None);
885 PyTuple_SET_ITEM(tuple, 1, Py_None);
886 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000887
888 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000889
890 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000891 Py_DECREF(tuple);
892 }
893 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000894 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000895
Richard Jones7b9558d2006-05-27 12:29:24 +0000896 return rtnval;
897}
898
899static PyMemberDef WindowsError_members[] = {
900 {"message", T_OBJECT, offsetof(PyWindowsErrorObject, message), 0,
901 PyDoc_STR("exception message")},
902 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000903 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000904 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000905 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000906 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000907 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000908 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000909 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000910 {NULL} /* Sentinel */
911};
912
Richard Jones2d555b32006-05-27 16:15:11 +0000913ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
914 WindowsError_dealloc, 0, WindowsError_members,
915 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000916
917#endif /* MS_WINDOWS */
918
919
920/*
921 * VMSError extends OSError (I think)
922 */
923#ifdef __VMS
924MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +0000925 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000926#endif
927
928
929/*
930 * EOFError extends StandardError
931 */
932SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +0000933 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000934
935
936/*
937 * RuntimeError extends StandardError
938 */
939SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000940 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000941
942
943/*
944 * NotImplementedError extends RuntimeError
945 */
946SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +0000947 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000948
949/*
950 * NameError extends StandardError
951 */
952SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +0000953 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000954
955/*
956 * UnboundLocalError extends NameError
957 */
958SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +0000959 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000960
961/*
962 * AttributeError extends StandardError
963 */
964SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000965 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000966
967
968/*
969 * SyntaxError extends StandardError
970 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000971
972static int
973SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
974{
975 PyObject *info = NULL;
976 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
977
978 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
979 return -1;
980
981 if (lenargs >= 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000982 Py_CLEAR(self->msg);
Richard Jones7b9558d2006-05-27 12:29:24 +0000983 self->msg = PyTuple_GET_ITEM(args, 0);
984 Py_INCREF(self->msg);
985 }
986 if (lenargs == 2) {
987 info = PyTuple_GET_ITEM(args, 1);
988 info = PySequence_Tuple(info);
989 if (!info) return -1;
990
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000991 if (PyTuple_GET_SIZE(info) != 4) {
992 /* not a very good error message, but it's what Python 2.4 gives */
993 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
994 Py_DECREF(info);
995 return -1;
996 }
997
998 Py_CLEAR(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +0000999 self->filename = PyTuple_GET_ITEM(info, 0);
1000 Py_INCREF(self->filename);
1001
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001002 Py_CLEAR(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001003 self->lineno = PyTuple_GET_ITEM(info, 1);
1004 Py_INCREF(self->lineno);
1005
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001006 Py_CLEAR(self->offset);
Richard Jones7b9558d2006-05-27 12:29:24 +00001007 self->offset = PyTuple_GET_ITEM(info, 2);
1008 Py_INCREF(self->offset);
1009
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001010 Py_CLEAR(self->text);
Richard Jones7b9558d2006-05-27 12:29:24 +00001011 self->text = PyTuple_GET_ITEM(info, 3);
1012 Py_INCREF(self->text);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001013
1014 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001015 }
1016 return 0;
1017}
1018
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001019static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001020SyntaxError_clear(PySyntaxErrorObject *self)
1021{
1022 Py_CLEAR(self->msg);
1023 Py_CLEAR(self->filename);
1024 Py_CLEAR(self->lineno);
1025 Py_CLEAR(self->offset);
1026 Py_CLEAR(self->text);
1027 Py_CLEAR(self->print_file_and_line);
1028 return BaseException_clear((PyBaseExceptionObject *)self);
1029}
1030
1031static void
1032SyntaxError_dealloc(PySyntaxErrorObject *self)
1033{
Georg Brandlecab6232006-09-06 06:47:02 +00001034 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001035 SyntaxError_clear(self);
1036 self->ob_type->tp_free((PyObject *)self);
1037}
1038
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001039static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001040SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1041{
1042 Py_VISIT(self->msg);
1043 Py_VISIT(self->filename);
1044 Py_VISIT(self->lineno);
1045 Py_VISIT(self->offset);
1046 Py_VISIT(self->text);
1047 Py_VISIT(self->print_file_and_line);
1048 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1049}
1050
1051/* This is called "my_basename" instead of just "basename" to avoid name
1052 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1053 defined, and Python does define that. */
1054static char *
1055my_basename(char *name)
1056{
1057 char *cp = name;
1058 char *result = name;
1059
1060 if (name == NULL)
1061 return "???";
1062 while (*cp != '\0') {
1063 if (*cp == SEP)
1064 result = cp + 1;
1065 ++cp;
1066 }
1067 return result;
1068}
1069
1070
1071static PyObject *
1072SyntaxError_str(PySyntaxErrorObject *self)
1073{
1074 PyObject *str;
1075 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001076 int have_filename = 0;
1077 int have_lineno = 0;
1078 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001079 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001080
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001081 if (self->msg)
1082 str = PyObject_Str(self->msg);
1083 else
1084 str = PyObject_Str(Py_None);
Georg Brandl43ab1002006-05-28 20:57:09 +00001085 if (!str) return NULL;
1086 /* Don't fiddle with non-string return (shouldn't happen anyway) */
1087 if (!PyString_Check(str)) return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001088
1089 /* XXX -- do all the additional formatting with filename and
1090 lineno here */
1091
Georg Brandl43ab1002006-05-28 20:57:09 +00001092 have_filename = (self->filename != NULL) &&
1093 PyString_Check(self->filename);
1094 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001095
Georg Brandl43ab1002006-05-28 20:57:09 +00001096 if (!have_filename && !have_lineno)
1097 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001098
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001099 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001100 if (have_filename)
1101 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001102
Georg Brandl43ab1002006-05-28 20:57:09 +00001103 buffer = PyMem_MALLOC(bufsize);
1104 if (buffer == NULL)
1105 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001106
Georg Brandl43ab1002006-05-28 20:57:09 +00001107 if (have_filename && have_lineno)
1108 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
1109 PyString_AS_STRING(str),
1110 my_basename(PyString_AS_STRING(self->filename)),
1111 PyInt_AsLong(self->lineno));
1112 else if (have_filename)
1113 PyOS_snprintf(buffer, bufsize, "%s (%s)",
1114 PyString_AS_STRING(str),
1115 my_basename(PyString_AS_STRING(self->filename)));
1116 else /* only have_lineno */
1117 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
1118 PyString_AS_STRING(str),
1119 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001120
Georg Brandl43ab1002006-05-28 20:57:09 +00001121 result = PyString_FromString(buffer);
1122 PyMem_FREE(buffer);
1123
1124 if (result == NULL)
1125 result = str;
1126 else
1127 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001128 return result;
1129}
1130
1131static PyMemberDef SyntaxError_members[] = {
1132 {"message", T_OBJECT, offsetof(PySyntaxErrorObject, message), 0,
1133 PyDoc_STR("exception message")},
1134 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1135 PyDoc_STR("exception msg")},
1136 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1137 PyDoc_STR("exception filename")},
1138 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1139 PyDoc_STR("exception lineno")},
1140 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1141 PyDoc_STR("exception offset")},
1142 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1143 PyDoc_STR("exception text")},
1144 {"print_file_and_line", T_OBJECT,
1145 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1146 PyDoc_STR("exception print_file_and_line")},
1147 {NULL} /* Sentinel */
1148};
1149
1150ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1151 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001152 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001153
1154
1155/*
1156 * IndentationError extends SyntaxError
1157 */
1158MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001159 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001160
1161
1162/*
1163 * TabError extends IndentationError
1164 */
1165MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001166 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001167
1168
1169/*
1170 * LookupError extends StandardError
1171 */
1172SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001173 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001174
1175
1176/*
1177 * IndexError extends LookupError
1178 */
1179SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001180 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001181
1182
1183/*
1184 * KeyError extends LookupError
1185 */
1186static PyObject *
1187KeyError_str(PyBaseExceptionObject *self)
1188{
1189 /* If args is a tuple of exactly one item, apply repr to args[0].
1190 This is done so that e.g. the exception raised by {}[''] prints
1191 KeyError: ''
1192 rather than the confusing
1193 KeyError
1194 alone. The downside is that if KeyError is raised with an explanatory
1195 string, that string will be displayed in quotes. Too bad.
1196 If args is anything else, use the default BaseException__str__().
1197 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001198 if (PyTuple_GET_SIZE(self->args) == 1) {
1199 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001200 }
1201 return BaseException_str(self);
1202}
1203
1204ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001205 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001206
1207
1208/*
1209 * ValueError extends StandardError
1210 */
1211SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001212 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001213
1214/*
1215 * UnicodeError extends ValueError
1216 */
1217
1218SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001219 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001220
1221#ifdef Py_USING_UNICODE
1222static int
1223get_int(PyObject *attr, Py_ssize_t *value, const char *name)
1224{
1225 if (!attr) {
1226 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1227 return -1;
1228 }
1229
1230 if (PyInt_Check(attr)) {
1231 *value = PyInt_AS_LONG(attr);
1232 } else if (PyLong_Check(attr)) {
1233 *value = _PyLong_AsSsize_t(attr);
1234 if (*value == -1 && PyErr_Occurred())
1235 return -1;
1236 } else {
1237 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
1238 return -1;
1239 }
1240 return 0;
1241}
1242
1243static int
1244set_ssize_t(PyObject **attr, Py_ssize_t value)
1245{
1246 PyObject *obj = PyInt_FromSsize_t(value);
1247 if (!obj)
1248 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001249 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001250 *attr = obj;
1251 return 0;
1252}
1253
1254static PyObject *
1255get_string(PyObject *attr, const char *name)
1256{
1257 if (!attr) {
1258 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1259 return NULL;
1260 }
1261
1262 if (!PyString_Check(attr)) {
1263 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1264 return NULL;
1265 }
1266 Py_INCREF(attr);
1267 return attr;
1268}
1269
1270
1271static int
1272set_string(PyObject **attr, const char *value)
1273{
1274 PyObject *obj = PyString_FromString(value);
1275 if (!obj)
1276 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001277 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001278 *attr = obj;
1279 return 0;
1280}
1281
1282
1283static PyObject *
1284get_unicode(PyObject *attr, const char *name)
1285{
1286 if (!attr) {
1287 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1288 return NULL;
1289 }
1290
1291 if (!PyUnicode_Check(attr)) {
1292 PyErr_Format(PyExc_TypeError,
1293 "%.200s attribute must be unicode", name);
1294 return NULL;
1295 }
1296 Py_INCREF(attr);
1297 return attr;
1298}
1299
1300PyObject *
1301PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1302{
1303 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1304}
1305
1306PyObject *
1307PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1308{
1309 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1310}
1311
1312PyObject *
1313PyUnicodeEncodeError_GetObject(PyObject *exc)
1314{
1315 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1316}
1317
1318PyObject *
1319PyUnicodeDecodeError_GetObject(PyObject *exc)
1320{
1321 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1322}
1323
1324PyObject *
1325PyUnicodeTranslateError_GetObject(PyObject *exc)
1326{
1327 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1328}
1329
1330int
1331PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1332{
1333 if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
1334 Py_ssize_t size;
1335 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1336 "object");
1337 if (!obj) return -1;
1338 size = PyUnicode_GET_SIZE(obj);
1339 if (*start<0)
1340 *start = 0; /*XXX check for values <0*/
1341 if (*start>=size)
1342 *start = size-1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001343 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001344 return 0;
1345 }
1346 return -1;
1347}
1348
1349
1350int
1351PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1352{
1353 if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
1354 Py_ssize_t size;
1355 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1356 "object");
1357 if (!obj) return -1;
1358 size = PyString_GET_SIZE(obj);
1359 if (*start<0)
1360 *start = 0;
1361 if (*start>=size)
1362 *start = size-1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001363 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001364 return 0;
1365 }
1366 return -1;
1367}
1368
1369
1370int
1371PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1372{
1373 return PyUnicodeEncodeError_GetStart(exc, start);
1374}
1375
1376
1377int
1378PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1379{
1380 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1381}
1382
1383
1384int
1385PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1386{
1387 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1388}
1389
1390
1391int
1392PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1393{
1394 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1395}
1396
1397
1398int
1399PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1400{
1401 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1402 Py_ssize_t size;
1403 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1404 "object");
1405 if (!obj) return -1;
1406 size = PyUnicode_GET_SIZE(obj);
1407 if (*end<1)
1408 *end = 1;
1409 if (*end>size)
1410 *end = size;
Georg Brandl43ab1002006-05-28 20:57:09 +00001411 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001412 return 0;
1413 }
1414 return -1;
1415}
1416
1417
1418int
1419PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1420{
1421 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1422 Py_ssize_t size;
1423 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1424 "object");
1425 if (!obj) return -1;
1426 size = PyString_GET_SIZE(obj);
1427 if (*end<1)
1428 *end = 1;
1429 if (*end>size)
1430 *end = size;
Georg Brandl43ab1002006-05-28 20:57:09 +00001431 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001432 return 0;
1433 }
1434 return -1;
1435}
1436
1437
1438int
1439PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1440{
1441 return PyUnicodeEncodeError_GetEnd(exc, start);
1442}
1443
1444
1445int
1446PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1447{
1448 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1449}
1450
1451
1452int
1453PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1454{
1455 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1456}
1457
1458
1459int
1460PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1461{
1462 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1463}
1464
1465PyObject *
1466PyUnicodeEncodeError_GetReason(PyObject *exc)
1467{
1468 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1469}
1470
1471
1472PyObject *
1473PyUnicodeDecodeError_GetReason(PyObject *exc)
1474{
1475 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1476}
1477
1478
1479PyObject *
1480PyUnicodeTranslateError_GetReason(PyObject *exc)
1481{
1482 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1483}
1484
1485
1486int
1487PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1488{
1489 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1490}
1491
1492
1493int
1494PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1495{
1496 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1497}
1498
1499
1500int
1501PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1502{
1503 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1504}
1505
1506
Richard Jones7b9558d2006-05-27 12:29:24 +00001507static int
1508UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1509 PyTypeObject *objecttype)
1510{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001511 Py_CLEAR(self->encoding);
1512 Py_CLEAR(self->object);
1513 Py_CLEAR(self->start);
1514 Py_CLEAR(self->end);
1515 Py_CLEAR(self->reason);
1516
Richard Jones7b9558d2006-05-27 12:29:24 +00001517 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1518 &PyString_Type, &self->encoding,
1519 objecttype, &self->object,
1520 &PyInt_Type, &self->start,
1521 &PyInt_Type, &self->end,
1522 &PyString_Type, &self->reason)) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001523 self->encoding = self->object = self->start = self->end =
Richard Jones7b9558d2006-05-27 12:29:24 +00001524 self->reason = NULL;
1525 return -1;
1526 }
1527
1528 Py_INCREF(self->encoding);
1529 Py_INCREF(self->object);
1530 Py_INCREF(self->start);
1531 Py_INCREF(self->end);
1532 Py_INCREF(self->reason);
1533
1534 return 0;
1535}
1536
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001537static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001538UnicodeError_clear(PyUnicodeErrorObject *self)
1539{
1540 Py_CLEAR(self->encoding);
1541 Py_CLEAR(self->object);
1542 Py_CLEAR(self->start);
1543 Py_CLEAR(self->end);
1544 Py_CLEAR(self->reason);
1545 return BaseException_clear((PyBaseExceptionObject *)self);
1546}
1547
1548static void
1549UnicodeError_dealloc(PyUnicodeErrorObject *self)
1550{
Georg Brandlecab6232006-09-06 06:47:02 +00001551 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001552 UnicodeError_clear(self);
1553 self->ob_type->tp_free((PyObject *)self);
1554}
1555
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001556static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001557UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1558{
1559 Py_VISIT(self->encoding);
1560 Py_VISIT(self->object);
1561 Py_VISIT(self->start);
1562 Py_VISIT(self->end);
1563 Py_VISIT(self->reason);
1564 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1565}
1566
1567static PyMemberDef UnicodeError_members[] = {
1568 {"message", T_OBJECT, offsetof(PyUnicodeErrorObject, message), 0,
1569 PyDoc_STR("exception message")},
1570 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1571 PyDoc_STR("exception encoding")},
1572 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1573 PyDoc_STR("exception object")},
1574 {"start", T_OBJECT, offsetof(PyUnicodeErrorObject, start), 0,
1575 PyDoc_STR("exception start")},
1576 {"end", T_OBJECT, offsetof(PyUnicodeErrorObject, end), 0,
1577 PyDoc_STR("exception end")},
1578 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1579 PyDoc_STR("exception reason")},
1580 {NULL} /* Sentinel */
1581};
1582
1583
1584/*
1585 * UnicodeEncodeError extends UnicodeError
1586 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001587
1588static int
1589UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1590{
1591 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1592 return -1;
1593 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1594 kwds, &PyUnicode_Type);
1595}
1596
1597static PyObject *
1598UnicodeEncodeError_str(PyObject *self)
1599{
1600 Py_ssize_t start;
1601 Py_ssize_t end;
1602
1603 if (PyUnicodeEncodeError_GetStart(self, &start))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001604 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001605
1606 if (PyUnicodeEncodeError_GetEnd(self, &end))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001607 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001608
1609 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001610 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1611 char badchar_str[20];
1612 if (badchar <= 0xff)
1613 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1614 else if (badchar <= 0xffff)
1615 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1616 else
1617 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1618 return PyString_FromFormat(
1619 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
1620 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1621 badchar_str,
1622 start,
1623 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1624 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001625 }
1626 return PyString_FromFormat(
1627 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
1628 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1629 start,
1630 (end-1),
1631 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1632 );
1633}
1634
1635static PyTypeObject _PyExc_UnicodeEncodeError = {
1636 PyObject_HEAD_INIT(NULL)
1637 0,
Georg Brandlecab6232006-09-06 06:47:02 +00001638 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001639 sizeof(PyUnicodeErrorObject), 0,
1640 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1641 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1642 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001643 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1644 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001645 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001646 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001647};
1648PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1649
1650PyObject *
1651PyUnicodeEncodeError_Create(
1652 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1653 Py_ssize_t start, Py_ssize_t end, const char *reason)
1654{
1655 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001656 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001657}
1658
1659
1660/*
1661 * UnicodeDecodeError extends UnicodeError
1662 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001663
1664static int
1665UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1666{
1667 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1668 return -1;
1669 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1670 kwds, &PyString_Type);
1671}
1672
1673static PyObject *
1674UnicodeDecodeError_str(PyObject *self)
1675{
Georg Brandl43ab1002006-05-28 20:57:09 +00001676 Py_ssize_t start = 0;
1677 Py_ssize_t end = 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001678
1679 if (PyUnicodeDecodeError_GetStart(self, &start))
1680 return NULL;
1681
1682 if (PyUnicodeDecodeError_GetEnd(self, &end))
1683 return NULL;
1684
1685 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001686 /* FromFormat does not support %02x, so format that separately */
1687 char byte[4];
1688 PyOS_snprintf(byte, sizeof(byte), "%02x",
1689 ((int)PyString_AS_STRING(((PyUnicodeErrorObject *)self)->object)[start])&0xff);
1690 return PyString_FromFormat(
1691 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
1692 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1693 byte,
1694 start,
1695 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1696 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001697 }
1698 return PyString_FromFormat(
1699 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
1700 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1701 start,
1702 (end-1),
1703 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1704 );
1705}
1706
1707static PyTypeObject _PyExc_UnicodeDecodeError = {
1708 PyObject_HEAD_INIT(NULL)
1709 0,
1710 EXC_MODULE_NAME "UnicodeDecodeError",
1711 sizeof(PyUnicodeErrorObject), 0,
1712 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1713 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1714 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001715 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1716 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001717 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001718 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001719};
1720PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1721
1722PyObject *
1723PyUnicodeDecodeError_Create(
1724 const char *encoding, const char *object, Py_ssize_t length,
1725 Py_ssize_t start, Py_ssize_t end, const char *reason)
1726{
1727 assert(length < INT_MAX);
1728 assert(start < INT_MAX);
1729 assert(end < INT_MAX);
1730 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001731 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001732}
1733
1734
1735/*
1736 * UnicodeTranslateError extends UnicodeError
1737 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001738
1739static int
1740UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1741 PyObject *kwds)
1742{
1743 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1744 return -1;
1745
1746 Py_CLEAR(self->object);
1747 Py_CLEAR(self->start);
1748 Py_CLEAR(self->end);
1749 Py_CLEAR(self->reason);
1750
1751 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1752 &PyUnicode_Type, &self->object,
1753 &PyInt_Type, &self->start,
1754 &PyInt_Type, &self->end,
1755 &PyString_Type, &self->reason)) {
1756 self->object = self->start = self->end = self->reason = NULL;
1757 return -1;
1758 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001759
Richard Jones7b9558d2006-05-27 12:29:24 +00001760 Py_INCREF(self->object);
1761 Py_INCREF(self->start);
1762 Py_INCREF(self->end);
1763 Py_INCREF(self->reason);
1764
1765 return 0;
1766}
1767
1768
1769static PyObject *
1770UnicodeTranslateError_str(PyObject *self)
1771{
1772 Py_ssize_t start;
1773 Py_ssize_t end;
1774
1775 if (PyUnicodeTranslateError_GetStart(self, &start))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001776 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001777
1778 if (PyUnicodeTranslateError_GetEnd(self, &end))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001779 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001780
1781 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001782 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1783 char badchar_str[20];
1784 if (badchar <= 0xff)
1785 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1786 else if (badchar <= 0xffff)
1787 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1788 else
1789 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1790 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001791 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001792 badchar_str,
1793 start,
1794 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1795 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001796 }
1797 return PyString_FromFormat(
1798 "can't translate characters in position %zd-%zd: %.400s",
1799 start,
1800 (end-1),
1801 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1802 );
1803}
1804
1805static PyTypeObject _PyExc_UnicodeTranslateError = {
1806 PyObject_HEAD_INIT(NULL)
1807 0,
1808 EXC_MODULE_NAME "UnicodeTranslateError",
1809 sizeof(PyUnicodeErrorObject), 0,
1810 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1811 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1812 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandlecab6232006-09-06 06:47:02 +00001813 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001814 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1815 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001816 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001817};
1818PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1819
1820PyObject *
1821PyUnicodeTranslateError_Create(
1822 const Py_UNICODE *object, Py_ssize_t length,
1823 Py_ssize_t start, Py_ssize_t end, const char *reason)
1824{
1825 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001826 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001827}
1828#endif
1829
1830
1831/*
1832 * AssertionError extends StandardError
1833 */
1834SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001835 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001836
1837
1838/*
1839 * ArithmeticError extends StandardError
1840 */
1841SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001842 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001843
1844
1845/*
1846 * FloatingPointError extends ArithmeticError
1847 */
1848SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001849 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001850
1851
1852/*
1853 * OverflowError extends ArithmeticError
1854 */
1855SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001856 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001857
1858
1859/*
1860 * ZeroDivisionError extends ArithmeticError
1861 */
1862SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001863 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001864
1865
1866/*
1867 * SystemError extends StandardError
1868 */
1869SimpleExtendsException(PyExc_StandardError, SystemError,
1870 "Internal error in the Python interpreter.\n"
1871 "\n"
1872 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001873 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001874
1875
1876/*
1877 * ReferenceError extends StandardError
1878 */
1879SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001880 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001881
1882
1883/*
1884 * MemoryError extends StandardError
1885 */
Richard Jones2d555b32006-05-27 16:15:11 +00001886SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001887
1888
1889/* Warning category docstrings */
1890
1891/*
1892 * Warning extends Exception
1893 */
1894SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001895 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001896
1897
1898/*
1899 * UserWarning extends Warning
1900 */
1901SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001902 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001903
1904
1905/*
1906 * DeprecationWarning extends Warning
1907 */
1908SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001909 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001910
1911
1912/*
1913 * PendingDeprecationWarning extends Warning
1914 */
1915SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1916 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001917 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001918
1919
1920/*
1921 * SyntaxWarning extends Warning
1922 */
1923SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001924 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001925
1926
1927/*
1928 * RuntimeWarning extends Warning
1929 */
1930SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001931 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001932
1933
1934/*
1935 * FutureWarning extends Warning
1936 */
1937SimpleExtendsException(PyExc_Warning, FutureWarning,
1938 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001939 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001940
1941
1942/*
1943 * ImportWarning extends Warning
1944 */
1945SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001946 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00001947
1948
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001949/*
1950 * UnicodeWarning extends Warning
1951 */
1952SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1953 "Base class for warnings about Unicode related problems, mostly\n"
1954 "related to conversion problems.");
1955
1956
Richard Jones7b9558d2006-05-27 12:29:24 +00001957/* Pre-computed MemoryError instance. Best to create this as early as
1958 * possible and not wait until a MemoryError is actually raised!
1959 */
1960PyObject *PyExc_MemoryErrorInst=NULL;
1961
1962/* module global functions */
1963static PyMethodDef functions[] = {
1964 /* Sentinel */
1965 {NULL, NULL}
1966};
1967
1968#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1969 Py_FatalError("exceptions bootstrapping error.");
1970
1971#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1972 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1973 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1974 Py_FatalError("Module dictionary insertion problem.");
1975
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00001976#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00001977/* crt variable checking in VisualStudio .NET 2005 */
1978#include <crtdbg.h>
1979
1980static int prevCrtReportMode;
1981static _invalid_parameter_handler prevCrtHandler;
1982
1983/* Invalid parameter handler. Sets a ValueError exception */
1984static void
1985InvalidParameterHandler(
1986 const wchar_t * expression,
1987 const wchar_t * function,
1988 const wchar_t * file,
1989 unsigned int line,
1990 uintptr_t pReserved)
1991{
1992 /* Do nothing, allow execution to continue. Usually this
1993 * means that the CRT will set errno to EINVAL
1994 */
1995}
1996#endif
1997
1998
Richard Jones7b9558d2006-05-27 12:29:24 +00001999PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00002000_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00002001{
2002 PyObject *m, *bltinmod, *bdict;
2003
2004 PRE_INIT(BaseException)
2005 PRE_INIT(Exception)
2006 PRE_INIT(StandardError)
2007 PRE_INIT(TypeError)
2008 PRE_INIT(StopIteration)
2009 PRE_INIT(GeneratorExit)
2010 PRE_INIT(SystemExit)
2011 PRE_INIT(KeyboardInterrupt)
2012 PRE_INIT(ImportError)
2013 PRE_INIT(EnvironmentError)
2014 PRE_INIT(IOError)
2015 PRE_INIT(OSError)
2016#ifdef MS_WINDOWS
2017 PRE_INIT(WindowsError)
2018#endif
2019#ifdef __VMS
2020 PRE_INIT(VMSError)
2021#endif
2022 PRE_INIT(EOFError)
2023 PRE_INIT(RuntimeError)
2024 PRE_INIT(NotImplementedError)
2025 PRE_INIT(NameError)
2026 PRE_INIT(UnboundLocalError)
2027 PRE_INIT(AttributeError)
2028 PRE_INIT(SyntaxError)
2029 PRE_INIT(IndentationError)
2030 PRE_INIT(TabError)
2031 PRE_INIT(LookupError)
2032 PRE_INIT(IndexError)
2033 PRE_INIT(KeyError)
2034 PRE_INIT(ValueError)
2035 PRE_INIT(UnicodeError)
2036#ifdef Py_USING_UNICODE
2037 PRE_INIT(UnicodeEncodeError)
2038 PRE_INIT(UnicodeDecodeError)
2039 PRE_INIT(UnicodeTranslateError)
2040#endif
2041 PRE_INIT(AssertionError)
2042 PRE_INIT(ArithmeticError)
2043 PRE_INIT(FloatingPointError)
2044 PRE_INIT(OverflowError)
2045 PRE_INIT(ZeroDivisionError)
2046 PRE_INIT(SystemError)
2047 PRE_INIT(ReferenceError)
2048 PRE_INIT(MemoryError)
2049 PRE_INIT(Warning)
2050 PRE_INIT(UserWarning)
2051 PRE_INIT(DeprecationWarning)
2052 PRE_INIT(PendingDeprecationWarning)
2053 PRE_INIT(SyntaxWarning)
2054 PRE_INIT(RuntimeWarning)
2055 PRE_INIT(FutureWarning)
2056 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002057 PRE_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002058
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002059 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2060 (PyObject *)NULL, PYTHON_API_VERSION);
Richard Jones7b9558d2006-05-27 12:29:24 +00002061 if (m == NULL) return;
2062
2063 bltinmod = PyImport_ImportModule("__builtin__");
2064 if (bltinmod == NULL)
2065 Py_FatalError("exceptions bootstrapping error.");
2066 bdict = PyModule_GetDict(bltinmod);
2067 if (bdict == NULL)
2068 Py_FatalError("exceptions bootstrapping error.");
2069
2070 POST_INIT(BaseException)
2071 POST_INIT(Exception)
2072 POST_INIT(StandardError)
2073 POST_INIT(TypeError)
2074 POST_INIT(StopIteration)
2075 POST_INIT(GeneratorExit)
2076 POST_INIT(SystemExit)
2077 POST_INIT(KeyboardInterrupt)
2078 POST_INIT(ImportError)
2079 POST_INIT(EnvironmentError)
2080 POST_INIT(IOError)
2081 POST_INIT(OSError)
2082#ifdef MS_WINDOWS
2083 POST_INIT(WindowsError)
2084#endif
2085#ifdef __VMS
2086 POST_INIT(VMSError)
2087#endif
2088 POST_INIT(EOFError)
2089 POST_INIT(RuntimeError)
2090 POST_INIT(NotImplementedError)
2091 POST_INIT(NameError)
2092 POST_INIT(UnboundLocalError)
2093 POST_INIT(AttributeError)
2094 POST_INIT(SyntaxError)
2095 POST_INIT(IndentationError)
2096 POST_INIT(TabError)
2097 POST_INIT(LookupError)
2098 POST_INIT(IndexError)
2099 POST_INIT(KeyError)
2100 POST_INIT(ValueError)
2101 POST_INIT(UnicodeError)
2102#ifdef Py_USING_UNICODE
2103 POST_INIT(UnicodeEncodeError)
2104 POST_INIT(UnicodeDecodeError)
2105 POST_INIT(UnicodeTranslateError)
2106#endif
2107 POST_INIT(AssertionError)
2108 POST_INIT(ArithmeticError)
2109 POST_INIT(FloatingPointError)
2110 POST_INIT(OverflowError)
2111 POST_INIT(ZeroDivisionError)
2112 POST_INIT(SystemError)
2113 POST_INIT(ReferenceError)
2114 POST_INIT(MemoryError)
2115 POST_INIT(Warning)
2116 POST_INIT(UserWarning)
2117 POST_INIT(DeprecationWarning)
2118 POST_INIT(PendingDeprecationWarning)
2119 POST_INIT(SyntaxWarning)
2120 POST_INIT(RuntimeWarning)
2121 POST_INIT(FutureWarning)
2122 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002123 POST_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002124
2125 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2126 if (!PyExc_MemoryErrorInst)
2127 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
2128
2129 Py_DECREF(bltinmod);
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002130
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002131#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002132 /* Set CRT argument error handler */
2133 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
2134 /* turn off assertions in debug mode */
2135 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
2136#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002137}
2138
2139void
2140_PyExc_Fini(void)
2141{
2142 Py_XDECREF(PyExc_MemoryErrorInst);
2143 PyExc_MemoryErrorInst = NULL;
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002144#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002145 /* reset CRT error handling */
2146 _set_invalid_parameter_handler(prevCrtHandler);
2147 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
2148#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002149}