blob: fda2ab1e301ec82b8f80b1df2eb71eaf3ffd393c [file] [log] [blame]
Georg Brandl43ab1002006-05-28 20:57:09 +00001/*
2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
3 *
4 * Thanks go to Tim Peters and Michael Hudson for debugging.
5 */
6
Richard Jones7b9558d2006-05-27 12:29:24 +00007#define PY_SSIZE_T_CLEAN
8#include <Python.h>
9#include "structmember.h"
10#include "osdefs.h"
11
12#define MAKE_IT_NONE(x) (x) = Py_None; Py_INCREF(Py_None);
13#define EXC_MODULE_NAME "exceptions."
14
Richard Jonesc5b2a2e2006-05-27 16:07:28 +000015/* NOTE: If the exception class hierarchy changes, don't forget to update
16 * Lib/test/exception_hierarchy.txt
17 */
18
19PyDoc_STRVAR(exceptions_doc, "Python's standard exception class hierarchy.\n\
20\n\
21Exceptions found here are defined both in the exceptions module and the\n\
22built-in namespace. It is recommended that user-defined exceptions\n\
23inherit from Exception. See the documentation for the exception\n\
24inheritance hierarchy.\n\
25");
26
Richard Jones7b9558d2006-05-27 12:29:24 +000027/*
28 * BaseException
29 */
30static PyObject *
31BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
32{
33 PyBaseExceptionObject *self;
34
35 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
36 /* the dict is created on the fly in PyObject_GenericSetAttr */
37 self->message = self->dict = NULL;
38
39 self->args = PyTuple_New(0);
40 if (!self->args) {
41 Py_DECREF(self);
42 return NULL;
43 }
44
Michael W. Hudson22a80e72006-05-28 15:51:40 +000045 self->message = PyString_FromString("");
Richard Jones7b9558d2006-05-27 12:29:24 +000046 if (!self->message) {
47 Py_DECREF(self);
48 return NULL;
49 }
50
51 return (PyObject *)self;
52}
53
54static int
55BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
56{
Georg Brandlb0432bc2006-05-30 08:17:00 +000057 if (!_PyArg_NoKeywords(self->ob_type->tp_name, kwds))
58 return -1;
59
Richard Jones7b9558d2006-05-27 12:29:24 +000060 Py_DECREF(self->args);
61 self->args = args;
62 Py_INCREF(self->args);
63
64 if (PyTuple_GET_SIZE(self->args) == 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +000065 Py_CLEAR(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000066 self->message = PyTuple_GET_ITEM(self->args, 0);
Michael W. Hudson22a80e72006-05-28 15:51:40 +000067 Py_INCREF(self->message);
Richard Jones7b9558d2006-05-27 12:29:24 +000068 }
69 return 0;
70}
71
Michael W. Hudson96495ee2006-05-28 17:40:29 +000072static int
Richard Jones7b9558d2006-05-27 12:29:24 +000073BaseException_clear(PyBaseExceptionObject *self)
74{
75 Py_CLEAR(self->dict);
76 Py_CLEAR(self->args);
77 Py_CLEAR(self->message);
78 return 0;
79}
80
81static void
82BaseException_dealloc(PyBaseExceptionObject *self)
83{
Georg Brandl38f62372006-09-06 06:50:05 +000084 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +000085 BaseException_clear(self);
86 self->ob_type->tp_free((PyObject *)self);
87}
88
Michael W. Hudson96495ee2006-05-28 17:40:29 +000089static int
Richard Jones7b9558d2006-05-27 12:29:24 +000090BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
91{
Michael W. Hudson22a80e72006-05-28 15:51:40 +000092 Py_VISIT(self->dict);
Richard Jones7b9558d2006-05-27 12:29:24 +000093 Py_VISIT(self->args);
94 Py_VISIT(self->message);
95 return 0;
96}
97
98static PyObject *
99BaseException_str(PyBaseExceptionObject *self)
100{
101 PyObject *out;
102
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000103 switch (PyTuple_GET_SIZE(self->args)) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000104 case 0:
105 out = PyString_FromString("");
106 break;
107 case 1:
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000108 out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +0000109 break;
Richard Jones7b9558d2006-05-27 12:29:24 +0000110 default:
111 out = PyObject_Str(self->args);
112 break;
113 }
114
115 return out;
116}
117
118static PyObject *
119BaseException_repr(PyBaseExceptionObject *self)
120{
Richard Jones7b9558d2006-05-27 12:29:24 +0000121 PyObject *repr_suffix;
122 PyObject *repr;
123 char *name;
124 char *dot;
125
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000126 repr_suffix = PyObject_Repr(self->args);
127 if (!repr_suffix)
Richard Jones7b9558d2006-05-27 12:29:24 +0000128 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000129
130 name = (char *)self->ob_type->tp_name;
131 dot = strrchr(name, '.');
132 if (dot != NULL) name = dot+1;
133
134 repr = PyString_FromString(name);
135 if (!repr) {
136 Py_DECREF(repr_suffix);
137 return NULL;
138 }
139
140 PyString_ConcatAndDel(&repr, repr_suffix);
141 return repr;
142}
143
144/* Pickling support */
145static PyObject *
146BaseException_reduce(PyBaseExceptionObject *self)
147{
Georg Brandlddba4732006-05-30 07:04:55 +0000148 if (self->args && self->dict)
149 return PyTuple_Pack(3, self->ob_type, self->args, self->dict);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000150 else
Georg Brandlddba4732006-05-30 07:04:55 +0000151 return PyTuple_Pack(2, self->ob_type, self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000152}
153
Georg Brandl85ac8502006-06-01 06:39:19 +0000154/*
155 * Needed for backward compatibility, since exceptions used to store
156 * all their attributes in the __dict__. Code is taken from cPickle's
157 * load_build function.
158 */
159static PyObject *
160BaseException_setstate(PyObject *self, PyObject *state)
161{
162 PyObject *d_key, *d_value;
163 Py_ssize_t i = 0;
164
165 if (state != Py_None) {
166 if (!PyDict_Check(state)) {
167 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
168 return NULL;
169 }
170 while (PyDict_Next(state, &i, &d_key, &d_value)) {
171 if (PyObject_SetAttr(self, d_key, d_value) < 0)
172 return NULL;
173 }
174 }
175 Py_RETURN_NONE;
176}
Richard Jones7b9558d2006-05-27 12:29:24 +0000177
Richard Jones7b9558d2006-05-27 12:29:24 +0000178
179static PyMethodDef BaseException_methods[] = {
180 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Georg Brandl85ac8502006-06-01 06:39:19 +0000181 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Richard Jones7b9558d2006-05-27 12:29:24 +0000182 {NULL, NULL, 0, NULL},
183};
184
185
186
187static PyObject *
188BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)
189{
190 return PySequence_GetItem(self->args, index);
191}
192
193static PySequenceMethods BaseException_as_sequence = {
194 0, /* sq_length; */
195 0, /* sq_concat; */
196 0, /* sq_repeat; */
197 (ssizeargfunc)BaseException_getitem, /* sq_item; */
198 0, /* sq_slice; */
199 0, /* sq_ass_item; */
200 0, /* sq_ass_slice; */
201 0, /* sq_contains; */
202 0, /* sq_inplace_concat; */
203 0 /* sq_inplace_repeat; */
204};
205
206static PyMemberDef BaseException_members[] = {
207 {"message", T_OBJECT, offsetof(PyBaseExceptionObject, message), 0,
208 PyDoc_STR("exception message")},
209 {NULL} /* Sentinel */
210};
211
212
213static PyObject *
214BaseException_get_dict(PyBaseExceptionObject *self)
215{
216 if (self->dict == NULL) {
217 self->dict = PyDict_New();
218 if (!self->dict)
219 return NULL;
220 }
221 Py_INCREF(self->dict);
222 return self->dict;
223}
224
225static int
226BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
227{
228 if (val == NULL) {
229 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
230 return -1;
231 }
232 if (!PyDict_Check(val)) {
233 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
234 return -1;
235 }
236 Py_CLEAR(self->dict);
237 Py_INCREF(val);
238 self->dict = val;
239 return 0;
240}
241
242static PyObject *
243BaseException_get_args(PyBaseExceptionObject *self)
244{
245 if (self->args == NULL) {
246 Py_INCREF(Py_None);
247 return Py_None;
248 }
249 Py_INCREF(self->args);
250 return self->args;
251}
252
253static int
254BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
255{
256 PyObject *seq;
257 if (val == NULL) {
258 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
259 return -1;
260 }
261 seq = PySequence_Tuple(val);
262 if (!seq) return -1;
Georg Brandlc7c51142006-05-29 09:46:51 +0000263 Py_CLEAR(self->args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000264 self->args = seq;
265 return 0;
266}
267
268static PyGetSetDef BaseException_getset[] = {
269 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
270 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
271 {NULL},
272};
273
274
275static PyTypeObject _PyExc_BaseException = {
276 PyObject_HEAD_INIT(NULL)
277 0, /*ob_size*/
278 EXC_MODULE_NAME "BaseException", /*tp_name*/
279 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
280 0, /*tp_itemsize*/
281 (destructor)BaseException_dealloc, /*tp_dealloc*/
282 0, /*tp_print*/
283 0, /*tp_getattr*/
284 0, /*tp_setattr*/
285 0, /* tp_compare; */
286 (reprfunc)BaseException_repr, /*tp_repr*/
287 0, /*tp_as_number*/
288 &BaseException_as_sequence, /*tp_as_sequence*/
289 0, /*tp_as_mapping*/
290 0, /*tp_hash */
291 0, /*tp_call*/
292 (reprfunc)BaseException_str, /*tp_str*/
293 PyObject_GenericGetAttr, /*tp_getattro*/
294 PyObject_GenericSetAttr, /*tp_setattro*/
295 0, /*tp_as_buffer*/
296 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
297 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
298 (traverseproc)BaseException_traverse, /* tp_traverse */
299 (inquiry)BaseException_clear, /* tp_clear */
300 0, /* tp_richcompare */
301 0, /* tp_weaklistoffset */
302 0, /* tp_iter */
303 0, /* tp_iternext */
304 BaseException_methods, /* tp_methods */
305 BaseException_members, /* tp_members */
306 BaseException_getset, /* tp_getset */
307 0, /* tp_base */
308 0, /* tp_dict */
309 0, /* tp_descr_get */
310 0, /* tp_descr_set */
311 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
312 (initproc)BaseException_init, /* tp_init */
313 0, /* tp_alloc */
314 BaseException_new, /* tp_new */
315};
316/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
317from the previous implmentation and also allowing Python objects to be used
318in the API */
319PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
320
Richard Jones2d555b32006-05-27 16:15:11 +0000321/* note these macros omit the last semicolon so the macro invocation may
322 * include it and not look strange.
323 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000324#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
325static PyTypeObject _PyExc_ ## EXCNAME = { \
326 PyObject_HEAD_INIT(NULL) \
327 0, \
328 EXC_MODULE_NAME # EXCNAME, \
329 sizeof(PyBaseExceptionObject), \
330 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
331 0, 0, 0, 0, 0, 0, 0, \
332 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
333 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
334 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
335 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
336 (initproc)BaseException_init, 0, BaseException_new,\
337}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000338PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000339
340#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
341static PyTypeObject _PyExc_ ## EXCNAME = { \
342 PyObject_HEAD_INIT(NULL) \
343 0, \
344 EXC_MODULE_NAME # EXCNAME, \
345 sizeof(Py ## EXCSTORE ## Object), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000346 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000347 0, 0, 0, 0, 0, \
348 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000349 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
350 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Richard Jones7b9558d2006-05-27 12:29:24 +0000351 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000352 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000353}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000354PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000355
356#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
357static PyTypeObject _PyExc_ ## EXCNAME = { \
358 PyObject_HEAD_INIT(NULL) \
359 0, \
360 EXC_MODULE_NAME # EXCNAME, \
361 sizeof(Py ## EXCSTORE ## Object), 0, \
362 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
363 (reprfunc)EXCSTR, 0, 0, 0, \
364 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
365 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
366 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
367 EXCMEMBERS, 0, &_ ## EXCBASE, \
368 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000369 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
Richard Jones7b9558d2006-05-27 12:29:24 +0000370}; \
Richard Jones2d555b32006-05-27 16:15:11 +0000371PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
Richard Jones7b9558d2006-05-27 12:29:24 +0000372
373
374/*
375 * Exception extends BaseException
376 */
377SimpleExtendsException(PyExc_BaseException, Exception,
Richard Jones2d555b32006-05-27 16:15:11 +0000378 "Common base class for all non-exit exceptions.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000379
380
381/*
382 * StandardError extends Exception
383 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000384SimpleExtendsException(PyExc_Exception, StandardError,
Richard Jones7b9558d2006-05-27 12:29:24 +0000385 "Base class for all standard Python exceptions that do not represent\n"
Richard Jones2d555b32006-05-27 16:15:11 +0000386 "interpreter exiting.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000387
388
389/*
390 * TypeError extends StandardError
391 */
392SimpleExtendsException(PyExc_StandardError, TypeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000393 "Inappropriate argument type.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000394
395
396/*
397 * StopIteration extends Exception
398 */
399SimpleExtendsException(PyExc_Exception, StopIteration,
Richard Jones2d555b32006-05-27 16:15:11 +0000400 "Signal the end from iterator.next().");
Richard Jones7b9558d2006-05-27 12:29:24 +0000401
402
403/*
404 * GeneratorExit extends Exception
405 */
406SimpleExtendsException(PyExc_Exception, GeneratorExit,
Richard Jones2d555b32006-05-27 16:15:11 +0000407 "Request that a generator exit.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000408
409
410/*
411 * SystemExit extends BaseException
412 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000413
414static int
415SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
416{
417 Py_ssize_t size = PyTuple_GET_SIZE(args);
418
419 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
420 return -1;
421
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000422 if (size == 0)
423 return 0;
424 Py_CLEAR(self->code);
Richard Jones7b9558d2006-05-27 12:29:24 +0000425 if (size == 1)
426 self->code = PyTuple_GET_ITEM(args, 0);
427 else if (size > 1)
428 self->code = args;
429 Py_INCREF(self->code);
430 return 0;
431}
432
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000433static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000434SystemExit_clear(PySystemExitObject *self)
435{
436 Py_CLEAR(self->code);
437 return BaseException_clear((PyBaseExceptionObject *)self);
438}
439
440static void
441SystemExit_dealloc(PySystemExitObject *self)
442{
Georg Brandl38f62372006-09-06 06:50:05 +0000443 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000444 SystemExit_clear(self);
445 self->ob_type->tp_free((PyObject *)self);
446}
447
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000448static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000449SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
450{
451 Py_VISIT(self->code);
452 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
453}
454
455static PyMemberDef SystemExit_members[] = {
456 {"message", T_OBJECT, offsetof(PySystemExitObject, message), 0,
457 PyDoc_STR("exception message")},
458 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
459 PyDoc_STR("exception code")},
460 {NULL} /* Sentinel */
461};
462
463ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
464 SystemExit_dealloc, 0, SystemExit_members, 0,
Richard Jones2d555b32006-05-27 16:15:11 +0000465 "Request to exit from the interpreter.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000466
467/*
468 * KeyboardInterrupt extends BaseException
469 */
470SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Richard Jones2d555b32006-05-27 16:15:11 +0000471 "Program interrupted by user.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000472
473
474/*
475 * ImportError extends StandardError
476 */
477SimpleExtendsException(PyExc_StandardError, ImportError,
Richard Jones2d555b32006-05-27 16:15:11 +0000478 "Import can't find module, or can't find name in module.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000479
480
481/*
482 * EnvironmentError extends StandardError
483 */
484
Richard Jones7b9558d2006-05-27 12:29:24 +0000485/* Where a function has a single filename, such as open() or some
486 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
487 * called, giving a third argument which is the filename. But, so
488 * that old code using in-place unpacking doesn't break, e.g.:
489 *
490 * except IOError, (errno, strerror):
491 *
492 * we hack args so that it only contains two items. This also
493 * means we need our own __str__() which prints out the filename
494 * when it was supplied.
495 */
496static int
497EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
498 PyObject *kwds)
499{
500 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
501 PyObject *subslice = NULL;
502
503 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
504 return -1;
505
506 if (PyTuple_GET_SIZE(args) <= 1) {
507 return 0;
508 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000509
510 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
Richard Jones7b9558d2006-05-27 12:29:24 +0000511 &myerrno, &strerror, &filename)) {
512 return -1;
513 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000514 Py_CLEAR(self->myerrno); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000515 self->myerrno = myerrno;
516 Py_INCREF(self->myerrno);
517
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000518 Py_CLEAR(self->strerror); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000519 self->strerror = strerror;
520 Py_INCREF(self->strerror);
521
522 /* self->filename will remain Py_None otherwise */
523 if (filename != NULL) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000524 Py_CLEAR(self->filename); /* replacing */
Richard Jones7b9558d2006-05-27 12:29:24 +0000525 self->filename = filename;
526 Py_INCREF(self->filename);
527
528 subslice = PyTuple_GetSlice(args, 0, 2);
529 if (!subslice)
530 return -1;
531
532 Py_DECREF(self->args); /* replacing args */
533 self->args = subslice;
534 }
535 return 0;
536}
537
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000538static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000539EnvironmentError_clear(PyEnvironmentErrorObject *self)
540{
541 Py_CLEAR(self->myerrno);
542 Py_CLEAR(self->strerror);
543 Py_CLEAR(self->filename);
544 return BaseException_clear((PyBaseExceptionObject *)self);
545}
546
547static void
548EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
549{
Georg Brandl38f62372006-09-06 06:50:05 +0000550 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000551 EnvironmentError_clear(self);
552 self->ob_type->tp_free((PyObject *)self);
553}
554
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000555static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000556EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
557 void *arg)
558{
559 Py_VISIT(self->myerrno);
560 Py_VISIT(self->strerror);
561 Py_VISIT(self->filename);
562 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
563}
564
565static PyObject *
566EnvironmentError_str(PyEnvironmentErrorObject *self)
567{
568 PyObject *rtnval = NULL;
569
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000570 if (self->filename) {
571 PyObject *fmt;
572 PyObject *repr;
573 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000574
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000575 fmt = PyString_FromString("[Errno %s] %s: %s");
576 if (!fmt)
577 return NULL;
578
579 repr = PyObject_Repr(self->filename);
580 if (!repr) {
581 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000582 return NULL;
583 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000584 tuple = PyTuple_New(3);
585 if (!tuple) {
586 Py_DECREF(repr);
587 Py_DECREF(fmt);
588 return NULL;
589 }
590
591 if (self->myerrno) {
592 Py_INCREF(self->myerrno);
593 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
594 }
595 else {
596 Py_INCREF(Py_None);
597 PyTuple_SET_ITEM(tuple, 0, Py_None);
598 }
599 if (self->strerror) {
600 Py_INCREF(self->strerror);
601 PyTuple_SET_ITEM(tuple, 1, self->strerror);
602 }
603 else {
604 Py_INCREF(Py_None);
605 PyTuple_SET_ITEM(tuple, 1, Py_None);
606 }
607
Richard Jones7b9558d2006-05-27 12:29:24 +0000608 PyTuple_SET_ITEM(tuple, 2, repr);
609
610 rtnval = PyString_Format(fmt, tuple);
611
612 Py_DECREF(fmt);
613 Py_DECREF(tuple);
614 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000615 else if (self->myerrno && self->strerror) {
616 PyObject *fmt;
617 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000618
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000619 fmt = PyString_FromString("[Errno %s] %s");
620 if (!fmt)
621 return NULL;
622
623 tuple = PyTuple_New(2);
624 if (!tuple) {
625 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000626 return NULL;
627 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000628
629 if (self->myerrno) {
630 Py_INCREF(self->myerrno);
631 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
632 }
633 else {
634 Py_INCREF(Py_None);
635 PyTuple_SET_ITEM(tuple, 0, Py_None);
636 }
637 if (self->strerror) {
638 Py_INCREF(self->strerror);
639 PyTuple_SET_ITEM(tuple, 1, self->strerror);
640 }
641 else {
642 Py_INCREF(Py_None);
643 PyTuple_SET_ITEM(tuple, 1, Py_None);
644 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000645
646 rtnval = PyString_Format(fmt, tuple);
647
648 Py_DECREF(fmt);
649 Py_DECREF(tuple);
650 }
651 else
652 rtnval = BaseException_str((PyBaseExceptionObject *)self);
653
654 return rtnval;
655}
656
657static PyMemberDef EnvironmentError_members[] = {
658 {"message", T_OBJECT, offsetof(PyEnvironmentErrorObject, message), 0,
659 PyDoc_STR("exception message")},
660 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000661 PyDoc_STR("exception errno")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000662 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000663 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000664 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000665 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000666 {NULL} /* Sentinel */
667};
668
669
670static PyObject *
671EnvironmentError_reduce(PyEnvironmentErrorObject *self)
672{
673 PyObject *args = self->args;
674 PyObject *res = NULL, *tmp;
Georg Brandl05f97bf2006-05-30 07:13:29 +0000675
Richard Jones7b9558d2006-05-27 12:29:24 +0000676 /* self->args is only the first two real arguments if there was a
677 * file name given to EnvironmentError. */
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000678 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000679 args = PyTuple_New(3);
680 if (!args) return NULL;
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000681
682 tmp = PyTuple_GET_ITEM(self->args, 0);
Richard Jones7b9558d2006-05-27 12:29:24 +0000683 Py_INCREF(tmp);
684 PyTuple_SET_ITEM(args, 0, tmp);
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000685
686 tmp = PyTuple_GET_ITEM(self->args, 1);
Richard Jones7b9558d2006-05-27 12:29:24 +0000687 Py_INCREF(tmp);
688 PyTuple_SET_ITEM(args, 1, tmp);
689
690 Py_INCREF(self->filename);
691 PyTuple_SET_ITEM(args, 2, self->filename);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000692 } else
Richard Jones7b9558d2006-05-27 12:29:24 +0000693 Py_INCREF(args);
Georg Brandl05f97bf2006-05-30 07:13:29 +0000694
695 if (self->dict)
696 res = PyTuple_Pack(3, self->ob_type, args, self->dict);
697 else
698 res = PyTuple_Pack(2, self->ob_type, args);
Richard Jones7b9558d2006-05-27 12:29:24 +0000699 Py_DECREF(args);
700 return res;
701}
702
703
704static PyMethodDef EnvironmentError_methods[] = {
705 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
706 {NULL}
707};
708
709ComplexExtendsException(PyExc_StandardError, EnvironmentError,
710 EnvironmentError, EnvironmentError_dealloc,
711 EnvironmentError_methods, EnvironmentError_members,
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000712 EnvironmentError_str,
Richard Jones2d555b32006-05-27 16:15:11 +0000713 "Base class for I/O related errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000714
715
716/*
717 * IOError extends EnvironmentError
718 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000719MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Richard Jones2d555b32006-05-27 16:15:11 +0000720 EnvironmentError, "I/O operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000721
722
723/*
724 * OSError extends EnvironmentError
725 */
726MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Richard Jones2d555b32006-05-27 16:15:11 +0000727 EnvironmentError, "OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000728
729
730/*
731 * WindowsError extends OSError
732 */
733#ifdef MS_WINDOWS
734#include "errmap.h"
735
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000736static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000737WindowsError_clear(PyWindowsErrorObject *self)
738{
739 Py_CLEAR(self->myerrno);
740 Py_CLEAR(self->strerror);
741 Py_CLEAR(self->filename);
742 Py_CLEAR(self->winerror);
743 return BaseException_clear((PyBaseExceptionObject *)self);
744}
745
746static void
747WindowsError_dealloc(PyWindowsErrorObject *self)
748{
Georg Brandl38f62372006-09-06 06:50:05 +0000749 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000750 WindowsError_clear(self);
751 self->ob_type->tp_free((PyObject *)self);
752}
753
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000754static int
Richard Jones7b9558d2006-05-27 12:29:24 +0000755WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
756{
757 Py_VISIT(self->myerrno);
758 Py_VISIT(self->strerror);
759 Py_VISIT(self->filename);
760 Py_VISIT(self->winerror);
761 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
762}
763
Richard Jones7b9558d2006-05-27 12:29:24 +0000764static int
765WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
766{
767 PyObject *o_errcode = NULL;
768 long errcode;
769 long posix_errno;
770
771 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
772 == -1)
773 return -1;
774
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000775 if (self->myerrno == NULL)
Richard Jones7b9558d2006-05-27 12:29:24 +0000776 return 0;
Richard Jones7b9558d2006-05-27 12:29:24 +0000777
778 /* Set errno to the POSIX errno, and winerror to the Win32
779 error code. */
780 errcode = PyInt_AsLong(self->myerrno);
781 if (errcode == -1 && PyErr_Occurred())
782 return -1;
783 posix_errno = winerror_to_errno(errcode);
784
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000785 Py_CLEAR(self->winerror);
Richard Jones7b9558d2006-05-27 12:29:24 +0000786 self->winerror = self->myerrno;
787
788 o_errcode = PyInt_FromLong(posix_errno);
789 if (!o_errcode)
790 return -1;
791
792 self->myerrno = o_errcode;
793
794 return 0;
795}
796
797
798static PyObject *
799WindowsError_str(PyWindowsErrorObject *self)
800{
Richard Jones7b9558d2006-05-27 12:29:24 +0000801 PyObject *rtnval = NULL;
802
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000803 if (self->filename) {
804 PyObject *fmt;
805 PyObject *repr;
806 PyObject *tuple;
Richard Jones7b9558d2006-05-27 12:29:24 +0000807
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000808 fmt = PyString_FromString("[Error %s] %s: %s");
809 if (!fmt)
810 return NULL;
811
812 repr = PyObject_Repr(self->filename);
813 if (!repr) {
814 Py_DECREF(fmt);
815 return NULL;
816 }
817 tuple = PyTuple_New(3);
818 if (!tuple) {
819 Py_DECREF(repr);
820 Py_DECREF(fmt);
821 return NULL;
822 }
823
824 if (self->myerrno) {
825 Py_INCREF(self->myerrno);
826 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
827 }
828 else {
829 Py_INCREF(Py_None);
830 PyTuple_SET_ITEM(tuple, 0, Py_None);
831 }
832 if (self->strerror) {
833 Py_INCREF(self->strerror);
834 PyTuple_SET_ITEM(tuple, 1, self->strerror);
835 }
836 else {
837 Py_INCREF(Py_None);
838 PyTuple_SET_ITEM(tuple, 1, Py_None);
839 }
840
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000841 PyTuple_SET_ITEM(tuple, 2, repr);
Richard Jones7b9558d2006-05-27 12:29:24 +0000842
843 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000844
845 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000846 Py_DECREF(tuple);
847 }
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000848 else if (self->myerrno && self->strerror) {
849 PyObject *fmt;
850 PyObject *tuple;
851
Richard Jones7b9558d2006-05-27 12:29:24 +0000852 fmt = PyString_FromString("[Error %s] %s");
853 if (!fmt)
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000854 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +0000855
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000856 tuple = PyTuple_New(2);
857 if (!tuple) {
858 Py_DECREF(fmt);
859 return NULL;
860 }
861
862 if (self->myerrno) {
863 Py_INCREF(self->myerrno);
864 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
865 }
866 else {
867 Py_INCREF(Py_None);
868 PyTuple_SET_ITEM(tuple, 0, Py_None);
869 }
870 if (self->strerror) {
871 Py_INCREF(self->strerror);
872 PyTuple_SET_ITEM(tuple, 1, self->strerror);
873 }
874 else {
875 Py_INCREF(Py_None);
876 PyTuple_SET_ITEM(tuple, 1, Py_None);
877 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000878
879 rtnval = PyString_Format(fmt, tuple);
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000880
881 Py_DECREF(fmt);
Richard Jones7b9558d2006-05-27 12:29:24 +0000882 Py_DECREF(tuple);
883 }
884 else
Michael W. Hudson96495ee2006-05-28 17:40:29 +0000885 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
Richard Jones7b9558d2006-05-27 12:29:24 +0000886
Richard Jones7b9558d2006-05-27 12:29:24 +0000887 return rtnval;
888}
889
890static PyMemberDef WindowsError_members[] = {
891 {"message", T_OBJECT, offsetof(PyWindowsErrorObject, message), 0,
892 PyDoc_STR("exception message")},
893 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000894 PyDoc_STR("POSIX exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000895 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000896 PyDoc_STR("exception strerror")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000897 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000898 PyDoc_STR("exception filename")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000899 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
Richard Jonesc5b2a2e2006-05-27 16:07:28 +0000900 PyDoc_STR("Win32 exception code")},
Richard Jones7b9558d2006-05-27 12:29:24 +0000901 {NULL} /* Sentinel */
902};
903
Richard Jones2d555b32006-05-27 16:15:11 +0000904ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
905 WindowsError_dealloc, 0, WindowsError_members,
906 WindowsError_str, "MS-Windows OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000907
908#endif /* MS_WINDOWS */
909
910
911/*
912 * VMSError extends OSError (I think)
913 */
914#ifdef __VMS
915MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Richard Jones2d555b32006-05-27 16:15:11 +0000916 "OpenVMS OS system call failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000917#endif
918
919
920/*
921 * EOFError extends StandardError
922 */
923SimpleExtendsException(PyExc_StandardError, EOFError,
Richard Jones2d555b32006-05-27 16:15:11 +0000924 "Read beyond end of file.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000925
926
927/*
928 * RuntimeError extends StandardError
929 */
930SimpleExtendsException(PyExc_StandardError, RuntimeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000931 "Unspecified run-time error.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000932
933
934/*
935 * NotImplementedError extends RuntimeError
936 */
937SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Richard Jones2d555b32006-05-27 16:15:11 +0000938 "Method or function hasn't been implemented yet.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000939
940/*
941 * NameError extends StandardError
942 */
943SimpleExtendsException(PyExc_StandardError, NameError,
Richard Jones2d555b32006-05-27 16:15:11 +0000944 "Name not found globally.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000945
946/*
947 * UnboundLocalError extends NameError
948 */
949SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Richard Jones2d555b32006-05-27 16:15:11 +0000950 "Local name referenced but not bound to a value.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000951
952/*
953 * AttributeError extends StandardError
954 */
955SimpleExtendsException(PyExc_StandardError, AttributeError,
Richard Jones2d555b32006-05-27 16:15:11 +0000956 "Attribute not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +0000957
958
959/*
960 * SyntaxError extends StandardError
961 */
Richard Jones7b9558d2006-05-27 12:29:24 +0000962
963static int
964SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
965{
966 PyObject *info = NULL;
967 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
968
969 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
970 return -1;
971
972 if (lenargs >= 1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000973 Py_CLEAR(self->msg);
Richard Jones7b9558d2006-05-27 12:29:24 +0000974 self->msg = PyTuple_GET_ITEM(args, 0);
975 Py_INCREF(self->msg);
976 }
977 if (lenargs == 2) {
978 info = PyTuple_GET_ITEM(args, 1);
979 info = PySequence_Tuple(info);
980 if (!info) return -1;
981
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000982 if (PyTuple_GET_SIZE(info) != 4) {
983 /* not a very good error message, but it's what Python 2.4 gives */
984 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
985 Py_DECREF(info);
986 return -1;
987 }
988
989 Py_CLEAR(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +0000990 self->filename = PyTuple_GET_ITEM(info, 0);
991 Py_INCREF(self->filename);
992
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000993 Py_CLEAR(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +0000994 self->lineno = PyTuple_GET_ITEM(info, 1);
995 Py_INCREF(self->lineno);
996
Michael W. Hudson22a80e72006-05-28 15:51:40 +0000997 Py_CLEAR(self->offset);
Richard Jones7b9558d2006-05-27 12:29:24 +0000998 self->offset = PyTuple_GET_ITEM(info, 2);
999 Py_INCREF(self->offset);
1000
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001001 Py_CLEAR(self->text);
Richard Jones7b9558d2006-05-27 12:29:24 +00001002 self->text = PyTuple_GET_ITEM(info, 3);
1003 Py_INCREF(self->text);
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001004
1005 Py_DECREF(info);
Richard Jones7b9558d2006-05-27 12:29:24 +00001006 }
1007 return 0;
1008}
1009
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001010static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001011SyntaxError_clear(PySyntaxErrorObject *self)
1012{
1013 Py_CLEAR(self->msg);
1014 Py_CLEAR(self->filename);
1015 Py_CLEAR(self->lineno);
1016 Py_CLEAR(self->offset);
1017 Py_CLEAR(self->text);
1018 Py_CLEAR(self->print_file_and_line);
1019 return BaseException_clear((PyBaseExceptionObject *)self);
1020}
1021
1022static void
1023SyntaxError_dealloc(PySyntaxErrorObject *self)
1024{
Georg Brandl38f62372006-09-06 06:50:05 +00001025 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001026 SyntaxError_clear(self);
1027 self->ob_type->tp_free((PyObject *)self);
1028}
1029
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001030static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001031SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1032{
1033 Py_VISIT(self->msg);
1034 Py_VISIT(self->filename);
1035 Py_VISIT(self->lineno);
1036 Py_VISIT(self->offset);
1037 Py_VISIT(self->text);
1038 Py_VISIT(self->print_file_and_line);
1039 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1040}
1041
1042/* This is called "my_basename" instead of just "basename" to avoid name
1043 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1044 defined, and Python does define that. */
1045static char *
1046my_basename(char *name)
1047{
1048 char *cp = name;
1049 char *result = name;
1050
1051 if (name == NULL)
1052 return "???";
1053 while (*cp != '\0') {
1054 if (*cp == SEP)
1055 result = cp + 1;
1056 ++cp;
1057 }
1058 return result;
1059}
1060
1061
1062static PyObject *
1063SyntaxError_str(PySyntaxErrorObject *self)
1064{
1065 PyObject *str;
1066 PyObject *result;
Georg Brandl43ab1002006-05-28 20:57:09 +00001067 int have_filename = 0;
1068 int have_lineno = 0;
1069 char *buffer = NULL;
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001070 Py_ssize_t bufsize;
Richard Jones7b9558d2006-05-27 12:29:24 +00001071
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001072 if (self->msg)
1073 str = PyObject_Str(self->msg);
1074 else
1075 str = PyObject_Str(Py_None);
Georg Brandl43ab1002006-05-28 20:57:09 +00001076 if (!str) return NULL;
1077 /* Don't fiddle with non-string return (shouldn't happen anyway) */
1078 if (!PyString_Check(str)) return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001079
1080 /* XXX -- do all the additional formatting with filename and
1081 lineno here */
1082
Georg Brandl43ab1002006-05-28 20:57:09 +00001083 have_filename = (self->filename != NULL) &&
1084 PyString_Check(self->filename);
1085 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
Richard Jones7b9558d2006-05-27 12:29:24 +00001086
Georg Brandl43ab1002006-05-28 20:57:09 +00001087 if (!have_filename && !have_lineno)
1088 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001089
Thomas Woutersc1282ee2006-05-28 21:32:12 +00001090 bufsize = PyString_GET_SIZE(str) + 64;
Georg Brandl43ab1002006-05-28 20:57:09 +00001091 if (have_filename)
1092 bufsize += PyString_GET_SIZE(self->filename);
Richard Jones7b9558d2006-05-27 12:29:24 +00001093
Georg Brandl43ab1002006-05-28 20:57:09 +00001094 buffer = PyMem_MALLOC(bufsize);
1095 if (buffer == NULL)
1096 return str;
Richard Jones7b9558d2006-05-27 12:29:24 +00001097
Georg Brandl43ab1002006-05-28 20:57:09 +00001098 if (have_filename && have_lineno)
1099 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
1100 PyString_AS_STRING(str),
1101 my_basename(PyString_AS_STRING(self->filename)),
1102 PyInt_AsLong(self->lineno));
1103 else if (have_filename)
1104 PyOS_snprintf(buffer, bufsize, "%s (%s)",
1105 PyString_AS_STRING(str),
1106 my_basename(PyString_AS_STRING(self->filename)));
1107 else /* only have_lineno */
1108 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
1109 PyString_AS_STRING(str),
1110 PyInt_AsLong(self->lineno));
Richard Jones7b9558d2006-05-27 12:29:24 +00001111
Georg Brandl43ab1002006-05-28 20:57:09 +00001112 result = PyString_FromString(buffer);
1113 PyMem_FREE(buffer);
1114
1115 if (result == NULL)
1116 result = str;
1117 else
1118 Py_DECREF(str);
Richard Jones7b9558d2006-05-27 12:29:24 +00001119 return result;
1120}
1121
1122static PyMemberDef SyntaxError_members[] = {
1123 {"message", T_OBJECT, offsetof(PySyntaxErrorObject, message), 0,
1124 PyDoc_STR("exception message")},
1125 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1126 PyDoc_STR("exception msg")},
1127 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1128 PyDoc_STR("exception filename")},
1129 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1130 PyDoc_STR("exception lineno")},
1131 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1132 PyDoc_STR("exception offset")},
1133 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1134 PyDoc_STR("exception text")},
1135 {"print_file_and_line", T_OBJECT,
1136 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1137 PyDoc_STR("exception print_file_and_line")},
1138 {NULL} /* Sentinel */
1139};
1140
1141ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,
1142 SyntaxError_dealloc, 0, SyntaxError_members,
Richard Jones2d555b32006-05-27 16:15:11 +00001143 SyntaxError_str, "Invalid syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001144
1145
1146/*
1147 * IndentationError extends SyntaxError
1148 */
1149MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001150 "Improper indentation.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001151
1152
1153/*
1154 * TabError extends IndentationError
1155 */
1156MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Richard Jones2d555b32006-05-27 16:15:11 +00001157 "Improper mixture of spaces and tabs.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001158
1159
1160/*
1161 * LookupError extends StandardError
1162 */
1163SimpleExtendsException(PyExc_StandardError, LookupError,
Richard Jones2d555b32006-05-27 16:15:11 +00001164 "Base class for lookup errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001165
1166
1167/*
1168 * IndexError extends LookupError
1169 */
1170SimpleExtendsException(PyExc_LookupError, IndexError,
Richard Jones2d555b32006-05-27 16:15:11 +00001171 "Sequence index out of range.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001172
1173
1174/*
1175 * KeyError extends LookupError
1176 */
1177static PyObject *
1178KeyError_str(PyBaseExceptionObject *self)
1179{
1180 /* If args is a tuple of exactly one item, apply repr to args[0].
1181 This is done so that e.g. the exception raised by {}[''] prints
1182 KeyError: ''
1183 rather than the confusing
1184 KeyError
1185 alone. The downside is that if KeyError is raised with an explanatory
1186 string, that string will be displayed in quotes. Too bad.
1187 If args is anything else, use the default BaseException__str__().
1188 */
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001189 if (PyTuple_GET_SIZE(self->args) == 1) {
1190 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Richard Jones7b9558d2006-05-27 12:29:24 +00001191 }
1192 return BaseException_str(self);
1193}
1194
1195ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Richard Jones2d555b32006-05-27 16:15:11 +00001196 0, 0, 0, KeyError_str, "Mapping key not found.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001197
1198
1199/*
1200 * ValueError extends StandardError
1201 */
1202SimpleExtendsException(PyExc_StandardError, ValueError,
Richard Jones2d555b32006-05-27 16:15:11 +00001203 "Inappropriate argument value (of correct type).");
Richard Jones7b9558d2006-05-27 12:29:24 +00001204
1205/*
1206 * UnicodeError extends ValueError
1207 */
1208
1209SimpleExtendsException(PyExc_ValueError, UnicodeError,
Richard Jones2d555b32006-05-27 16:15:11 +00001210 "Unicode related error.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001211
1212#ifdef Py_USING_UNICODE
1213static int
1214get_int(PyObject *attr, Py_ssize_t *value, const char *name)
1215{
1216 if (!attr) {
1217 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1218 return -1;
1219 }
1220
1221 if (PyInt_Check(attr)) {
1222 *value = PyInt_AS_LONG(attr);
1223 } else if (PyLong_Check(attr)) {
1224 *value = _PyLong_AsSsize_t(attr);
1225 if (*value == -1 && PyErr_Occurred())
1226 return -1;
1227 } else {
1228 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
1229 return -1;
1230 }
1231 return 0;
1232}
1233
1234static int
1235set_ssize_t(PyObject **attr, Py_ssize_t value)
1236{
1237 PyObject *obj = PyInt_FromSsize_t(value);
1238 if (!obj)
1239 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001240 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001241 *attr = obj;
1242 return 0;
1243}
1244
1245static PyObject *
1246get_string(PyObject *attr, const char *name)
1247{
1248 if (!attr) {
1249 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1250 return NULL;
1251 }
1252
1253 if (!PyString_Check(attr)) {
1254 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1255 return NULL;
1256 }
1257 Py_INCREF(attr);
1258 return attr;
1259}
1260
1261
1262static int
1263set_string(PyObject **attr, const char *value)
1264{
1265 PyObject *obj = PyString_FromString(value);
1266 if (!obj)
1267 return -1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001268 Py_CLEAR(*attr);
Richard Jones7b9558d2006-05-27 12:29:24 +00001269 *attr = obj;
1270 return 0;
1271}
1272
1273
1274static PyObject *
1275get_unicode(PyObject *attr, const char *name)
1276{
1277 if (!attr) {
1278 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1279 return NULL;
1280 }
1281
1282 if (!PyUnicode_Check(attr)) {
1283 PyErr_Format(PyExc_TypeError,
1284 "%.200s attribute must be unicode", name);
1285 return NULL;
1286 }
1287 Py_INCREF(attr);
1288 return attr;
1289}
1290
1291PyObject *
1292PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1293{
1294 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1295}
1296
1297PyObject *
1298PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1299{
1300 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1301}
1302
1303PyObject *
1304PyUnicodeEncodeError_GetObject(PyObject *exc)
1305{
1306 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1307}
1308
1309PyObject *
1310PyUnicodeDecodeError_GetObject(PyObject *exc)
1311{
1312 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1313}
1314
1315PyObject *
1316PyUnicodeTranslateError_GetObject(PyObject *exc)
1317{
1318 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1319}
1320
1321int
1322PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1323{
1324 if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
1325 Py_ssize_t size;
1326 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1327 "object");
1328 if (!obj) return -1;
1329 size = PyUnicode_GET_SIZE(obj);
1330 if (*start<0)
1331 *start = 0; /*XXX check for values <0*/
1332 if (*start>=size)
1333 *start = size-1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001334 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001335 return 0;
1336 }
1337 return -1;
1338}
1339
1340
1341int
1342PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1343{
1344 if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
1345 Py_ssize_t size;
1346 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1347 "object");
1348 if (!obj) return -1;
1349 size = PyString_GET_SIZE(obj);
1350 if (*start<0)
1351 *start = 0;
1352 if (*start>=size)
1353 *start = size-1;
Georg Brandl43ab1002006-05-28 20:57:09 +00001354 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001355 return 0;
1356 }
1357 return -1;
1358}
1359
1360
1361int
1362PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1363{
1364 return PyUnicodeEncodeError_GetStart(exc, start);
1365}
1366
1367
1368int
1369PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1370{
1371 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1372}
1373
1374
1375int
1376PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1377{
1378 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1379}
1380
1381
1382int
1383PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1384{
1385 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1386}
1387
1388
1389int
1390PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1391{
1392 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1393 Py_ssize_t size;
1394 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1395 "object");
1396 if (!obj) return -1;
1397 size = PyUnicode_GET_SIZE(obj);
1398 if (*end<1)
1399 *end = 1;
1400 if (*end>size)
1401 *end = size;
Georg Brandl43ab1002006-05-28 20:57:09 +00001402 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001403 return 0;
1404 }
1405 return -1;
1406}
1407
1408
1409int
1410PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1411{
1412 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1413 Py_ssize_t size;
1414 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1415 "object");
1416 if (!obj) return -1;
1417 size = PyString_GET_SIZE(obj);
1418 if (*end<1)
1419 *end = 1;
1420 if (*end>size)
1421 *end = size;
Georg Brandl43ab1002006-05-28 20:57:09 +00001422 Py_DECREF(obj);
Richard Jones7b9558d2006-05-27 12:29:24 +00001423 return 0;
1424 }
1425 return -1;
1426}
1427
1428
1429int
1430PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1431{
1432 return PyUnicodeEncodeError_GetEnd(exc, start);
1433}
1434
1435
1436int
1437PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1438{
1439 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1440}
1441
1442
1443int
1444PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1445{
1446 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1447}
1448
1449
1450int
1451PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1452{
1453 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1454}
1455
1456PyObject *
1457PyUnicodeEncodeError_GetReason(PyObject *exc)
1458{
1459 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1460}
1461
1462
1463PyObject *
1464PyUnicodeDecodeError_GetReason(PyObject *exc)
1465{
1466 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1467}
1468
1469
1470PyObject *
1471PyUnicodeTranslateError_GetReason(PyObject *exc)
1472{
1473 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1474}
1475
1476
1477int
1478PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1479{
1480 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1481}
1482
1483
1484int
1485PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1486{
1487 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1488}
1489
1490
1491int
1492PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1493{
1494 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1495}
1496
1497
Richard Jones7b9558d2006-05-27 12:29:24 +00001498static int
1499UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1500 PyTypeObject *objecttype)
1501{
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001502 Py_CLEAR(self->encoding);
1503 Py_CLEAR(self->object);
1504 Py_CLEAR(self->start);
1505 Py_CLEAR(self->end);
1506 Py_CLEAR(self->reason);
1507
Richard Jones7b9558d2006-05-27 12:29:24 +00001508 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1509 &PyString_Type, &self->encoding,
1510 objecttype, &self->object,
1511 &PyInt_Type, &self->start,
1512 &PyInt_Type, &self->end,
1513 &PyString_Type, &self->reason)) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001514 self->encoding = self->object = self->start = self->end =
Richard Jones7b9558d2006-05-27 12:29:24 +00001515 self->reason = NULL;
1516 return -1;
1517 }
1518
1519 Py_INCREF(self->encoding);
1520 Py_INCREF(self->object);
1521 Py_INCREF(self->start);
1522 Py_INCREF(self->end);
1523 Py_INCREF(self->reason);
1524
1525 return 0;
1526}
1527
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001528static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001529UnicodeError_clear(PyUnicodeErrorObject *self)
1530{
1531 Py_CLEAR(self->encoding);
1532 Py_CLEAR(self->object);
1533 Py_CLEAR(self->start);
1534 Py_CLEAR(self->end);
1535 Py_CLEAR(self->reason);
1536 return BaseException_clear((PyBaseExceptionObject *)self);
1537}
1538
1539static void
1540UnicodeError_dealloc(PyUnicodeErrorObject *self)
1541{
Georg Brandl38f62372006-09-06 06:50:05 +00001542 _PyObject_GC_UNTRACK(self);
Richard Jones7b9558d2006-05-27 12:29:24 +00001543 UnicodeError_clear(self);
1544 self->ob_type->tp_free((PyObject *)self);
1545}
1546
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001547static int
Richard Jones7b9558d2006-05-27 12:29:24 +00001548UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1549{
1550 Py_VISIT(self->encoding);
1551 Py_VISIT(self->object);
1552 Py_VISIT(self->start);
1553 Py_VISIT(self->end);
1554 Py_VISIT(self->reason);
1555 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1556}
1557
1558static PyMemberDef UnicodeError_members[] = {
1559 {"message", T_OBJECT, offsetof(PyUnicodeErrorObject, message), 0,
1560 PyDoc_STR("exception message")},
1561 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1562 PyDoc_STR("exception encoding")},
1563 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1564 PyDoc_STR("exception object")},
1565 {"start", T_OBJECT, offsetof(PyUnicodeErrorObject, start), 0,
1566 PyDoc_STR("exception start")},
1567 {"end", T_OBJECT, offsetof(PyUnicodeErrorObject, end), 0,
1568 PyDoc_STR("exception end")},
1569 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1570 PyDoc_STR("exception reason")},
1571 {NULL} /* Sentinel */
1572};
1573
1574
1575/*
1576 * UnicodeEncodeError extends UnicodeError
1577 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001578
1579static int
1580UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1581{
1582 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1583 return -1;
1584 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1585 kwds, &PyUnicode_Type);
1586}
1587
1588static PyObject *
1589UnicodeEncodeError_str(PyObject *self)
1590{
1591 Py_ssize_t start;
1592 Py_ssize_t end;
1593
1594 if (PyUnicodeEncodeError_GetStart(self, &start))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001595 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001596
1597 if (PyUnicodeEncodeError_GetEnd(self, &end))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001598 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001599
1600 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001601 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1602 char badchar_str[20];
1603 if (badchar <= 0xff)
1604 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1605 else if (badchar <= 0xffff)
1606 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1607 else
1608 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1609 return PyString_FromFormat(
1610 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
1611 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1612 badchar_str,
1613 start,
1614 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1615 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001616 }
1617 return PyString_FromFormat(
1618 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
1619 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1620 start,
1621 (end-1),
1622 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1623 );
1624}
1625
1626static PyTypeObject _PyExc_UnicodeEncodeError = {
1627 PyObject_HEAD_INIT(NULL)
1628 0,
Georg Brandl38f62372006-09-06 06:50:05 +00001629 EXC_MODULE_NAME "UnicodeEncodeError",
Richard Jones7b9558d2006-05-27 12:29:24 +00001630 sizeof(PyUnicodeErrorObject), 0,
1631 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1632 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1633 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001634 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1635 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001636 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001637 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001638};
1639PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1640
1641PyObject *
1642PyUnicodeEncodeError_Create(
1643 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1644 Py_ssize_t start, Py_ssize_t end, const char *reason)
1645{
1646 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001647 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001648}
1649
1650
1651/*
1652 * UnicodeDecodeError extends UnicodeError
1653 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001654
1655static int
1656UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1657{
1658 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1659 return -1;
1660 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1661 kwds, &PyString_Type);
1662}
1663
1664static PyObject *
1665UnicodeDecodeError_str(PyObject *self)
1666{
Georg Brandl43ab1002006-05-28 20:57:09 +00001667 Py_ssize_t start = 0;
1668 Py_ssize_t end = 0;
Richard Jones7b9558d2006-05-27 12:29:24 +00001669
1670 if (PyUnicodeDecodeError_GetStart(self, &start))
1671 return NULL;
1672
1673 if (PyUnicodeDecodeError_GetEnd(self, &end))
1674 return NULL;
1675
1676 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001677 /* FromFormat does not support %02x, so format that separately */
1678 char byte[4];
1679 PyOS_snprintf(byte, sizeof(byte), "%02x",
1680 ((int)PyString_AS_STRING(((PyUnicodeErrorObject *)self)->object)[start])&0xff);
1681 return PyString_FromFormat(
1682 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
1683 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1684 byte,
1685 start,
1686 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1687 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001688 }
1689 return PyString_FromFormat(
1690 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
1691 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1692 start,
1693 (end-1),
1694 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1695 );
1696}
1697
1698static PyTypeObject _PyExc_UnicodeDecodeError = {
1699 PyObject_HEAD_INIT(NULL)
1700 0,
1701 EXC_MODULE_NAME "UnicodeDecodeError",
1702 sizeof(PyUnicodeErrorObject), 0,
1703 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1704 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1705 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Michael W. Hudson27596272006-05-28 21:19:03 +00001706 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1707 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Richard Jones7b9558d2006-05-27 12:29:24 +00001708 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001709 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001710};
1711PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1712
1713PyObject *
1714PyUnicodeDecodeError_Create(
1715 const char *encoding, const char *object, Py_ssize_t length,
1716 Py_ssize_t start, Py_ssize_t end, const char *reason)
1717{
1718 assert(length < INT_MAX);
1719 assert(start < INT_MAX);
1720 assert(end < INT_MAX);
1721 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001722 encoding, object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001723}
1724
1725
1726/*
1727 * UnicodeTranslateError extends UnicodeError
1728 */
Richard Jones7b9558d2006-05-27 12:29:24 +00001729
1730static int
1731UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1732 PyObject *kwds)
1733{
1734 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1735 return -1;
1736
1737 Py_CLEAR(self->object);
1738 Py_CLEAR(self->start);
1739 Py_CLEAR(self->end);
1740 Py_CLEAR(self->reason);
1741
1742 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1743 &PyUnicode_Type, &self->object,
1744 &PyInt_Type, &self->start,
1745 &PyInt_Type, &self->end,
1746 &PyString_Type, &self->reason)) {
1747 self->object = self->start = self->end = self->reason = NULL;
1748 return -1;
1749 }
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001750
Richard Jones7b9558d2006-05-27 12:29:24 +00001751 Py_INCREF(self->object);
1752 Py_INCREF(self->start);
1753 Py_INCREF(self->end);
1754 Py_INCREF(self->reason);
1755
1756 return 0;
1757}
1758
1759
1760static PyObject *
1761UnicodeTranslateError_str(PyObject *self)
1762{
1763 Py_ssize_t start;
1764 Py_ssize_t end;
1765
1766 if (PyUnicodeTranslateError_GetStart(self, &start))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001767 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001768
1769 if (PyUnicodeTranslateError_GetEnd(self, &end))
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001770 return NULL;
Richard Jones7b9558d2006-05-27 12:29:24 +00001771
1772 if (end==start+1) {
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001773 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1774 char badchar_str[20];
1775 if (badchar <= 0xff)
1776 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1777 else if (badchar <= 0xffff)
1778 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1779 else
1780 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1781 return PyString_FromFormat(
Richard Jones7b9558d2006-05-27 12:29:24 +00001782 "can't translate character u'\\%s' in position %zd: %.400s",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001783 badchar_str,
1784 start,
1785 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1786 );
Richard Jones7b9558d2006-05-27 12:29:24 +00001787 }
1788 return PyString_FromFormat(
1789 "can't translate characters in position %zd-%zd: %.400s",
1790 start,
1791 (end-1),
1792 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1793 );
1794}
1795
1796static PyTypeObject _PyExc_UnicodeTranslateError = {
1797 PyObject_HEAD_INIT(NULL)
1798 0,
1799 EXC_MODULE_NAME "UnicodeTranslateError",
1800 sizeof(PyUnicodeErrorObject), 0,
1801 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1802 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1803 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Georg Brandl38f62372006-09-06 06:50:05 +00001804 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Richard Jones7b9558d2006-05-27 12:29:24 +00001805 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1806 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Michael W. Hudson96495ee2006-05-28 17:40:29 +00001807 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Richard Jones7b9558d2006-05-27 12:29:24 +00001808};
1809PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1810
1811PyObject *
1812PyUnicodeTranslateError_Create(
1813 const Py_UNICODE *object, Py_ssize_t length,
1814 Py_ssize_t start, Py_ssize_t end, const char *reason)
1815{
1816 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001817 object, length, start, end, reason);
Richard Jones7b9558d2006-05-27 12:29:24 +00001818}
1819#endif
1820
1821
1822/*
1823 * AssertionError extends StandardError
1824 */
1825SimpleExtendsException(PyExc_StandardError, AssertionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001826 "Assertion failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001827
1828
1829/*
1830 * ArithmeticError extends StandardError
1831 */
1832SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Richard Jones2d555b32006-05-27 16:15:11 +00001833 "Base class for arithmetic errors.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001834
1835
1836/*
1837 * FloatingPointError extends ArithmeticError
1838 */
1839SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Richard Jones2d555b32006-05-27 16:15:11 +00001840 "Floating point operation failed.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001841
1842
1843/*
1844 * OverflowError extends ArithmeticError
1845 */
1846SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Richard Jones2d555b32006-05-27 16:15:11 +00001847 "Result too large to be represented.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001848
1849
1850/*
1851 * ZeroDivisionError extends ArithmeticError
1852 */
1853SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Richard Jones2d555b32006-05-27 16:15:11 +00001854 "Second argument to a division or modulo operation was zero.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001855
1856
1857/*
1858 * SystemError extends StandardError
1859 */
1860SimpleExtendsException(PyExc_StandardError, SystemError,
1861 "Internal error in the Python interpreter.\n"
1862 "\n"
1863 "Please report this to the Python maintainer, along with the traceback,\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001864 "the Python version, and the hardware/OS platform and version.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001865
1866
1867/*
1868 * ReferenceError extends StandardError
1869 */
1870SimpleExtendsException(PyExc_StandardError, ReferenceError,
Richard Jones2d555b32006-05-27 16:15:11 +00001871 "Weak ref proxy used after referent went away.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001872
1873
1874/*
1875 * MemoryError extends StandardError
1876 */
Richard Jones2d555b32006-05-27 16:15:11 +00001877SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001878
1879
1880/* Warning category docstrings */
1881
1882/*
1883 * Warning extends Exception
1884 */
1885SimpleExtendsException(PyExc_Exception, Warning,
Richard Jones2d555b32006-05-27 16:15:11 +00001886 "Base class for warning categories.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001887
1888
1889/*
1890 * UserWarning extends Warning
1891 */
1892SimpleExtendsException(PyExc_Warning, UserWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001893 "Base class for warnings generated by user code.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001894
1895
1896/*
1897 * DeprecationWarning extends Warning
1898 */
1899SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001900 "Base class for warnings about deprecated features.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001901
1902
1903/*
1904 * PendingDeprecationWarning extends Warning
1905 */
1906SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1907 "Base class for warnings about features which will be deprecated\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001908 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001909
1910
1911/*
1912 * SyntaxWarning extends Warning
1913 */
1914SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001915 "Base class for warnings about dubious syntax.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001916
1917
1918/*
1919 * RuntimeWarning extends Warning
1920 */
1921SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001922 "Base class for warnings about dubious runtime behavior.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001923
1924
1925/*
1926 * FutureWarning extends Warning
1927 */
1928SimpleExtendsException(PyExc_Warning, FutureWarning,
1929 "Base class for warnings about constructs that will change semantically\n"
Richard Jones2d555b32006-05-27 16:15:11 +00001930 "in the future.");
Richard Jones7b9558d2006-05-27 12:29:24 +00001931
1932
1933/*
1934 * ImportWarning extends Warning
1935 */
1936SimpleExtendsException(PyExc_Warning, ImportWarning,
Richard Jones2d555b32006-05-27 16:15:11 +00001937 "Base class for warnings about probable mistakes in module imports");
Richard Jones7b9558d2006-05-27 12:29:24 +00001938
1939
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001940/*
1941 * UnicodeWarning extends Warning
1942 */
1943SimpleExtendsException(PyExc_Warning, UnicodeWarning,
1944 "Base class for warnings about Unicode related problems, mostly\n"
1945 "related to conversion problems.");
1946
1947
Richard Jones7b9558d2006-05-27 12:29:24 +00001948/* Pre-computed MemoryError instance. Best to create this as early as
1949 * possible and not wait until a MemoryError is actually raised!
1950 */
1951PyObject *PyExc_MemoryErrorInst=NULL;
1952
1953/* module global functions */
1954static PyMethodDef functions[] = {
1955 /* Sentinel */
1956 {NULL, NULL}
1957};
1958
1959#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1960 Py_FatalError("exceptions bootstrapping error.");
1961
1962#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1963 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1964 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1965 Py_FatalError("Module dictionary insertion problem.");
1966
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00001967#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00001968/* crt variable checking in VisualStudio .NET 2005 */
1969#include <crtdbg.h>
1970
1971static int prevCrtReportMode;
1972static _invalid_parameter_handler prevCrtHandler;
1973
1974/* Invalid parameter handler. Sets a ValueError exception */
1975static void
1976InvalidParameterHandler(
1977 const wchar_t * expression,
1978 const wchar_t * function,
1979 const wchar_t * file,
1980 unsigned int line,
1981 uintptr_t pReserved)
1982{
1983 /* Do nothing, allow execution to continue. Usually this
1984 * means that the CRT will set errno to EINVAL
1985 */
1986}
1987#endif
1988
1989
Richard Jones7b9558d2006-05-27 12:29:24 +00001990PyMODINIT_FUNC
Michael W. Hudson22a80e72006-05-28 15:51:40 +00001991_PyExc_Init(void)
Richard Jones7b9558d2006-05-27 12:29:24 +00001992{
1993 PyObject *m, *bltinmod, *bdict;
1994
1995 PRE_INIT(BaseException)
1996 PRE_INIT(Exception)
1997 PRE_INIT(StandardError)
1998 PRE_INIT(TypeError)
1999 PRE_INIT(StopIteration)
2000 PRE_INIT(GeneratorExit)
2001 PRE_INIT(SystemExit)
2002 PRE_INIT(KeyboardInterrupt)
2003 PRE_INIT(ImportError)
2004 PRE_INIT(EnvironmentError)
2005 PRE_INIT(IOError)
2006 PRE_INIT(OSError)
2007#ifdef MS_WINDOWS
2008 PRE_INIT(WindowsError)
2009#endif
2010#ifdef __VMS
2011 PRE_INIT(VMSError)
2012#endif
2013 PRE_INIT(EOFError)
2014 PRE_INIT(RuntimeError)
2015 PRE_INIT(NotImplementedError)
2016 PRE_INIT(NameError)
2017 PRE_INIT(UnboundLocalError)
2018 PRE_INIT(AttributeError)
2019 PRE_INIT(SyntaxError)
2020 PRE_INIT(IndentationError)
2021 PRE_INIT(TabError)
2022 PRE_INIT(LookupError)
2023 PRE_INIT(IndexError)
2024 PRE_INIT(KeyError)
2025 PRE_INIT(ValueError)
2026 PRE_INIT(UnicodeError)
2027#ifdef Py_USING_UNICODE
2028 PRE_INIT(UnicodeEncodeError)
2029 PRE_INIT(UnicodeDecodeError)
2030 PRE_INIT(UnicodeTranslateError)
2031#endif
2032 PRE_INIT(AssertionError)
2033 PRE_INIT(ArithmeticError)
2034 PRE_INIT(FloatingPointError)
2035 PRE_INIT(OverflowError)
2036 PRE_INIT(ZeroDivisionError)
2037 PRE_INIT(SystemError)
2038 PRE_INIT(ReferenceError)
2039 PRE_INIT(MemoryError)
2040 PRE_INIT(Warning)
2041 PRE_INIT(UserWarning)
2042 PRE_INIT(DeprecationWarning)
2043 PRE_INIT(PendingDeprecationWarning)
2044 PRE_INIT(SyntaxWarning)
2045 PRE_INIT(RuntimeWarning)
2046 PRE_INIT(FutureWarning)
2047 PRE_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002048 PRE_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002049
Richard Jonesc5b2a2e2006-05-27 16:07:28 +00002050 m = Py_InitModule4("exceptions", functions, exceptions_doc,
2051 (PyObject *)NULL, PYTHON_API_VERSION);
Richard Jones7b9558d2006-05-27 12:29:24 +00002052 if (m == NULL) return;
2053
2054 bltinmod = PyImport_ImportModule("__builtin__");
2055 if (bltinmod == NULL)
2056 Py_FatalError("exceptions bootstrapping error.");
2057 bdict = PyModule_GetDict(bltinmod);
2058 if (bdict == NULL)
2059 Py_FatalError("exceptions bootstrapping error.");
2060
2061 POST_INIT(BaseException)
2062 POST_INIT(Exception)
2063 POST_INIT(StandardError)
2064 POST_INIT(TypeError)
2065 POST_INIT(StopIteration)
2066 POST_INIT(GeneratorExit)
2067 POST_INIT(SystemExit)
2068 POST_INIT(KeyboardInterrupt)
2069 POST_INIT(ImportError)
2070 POST_INIT(EnvironmentError)
2071 POST_INIT(IOError)
2072 POST_INIT(OSError)
2073#ifdef MS_WINDOWS
2074 POST_INIT(WindowsError)
2075#endif
2076#ifdef __VMS
2077 POST_INIT(VMSError)
2078#endif
2079 POST_INIT(EOFError)
2080 POST_INIT(RuntimeError)
2081 POST_INIT(NotImplementedError)
2082 POST_INIT(NameError)
2083 POST_INIT(UnboundLocalError)
2084 POST_INIT(AttributeError)
2085 POST_INIT(SyntaxError)
2086 POST_INIT(IndentationError)
2087 POST_INIT(TabError)
2088 POST_INIT(LookupError)
2089 POST_INIT(IndexError)
2090 POST_INIT(KeyError)
2091 POST_INIT(ValueError)
2092 POST_INIT(UnicodeError)
2093#ifdef Py_USING_UNICODE
2094 POST_INIT(UnicodeEncodeError)
2095 POST_INIT(UnicodeDecodeError)
2096 POST_INIT(UnicodeTranslateError)
2097#endif
2098 POST_INIT(AssertionError)
2099 POST_INIT(ArithmeticError)
2100 POST_INIT(FloatingPointError)
2101 POST_INIT(OverflowError)
2102 POST_INIT(ZeroDivisionError)
2103 POST_INIT(SystemError)
2104 POST_INIT(ReferenceError)
2105 POST_INIT(MemoryError)
2106 POST_INIT(Warning)
2107 POST_INIT(UserWarning)
2108 POST_INIT(DeprecationWarning)
2109 POST_INIT(PendingDeprecationWarning)
2110 POST_INIT(SyntaxWarning)
2111 POST_INIT(RuntimeWarning)
2112 POST_INIT(FutureWarning)
2113 POST_INIT(ImportWarning)
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00002114 POST_INIT(UnicodeWarning)
Richard Jones7b9558d2006-05-27 12:29:24 +00002115
2116 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2117 if (!PyExc_MemoryErrorInst)
2118 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
2119
2120 Py_DECREF(bltinmod);
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002121
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002122#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002123 /* Set CRT argument error handler */
2124 prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
2125 /* turn off assertions in debug mode */
2126 prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
2127#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002128}
2129
2130void
2131_PyExc_Fini(void)
2132{
2133 Py_XDECREF(PyExc_MemoryErrorInst);
2134 PyExc_MemoryErrorInst = NULL;
Kristján Valur Jónsson74c3ea02006-07-03 14:59:05 +00002135#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Kristján Valur Jónssonf6083172006-06-12 15:45:12 +00002136 /* reset CRT error handling */
2137 _set_invalid_parameter_handler(prevCrtHandler);
2138 _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
2139#endif
Richard Jones7b9558d2006-05-27 12:29:24 +00002140}