blob: 1019b29247db4c3b3cb9cd433c7ed577db296fb1 [file] [log] [blame]
Richard Jones7b9558d2006-05-27 12:29:24 +00001#define PY_SSIZE_T_CLEAN
2#include <Python.h>
3#include "structmember.h"
4#include "osdefs.h"
5
6#define MAKE_IT_NONE(x) (x) = Py_None; Py_INCREF(Py_None);
7#define EXC_MODULE_NAME "exceptions."
8
9/*
10 * BaseException
11 */
12static PyObject *
13BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14{
15 PyBaseExceptionObject *self;
16
17 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
18 /* the dict is created on the fly in PyObject_GenericSetAttr */
19 self->message = self->dict = NULL;
20
21 self->args = PyTuple_New(0);
22 if (!self->args) {
23 Py_DECREF(self);
24 return NULL;
25 }
26
27 self->message = PyString_FromString("");
28 if (!self->message) {
29 Py_DECREF(self);
30 return NULL;
31 }
32
33 return (PyObject *)self;
34}
35
36static int
37BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
38{
39 Py_DECREF(self->args);
40 self->args = args;
41 Py_INCREF(self->args);
42
43 if (PyTuple_GET_SIZE(self->args) == 1) {
44 Py_DECREF(self->message);
45 self->message = PyTuple_GET_ITEM(self->args, 0);
46 Py_INCREF(self->message);
47 }
48 return 0;
49}
50
51int
52BaseException_clear(PyBaseExceptionObject *self)
53{
54 Py_CLEAR(self->dict);
55 Py_CLEAR(self->args);
56 Py_CLEAR(self->message);
57 return 0;
58}
59
60static void
61BaseException_dealloc(PyBaseExceptionObject *self)
62{
63 BaseException_clear(self);
64 self->ob_type->tp_free((PyObject *)self);
65}
66
67int
68BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
69{
70 if (self->dict)
71 Py_VISIT(self->dict);
72 Py_VISIT(self->args);
73 Py_VISIT(self->message);
74 return 0;
75}
76
77static PyObject *
78BaseException_str(PyBaseExceptionObject *self)
79{
80 PyObject *out;
81
82 switch (PySequence_Length(self->args)) {
83 case 0:
84 out = PyString_FromString("");
85 break;
86 case 1:
87 {
88 PyObject *tmp = PySequence_GetItem(self->args, 0);
89 if (tmp) {
90 out = PyObject_Str(tmp);
91 Py_DECREF(tmp);
92 }
93 else
94 out = NULL;
95 break;
96 }
97 case -1:
98 PyErr_Clear();
99 /* Fall through */
100 default:
101 out = PyObject_Str(self->args);
102 break;
103 }
104
105 return out;
106}
107
108static PyObject *
109BaseException_repr(PyBaseExceptionObject *self)
110{
111 Py_ssize_t args_len;
112 PyObject *repr_suffix;
113 PyObject *repr;
114 char *name;
115 char *dot;
116
117 args_len = PySequence_Length(self->args);
118 if (args_len < 0) {
119 return NULL;
120 }
121
122 if (args_len == 0) {
123 repr_suffix = PyString_FromString("()");
124 if (!repr_suffix)
125 return NULL;
126 }
127 else {
128 PyObject *args_repr = PyObject_Repr(self->args);
129 if (!args_repr)
130 return NULL;
131 repr_suffix = args_repr;
132 }
133
134 name = (char *)self->ob_type->tp_name;
135 dot = strrchr(name, '.');
136 if (dot != NULL) name = dot+1;
137
138 repr = PyString_FromString(name);
139 if (!repr) {
140 Py_DECREF(repr_suffix);
141 return NULL;
142 }
143
144 PyString_ConcatAndDel(&repr, repr_suffix);
145 return repr;
146}
147
148/* Pickling support */
149static PyObject *
150BaseException_reduce(PyBaseExceptionObject *self)
151{
152 return PyTuple_Pack(3, self->ob_type, self->args, self->dict);
153}
154
155
156#ifdef Py_USING_UNICODE
157/* while this method generates fairly uninspired output, it a least
158 * guarantees that we can display exceptions that have unicode attributes
159 */
160static PyObject *
161BaseException_unicode(PyBaseExceptionObject *self)
162{
163 if (PySequence_Length(self->args) == 0)
164 return PyUnicode_FromUnicode(NULL, 0);
165 if (PySequence_Length(self->args) == 1) {
166 PyObject *temp = PySequence_GetItem(self->args, 0);
167 PyObject *unicode_obj;
168 if (!temp) {
169 return NULL;
170 }
171 unicode_obj = PyObject_Unicode(temp);
172 Py_DECREF(temp);
173 return unicode_obj;
174 }
175 return PyObject_Unicode(self->args);
176}
177#endif /* Py_USING_UNICODE */
178
179static PyMethodDef BaseException_methods[] = {
180 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
181#ifdef Py_USING_UNICODE
182 {"__unicode__", (PyCFunction)BaseException_unicode, METH_NOARGS },
183#endif
184 {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
195static PySequenceMethods BaseException_as_sequence = {
196 0, /* sq_length; */
197 0, /* sq_concat; */
198 0, /* sq_repeat; */
199 (ssizeargfunc)BaseException_getitem, /* sq_item; */
200 0, /* sq_slice; */
201 0, /* sq_ass_item; */
202 0, /* sq_ass_slice; */
203 0, /* sq_contains; */
204 0, /* sq_inplace_concat; */
205 0 /* sq_inplace_repeat; */
206};
207
208static PyMemberDef BaseException_members[] = {
209 {"message", T_OBJECT, offsetof(PyBaseExceptionObject, message), 0,
210 PyDoc_STR("exception message")},
211 {NULL} /* Sentinel */
212};
213
214
215static PyObject *
216BaseException_get_dict(PyBaseExceptionObject *self)
217{
218 if (self->dict == NULL) {
219 self->dict = PyDict_New();
220 if (!self->dict)
221 return NULL;
222 }
223 Py_INCREF(self->dict);
224 return self->dict;
225}
226
227static int
228BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
229{
230 if (val == NULL) {
231 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
232 return -1;
233 }
234 if (!PyDict_Check(val)) {
235 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
236 return -1;
237 }
238 Py_CLEAR(self->dict);
239 Py_INCREF(val);
240 self->dict = val;
241 return 0;
242}
243
244static PyObject *
245BaseException_get_args(PyBaseExceptionObject *self)
246{
247 if (self->args == NULL) {
248 Py_INCREF(Py_None);
249 return Py_None;
250 }
251 Py_INCREF(self->args);
252 return self->args;
253}
254
255static int
256BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
257{
258 PyObject *seq;
259 if (val == NULL) {
260 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
261 return -1;
262 }
263 seq = PySequence_Tuple(val);
264 if (!seq) return -1;
265 self->args = seq;
266 return 0;
267}
268
269static PyGetSetDef BaseException_getset[] = {
270 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
271 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
272 {NULL},
273};
274
275
276static PyTypeObject _PyExc_BaseException = {
277 PyObject_HEAD_INIT(NULL)
278 0, /*ob_size*/
279 EXC_MODULE_NAME "BaseException", /*tp_name*/
280 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
281 0, /*tp_itemsize*/
282 (destructor)BaseException_dealloc, /*tp_dealloc*/
283 0, /*tp_print*/
284 0, /*tp_getattr*/
285 0, /*tp_setattr*/
286 0, /* tp_compare; */
287 (reprfunc)BaseException_repr, /*tp_repr*/
288 0, /*tp_as_number*/
289 &BaseException_as_sequence, /*tp_as_sequence*/
290 0, /*tp_as_mapping*/
291 0, /*tp_hash */
292 0, /*tp_call*/
293 (reprfunc)BaseException_str, /*tp_str*/
294 PyObject_GenericGetAttr, /*tp_getattro*/
295 PyObject_GenericSetAttr, /*tp_setattro*/
296 0, /*tp_as_buffer*/
297 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
298 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
299 (traverseproc)BaseException_traverse, /* tp_traverse */
300 (inquiry)BaseException_clear, /* tp_clear */
301 0, /* tp_richcompare */
302 0, /* tp_weaklistoffset */
303 0, /* tp_iter */
304 0, /* tp_iternext */
305 BaseException_methods, /* tp_methods */
306 BaseException_members, /* tp_members */
307 BaseException_getset, /* tp_getset */
308 0, /* tp_base */
309 0, /* tp_dict */
310 0, /* tp_descr_get */
311 0, /* tp_descr_set */
312 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
313 (initproc)BaseException_init, /* tp_init */
314 0, /* tp_alloc */
315 BaseException_new, /* tp_new */
316};
317/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
318from the previous implmentation and also allowing Python objects to be used
319in the API */
320PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
321
322#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
323static PyTypeObject _PyExc_ ## EXCNAME = { \
324 PyObject_HEAD_INIT(NULL) \
325 0, \
326 EXC_MODULE_NAME # EXCNAME, \
327 sizeof(PyBaseExceptionObject), \
328 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
329 0, 0, 0, 0, 0, 0, 0, \
330 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
331 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
332 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
333 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
334 (initproc)BaseException_init, 0, BaseException_new,\
335}; \
336PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME;
337
338#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
339static PyTypeObject _PyExc_ ## EXCNAME = { \
340 PyObject_HEAD_INIT(NULL) \
341 0, \
342 EXC_MODULE_NAME # EXCNAME, \
343 sizeof(Py ## EXCSTORE ## Object), \
344 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
345 0, 0, 0, 0, 0, \
346 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
347 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
348 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
349 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
350 (initproc)EXCSTORE ## _init, 0, EXCSTORE ## _new,\
351}; \
352PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME;
353
354#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
355static PyTypeObject _PyExc_ ## EXCNAME = { \
356 PyObject_HEAD_INIT(NULL) \
357 0, \
358 EXC_MODULE_NAME # EXCNAME, \
359 sizeof(Py ## EXCSTORE ## Object), 0, \
360 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
361 (reprfunc)EXCSTR, 0, 0, 0, \
362 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
363 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
364 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
365 EXCMEMBERS, 0, &_ ## EXCBASE, \
366 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
367 (initproc)EXCSTORE ## _init, 0, EXCSTORE ## _new,\
368}; \
369PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME;
370
371
372/*
373 * Exception extends BaseException
374 */
375SimpleExtendsException(PyExc_BaseException, Exception,
376 "Common base class for all non-exit exceptions.")
377
378
379/*
380 * StandardError extends Exception
381 */
382SimpleExtendsException(PyExc_Exception, StandardError,
383 "Base class for all standard Python exceptions that do not represent\n"
Georg Brandl94b8c122006-05-27 14:41:55 +0000384 "interpreter exiting.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000385
386
387/*
388 * TypeError extends StandardError
389 */
390SimpleExtendsException(PyExc_StandardError, TypeError,
Georg Brandl94b8c122006-05-27 14:41:55 +0000391 "Inappropriate argument type.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000392
393
394/*
395 * StopIteration extends Exception
396 */
397SimpleExtendsException(PyExc_Exception, StopIteration,
Georg Brandl94b8c122006-05-27 14:41:55 +0000398 "Signal the end from iterator.next().")
Richard Jones7b9558d2006-05-27 12:29:24 +0000399
400
401/*
402 * GeneratorExit extends Exception
403 */
404SimpleExtendsException(PyExc_Exception, GeneratorExit,
Georg Brandl94b8c122006-05-27 14:41:55 +0000405 "Request that a generator exit.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000406
407
408/*
409 * SystemExit extends BaseException
410 */
411static PyObject *
412SystemExit_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
413{
414 PySystemExitObject *self;
415
416 self = (PySystemExitObject *)BaseException_new(type, args, kwds);
417 if (!self)
418 return NULL;
419
420 MAKE_IT_NONE(self->code);
421
422 return (PyObject *)self;
423}
424
425static int
426SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
427{
428 Py_ssize_t size = PyTuple_GET_SIZE(args);
429
430 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
431 return -1;
432
433 Py_DECREF(self->code);
434 if (size == 1)
435 self->code = PyTuple_GET_ITEM(args, 0);
436 else if (size > 1)
437 self->code = args;
438 Py_INCREF(self->code);
439 return 0;
440}
441
442int
443SystemExit_clear(PySystemExitObject *self)
444{
445 Py_CLEAR(self->code);
446 return BaseException_clear((PyBaseExceptionObject *)self);
447}
448
449static void
450SystemExit_dealloc(PySystemExitObject *self)
451{
452 SystemExit_clear(self);
453 self->ob_type->tp_free((PyObject *)self);
454}
455
456int
457SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
458{
459 Py_VISIT(self->code);
460 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
461}
462
463static PyMemberDef SystemExit_members[] = {
464 {"message", T_OBJECT, offsetof(PySystemExitObject, message), 0,
465 PyDoc_STR("exception message")},
466 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
467 PyDoc_STR("exception code")},
468 {NULL} /* Sentinel */
469};
470
471ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
472 SystemExit_dealloc, 0, SystemExit_members, 0,
Georg Brandl94b8c122006-05-27 14:41:55 +0000473 "Request to exit from the interpreter.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000474
475/*
476 * KeyboardInterrupt extends BaseException
477 */
478SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
Georg Brandl94b8c122006-05-27 14:41:55 +0000479 "Program interrupted by user.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000480
481
482/*
483 * ImportError extends StandardError
484 */
485SimpleExtendsException(PyExc_StandardError, ImportError,
Georg Brandl94b8c122006-05-27 14:41:55 +0000486 "Import can't find module, or can't find name in module.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000487
488
489/*
490 * EnvironmentError extends StandardError
491 */
492
493static PyObject *
494EnvironmentError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
495{
496 PyEnvironmentErrorObject *self = NULL;
497
498 self = (PyEnvironmentErrorObject *)BaseException_new(type, args, kwds);
499 if (!self)
500 return NULL;
501
502 self->myerrno = Py_None;
503 Py_INCREF(Py_None);
504 self->strerror = Py_None;
505 Py_INCREF(Py_None);
506 self->filename = Py_None;
507 Py_INCREF(Py_None);
508
509 return (PyObject *)self;
510}
511
512/* Where a function has a single filename, such as open() or some
513 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
514 * called, giving a third argument which is the filename. But, so
515 * that old code using in-place unpacking doesn't break, e.g.:
516 *
517 * except IOError, (errno, strerror):
518 *
519 * we hack args so that it only contains two items. This also
520 * means we need our own __str__() which prints out the filename
521 * when it was supplied.
522 */
523static int
524EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
525 PyObject *kwds)
526{
527 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
528 PyObject *subslice = NULL;
529
530 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
531 return -1;
532
533 if (PyTuple_GET_SIZE(args) <= 1) {
534 return 0;
535 }
536
537 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
538 &myerrno, &strerror, &filename)) {
539 return -1;
540 }
541 Py_DECREF(self->myerrno); /* replacing */
542 self->myerrno = myerrno;
543 Py_INCREF(self->myerrno);
544
545 Py_DECREF(self->strerror); /* replacing */
546 self->strerror = strerror;
547 Py_INCREF(self->strerror);
548
549 /* self->filename will remain Py_None otherwise */
550 if (filename != NULL) {
551 Py_DECREF(self->filename); /* replacing */
552 self->filename = filename;
553 Py_INCREF(self->filename);
554
555 subslice = PyTuple_GetSlice(args, 0, 2);
556 if (!subslice)
557 return -1;
558
559 Py_DECREF(self->args); /* replacing args */
560 self->args = subslice;
561 }
562 return 0;
563}
564
565int
566EnvironmentError_clear(PyEnvironmentErrorObject *self)
567{
568 Py_CLEAR(self->myerrno);
569 Py_CLEAR(self->strerror);
570 Py_CLEAR(self->filename);
571 return BaseException_clear((PyBaseExceptionObject *)self);
572}
573
574static void
575EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
576{
577 EnvironmentError_clear(self);
578 self->ob_type->tp_free((PyObject *)self);
579}
580
581int
582EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
583 void *arg)
584{
585 Py_VISIT(self->myerrno);
586 Py_VISIT(self->strerror);
587 Py_VISIT(self->filename);
588 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
589}
590
591static PyObject *
592EnvironmentError_str(PyEnvironmentErrorObject *self)
593{
594 PyObject *rtnval = NULL;
595
596 if (self->filename != Py_None) {
597 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
598 PyObject *repr = PyObject_Repr(self->filename);
599 PyObject *tuple = PyTuple_New(3);
600
601 if (!fmt || !repr || !tuple) {
602 Py_XDECREF(fmt);
603 Py_XDECREF(repr);
604 Py_XDECREF(tuple);
605 return NULL;
606 }
607 Py_INCREF(self->myerrno);
608 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
609 Py_INCREF(self->strerror);
610 PyTuple_SET_ITEM(tuple, 1, self->strerror);
611 Py_INCREF(repr);
612 PyTuple_SET_ITEM(tuple, 2, repr);
613
614 rtnval = PyString_Format(fmt, tuple);
615
616 Py_DECREF(fmt);
617 Py_DECREF(tuple);
618 }
619 else if (PyObject_IsTrue(self->myerrno) &&
620 PyObject_IsTrue(self->strerror)) {
621 PyObject *fmt = PyString_FromString("[Errno %s] %s");
622 PyObject *tuple = PyTuple_New(2);
623
624 if (!fmt || !tuple) {
625 Py_XDECREF(fmt);
626 Py_XDECREF(tuple);
627 return NULL;
628 }
629 Py_INCREF(self->myerrno);
630 PyTuple_SET_ITEM(tuple, 0, self->myerrno);
631 Py_INCREF(self->strerror);
632 PyTuple_SET_ITEM(tuple, 1, self->strerror);
633
634 rtnval = PyString_Format(fmt, tuple);
635
636 Py_DECREF(fmt);
637 Py_DECREF(tuple);
638 }
639 else
640 rtnval = BaseException_str((PyBaseExceptionObject *)self);
641
642 return rtnval;
643}
644
645static PyMemberDef EnvironmentError_members[] = {
646 {"message", T_OBJECT, offsetof(PyEnvironmentErrorObject, message), 0,
647 PyDoc_STR("exception message")},
648 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
649 PyDoc_STR("exception code")},
650 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
651 PyDoc_STR("exception code")},
652 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
653 PyDoc_STR("exception code")},
654 {NULL} /* Sentinel */
655};
656
657
658static PyObject *
659EnvironmentError_reduce(PyEnvironmentErrorObject *self)
660{
661 PyObject *args = self->args;
662 PyObject *res = NULL, *tmp;
663 /* self->args is only the first two real arguments if there was a
664 * file name given to EnvironmentError. */
665 if (PyTuple_Check(args) &&
666 PyTuple_GET_SIZE(args) == 2 &&
667 self->filename != Py_None) {
668
669 args = PyTuple_New(3);
670 if (!args) return NULL;
671
672 tmp = PyTuple_GetItem(self->args, 0);
673 if (!tmp) goto finish;
674 Py_INCREF(tmp);
675 PyTuple_SET_ITEM(args, 0, tmp);
676
677 tmp = PyTuple_GetItem(self->args, 1);
678 if (!tmp) goto finish;
679 Py_INCREF(tmp);
680 PyTuple_SET_ITEM(args, 1, tmp);
681
682 Py_INCREF(self->filename);
683 PyTuple_SET_ITEM(args, 2, self->filename);
684 } else {
685 Py_INCREF(args);
686 }
687 res = PyTuple_Pack(3, self->ob_type, args, self->dict);
688 finish:
689 Py_DECREF(args);
690 return res;
691}
692
693
694static PyMethodDef EnvironmentError_methods[] = {
695 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
696 {NULL}
697};
698
699ComplexExtendsException(PyExc_StandardError, EnvironmentError,
700 EnvironmentError, EnvironmentError_dealloc,
701 EnvironmentError_methods, EnvironmentError_members,
702 EnvironmentError_str,
Georg Brandl94b8c122006-05-27 14:41:55 +0000703 "Base class for I/O related errors.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000704
705
706/*
707 * IOError extends EnvironmentError
708 */
709MiddlingExtendsException(PyExc_EnvironmentError, IOError,
Georg Brandl94b8c122006-05-27 14:41:55 +0000710 EnvironmentError, "I/O operation failed.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000711
712
713/*
714 * OSError extends EnvironmentError
715 */
716MiddlingExtendsException(PyExc_EnvironmentError, OSError,
Georg Brandl94b8c122006-05-27 14:41:55 +0000717 EnvironmentError, "OS system call failed.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000718
719
720/*
721 * WindowsError extends OSError
722 */
723#ifdef MS_WINDOWS
724#include "errmap.h"
725
726int
727WindowsError_clear(PyWindowsErrorObject *self)
728{
729 Py_CLEAR(self->myerrno);
730 Py_CLEAR(self->strerror);
731 Py_CLEAR(self->filename);
732 Py_CLEAR(self->winerror);
733 return BaseException_clear((PyBaseExceptionObject *)self);
734}
735
736static void
737WindowsError_dealloc(PyWindowsErrorObject *self)
738{
739 WindowsError_clear(self);
740 self->ob_type->tp_free((PyObject *)self);
741}
742
743int
744WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
745{
746 Py_VISIT(self->myerrno);
747 Py_VISIT(self->strerror);
748 Py_VISIT(self->filename);
749 Py_VISIT(self->winerror);
750 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
751}
752
753static PyObject *
754WindowsError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
755{
756 PyObject *o_errcode = NULL;
757 long errcode;
758 PyWindowsErrorObject *self;
759 long posix_errno;
760
761 self = (PyWindowsErrorObject *)EnvironmentError_new(type, args, kwds);
762 if (!self)
763 return NULL;
764
765 if (self->myerrno == Py_None) {
766 self->winerror = self->myerrno;
767 Py_INCREF(self->winerror);
768 return (PyObject *)self;
769 }
770
771 /* Set errno to the POSIX errno, and winerror to the Win32
772 error code. */
773 errcode = PyInt_AsLong(self->myerrno);
774 if (errcode == -1 && PyErr_Occurred()) {
775 if (PyErr_ExceptionMatches(PyExc_TypeError))
776 /* give a clearer error message */
777 PyErr_SetString(PyExc_TypeError, "errno has to be an integer");
778 goto failed;
779 }
780 posix_errno = winerror_to_errno(errcode);
781
782 self->winerror = self->myerrno;
783
784 o_errcode = PyInt_FromLong(posix_errno);
785 if (!o_errcode)
786 goto failed;
787
788 self->myerrno = o_errcode;
789
790 return (PyObject *)self;
791failed:
792 /* Could not set errno. */
793 Py_DECREF(self);
794 return NULL;
795}
796
797static int
798WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
799{
800 PyObject *o_errcode = NULL;
801 long errcode;
802 long posix_errno;
803
804 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
805 == -1)
806 return -1;
807
808 if (self->myerrno == Py_None) {
809 Py_DECREF(self->winerror);
810 self->winerror = self->myerrno;
811 Py_INCREF(self->winerror);
812 return 0;
813 }
814
815 /* Set errno to the POSIX errno, and winerror to the Win32
816 error code. */
817 errcode = PyInt_AsLong(self->myerrno);
818 if (errcode == -1 && PyErr_Occurred())
819 return -1;
820 posix_errno = winerror_to_errno(errcode);
821
822 Py_DECREF(self->winerror);
823 self->winerror = self->myerrno;
824
825 o_errcode = PyInt_FromLong(posix_errno);
826 if (!o_errcode)
827 return -1;
828
829 self->myerrno = o_errcode;
830
831 return 0;
832}
833
834
835static PyObject *
836WindowsError_str(PyWindowsErrorObject *self)
837{
838 PyObject *repr = NULL;
839 PyObject *fmt = NULL;
840 PyObject *tuple = NULL;
841 PyObject *rtnval = NULL;
842
843 if (self->filename != Py_None) {
844 fmt = PyString_FromString("[Error %s] %s: %s");
845 repr = PyObject_Repr(self->filename);
846 if (!fmt || !repr)
847 goto finally;
848
849 tuple = PyTuple_Pack(3, self->myerrno, self->strerror, repr);
850 if (!tuple)
851 goto finally;
852
853 rtnval = PyString_Format(fmt, tuple);
854 Py_DECREF(tuple);
855 }
856 else if (PyObject_IsTrue(self->myerrno) &&
857 PyObject_IsTrue(self->strerror)) {
858 fmt = PyString_FromString("[Error %s] %s");
859 if (!fmt)
860 goto finally;
861
862 tuple = PyTuple_Pack(2, self->myerrno, self->strerror);
863 if (!tuple)
864 goto finally;
865
866 rtnval = PyString_Format(fmt, tuple);
867 Py_DECREF(tuple);
868 }
869 else
870 rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);
871
872 finally:
873 Py_XDECREF(repr);
874 Py_XDECREF(fmt);
875 Py_XDECREF(tuple);
876 return rtnval;
877}
878
879static PyMemberDef WindowsError_members[] = {
880 {"message", T_OBJECT, offsetof(PyWindowsErrorObject, message), 0,
881 PyDoc_STR("exception message")},
882 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
883 PyDoc_STR("exception code")},
884 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
885 PyDoc_STR("exception code")},
886 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
887 PyDoc_STR("exception code")},
888 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
889 PyDoc_STR("windows exception code")},
890 {NULL} /* Sentinel */
891};
892
893ComplexExtendsException(PyExc_OSError,
894 WindowsError,
895 WindowsError,
896 WindowsError_dealloc,
897 0,
898 WindowsError_members,
899 WindowsError_str,
Georg Brandl94b8c122006-05-27 14:41:55 +0000900 "MS-Windows OS system call failed.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000901
902#endif /* MS_WINDOWS */
903
904
905/*
906 * VMSError extends OSError (I think)
907 */
908#ifdef __VMS
909MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
Georg Brandl94b8c122006-05-27 14:41:55 +0000910 "OpenVMS OS system call failed.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000911#endif
912
913
914/*
915 * EOFError extends StandardError
916 */
917SimpleExtendsException(PyExc_StandardError, EOFError,
Georg Brandl94b8c122006-05-27 14:41:55 +0000918 "Read beyond end of file.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000919
920
921/*
922 * RuntimeError extends StandardError
923 */
924SimpleExtendsException(PyExc_StandardError, RuntimeError,
Georg Brandl94b8c122006-05-27 14:41:55 +0000925 "Unspecified run-time error.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000926
927
928/*
929 * NotImplementedError extends RuntimeError
930 */
931SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
Georg Brandl94b8c122006-05-27 14:41:55 +0000932 "Method or function hasn't been implemented yet.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000933
934/*
935 * NameError extends StandardError
936 */
937SimpleExtendsException(PyExc_StandardError, NameError,
Georg Brandl94b8c122006-05-27 14:41:55 +0000938 "Name not found globally.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000939
940/*
941 * UnboundLocalError extends NameError
942 */
943SimpleExtendsException(PyExc_NameError, UnboundLocalError,
Georg Brandl94b8c122006-05-27 14:41:55 +0000944 "Local name referenced but not bound to a value.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000945
946/*
947 * AttributeError extends StandardError
948 */
949SimpleExtendsException(PyExc_StandardError, AttributeError,
Georg Brandl94b8c122006-05-27 14:41:55 +0000950 "Attribute not found.")
Richard Jones7b9558d2006-05-27 12:29:24 +0000951
952
953/*
954 * SyntaxError extends StandardError
955 */
956static PyObject *
957SyntaxError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
958{
959 PySyntaxErrorObject *self = NULL;
960
961 self = (PySyntaxErrorObject *)BaseException_new(type, args, kwds);
962 if (!self)
963 return NULL;
964
965 MAKE_IT_NONE(self->msg)
966 MAKE_IT_NONE(self->filename)
967 MAKE_IT_NONE(self->lineno)
968 MAKE_IT_NONE(self->offset)
969 MAKE_IT_NONE(self->text)
970
971 /* this is always None - yes, I know it doesn't seem to be used
972 anywhere, but it was in the previous implementation */
973 MAKE_IT_NONE(self->print_file_and_line)
974
975 return (PyObject *)self;
976}
977
978static int
979SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
980{
981 PyObject *info = NULL;
982 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
983
984 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
985 return -1;
986
987 if (lenargs >= 1) {
988 Py_DECREF(self->msg);
989 self->msg = PyTuple_GET_ITEM(args, 0);
990 Py_INCREF(self->msg);
991 }
992 if (lenargs == 2) {
993 info = PyTuple_GET_ITEM(args, 1);
994 info = PySequence_Tuple(info);
995 if (!info) return -1;
996
997 Py_DECREF(self->filename);
998 self->filename = PyTuple_GET_ITEM(info, 0);
999 Py_INCREF(self->filename);
1000
1001 Py_DECREF(self->lineno);
1002 self->lineno = PyTuple_GET_ITEM(info, 1);
1003 Py_INCREF(self->lineno);
1004
1005 Py_DECREF(self->offset);
1006 self->offset = PyTuple_GET_ITEM(info, 2);
1007 Py_INCREF(self->offset);
1008
1009 Py_DECREF(self->text);
1010 self->text = PyTuple_GET_ITEM(info, 3);
1011 Py_INCREF(self->text);
1012 }
1013 return 0;
1014}
1015
1016int
1017SyntaxError_clear(PySyntaxErrorObject *self)
1018{
1019 Py_CLEAR(self->msg);
1020 Py_CLEAR(self->filename);
1021 Py_CLEAR(self->lineno);
1022 Py_CLEAR(self->offset);
1023 Py_CLEAR(self->text);
1024 Py_CLEAR(self->print_file_and_line);
1025 return BaseException_clear((PyBaseExceptionObject *)self);
1026}
1027
1028static void
1029SyntaxError_dealloc(PySyntaxErrorObject *self)
1030{
1031 SyntaxError_clear(self);
1032 self->ob_type->tp_free((PyObject *)self);
1033}
1034
1035int
1036SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1037{
1038 Py_VISIT(self->msg);
1039 Py_VISIT(self->filename);
1040 Py_VISIT(self->lineno);
1041 Py_VISIT(self->offset);
1042 Py_VISIT(self->text);
1043 Py_VISIT(self->print_file_and_line);
1044 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1045}
1046
1047/* This is called "my_basename" instead of just "basename" to avoid name
1048 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1049 defined, and Python does define that. */
1050static char *
1051my_basename(char *name)
1052{
1053 char *cp = name;
1054 char *result = name;
1055
1056 if (name == NULL)
1057 return "???";
1058 while (*cp != '\0') {
1059 if (*cp == SEP)
1060 result = cp + 1;
1061 ++cp;
1062 }
1063 return result;
1064}
1065
1066
1067static PyObject *
1068SyntaxError_str(PySyntaxErrorObject *self)
1069{
1070 PyObject *str;
1071 PyObject *result;
1072
1073 str = PyObject_Str(self->msg);
1074 result = str;
1075
1076 /* XXX -- do all the additional formatting with filename and
1077 lineno here */
1078
1079 if (str != NULL && PyString_Check(str)) {
1080 int have_filename = 0;
1081 int have_lineno = 0;
1082 char *buffer = NULL;
1083
1084 have_filename = (self->filename != NULL) &&
1085 PyString_Check(self->filename);
1086 have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
1087
1088 if (have_filename || have_lineno) {
1089 Py_ssize_t bufsize = PyString_GET_SIZE(str) + 64;
1090 if (have_filename)
1091 bufsize += PyString_GET_SIZE(self->filename);
1092
1093 buffer = (char *)PyMem_MALLOC(bufsize);
1094 if (buffer != NULL) {
1095 if (have_filename && have_lineno)
1096 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
1097 PyString_AS_STRING(str),
1098 my_basename(PyString_AS_STRING(self->filename)),
1099 PyInt_AsLong(self->lineno));
1100 else if (have_filename)
1101 PyOS_snprintf(buffer, bufsize, "%s (%s)",
1102 PyString_AS_STRING(str),
1103 my_basename(PyString_AS_STRING(self->filename)));
1104 else if (have_lineno)
1105 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
1106 PyString_AS_STRING(str),
1107 PyInt_AsLong(self->lineno));
1108
1109 result = PyString_FromString(buffer);
1110 PyMem_FREE(buffer);
1111
1112 if (result == NULL)
1113 result = str;
1114 else
1115 Py_DECREF(str);
1116 }
1117 }
1118 }
1119 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,
Georg Brandl94b8c122006-05-27 14:41:55 +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,
Georg Brandl94b8c122006-05-27 14:41:55 +00001150 "Improper indentation.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001151
1152
1153/*
1154 * TabError extends IndentationError
1155 */
1156MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
Georg Brandl94b8c122006-05-27 14:41:55 +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,
Georg Brandl94b8c122006-05-27 14:41:55 +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,
Georg Brandl94b8c122006-05-27 14:41:55 +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 */
1189 if (PyTuple_Check(self->args) && PyTuple_GET_SIZE(self->args) == 1) {
1190 PyObject *key = PyTuple_GET_ITEM(self->args, 0);
1191 return PyObject_Repr(key);
1192 }
1193 return BaseException_str(self);
1194}
1195
1196ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Georg Brandl94b8c122006-05-27 14:41:55 +00001197 0, 0, 0, KeyError_str, "Mapping key not found.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001198
1199
1200/*
1201 * ValueError extends StandardError
1202 */
1203SimpleExtendsException(PyExc_StandardError, ValueError,
Georg Brandl94b8c122006-05-27 14:41:55 +00001204 "Inappropriate argument value (of correct type).")
Richard Jones7b9558d2006-05-27 12:29:24 +00001205
1206/*
1207 * UnicodeError extends ValueError
1208 */
1209
1210SimpleExtendsException(PyExc_ValueError, UnicodeError,
Georg Brandl94b8c122006-05-27 14:41:55 +00001211 "Unicode related error.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001212
1213#ifdef Py_USING_UNICODE
1214static int
1215get_int(PyObject *attr, Py_ssize_t *value, const char *name)
1216{
1217 if (!attr) {
1218 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1219 return -1;
1220 }
1221
1222 if (PyInt_Check(attr)) {
1223 *value = PyInt_AS_LONG(attr);
1224 } else if (PyLong_Check(attr)) {
1225 *value = _PyLong_AsSsize_t(attr);
1226 if (*value == -1 && PyErr_Occurred())
1227 return -1;
1228 } else {
1229 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
1230 return -1;
1231 }
1232 return 0;
1233}
1234
1235static int
1236set_ssize_t(PyObject **attr, Py_ssize_t value)
1237{
1238 PyObject *obj = PyInt_FromSsize_t(value);
1239 if (!obj)
1240 return -1;
1241 Py_XDECREF(*attr);
1242 *attr = obj;
1243 return 0;
1244}
1245
1246static PyObject *
1247get_string(PyObject *attr, const char *name)
1248{
1249 if (!attr) {
1250 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1251 return NULL;
1252 }
1253
1254 if (!PyString_Check(attr)) {
1255 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
1256 return NULL;
1257 }
1258 Py_INCREF(attr);
1259 return attr;
1260}
1261
1262
1263static int
1264set_string(PyObject **attr, const char *value)
1265{
1266 PyObject *obj = PyString_FromString(value);
1267 if (!obj)
1268 return -1;
1269 Py_XDECREF(*attr);
1270 *attr = obj;
1271 return 0;
1272}
1273
1274
1275static PyObject *
1276get_unicode(PyObject *attr, const char *name)
1277{
1278 if (!attr) {
1279 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1280 return NULL;
1281 }
1282
1283 if (!PyUnicode_Check(attr)) {
1284 PyErr_Format(PyExc_TypeError,
1285 "%.200s attribute must be unicode", name);
1286 return NULL;
1287 }
1288 Py_INCREF(attr);
1289 return attr;
1290}
1291
1292PyObject *
1293PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1294{
1295 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1296}
1297
1298PyObject *
1299PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1300{
1301 return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1302}
1303
1304PyObject *
1305PyUnicodeEncodeError_GetObject(PyObject *exc)
1306{
1307 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1308}
1309
1310PyObject *
1311PyUnicodeDecodeError_GetObject(PyObject *exc)
1312{
1313 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1314}
1315
1316PyObject *
1317PyUnicodeTranslateError_GetObject(PyObject *exc)
1318{
1319 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1320}
1321
1322int
1323PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1324{
1325 if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
1326 Py_ssize_t size;
1327 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1328 "object");
1329 if (!obj) return -1;
1330 size = PyUnicode_GET_SIZE(obj);
1331 if (*start<0)
1332 *start = 0; /*XXX check for values <0*/
1333 if (*start>=size)
1334 *start = size-1;
1335 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;
1354 return 0;
1355 }
1356 return -1;
1357}
1358
1359
1360int
1361PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1362{
1363 return PyUnicodeEncodeError_GetStart(exc, start);
1364}
1365
1366
1367int
1368PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1369{
1370 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1371}
1372
1373
1374int
1375PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1376{
1377 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1378}
1379
1380
1381int
1382PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1383{
1384 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
1385}
1386
1387
1388int
1389PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1390{
1391 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1392 Py_ssize_t size;
1393 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1394 "object");
1395 if (!obj) return -1;
1396 size = PyUnicode_GET_SIZE(obj);
1397 if (*end<1)
1398 *end = 1;
1399 if (*end>size)
1400 *end = size;
1401 return 0;
1402 }
1403 return -1;
1404}
1405
1406
1407int
1408PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1409{
1410 if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
1411 Py_ssize_t size;
1412 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,
1413 "object");
1414 if (!obj) return -1;
1415 size = PyString_GET_SIZE(obj);
1416 if (*end<1)
1417 *end = 1;
1418 if (*end>size)
1419 *end = size;
1420 return 0;
1421 }
1422 return -1;
1423}
1424
1425
1426int
1427PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1428{
1429 return PyUnicodeEncodeError_GetEnd(exc, start);
1430}
1431
1432
1433int
1434PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1435{
1436 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1437}
1438
1439
1440int
1441PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1442{
1443 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1444}
1445
1446
1447int
1448PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1449{
1450 return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
1451}
1452
1453PyObject *
1454PyUnicodeEncodeError_GetReason(PyObject *exc)
1455{
1456 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1457}
1458
1459
1460PyObject *
1461PyUnicodeDecodeError_GetReason(PyObject *exc)
1462{
1463 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1464}
1465
1466
1467PyObject *
1468PyUnicodeTranslateError_GetReason(PyObject *exc)
1469{
1470 return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");
1471}
1472
1473
1474int
1475PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1476{
1477 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1478}
1479
1480
1481int
1482PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1483{
1484 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1485}
1486
1487
1488int
1489PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1490{
1491 return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);
1492}
1493
1494
1495static PyObject *
1496UnicodeError_new(PyTypeObject *type, PyObject *args, PyObject *kwds,
1497 PyTypeObject *objecttype)
1498{
1499 PyUnicodeErrorObject *self;
1500
1501 self = (PyUnicodeErrorObject *)BaseException_new(type, args, kwds);
1502 if (!self)
1503 return NULL;
1504
1505 MAKE_IT_NONE(self->encoding);
1506 MAKE_IT_NONE(self->object);
1507 MAKE_IT_NONE(self->start);
1508 MAKE_IT_NONE(self->end);
1509 MAKE_IT_NONE(self->reason);
1510
1511 return (PyObject *)self;
1512}
1513
1514static int
1515UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
1516 PyTypeObject *objecttype)
1517{
1518 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)) {
1524 self->encoding = self->object = self->start = self->end =
1525 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
1538int
1539UnicodeError_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{
1552 UnicodeError_clear(self);
1553 self->ob_type->tp_free((PyObject *)self);
1554}
1555
1556int
1557UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1558{
1559 Py_VISIT(self->encoding);
1560 Py_VISIT(self->object);
1561 Py_VISIT(self->start);
1562 Py_VISIT(self->end);
1563 Py_VISIT(self->reason);
1564 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1565}
1566
1567static PyMemberDef UnicodeError_members[] = {
1568 {"message", T_OBJECT, offsetof(PyUnicodeErrorObject, message), 0,
1569 PyDoc_STR("exception message")},
1570 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1571 PyDoc_STR("exception encoding")},
1572 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1573 PyDoc_STR("exception object")},
1574 {"start", T_OBJECT, offsetof(PyUnicodeErrorObject, start), 0,
1575 PyDoc_STR("exception start")},
1576 {"end", T_OBJECT, offsetof(PyUnicodeErrorObject, end), 0,
1577 PyDoc_STR("exception end")},
1578 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1579 PyDoc_STR("exception reason")},
1580 {NULL} /* Sentinel */
1581};
1582
1583
1584/*
1585 * UnicodeEncodeError extends UnicodeError
1586 */
1587static PyObject *
1588UnicodeEncodeError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1589{
1590 return UnicodeError_new(type, args, kwds, &PyUnicode_Type);
1591}
1592
1593static int
1594UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1595{
1596 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1597 return -1;
1598 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1599 kwds, &PyUnicode_Type);
1600}
1601
1602static PyObject *
1603UnicodeEncodeError_str(PyObject *self)
1604{
1605 Py_ssize_t start;
1606 Py_ssize_t end;
1607
1608 if (PyUnicodeEncodeError_GetStart(self, &start))
1609 return NULL;
1610
1611 if (PyUnicodeEncodeError_GetEnd(self, &end))
1612 return NULL;
1613
1614 if (end==start+1) {
1615 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1616 char badchar_str[20];
1617 if (badchar <= 0xff)
1618 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1619 else if (badchar <= 0xffff)
1620 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1621 else
1622 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1623 return PyString_FromFormat(
1624 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
1625 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1626 badchar_str,
1627 start,
1628 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1629 );
1630 }
1631 return PyString_FromFormat(
1632 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
1633 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1634 start,
1635 (end-1),
1636 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1637 );
1638}
1639
1640static PyTypeObject _PyExc_UnicodeEncodeError = {
1641 PyObject_HEAD_INIT(NULL)
1642 0,
1643 "UnicodeEncodeError",
1644 sizeof(PyUnicodeErrorObject), 0,
1645 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1646 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1647 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1648 PyDoc_STR("Unicode encoding error."), (traverseproc)BaseException_traverse,
1649 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1650 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
1651 (initproc)UnicodeEncodeError_init, 0, UnicodeEncodeError_new,
1652};
1653PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1654
1655PyObject *
1656PyUnicodeEncodeError_Create(
1657 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1658 Py_ssize_t start, Py_ssize_t end, const char *reason)
1659{
1660 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
1661 encoding, object, length, start, end, reason);
1662}
1663
1664
1665/*
1666 * UnicodeDecodeError extends UnicodeError
1667 */
1668static PyObject *
1669UnicodeDecodeError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1670{
1671 return UnicodeError_new(type, args, kwds, &PyString_Type);
1672}
1673
1674static int
1675UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1676{
1677 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1678 return -1;
1679 return UnicodeError_init((PyUnicodeErrorObject *)self, args,
1680 kwds, &PyString_Type);
1681}
1682
1683static PyObject *
1684UnicodeDecodeError_str(PyObject *self)
1685{
1686 Py_ssize_t start;
1687 Py_ssize_t end;
1688
1689 if (PyUnicodeDecodeError_GetStart(self, &start))
1690 return NULL;
1691
1692 if (PyUnicodeDecodeError_GetEnd(self, &end))
1693 return NULL;
1694
1695 if (end==start+1) {
1696 /* FromFormat does not support %02x, so format that separately */
1697 char byte[4];
1698 PyOS_snprintf(byte, sizeof(byte), "%02x",
1699 ((int)PyString_AS_STRING(((PyUnicodeErrorObject *)self)->object)[start])&0xff);
1700 return PyString_FromFormat(
1701 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
1702 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1703 byte,
1704 start,
1705 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1706 );
1707 }
1708 return PyString_FromFormat(
1709 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
1710 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->encoding),
1711 start,
1712 (end-1),
1713 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1714 );
1715}
1716
1717static PyTypeObject _PyExc_UnicodeDecodeError = {
1718 PyObject_HEAD_INIT(NULL)
1719 0,
1720 EXC_MODULE_NAME "UnicodeDecodeError",
1721 sizeof(PyUnicodeErrorObject), 0,
1722 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1723 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1724 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1725 PyDoc_STR("Unicode decoding error."), (traverseproc)BaseException_traverse,
1726 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1727 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
1728 (initproc)UnicodeDecodeError_init, 0, UnicodeDecodeError_new,
1729};
1730PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1731
1732PyObject *
1733PyUnicodeDecodeError_Create(
1734 const char *encoding, const char *object, Py_ssize_t length,
1735 Py_ssize_t start, Py_ssize_t end, const char *reason)
1736{
1737 assert(length < INT_MAX);
1738 assert(start < INT_MAX);
1739 assert(end < INT_MAX);
1740 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
1741 encoding, object, length, start, end, reason);
1742}
1743
1744
1745/*
1746 * UnicodeTranslateError extends UnicodeError
1747 */
1748static PyObject *
1749UnicodeTranslateError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1750{
1751 PyUnicodeErrorObject *self = NULL;
1752
1753 self = (PyUnicodeErrorObject *)BaseException_new(type, args, kwds);
1754 if (!self)
1755 return NULL;
1756
1757 MAKE_IT_NONE(self->encoding);
1758 MAKE_IT_NONE(self->object);
1759 MAKE_IT_NONE(self->start);
1760 MAKE_IT_NONE(self->end);
1761 MAKE_IT_NONE(self->reason);
1762
1763 return (PyObject *)self;
1764}
1765
1766static int
1767UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1768 PyObject *kwds)
1769{
1770 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1771 return -1;
1772
1773 Py_CLEAR(self->object);
1774 Py_CLEAR(self->start);
1775 Py_CLEAR(self->end);
1776 Py_CLEAR(self->reason);
1777
1778 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1779 &PyUnicode_Type, &self->object,
1780 &PyInt_Type, &self->start,
1781 &PyInt_Type, &self->end,
1782 &PyString_Type, &self->reason)) {
1783 self->object = self->start = self->end = self->reason = NULL;
1784 return -1;
1785 }
1786
1787 Py_INCREF(self->object);
1788 Py_INCREF(self->start);
1789 Py_INCREF(self->end);
1790 Py_INCREF(self->reason);
1791
1792 return 0;
1793}
1794
1795
1796static PyObject *
1797UnicodeTranslateError_str(PyObject *self)
1798{
1799 Py_ssize_t start;
1800 Py_ssize_t end;
1801
1802 if (PyUnicodeTranslateError_GetStart(self, &start))
1803 return NULL;
1804
1805 if (PyUnicodeTranslateError_GetEnd(self, &end))
1806 return NULL;
1807
1808 if (end==start+1) {
1809 int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
1810 char badchar_str[20];
1811 if (badchar <= 0xff)
1812 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
1813 else if (badchar <= 0xffff)
1814 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
1815 else
1816 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1817 return PyString_FromFormat(
1818 "can't translate character u'\\%s' in position %zd: %.400s",
1819 badchar_str,
1820 start,
1821 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1822 );
1823 }
1824 return PyString_FromFormat(
1825 "can't translate characters in position %zd-%zd: %.400s",
1826 start,
1827 (end-1),
1828 PyString_AS_STRING(((PyUnicodeErrorObject *)self)->reason)
1829 );
1830}
1831
1832static PyTypeObject _PyExc_UnicodeTranslateError = {
1833 PyObject_HEAD_INIT(NULL)
1834 0,
1835 EXC_MODULE_NAME "UnicodeTranslateError",
1836 sizeof(PyUnicodeErrorObject), 0,
1837 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1838 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1839 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1840 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1841 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1842 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
1843 (initproc)UnicodeTranslateError_init, 0, UnicodeTranslateError_new,
1844};
1845PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1846
1847PyObject *
1848PyUnicodeTranslateError_Create(
1849 const Py_UNICODE *object, Py_ssize_t length,
1850 Py_ssize_t start, Py_ssize_t end, const char *reason)
1851{
1852 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
1853 object, length, start, end, reason);
1854}
1855#endif
1856
1857
1858/*
1859 * AssertionError extends StandardError
1860 */
1861SimpleExtendsException(PyExc_StandardError, AssertionError,
Georg Brandl94b8c122006-05-27 14:41:55 +00001862 "Assertion failed.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001863
1864
1865/*
1866 * ArithmeticError extends StandardError
1867 */
1868SimpleExtendsException(PyExc_StandardError, ArithmeticError,
Georg Brandl94b8c122006-05-27 14:41:55 +00001869 "Base class for arithmetic errors.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001870
1871
1872/*
1873 * FloatingPointError extends ArithmeticError
1874 */
1875SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
Georg Brandl94b8c122006-05-27 14:41:55 +00001876 "Floating point operation failed.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001877
1878
1879/*
1880 * OverflowError extends ArithmeticError
1881 */
1882SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
Georg Brandl94b8c122006-05-27 14:41:55 +00001883 "Result too large to be represented.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001884
1885
1886/*
1887 * ZeroDivisionError extends ArithmeticError
1888 */
1889SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
Georg Brandl94b8c122006-05-27 14:41:55 +00001890 "Second argument to a division or modulo operation was zero.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001891
1892
1893/*
1894 * SystemError extends StandardError
1895 */
1896SimpleExtendsException(PyExc_StandardError, SystemError,
1897 "Internal error in the Python interpreter.\n"
1898 "\n"
1899 "Please report this to the Python maintainer, along with the traceback,\n"
Georg Brandl94b8c122006-05-27 14:41:55 +00001900 "the Python version, and the hardware/OS platform and version.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001901
1902
1903/*
1904 * ReferenceError extends StandardError
1905 */
1906SimpleExtendsException(PyExc_StandardError, ReferenceError,
Georg Brandl94b8c122006-05-27 14:41:55 +00001907 "Weak ref proxy used after referent went away.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001908
1909
1910/*
1911 * MemoryError extends StandardError
1912 */
Georg Brandl94b8c122006-05-27 14:41:55 +00001913SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001914
1915
1916/* Warning category docstrings */
1917
1918/*
1919 * Warning extends Exception
1920 */
1921SimpleExtendsException(PyExc_Exception, Warning,
Georg Brandl94b8c122006-05-27 14:41:55 +00001922 "Base class for warning categories.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001923
1924
1925/*
1926 * UserWarning extends Warning
1927 */
1928SimpleExtendsException(PyExc_Warning, UserWarning,
Georg Brandl94b8c122006-05-27 14:41:55 +00001929 "Base class for warnings generated by user code.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001930
1931
1932/*
1933 * DeprecationWarning extends Warning
1934 */
1935SimpleExtendsException(PyExc_Warning, DeprecationWarning,
Georg Brandl94b8c122006-05-27 14:41:55 +00001936 "Base class for warnings about deprecated features.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001937
1938
1939/*
1940 * PendingDeprecationWarning extends Warning
1941 */
1942SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
1943 "Base class for warnings about features which will be deprecated\n"
Georg Brandl94b8c122006-05-27 14:41:55 +00001944 "in the future.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001945
1946
1947/*
1948 * SyntaxWarning extends Warning
1949 */
1950SimpleExtendsException(PyExc_Warning, SyntaxWarning,
Georg Brandl94b8c122006-05-27 14:41:55 +00001951 "Base class for warnings about dubious syntax.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001952
1953
1954/*
1955 * RuntimeWarning extends Warning
1956 */
1957SimpleExtendsException(PyExc_Warning, RuntimeWarning,
Georg Brandl94b8c122006-05-27 14:41:55 +00001958 "Base class for warnings about dubious runtime behavior.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001959
1960
1961/*
1962 * FutureWarning extends Warning
1963 */
1964SimpleExtendsException(PyExc_Warning, FutureWarning,
1965 "Base class for warnings about constructs that will change semantically\n"
Georg Brandl94b8c122006-05-27 14:41:55 +00001966 "in the future.")
Richard Jones7b9558d2006-05-27 12:29:24 +00001967
1968
1969/*
1970 * ImportWarning extends Warning
1971 */
1972SimpleExtendsException(PyExc_Warning, ImportWarning,
Georg Brandl94b8c122006-05-27 14:41:55 +00001973 "Base class for warnings about probable mistakes in module imports")
Richard Jones7b9558d2006-05-27 12:29:24 +00001974
1975
1976/* Pre-computed MemoryError instance. Best to create this as early as
1977 * possible and not wait until a MemoryError is actually raised!
1978 */
1979PyObject *PyExc_MemoryErrorInst=NULL;
1980
1981/* module global functions */
1982static PyMethodDef functions[] = {
1983 /* Sentinel */
1984 {NULL, NULL}
1985};
1986
1987#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
1988 Py_FatalError("exceptions bootstrapping error.");
1989
1990#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
1991 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
1992 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
1993 Py_FatalError("Module dictionary insertion problem.");
1994
1995PyMODINIT_FUNC
1996_PyExc_Init(void)
1997{
1998 PyObject *m, *bltinmod, *bdict;
1999
2000 PRE_INIT(BaseException)
2001 PRE_INIT(Exception)
2002 PRE_INIT(StandardError)
2003 PRE_INIT(TypeError)
2004 PRE_INIT(StopIteration)
2005 PRE_INIT(GeneratorExit)
2006 PRE_INIT(SystemExit)
2007 PRE_INIT(KeyboardInterrupt)
2008 PRE_INIT(ImportError)
2009 PRE_INIT(EnvironmentError)
2010 PRE_INIT(IOError)
2011 PRE_INIT(OSError)
2012#ifdef MS_WINDOWS
2013 PRE_INIT(WindowsError)
2014#endif
2015#ifdef __VMS
2016 PRE_INIT(VMSError)
2017#endif
2018 PRE_INIT(EOFError)
2019 PRE_INIT(RuntimeError)
2020 PRE_INIT(NotImplementedError)
2021 PRE_INIT(NameError)
2022 PRE_INIT(UnboundLocalError)
2023 PRE_INIT(AttributeError)
2024 PRE_INIT(SyntaxError)
2025 PRE_INIT(IndentationError)
2026 PRE_INIT(TabError)
2027 PRE_INIT(LookupError)
2028 PRE_INIT(IndexError)
2029 PRE_INIT(KeyError)
2030 PRE_INIT(ValueError)
2031 PRE_INIT(UnicodeError)
2032#ifdef Py_USING_UNICODE
2033 PRE_INIT(UnicodeEncodeError)
2034 PRE_INIT(UnicodeDecodeError)
2035 PRE_INIT(UnicodeTranslateError)
2036#endif
2037 PRE_INIT(AssertionError)
2038 PRE_INIT(ArithmeticError)
2039 PRE_INIT(FloatingPointError)
2040 PRE_INIT(OverflowError)
2041 PRE_INIT(ZeroDivisionError)
2042 PRE_INIT(SystemError)
2043 PRE_INIT(ReferenceError)
2044 PRE_INIT(MemoryError)
2045 PRE_INIT(Warning)
2046 PRE_INIT(UserWarning)
2047 PRE_INIT(DeprecationWarning)
2048 PRE_INIT(PendingDeprecationWarning)
2049 PRE_INIT(SyntaxWarning)
2050 PRE_INIT(RuntimeWarning)
2051 PRE_INIT(FutureWarning)
2052 PRE_INIT(ImportWarning)
2053
2054 m = Py_InitModule("exceptions", functions);
2055 if (m == NULL) return;
2056
2057 bltinmod = PyImport_ImportModule("__builtin__");
2058 if (bltinmod == NULL)
2059 Py_FatalError("exceptions bootstrapping error.");
2060 bdict = PyModule_GetDict(bltinmod);
2061 if (bdict == NULL)
2062 Py_FatalError("exceptions bootstrapping error.");
2063
2064 POST_INIT(BaseException)
2065 POST_INIT(Exception)
2066 POST_INIT(StandardError)
2067 POST_INIT(TypeError)
2068 POST_INIT(StopIteration)
2069 POST_INIT(GeneratorExit)
2070 POST_INIT(SystemExit)
2071 POST_INIT(KeyboardInterrupt)
2072 POST_INIT(ImportError)
2073 POST_INIT(EnvironmentError)
2074 POST_INIT(IOError)
2075 POST_INIT(OSError)
2076#ifdef MS_WINDOWS
2077 POST_INIT(WindowsError)
2078#endif
2079#ifdef __VMS
2080 POST_INIT(VMSError)
2081#endif
2082 POST_INIT(EOFError)
2083 POST_INIT(RuntimeError)
2084 POST_INIT(NotImplementedError)
2085 POST_INIT(NameError)
2086 POST_INIT(UnboundLocalError)
2087 POST_INIT(AttributeError)
2088 POST_INIT(SyntaxError)
2089 POST_INIT(IndentationError)
2090 POST_INIT(TabError)
2091 POST_INIT(LookupError)
2092 POST_INIT(IndexError)
2093 POST_INIT(KeyError)
2094 POST_INIT(ValueError)
2095 POST_INIT(UnicodeError)
2096#ifdef Py_USING_UNICODE
2097 POST_INIT(UnicodeEncodeError)
2098 POST_INIT(UnicodeDecodeError)
2099 POST_INIT(UnicodeTranslateError)
2100#endif
2101 POST_INIT(AssertionError)
2102 POST_INIT(ArithmeticError)
2103 POST_INIT(FloatingPointError)
2104 POST_INIT(OverflowError)
2105 POST_INIT(ZeroDivisionError)
2106 POST_INIT(SystemError)
2107 POST_INIT(ReferenceError)
2108 POST_INIT(MemoryError)
2109 POST_INIT(Warning)
2110 POST_INIT(UserWarning)
2111 POST_INIT(DeprecationWarning)
2112 POST_INIT(PendingDeprecationWarning)
2113 POST_INIT(SyntaxWarning)
2114 POST_INIT(RuntimeWarning)
2115 POST_INIT(FutureWarning)
2116 POST_INIT(ImportWarning)
2117
2118 PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
2119 if (!PyExc_MemoryErrorInst)
2120 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
2121
2122 Py_DECREF(bltinmod);
2123}
2124
2125void
2126_PyExc_Fini(void)
2127{
2128 Py_XDECREF(PyExc_MemoryErrorInst);
2129 PyExc_MemoryErrorInst = NULL;
2130}