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