blob: 65419deba5fde4ad247a844247c1b34711d029b4 [file] [log] [blame]
Georg Brandl43ab1002006-05-28 20:57:09 +00001/*
2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
3 *
4 * Thanks go to Tim Peters and Michael Hudson for debugging.
5 */
6
Richard Jones7b9558d2006-05-27 12:29:24 +00007#define PY_SSIZE_T_CLEAN
8#include <Python.h>
9#include "structmember.h"
10#include "osdefs.h"
11
12#define MAKE_IT_NONE(x) (x) = Py_None; Py_INCREF(Py_None);
13#define EXC_MODULE_NAME "exceptions."
14
Richard Jonesc5b2a2e2006-05-27 16:07:28 +000015/* NOTE: If the exception class hierarchy changes, don't forget to update
16 * Lib/test/exception_hierarchy.txt
17 */
18
19PyDoc_STRVAR(exceptions_doc, "Python's standard exception class hierarchy.\n\
20\n\
21Exceptions found here are defined both in the exceptions module and the\n\
22built-in namespace. It is recommended that user-defined exceptions\n\
23inherit from Exception. See the documentation for the exception\n\
24inheritance hierarchy.\n\
25");
26
Richard Jones7b9558d2006-05-27 12:29:24 +000027/*
28 * BaseException
29 */
30static PyObject *
31BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
32{
33 PyBaseExceptionObject *self;
34
35 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
Georg Brandlc02e1312007-04-11 17:16:24 +000036 if (!self)
37 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +000038 /* the dict is created on the fly in PyObject_GenericSetAttr */
39 self->message = self->dict = NULL;
40
41 self->args = PyTuple_New(0);
42 if (!self->args) {
43 Py_DECREF(self);
44 return NULL;
45 }
46
Michael W. Hudson22a80e72006-05-28 15:51:40 +000047 self->message = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +000048 if (!self->message) {
49 Py_DECREF(self);
50 return NULL;
51 }
52
53 return (PyObject *)self;
54}
55
56static int
57BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
58{
Georg Brandlb0432bc2006-05-30 08:17:00 +000059 if (!_PyArg_NoKeywords(self->ob_type->tp_name, kwds))
60 return -1;
61
Richard Jones7b9558d2006-05-27 12:29:24 +000062 Py_DECREF(self->args);
63 self->args = args;
64 Py_INCREF(self->args);
65
66 if (PyTuple_GET_SIZE(self->args) == 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +000067 Py_CLEAR(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000068 self->message = PyTuple_GET_ITEM(self->args, 0);
Michael W. Hudson22a80e72006-05-28 15:51:40 +000069 Py_INCREF(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000070 }
71 return 0;
72}
73
Michael W. Hudson96495ee2006-05-28 17:40:29 +000074static int
Richard Jones7b9558d2006-05-27 12:29:24 +000075BaseException_clear(PyBaseExceptionObject *self)
76{
77 Py_CLEAR(self->dict);
78 Py_CLEAR(self->args);
79 Py_CLEAR(self->message);
80 return 0;
81}
82
83static void
84BaseException_dealloc(PyBaseExceptionObject *self)
85{
Georg Brandl38f62372006-09-06 06:50:05 +000086 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +000087 BaseException_clear(self);
88 self->ob_type->tp_free((PyObject *)self);
89}
90
Michael W. Hudson96495ee2006-05-28 17:40:29 +000091static int
Richard Jones7b9558d2006-05-27 12:29:24 +000092BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
93{
Michael W. Hudson22a80e72006-05-28 15:51:40 +000094 Py_VISIT(self->dict);
Richard Jones7b9558d2006-05-27 12:29:24 +000095 Py_VISIT(self->args);
96 Py_VISIT(self->message);
97 return 0;
98}
99
100static PyObject *
101BaseException_str(PyBaseExceptionObject *self)
102{
103 PyObject *out;
104
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000105 switch (PyTuple_GET_SIZE(self->args)) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000106 case 0:
107 out = PyString_FromString("");
108 break;
109 case 1:
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000110 out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +0000111 break;
Richard Jones7b9558d2006-05-27 12:29:24 +0000112 default:
113 out = PyObject_Str(self->args);
114 break;
115 }
116
117 return out;
118}
119
120static PyObject *
121BaseException_repr(PyBaseExceptionObject *self)
122{
Richard Jones7b9558d2006-05-27 12:29:24 +0000123 PyObject *repr_suffix;
124 PyObject *repr;
125 char *name;
126 char *dot;
127
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000128 repr_suffix = PyObject_Repr(self->args);
129 if (!repr_suffix)
Richard Jones7b9558d2006-05-27 12:29:24 +0000130 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000131
132 name = (char *)self->ob_type->tp_name;
133 dot = strrchr(name, '.');
134 if (dot != NULL) name = dot+1;
135
136 repr = PyString_FromString(name);
137 if (!repr) {
138 Py_DECREF(repr_suffix);
139 return NULL;
140 }
141
142 PyString_ConcatAndDel(&repr, repr_suffix);
143 return repr;
144}
145
146/* Pickling support */
147static PyObject *
148BaseException_reduce(PyBaseExceptionObject *self)
149{
Georg Brandlddba4732006-05-30 07:04:55 +0000150 if (self->args && self->dict)
151 return PyTuple_Pack(3, self->ob_type, self->args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000152 else
Georg Brandlddba4732006-05-30 07:04:55 +0000153 return PyTuple_Pack(2, self->ob_type, self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000154}
155
Georg Brandl85ac8502006-06-01 06:39:19 +0000156/*
157 * Needed for backward compatibility, since exceptions used to store
158 * all their attributes in the __dict__. Code is taken from cPickle's
159 * load_build function.
160 */
161static PyObject *
162BaseException_setstate(PyObject *self, PyObject *state)
163{
164 PyObject *d_key, *d_value;
165 Py_ssize_t i = 0;
166
167 if (state != Py_None) {
168 if (!PyDict_Check(state)) {
169 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
170 return NULL;
171 }
172 while (PyDict_Next(state, &i, &d_key, &d_value)) {
173 if (PyObject_SetAttr(self, d_key, d_value) < 0)
174 return NULL;
175 }
176 }
177 Py_RETURN_NONE;
178}
Richard Jones7b9558d2006-05-27 12:29:24 +0000179
Richard Jones7b9558d2006-05-27 12:29:24 +0000180
181static PyMethodDef BaseException_methods[] = {
182 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Georg Brandl85ac8502006-06-01 06:39:19 +0000183 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Richard Jones7b9558d2006-05-27 12:29:24 +0000184 {NULL, NULL, 0, NULL},
185};
186
187
188
189static PyObject *
190BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
191{
192 return PySequence_GetItem(self->args, index);
193}
194
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000195static PyObject *
196BaseException_getslice(PyBaseExceptionObject *self,
197 Py_ssize_t start, Py_ssize_t stop)
198{
199 return PySequence_GetSlice(self->args, start, stop);
200}
201
Richard Jones7b9558d2006-05-27 12:29:24 +0000202static PySequenceMethods BaseException_as_sequence = {
203 0, /* sq_length; */
204 0, /* sq_concat; */
205 0, /* sq_repeat; */
206 (ssizeargfunc)BaseException_getitem, /* sq_item; */
Brett Cannonf6aa86e2006-09-20 18:43:13 +0000207 (ssizessizeargfunc)BaseException_getslice, /* sq_slice; */
Richard Jones7b9558d2006-05-27 12:29:24 +0000208 0, /* sq_ass_item; */
209 0, /* sq_ass_slice; */
210 0, /* sq_contains; */
211 0, /* sq_inplace_concat; */
212 0 /* sq_inplace_repeat; */
213};
214
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*/
Neal Norwitzee3a1b52007-02-25 19:44:48 +0000305 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
306 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Richard Jones7b9558d2006-05-27 12:29:24 +0000307 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
308 (traverseproc)BaseException_traverse, /* tp_traverse */
309 (inquiry)BaseException_clear, /* tp_clear */
310 0, /* tp_richcompare */
311 0, /* tp_weaklistoffset */
312 0, /* tp_iter */
313 0, /* tp_iternext */
314 BaseException_methods, /* tp_methods */
315 BaseException_members, /* tp_members */
316 BaseException_getset, /* tp_getset */
317 0, /* tp_base */
318 0, /* tp_dict */
319 0, /* tp_descr_get */
320 0, /* tp_descr_set */
321 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
322 (initproc)BaseException_init, /* tp_init */
323 0, /* tp_alloc */
324 BaseException_new, /* tp_new */
325};
326/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
327from the previous implmentation and also allowing Python objects to be used
328in the API */
329PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
330
Richard Jones2d555b32006-05-27 16:15:11 +0000331/* note these macros omit the last semicolon so the macro invocation may
332 * include it and not look strange.
333 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000334#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
335static PyTypeObject _PyExc_ ## EXCNAME = { \
336 PyObject_HEAD_INIT(NULL) \
337 0, \
338 EXC_MODULE_NAME # EXCNAME, \
339 sizeof(PyBaseExceptionObject), \
340 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
341 0, 0, 0, 0, 0, 0, 0, \
342 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
343 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
344 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
345 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
346 (initproc)BaseException_init, 0, BaseException_new,\
347}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000348PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000349
350#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
351static PyTypeObject _PyExc_ ## EXCNAME = { \
352 PyObject_HEAD_INIT(NULL) \
353 0, \
354 EXC_MODULE_NAME # EXCNAME, \
355 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000356 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000357 0, 0, 0, 0, 0, \
358 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000359 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
360 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000361 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000362 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000363}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000364PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000365
366#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
367static PyTypeObject _PyExc_ ## EXCNAME = { \
368 PyObject_HEAD_INIT(NULL) \
369 0, \
370 EXC_MODULE_NAME # EXCNAME, \
371 sizeof(Py ## EXCSTORE ## Object), 0, \
372 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
373 (reprfunc)EXCSTR, 0, 0, 0, \
374 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
375 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
376 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
377 EXCMEMBERS, 0, &_ ## EXCBASE, \
378 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000379 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000380}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000381PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000382
383
384/*
385 * Exception extends BaseException
386 */
387SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000388 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000389
390
391/*
392 * StandardError extends Exception
393 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000394SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000395 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000396 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000397
398
399/*
400 * TypeError extends StandardError
401 */
402SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000403 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000404
405
406/*
407 * StopIteration extends Exception
408 */
409SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000410 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000411
412
413/*
414 * GeneratorExit extends Exception
415 */
416SimpleExtendsException(PyExc_Exception, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000417 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000418
419
420/*
421 * SystemExit extends BaseException
422 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000423
424static int
425SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
426{
427 Py_ssize_t size = PyTuple_GET_SIZE(args);
428
429 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
430 return -1;
431
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000432 if (size == 0)
433 return 0;
434 Py_CLEAR(self->code);
Richard Jones7b9558d2006-05-27 12:29:24 +0000435 if (size == 1)
436 self->code = PyTuple_GET_ITEM(args, 0);
437 else if (size > 1)
438 self->code = args;
439 Py_INCREF(self->code);
440 return 0;
441}
442
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000443static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000444SystemExit_clear(PySystemExitObject *self)
445{
446 Py_CLEAR(self->code);
447 return BaseException_clear((PyBaseExceptionObject *)self);
448}
449
450static void
451SystemExit_dealloc(PySystemExitObject *self)
452{
Georg Brandl38f62372006-09-06 06:50:05 +0000453 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000454 SystemExit_clear(self);
455 self->ob_type->tp_free((PyObject *)self);
456}
457
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000458static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000459SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
460{
461 Py_VISIT(self->code);
462 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
463}
464
465static PyMemberDef SystemExit_members[] = {
466 {"message", T_OBJECT, offsetof(PySystemExitObject, message), 0,
467 PyDoc_STR("exception message")},
468 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
469 PyDoc_STR("exception code")},
470 {NULL} /* Sentinel */
471};
472
473ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
474 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000475 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000476
477/*
478 * KeyboardInterrupt extends BaseException
479 */
480SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000481 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000482
483
484/*
485 * ImportError extends StandardError
486 */
487SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000488 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000489
490
491/*
492 * EnvironmentError extends StandardError
493 */
494
Richard Jones7b9558d2006-05-27 12:29:24 +0000495/* Where a function has a single filename, such as open() or some
496 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
497 * called, giving a third argument which is the filename. But, so
498 * that old code using in-place unpacking doesn't break, e.g.:
499 *
500 * except IOError, (errno, strerror):
501 *
502 * we hack args so that it only contains two items. This also
503 * means we need our own __str__() which prints out the filename
504 * when it was supplied.
505 */
506static int
507EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
508 PyObject *kwds)
509{
510 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
511 PyObject *subslice = NULL;
512
513 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
514 return -1;
515
Georg Brandl3267d282006-09-30 09:03:42 +0000516 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000517 return 0;
518 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000519
520 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000521 &myerrno, &strerror, &filename)) {
522 return -1;
523 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000524 Py_CLEAR(self->myerrno); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000525 self->myerrno = myerrno;
526 Py_INCREF(self->myerrno);
527
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000528 Py_CLEAR(self->strerror); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000529 self->strerror = strerror;
530 Py_INCREF(self->strerror);
531
532 /* self->filename will remain Py_None otherwise */
533 if (filename != NULL) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000534 Py_CLEAR(self->filename); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000535 self->filename = filename;
536 Py_INCREF(self->filename);
537
538 subslice = PyTuple_GetSlice(args, 0, 2);
539 if (!subslice)
540 return -1;
541
542 Py_DECREF(self->args); /* replacing args */
543 self->args = subslice;
544 }
545 return 0;
546}
547
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000548static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000549EnvironmentError_clear(PyEnvironmentErrorObject *self)
550{
551 Py_CLEAR(self->myerrno);
552 Py_CLEAR(self->strerror);
553 Py_CLEAR(self->filename);
554 return BaseException_clear((PyBaseExceptionObject *)self);
555}
556
557static void
558EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
559{
Georg Brandl38f62372006-09-06 06:50:05 +0000560 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000561 EnvironmentError_clear(self);
562 self->ob_type->tp_free((PyObject *)self);
563}
564
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000565static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000566EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
567 void *arg)
568{
569 Py_VISIT(self->myerrno);
570 Py_VISIT(self->strerror);
571 Py_VISIT(self->filename);
572 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
573}
574
575static PyObject *
576EnvironmentError_str(PyEnvironmentErrorObject *self)
577{
578 PyObject *rtnval = NULL;
579
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000580 if (self->filename) {
581 PyObject *fmt;
582 PyObject *repr;
583 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000584
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000585 fmt = PyString_FromString("[Errno %s] %s: %s");
586 if (!fmt)
587 return NULL;
588
589 repr = PyObject_Repr(self->filename);
590 if (!repr) {
591 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000592 return NULL;
593 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000594 tuple = PyTuple_New(3);
595 if (!tuple) {
596 Py_DECREF(repr);
597 Py_DECREF(fmt);
598 return NULL;
599 }
600
601 if (self->myerrno) {
602 Py_INCREF(self->myerrno);
603 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
604 }
605 else {
606 Py_INCREF(Py_None);
607 PyTuple_SET_ITEM(tuple, 0, Py_None);
608 }
609 if (self->strerror) {
610 Py_INCREF(self->strerror);
611 PyTuple_SET_ITEM(tuple, 1, self->strerror);
612 }
613 else {
614 Py_INCREF(Py_None);
615 PyTuple_SET_ITEM(tuple, 1, Py_None);
616 }
617
Richard Jones7b9558d2006-05-27 12:29:24 +0000618 PyTuple_SET_ITEM(tuple, 2, repr);
619
620 rtnval = PyString_Format(fmt, tuple);
621
622 Py_DECREF(fmt);
623 Py_DECREF(tuple);
624 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000625 else if (self->myerrno && self->strerror) {
626 PyObject *fmt;
627 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000628
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000629 fmt = PyString_FromString("[Errno %s] %s");
630 if (!fmt)
631 return NULL;
632
633 tuple = PyTuple_New(2);
634 if (!tuple) {
635 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000636 return NULL;
637 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000638
639 if (self->myerrno) {
640 Py_INCREF(self->myerrno);
641 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
642 }
643 else {
644 Py_INCREF(Py_None);
645 PyTuple_SET_ITEM(tuple, 0, Py_None);
646 }
647 if (self->strerror) {
648 Py_INCREF(self->strerror);
649 PyTuple_SET_ITEM(tuple, 1, self->strerror);
650 }
651 else {
652 Py_INCREF(Py_None);
653 PyTuple_SET_ITEM(tuple, 1, Py_None);
654 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000655
656 rtnval = PyString_Format(fmt, tuple);
657
658 Py_DECREF(fmt);
659 Py_DECREF(tuple);
660 }
661 else
662 rtnval = BaseException_str((PyBaseExceptionObject *)self);
663
664 return rtnval;
665}
666
667static PyMemberDef EnvironmentError_members[] = {
668 {"message", T_OBJECT, offsetof(PyEnvironmentErrorObject, message), 0,
669 PyDoc_STR("exception message")},
670 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000671 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000672 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000673 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000674 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000675 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000676 {NULL} /* Sentinel */
677};
678
679
680static PyObject *
681EnvironmentError_reduce(PyEnvironmentErrorObject *self)
682{
683 PyObject *args = self->args;
684 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000685
Richard Jones7b9558d2006-05-27 12:29:24 +0000686 /* self->args is only the first two real arguments if there was a
687 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000688 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000689 args = PyTuple_New(3);
690 if (!args) return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000691
692 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000693 Py_INCREF(tmp);
694 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000695
696 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000697 Py_INCREF(tmp);
698 PyTuple_SET_ITEM(args, 1, tmp);
699
700 Py_INCREF(self->filename);
701 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000702 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000703 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000704
705 if (self->dict)
706 res = PyTuple_Pack(3, self->ob_type, args, self->dict);
707 else
708 res = PyTuple_Pack(2, self->ob_type, args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000709 Py_DECREF(args);
710 return res;
711}
712
713
714static PyMethodDef EnvironmentError_methods[] = {
715 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
716 {NULL}
717};
718
719ComplexExtendsException(PyExc_StandardError, EnvironmentError,
720 EnvironmentError, EnvironmentError_dealloc,
721 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000722 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000723 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000724
725
726/*
727 * IOError extends EnvironmentError
728 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000729MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000730 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000731
732
733/*
734 * OSError extends EnvironmentError
735 */
736MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000737 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000738
739
740/*
741 * WindowsError extends OSError
742 */
743#ifdef MS_WINDOWS
744#include "errmap.h"
745
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000746static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000747WindowsError_clear(PyWindowsErrorObject *self)
748{
749 Py_CLEAR(self->myerrno);
750 Py_CLEAR(self->strerror);
751 Py_CLEAR(self->filename);
752 Py_CLEAR(self->winerror);
753 return BaseException_clear((PyBaseExceptionObject *)self);
754}
755
756static void
757WindowsError_dealloc(PyWindowsErrorObject *self)
758{
Georg Brandl38f62372006-09-06 06:50:05 +0000759 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000760 WindowsError_clear(self);
761 self->ob_type->tp_free((PyObject *)self);
762}
763
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000764static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000765WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
766{
767 Py_VISIT(self->myerrno);
768 Py_VISIT(self->strerror);
769 Py_VISIT(self->filename);
770 Py_VISIT(self->winerror);
771 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
772}
773
Richard Jones7b9558d2006-05-27 12:29:24 +0000774static int
775WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
776{
777 PyObject *o_errcode = NULL;
778 long errcode;
779 long posix_errno;
780
781 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
782 == -1)
783 return -1;
784
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000785 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000786 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000787
788 /* Set errno to the POSIX errno, and winerror to the Win32
789 error code. */
790 errcode = PyInt_AsLong(self->myerrno);
791 if (errcode == -1 && PyErr_Occurred())
792 return -1;
793 posix_errno = winerror_to_errno(errcode);
794
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000795 Py_CLEAR(self->winerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000796 self->winerror = self->myerrno;
797
798 o_errcode = PyInt_FromLong(posix_errno);
799 if (!o_errcode)
800 return -1;
801
802 self->myerrno = o_errcode;
803
804 return 0;
805}
806
807
808static PyObject *
809WindowsError_str(PyWindowsErrorObject *self)
810{
Richard Jones7b9558d2006-05-27 12:29:24 +0000811 PyObject *rtnval = NULL;
812
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000813 if (self->filename) {
814 PyObject *fmt;
815 PyObject *repr;
816 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000817
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000818 fmt = PyString_FromString("[Error %s] %s: %s");
819 if (!fmt)
820 return NULL;
821
822 repr = PyObject_Repr(self->filename);
823 if (!repr) {
824 Py_DECREF(fmt);
825 return NULL;
826 }
827 tuple = PyTuple_New(3);
828 if (!tuple) {
829 Py_DECREF(repr);
830 Py_DECREF(fmt);
831 return NULL;
832 }
833
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000834 if (self->winerror) {
835 Py_INCREF(self->winerror);
836 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000837 }
838 else {
839 Py_INCREF(Py_None);
840 PyTuple_SET_ITEM(tuple, 0, Py_None);
841 }
842 if (self->strerror) {
843 Py_INCREF(self->strerror);
844 PyTuple_SET_ITEM(tuple, 1, self->strerror);
845 }
846 else {
847 Py_INCREF(Py_None);
848 PyTuple_SET_ITEM(tuple, 1, Py_None);
849 }
850
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000851 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000852
853 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000854
855 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000856 Py_DECREF(tuple);
857 }
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000858 else if (self->winerror && self->strerror) {
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000859 PyObject *fmt;
860 PyObject *tuple;
861
Richard Jones7b9558d2006-05-27 12:29:24 +0000862 fmt = PyString_FromString("[Error %s] %s");
863 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000864 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000865
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000866 tuple = PyTuple_New(2);
867 if (!tuple) {
868 Py_DECREF(fmt);
869 return NULL;
870 }
871
Thomas Hellerdf08f0b2006-10-27 18:31:36 +0000872 if (self->winerror) {
873 Py_INCREF(self->winerror);
874 PyTuple_SET_ITEM(tuple, 0, self->winerror);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000875 }
876 else {
877 Py_INCREF(Py_None);
878 PyTuple_SET_ITEM(tuple, 0, Py_None);
879 }
880 if (self->strerror) {
881 Py_INCREF(self->strerror);
882 PyTuple_SET_ITEM(tuple, 1, self->strerror);
883 }
884 else {
885 Py_INCREF(Py_None);
886 PyTuple_SET_ITEM(tuple, 1, Py_None);
887 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000888
889 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000890
891 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000892 Py_DECREF(tuple);
893 }
894 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000895 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000896
Richard Jones7b9558d2006-05-27 12:29:24 +0000897 return rtnval;
898}
899
900static PyMemberDef WindowsError_members[] = {
901 {"message", T_OBJECT, offsetof(PyWindowsErrorObject, message), 0,
902 PyDoc_STR("exception message")},
903 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000904 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000905 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000906 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000907 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000908 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000909 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000910 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000911 {NULL} /* Sentinel */
912};
913
Richard Jones2d555b32006-05-27 16:15:11 +0000914ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
915 WindowsError_dealloc, 0, WindowsError_members,
916 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000917
918#endif /* MS_WINDOWS */
919
920
921/*
922 * VMSError extends OSError (I think)
923 */
924#ifdef __VMS
925MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +0000926 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000927#endif
928
929
930/*
931 * EOFError extends StandardError
932 */
933SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +0000934 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000935
936
937/*
938 * RuntimeError extends StandardError
939 */
940SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000941 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000942
943
944/*
945 * NotImplementedError extends RuntimeError
946 */
947SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +0000948 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000949
950/*
951 * NameError extends StandardError
952 */
953SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +0000954 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000955
956/*
957 * UnboundLocalError extends NameError
958 */
959SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +0000960 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000961
962/*
963 * AttributeError extends StandardError
964 */
965SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000966 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000967
968
969/*
970 * SyntaxError extends StandardError
971 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000972
973static int
974SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
975{
976 PyObject *info = NULL;
977 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
978
979 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
980 return -1;
981
982 if (lenargs >= 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000983 Py_CLEAR(self->msg);
Richard Jones7b9558d2006-05-27 12:29:24 +0000984 self->msg = PyTuple_GET_ITEM(args, 0);
985 Py_INCREF(self->msg);
986 }
987 if (lenargs == 2) {
988 info = PyTuple_GET_ITEM(args, 1);
989 info = PySequence_Tuple(info);
990 if (!info) return -1;
991
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000992 if (PyTuple_GET_SIZE(info) != 4) {
993 /* not a very good error message, but it's what Python 2.4 gives */
994 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
995 Py_DECREF(info);
996 return -1;
997 }
998
999 Py_CLEAR(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001000 self->filename = PyTuple_GET_ITEM(info, 0);
1001 Py_INCREF(self->filename);
1002
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001003 Py_CLEAR(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001004 self->lineno = PyTuple_GET_ITEM(info, 1);
1005 Py_INCREF(self->lineno);
1006
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001007 Py_CLEAR(self->offset);
Richard Jones7b9558d2006-05-27 12:29:24 +00001008 self->offset = PyTuple_GET_ITEM(info, 2);
1009 Py_INCREF(self->offset);
1010
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001011 Py_CLEAR(self->text);
Richard Jones7b9558d2006-05-27 12:29:24 +00001012 self->text = PyTuple_GET_ITEM(info, 3);
1013 Py_INCREF(self->text);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001014
1015 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001016 }
1017 return 0;
1018}
1019
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001020static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001021SyntaxError_clear(PySyntaxErrorObject *self)
1022{
1023 Py_CLEAR(self->msg);
1024 Py_CLEAR(self->filename);
1025 Py_CLEAR(self->lineno);
1026 Py_CLEAR(self->offset);
1027 Py_CLEAR(self->text);
1028 Py_CLEAR(self->print_file_and_line);
1029 return BaseException_clear((PyBaseExceptionObject *)self);
1030}
1031
1032static void
1033SyntaxError_dealloc(PySyntaxErrorObject *self)
1034{
Georg Brandl38f62372006-09-06 06:50:05 +00001035 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001036 SyntaxError_clear(self);
1037 self->ob_type->tp_free((PyObject *)self);
1038}
1039
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001040static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001041SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1042{
1043 Py_VISIT(self->msg);
1044 Py_VISIT(self->filename);
1045 Py_VISIT(self->lineno);
1046 Py_VISIT(self->offset);
1047 Py_VISIT(self->text);
1048 Py_VISIT(self->print_file_and_line);
1049 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1050}
1051
1052/* This is called "my_basename" instead of just "basename" to avoid name
1053 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1054 defined, and Python does define that. */
1055static char *
1056my_basename(char *name)
1057{
1058 char *cp = name;
1059 char *result = name;
1060
1061 if (name == NULL)
1062 return "???";
1063 while (*cp != '\0') {
1064 if (*cp == SEP)
1065 result = cp + 1;
1066 ++cp;
1067 }
1068 return result;
1069}
1070
1071
1072static PyObject *
1073SyntaxError_str(PySyntaxErrorObject *self)
1074{
1075 PyObject *str;
1076 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001077 int have_filename = 0;
1078 int have_lineno = 0;
1079 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001080 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001081
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001082 if (self->msg)
1083 str = PyObject_Str(self->msg);
1084 else
1085 str = PyObject_Str(Py_None);
Georg Brandl43ab1002006-05-28 20:57:09 +00001086 if (!str) return NULL;
1087 /* Don't fiddle with non-string return (shouldn't happen anyway) */
1088 if (!PyString_Check(str)) return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001089
1090 /* XXX -- do all the additional formatting with filename and
1091 lineno here */
1092
Georg Brandl43ab1002006-05-28 20:57:09 +00001093 have_filename = (self->filename != NULL) &&
1094 PyString_Check(self->filename);
1095 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001096
Georg Brandl43ab1002006-05-28 20:57:09 +00001097 if (!have_filename && !have_lineno)
1098 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001099
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001100 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001101 if (have_filename)
1102 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001103
Georg Brandl43ab1002006-05-28 20:57:09 +00001104 buffer = PyMem_MALLOC(bufsize);
1105 if (buffer == NULL)
1106 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001107
Georg Brandl43ab1002006-05-28 20:57:09 +00001108 if (have_filename && have_lineno)
1109 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
1110 PyString_AS_STRING(str),
1111 my_basename(PyString_AS_STRING(self->filename)),
1112 PyInt_AsLong(self->lineno));
1113 else if (have_filename)
1114 PyOS_snprintf(buffer, bufsize, "%s (%s)",
1115 PyString_AS_STRING(str),
1116 my_basename(PyString_AS_STRING(self->filename)));
1117 else /* only have_lineno */
1118 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
1119 PyString_AS_STRING(str),
1120 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001121
Georg Brandl43ab1002006-05-28 20:57:09 +00001122 result = PyString_FromString(buffer);
1123 PyMem_FREE(buffer);
1124
1125 if (result == NULL)
1126 result = str;
1127 else
1128 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001129 return result;
1130}
1131
1132static PyMemberDef SyntaxError_members[] = {
1133 {"message", T_OBJECT, offsetof(PySyntaxErrorObject, message), 0,
1134 PyDoc_STR("exception message")},
1135 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1136 PyDoc_STR("exception msg")},
1137 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1138 PyDoc_STR("exception filename")},
1139 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1140 PyDoc_STR("exception lineno")},
1141 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1142 PyDoc_STR("exception offset")},
1143 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1144 PyDoc_STR("exception text")},
1145 {"print_file_and_line", T_OBJECT,
1146 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1147 PyDoc_STR("exception print_file_and_line")},
1148 {NULL} /* Sentinel */
1149};
1150
1151ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1152 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001153 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001154
1155
1156/*
1157 * IndentationError extends SyntaxError
1158 */
1159MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001160 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001161
1162
1163/*
1164 * TabError extends IndentationError
1165 */
1166MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001167 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001168
1169
1170/*
1171 * LookupError extends StandardError
1172 */
1173SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001174 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001175
1176
1177/*
1178 * IndexError extends LookupError
1179 */
1180SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001181 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001182
1183
1184/*
1185 * KeyError extends LookupError
1186 */
1187static PyObject *
1188KeyError_str(PyBaseExceptionObject *self)
1189{
1190 /* If args is a tuple of exactly one item, apply repr to args[0].
1191 This is done so that e.g. the exception raised by {}[''] prints
1192 KeyError: ''
1193 rather than the confusing
1194 KeyError
1195 alone. The downside is that if KeyError is raised with an explanatory
1196 string, that string will be displayed in quotes. Too bad.
1197 If args is anything else, use the default BaseException__str__().
1198 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001199 if (PyTuple_GET_SIZE(self->args) == 1) {
1200 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001201 }
1202 return BaseException_str(self);
1203}
1204
1205ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001206 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001207
1208
1209/*
1210 * ValueError extends StandardError
1211 */
1212SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001213 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001214
1215/*
1216 * UnicodeError extends ValueError
1217 */
1218
1219SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001220 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001221
1222#ifdef Py_USING_UNICODE
1223static int
1224get_int(PyObject *attr, Py_ssize_t *value, const char *name)
1225{
1226 if (!attr) {
1227 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1228 return -1;
1229 }
1230
1231 if (PyInt_Check(attr)) {
1232 *value = PyInt_AS_LONG(attr);
1233 } else if (PyLong_Check(attr)) {
1234 *value = _PyLong_AsSsize_t(attr);
1235 if (*value == -1 && PyErr_Occurred())
1236 return -1;
1237 } else {
1238 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
1239 return -1;
1240 }
1241 return 0;
1242}
1243
1244static int
1245set_ssize_t(PyObject **attr, Py_ssize_t value)
1246{
1247 PyObject *obj = PyInt_FromSsize_t(value);
1248 if (!obj)
1249 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001250 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001251 *attr = obj;
1252 return 0;
1253}
1254
1255static PyObject *
1256get_string(PyObject *attr, const char *name)
1257{
1258 if (!attr) {
1259 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1260 return NULL;
1261 }
1262
1263 if (!PyString_Check(attr)) {
1264 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1265 return NULL;
1266 }
1267 Py_INCREF(attr);
1268 return attr;
1269}
1270
1271
1272static int
1273set_string(PyObject **attr, const char *value)
1274{
1275 PyObject *obj = PyString_FromString(value);
1276 if (!obj)
1277 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001278 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001279 *attr = obj;
1280 return 0;
1281}
1282
1283
1284static PyObject *
1285get_unicode(PyObject *attr, const char *name)
1286{
1287 if (!attr) {
1288 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1289 return NULL;
1290 }
1291
1292 if (!PyUnicode_Check(attr)) {
1293 PyErr_Format(PyExc_TypeError,
1294 "%.200s attribute must be unicode", name);
1295 return NULL;
1296 }
1297 Py_INCREF(attr);
1298 return attr;
1299}
1300
1301PyObject *
1302PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1303{
1304 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1305}
1306
1307PyObject *
1308PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1309{
1310 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1311}
1312
1313PyObject *
1314PyUnicodeEncodeError_GetObject(PyObject *exc)
1315{
1316 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1317}
1318
1319PyObject *
1320PyUnicodeDecodeError_GetObject(PyObject *exc)
1321{
1322 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1323}
1324
1325PyObject *
1326PyUnicodeTranslateError_GetObject(PyObject *exc)
1327{
1328 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1329}
1330
1331int
1332PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1333{
1334 if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
1335 Py_ssize_t size;
1336 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1337 "object");
1338 if (!obj) return -1;
1339 size = PyUnicode_GET_SIZE(obj);
1340 if (*start<0)
1341 *start = 0; /*XXX check for values <0*/
1342 if (*start>=size)
1343 *start = size-1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001344 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001345 return 0;
1346 }
1347 return -1;
1348}
1349
1350
1351int
1352PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1353{
1354 if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
1355 Py_ssize_t size;
1356 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1357 "object");
1358 if (!obj) return -1;
1359 size = PyString_GET_SIZE(obj);
1360 if (*start<0)
1361 *start = 0;
1362 if (*start>=size)
1363 *start = size-1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001364 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001365 return 0;
1366 }
1367 return -1;
1368}
1369
1370
1371int
1372PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1373{
1374 return PyUnicodeEncodeError_GetStart(exc, start);
1375}
1376
1377
1378int
1379PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1380{
1381 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1382}
1383
1384
1385int
1386PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1387{
1388 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1389}
1390
1391
1392int
1393PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1394{
1395 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1396}
1397
1398
1399int
1400PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1401{
1402 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1403 Py_ssize_t size;
1404 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1405 "object");
1406 if (!obj) return -1;
1407 size = PyUnicode_GET_SIZE(obj);
1408 if (*end<1)
1409 *end = 1;
1410 if (*end>size)
1411 *end = size;
Georg Brandl43ab1002006-05-28 20:57:09 +00001412 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001413 return 0;
1414 }
1415 return -1;
1416}
1417
1418
1419int
1420PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1421{
1422 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1423 Py_ssize_t size;
1424 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1425 "object");
1426 if (!obj) return -1;
1427 size = PyString_GET_SIZE(obj);
1428 if (*end<1)
1429 *end = 1;
1430 if (*end>size)
1431 *end = size;
Georg Brandl43ab1002006-05-28 20:57:09 +00001432 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001433 return 0;
1434 }
1435 return -1;
1436}
1437
1438
1439int
1440PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1441{
1442 return PyUnicodeEncodeError_GetEnd(exc, start);
1443}
1444
1445
1446int
1447PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1448{
1449 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1450}
1451
1452
1453int
1454PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1455{
1456 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1457}
1458
1459
1460int
1461PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1462{
1463 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1464}
1465
1466PyObject *
1467PyUnicodeEncodeError_GetReason(PyObject *exc)
1468{
1469 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1470}
1471
1472
1473PyObject *
1474PyUnicodeDecodeError_GetReason(PyObject *exc)
1475{
1476 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1477}
1478
1479
1480PyObject *
1481PyUnicodeTranslateError_GetReason(PyObject *exc)
1482{
1483 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1484}
1485
1486
1487int
1488PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1489{
1490 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1491}
1492
1493
1494int
1495PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1496{
1497 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1498}
1499
1500
1501int
1502PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1503{
1504 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1505}
1506
1507
Richard Jones7b9558d2006-05-27 12:29:24 +00001508static int
1509UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1510 PyTypeObject *objecttype)
1511{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001512 Py_CLEAR(self->encoding);
1513 Py_CLEAR(self->object);
1514 Py_CLEAR(self->start);
1515 Py_CLEAR(self->end);
1516 Py_CLEAR(self->reason);
1517
Richard Jones7b9558d2006-05-27 12:29:24 +00001518 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1519 &PyString_Type, &self->encoding,
1520 objecttype, &self->object,
1521 &PyInt_Type, &self->start,
1522 &PyInt_Type, &self->end,
1523 &PyString_Type, &self->reason)) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001524 self->encoding = self->object = self->start = self->end =
Richard Jones7b9558d2006-05-27 12:29:24 +00001525 self->reason = NULL;
1526 return -1;
1527 }
1528
1529 Py_INCREF(self->encoding);
1530 Py_INCREF(self->object);
1531 Py_INCREF(self->start);
1532 Py_INCREF(self->end);
1533 Py_INCREF(self->reason);
1534
1535 return 0;
1536}
1537
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001538static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001539UnicodeError_clear(PyUnicodeErrorObject *self)
1540{
1541 Py_CLEAR(self->encoding);
1542 Py_CLEAR(self->object);
1543 Py_CLEAR(self->start);
1544 Py_CLEAR(self->end);
1545 Py_CLEAR(self->reason);
1546 return BaseException_clear((PyBaseExceptionObject *)self);
1547}
1548
1549static void
1550UnicodeError_dealloc(PyUnicodeErrorObject *self)
1551{
Georg Brandl38f62372006-09-06 06:50:05 +00001552 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001553 UnicodeError_clear(self);
1554 self->ob_type->tp_free((PyObject *)self);
1555}
1556
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001557static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001558UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1559{
1560 Py_VISIT(self->encoding);
1561 Py_VISIT(self->object);
1562 Py_VISIT(self->start);
1563 Py_VISIT(self->end);
1564 Py_VISIT(self->reason);
1565 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1566}
1567
1568static PyMemberDef UnicodeError_members[] = {
1569 {"message", T_OBJECT, offsetof(PyUnicodeErrorObject, message), 0,
1570 PyDoc_STR("exception message")},
1571 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1572 PyDoc_STR("exception encoding")},
1573 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1574 PyDoc_STR("exception object")},
1575 {"start", T_OBJECT, offsetof(PyUnicodeErrorObject, start), 0,
1576 PyDoc_STR("exception start")},
1577 {"end", T_OBJECT, offsetof(PyUnicodeErrorObject, end), 0,
1578 PyDoc_STR("exception end")},
1579 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1580 PyDoc_STR("exception reason")},
1581 {NULL} /* Sentinel */
1582};
1583
1584
1585/*
1586 * UnicodeEncodeError extends UnicodeError
1587 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001588
1589static int
1590UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1591{
1592 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1593 return -1;
1594 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1595 kwds, &PyUnicode_Type);
1596}
1597
1598static PyObject *
1599UnicodeEncodeError_str(PyObject *self)
1600{
1601 Py_ssize_t start;
1602 Py_ssize_t end;
1603
1604 if (PyUnicodeEncodeError_GetStart(self, &start))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001605 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001606
1607 if (PyUnicodeEncodeError_GetEnd(self, &end))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001608 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001609
1610 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001611 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1612 char badchar_str[20];
1613 if (badchar <= 0xff)
1614 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1615 else if (badchar <= 0xffff)
1616 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1617 else
1618 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1619 return PyString_FromFormat(
1620 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
1621 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1622 badchar_str,
1623 start,
1624 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1625 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001626 }
1627 return PyString_FromFormat(
1628 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
1629 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1630 start,
1631 (end-1),
1632 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1633 );
1634}
1635
1636static PyTypeObject _PyExc_UnicodeEncodeError = {
1637 PyObject_HEAD_INIT(NULL)
1638 0,
Georg Brandl38f62372006-09-06 06:50:05 +00001639 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001640 sizeof(PyUnicodeErrorObject), 0,
1641 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1642 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1643 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001644 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1645 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001646 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001647 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001648};
1649PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1650
1651PyObject *
1652PyUnicodeEncodeError_Create(
1653 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1654 Py_ssize_t start, Py_ssize_t end, const char *reason)
1655{
1656 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001657 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001658}
1659
1660
1661/*
1662 * UnicodeDecodeError extends UnicodeError
1663 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001664
1665static int
1666UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1667{
1668 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1669 return -1;
1670 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1671 kwds, &PyString_Type);
1672}
1673
1674static PyObject *
1675UnicodeDecodeError_str(PyObject *self)
1676{
Georg Brandl43ab1002006-05-28 20:57:09 +00001677 Py_ssize_t start = 0;
1678 Py_ssize_t end = 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001679
1680 if (PyUnicodeDecodeError_GetStart(self, &start))
1681 return NULL;
1682
1683 if (PyUnicodeDecodeError_GetEnd(self, &end))
1684 return NULL;
1685
1686 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001687 /* FromFormat does not support %02x, so format that separately */
1688 char byte[4];
1689 PyOS_snprintf(byte, sizeof(byte), "%02x",
1690 ((int)PyString_AS_STRING(((PyUnicodeErrorObject *)self)->object)[start])&0xff);
1691 return PyString_FromFormat(
1692 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
1693 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1694 byte,
1695 start,
1696 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1697 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001698 }
1699 return PyString_FromFormat(
1700 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
1701 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1702 start,
1703 (end-1),
1704 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1705 );
1706}
1707
1708static PyTypeObject _PyExc_UnicodeDecodeError = {
1709 PyObject_HEAD_INIT(NULL)
1710 0,
1711 EXC_MODULE_NAME "UnicodeDecodeError",
1712 sizeof(PyUnicodeErrorObject), 0,
1713 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1714 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1715 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001716 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1717 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001718 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001719 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001720};
1721PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1722
1723PyObject *
1724PyUnicodeDecodeError_Create(
1725 const char *encoding, const char *object, Py_ssize_t length,
1726 Py_ssize_t start, Py_ssize_t end, const char *reason)
1727{
1728 assert(length < INT_MAX);
1729 assert(start < INT_MAX);
1730 assert(end < INT_MAX);
1731 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001732 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001733}
1734
1735
1736/*
1737 * UnicodeTranslateError extends UnicodeError
1738 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001739
1740static int
1741UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1742 PyObject *kwds)
1743{
1744 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1745 return -1;
1746
1747 Py_CLEAR(self->object);
1748 Py_CLEAR(self->start);
1749 Py_CLEAR(self->end);
1750 Py_CLEAR(self->reason);
1751
1752 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1753 &PyUnicode_Type, &self->object,
1754 &PyInt_Type, &self->start,
1755 &PyInt_Type, &self->end,
1756 &PyString_Type, &self->reason)) {
1757 self->object = self->start = self->end = self->reason = NULL;
1758 return -1;
1759 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001760
Richard Jones7b9558d2006-05-27 12:29:24 +00001761 Py_INCREF(self->object);
1762 Py_INCREF(self->start);
1763 Py_INCREF(self->end);
1764 Py_INCREF(self->reason);
1765
1766 return 0;
1767}
1768
1769
1770static PyObject *
1771UnicodeTranslateError_str(PyObject *self)
1772{
1773 Py_ssize_t start;
1774 Py_ssize_t end;
1775
1776 if (PyUnicodeTranslateError_GetStart(self, &start))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001777 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001778
1779 if (PyUnicodeTranslateError_GetEnd(self, &end))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001780 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001781
1782 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001783 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1784 char badchar_str[20];
1785 if (badchar <= 0xff)
1786 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1787 else if (badchar <= 0xffff)
1788 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1789 else
1790 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1791 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001792 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001793 badchar_str,
1794 start,
1795 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1796 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001797 }
1798 return PyString_FromFormat(
1799 "can't translate characters in position %zd-%zd: %.400s",
1800 start,
1801 (end-1),
1802 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1803 );
1804}
1805
1806static PyTypeObject _PyExc_UnicodeTranslateError = {
1807 PyObject_HEAD_INIT(NULL)
1808 0,
1809 EXC_MODULE_NAME "UnicodeTranslateError",
1810 sizeof(PyUnicodeErrorObject), 0,
1811 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1812 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1813 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001814 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001815 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1816 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001817 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001818};
1819PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1820
1821PyObject *
1822PyUnicodeTranslateError_Create(
1823 const Py_UNICODE *object, Py_ssize_t length,
1824 Py_ssize_t start, Py_ssize_t end, const char *reason)
1825{
1826 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001827 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001828}
1829#endif
1830
1831
1832/*
1833 * AssertionError extends StandardError
1834 */
1835SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001836 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001837
1838
1839/*
1840 * ArithmeticError extends StandardError
1841 */
1842SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001843 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001844
1845
1846/*
1847 * FloatingPointError extends ArithmeticError
1848 */
1849SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001850 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001851
1852
1853/*
1854 * OverflowError extends ArithmeticError
1855 */
1856SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001857 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001858
1859
1860/*
1861 * ZeroDivisionError extends ArithmeticError
1862 */
1863SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001864 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001865
1866
1867/*
1868 * SystemError extends StandardError
1869 */
1870SimpleExtendsException(PyExc_StandardError, SystemError,
1871 "Internal error in the Python interpreter.\n"
1872 "\n"
1873 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001874 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001875
1876
1877/*
1878 * ReferenceError extends StandardError
1879 */
1880SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001881 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001882
1883
1884/*
1885 * MemoryError extends StandardError
1886 */
Richard Jones2d555b32006-05-27 16:15:11 +00001887SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001888
1889
1890/* Warning category docstrings */
1891
1892/*
1893 * Warning extends Exception
1894 */
1895SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001896 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001897
1898
1899/*
1900 * UserWarning extends Warning
1901 */
1902SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001903 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001904
1905
1906/*
1907 * DeprecationWarning extends Warning
1908 */
1909SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001910 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001911
1912
1913/*
1914 * PendingDeprecationWarning extends Warning
1915 */
1916SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1917 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001918 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001919
1920
1921/*
1922 * SyntaxWarning extends Warning
1923 */
1924SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001925 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001926
1927
1928/*
1929 * RuntimeWarning extends Warning
1930 */
1931SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001932 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001933
1934
1935/*
1936 * FutureWarning extends Warning
1937 */
1938SimpleExtendsException(PyExc_Warning, FutureWarning,
1939 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001940 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001941
1942
1943/*
1944 * ImportWarning extends Warning
1945 */
1946SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001947 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00001948
1949
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001950/*
1951 * UnicodeWarning extends Warning
1952 */
1953SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1954 "Base class for warnings about Unicode related problems, mostly\n"
1955 "related to conversion problems.");
1956
1957
Richard Jones7b9558d2006-05-27 12:29:24 +00001958/* Pre-computed MemoryError instance. Best to create this as early as
1959 * possible and not wait until a MemoryError is actually raised!
1960 */
1961PyObject *PyExc_MemoryErrorInst=NULL;
1962
1963/* module global functions */
1964static PyMethodDef functions[] = {
1965 /* Sentinel */
1966 {NULL, NULL}
1967};
1968
1969#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1970 Py_FatalError("exceptions bootstrapping error.");
1971
1972#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1973 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1974 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1975 Py_FatalError("Module dictionary insertion problem.");
1976
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00001977#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00001978/* crt variable checking in VisualStudio .NET 2005 */
1979#include <crtdbg.h>
1980
1981static int prevCrtReportMode;
1982static _invalid_parameter_handler prevCrtHandler;
1983
1984/* Invalid parameter handler. Sets a ValueError exception */
1985static void
1986InvalidParameterHandler(
1987 const wchar_t * expression,
1988 const wchar_t * function,
1989 const wchar_t * file,
1990 unsigned int line,
1991 uintptr_t pReserved)
1992{
1993 /* Do nothing, allow execution to continue. Usually this
1994 * means that the CRT will set errno to EINVAL
1995 */
1996}
1997#endif
1998
1999
Richard Jones7b9558d2006-05-27 12:29:24 +00002000PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00002001_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00002002{
2003 PyObject *m, *bltinmod, *bdict;
2004
2005 PRE_INIT(BaseException)
2006 PRE_INIT(Exception)
2007 PRE_INIT(StandardError)
2008 PRE_INIT(TypeError)
2009 PRE_INIT(StopIteration)
2010 PRE_INIT(GeneratorExit)
2011 PRE_INIT(SystemExit)
2012 PRE_INIT(KeyboardInterrupt)
2013 PRE_INIT(ImportError)
2014 PRE_INIT(EnvironmentError)
2015 PRE_INIT(IOError)
2016 PRE_INIT(OSError)
2017#ifdef MS_WINDOWS
2018 PRE_INIT(WindowsError)
2019#endif
2020#ifdef __VMS
2021 PRE_INIT(VMSError)
2022#endif
2023 PRE_INIT(EOFError)
2024 PRE_INIT(RuntimeError)
2025 PRE_INIT(NotImplementedError)
2026 PRE_INIT(NameError)
2027 PRE_INIT(UnboundLocalError)
2028 PRE_INIT(AttributeError)
2029 PRE_INIT(SyntaxError)
2030 PRE_INIT(IndentationError)
2031 PRE_INIT(TabError)
2032 PRE_INIT(LookupError)
2033 PRE_INIT(IndexError)
2034 PRE_INIT(KeyError)
2035 PRE_INIT(ValueError)
2036 PRE_INIT(UnicodeError)
2037#ifdef Py_USING_UNICODE
2038 PRE_INIT(UnicodeEncodeError)
2039 PRE_INIT(UnicodeDecodeError)
2040 PRE_INIT(UnicodeTranslateError)
2041#endif
2042 PRE_INIT(AssertionError)
2043 PRE_INIT(ArithmeticError)
2044 PRE_INIT(FloatingPointError)
2045 PRE_INIT(OverflowError)
2046 PRE_INIT(ZeroDivisionError)
2047 PRE_INIT(SystemError)
2048 PRE_INIT(ReferenceError)
2049 PRE_INIT(MemoryError)
2050 PRE_INIT(Warning)
2051 PRE_INIT(UserWarning)
2052 PRE_INIT(DeprecationWarning)
2053 PRE_INIT(PendingDeprecationWarning)
2054 PRE_INIT(SyntaxWarning)
2055 PRE_INIT(RuntimeWarning)
2056 PRE_INIT(FutureWarning)
2057 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002058 PRE_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002059
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002060 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2061 (PyObject *)NULL, PYTHON_API_VERSION);
Richard Jones7b9558d2006-05-27 12:29:24 +00002062 if (m == NULL) return;
2063
2064 bltinmod = PyImport_ImportModule("__builtin__");
2065 if (bltinmod == NULL)
2066 Py_FatalError("exceptions bootstrapping error.");
2067 bdict = PyModule_GetDict(bltinmod);
2068 if (bdict == NULL)
2069 Py_FatalError("exceptions bootstrapping error.");
2070
2071 POST_INIT(BaseException)
2072 POST_INIT(Exception)
2073 POST_INIT(StandardError)
2074 POST_INIT(TypeError)
2075 POST_INIT(StopIteration)
2076 POST_INIT(GeneratorExit)
2077 POST_INIT(SystemExit)
2078 POST_INIT(KeyboardInterrupt)
2079 POST_INIT(ImportError)
2080 POST_INIT(EnvironmentError)
2081 POST_INIT(IOError)
2082 POST_INIT(OSError)
2083#ifdef MS_WINDOWS
2084 POST_INIT(WindowsError)
2085#endif
2086#ifdef __VMS
2087 POST_INIT(VMSError)
2088#endif
2089 POST_INIT(EOFError)
2090 POST_INIT(RuntimeError)
2091 POST_INIT(NotImplementedError)
2092 POST_INIT(NameError)
2093 POST_INIT(UnboundLocalError)
2094 POST_INIT(AttributeError)
2095 POST_INIT(SyntaxError)
2096 POST_INIT(IndentationError)
2097 POST_INIT(TabError)
2098 POST_INIT(LookupError)
2099 POST_INIT(IndexError)
2100 POST_INIT(KeyError)
2101 POST_INIT(ValueError)
2102 POST_INIT(UnicodeError)
2103#ifdef Py_USING_UNICODE
2104 POST_INIT(UnicodeEncodeError)
2105 POST_INIT(UnicodeDecodeError)
2106 POST_INIT(UnicodeTranslateError)
2107#endif
2108 POST_INIT(AssertionError)
2109 POST_INIT(ArithmeticError)
2110 POST_INIT(FloatingPointError)
2111 POST_INIT(OverflowError)
2112 POST_INIT(ZeroDivisionError)
2113 POST_INIT(SystemError)
2114 POST_INIT(ReferenceError)
2115 POST_INIT(MemoryError)
2116 POST_INIT(Warning)
2117 POST_INIT(UserWarning)
2118 POST_INIT(DeprecationWarning)
2119 POST_INIT(PendingDeprecationWarning)
2120 POST_INIT(SyntaxWarning)
2121 POST_INIT(RuntimeWarning)
2122 POST_INIT(FutureWarning)
2123 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002124 POST_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002125
2126 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2127 if (!PyExc_MemoryErrorInst)
2128 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
2129
2130 Py_DECREF(bltinmod);
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002131
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002132#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002133 /* Set CRT argument error handler */
2134 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
2135 /* turn off assertions in debug mode */
2136 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
2137#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002138}
2139
2140void
2141_PyExc_Fini(void)
2142{
2143 Py_XDECREF(PyExc_MemoryErrorInst);
2144 PyExc_MemoryErrorInst = NULL;
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002145#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002146 /* reset CRT error handling */
2147 _set_invalid_parameter_handler(prevCrtHandler);
2148 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
2149#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002150}