2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
4 * Thanks go to Tim Peters and Michael Hudson for debugging.
7 #define PY_SSIZE_T_CLEAN
9 #include "structmember.h"
12 #define EXC_MODULE_NAME "exceptions."
14 /* NOTE: If the exception class hierarchy changes, don't forget to update
15 * Lib/test/exception_hierarchy.txt
18 PyDoc_STRVAR(exceptions_doc
, "Python's standard exception class hierarchy.\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\
30 BaseException_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
32 PyBaseExceptionObject
*self
;
34 self
= (PyBaseExceptionObject
*)type
->tp_alloc(type
, 0);
37 /* the dict is created on the fly in PyObject_GenericSetAttr */
38 self
->message
= self
->dict
= NULL
;
40 self
->args
= PyTuple_New(0);
46 self
->message
= PyString_FromString("");
52 return (PyObject
*)self
;
56 BaseException_init(PyBaseExceptionObject
*self
, PyObject
*args
, PyObject
*kwds
)
58 if (!_PyArg_NoKeywords(Py_TYPE(self
)->tp_name
, kwds
))
61 Py_DECREF(self
->args
);
63 Py_INCREF(self
->args
);
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
);
74 BaseException_clear(PyBaseExceptionObject
*self
)
78 Py_CLEAR(self
->message
);
83 BaseException_dealloc(PyBaseExceptionObject
*self
)
85 _PyObject_GC_UNTRACK(self
);
86 BaseException_clear(self
);
87 Py_TYPE(self
)->tp_free((PyObject
*)self
);
91 BaseException_traverse(PyBaseExceptionObject
*self
, visitproc visit
, void *arg
)
95 Py_VISIT(self
->message
);
100 BaseException_str(PyBaseExceptionObject
*self
)
104 switch (PyTuple_GET_SIZE(self
->args
)) {
106 out
= PyString_FromString("");
109 out
= PyObject_Str(PyTuple_GET_ITEM(self
->args
, 0));
112 out
= PyObject_Str(self
->args
);
119 #ifdef Py_USING_UNICODE
121 BaseException_unicode(PyBaseExceptionObject
*self
)
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
) {
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
);
135 out
= PyObject_Unicode(str
);
140 switch (PyTuple_GET_SIZE(self
->args
)) {
142 out
= PyUnicode_FromString("");
145 out
= PyObject_Unicode(PyTuple_GET_ITEM(self
->args
, 0));
148 out
= PyObject_Unicode(self
->args
);
157 BaseException_repr(PyBaseExceptionObject
*self
)
159 PyObject
*repr_suffix
;
164 repr_suffix
= PyObject_Repr(self
->args
);
168 name
= (char *)Py_TYPE(self
)->tp_name
;
169 dot
= strrchr(name
, '.');
170 if (dot
!= NULL
) name
= dot
+1;
172 repr
= PyString_FromString(name
);
174 Py_DECREF(repr_suffix
);
178 PyString_ConcatAndDel(&repr
, repr_suffix
);
182 /* Pickling support */
184 BaseException_reduce(PyBaseExceptionObject
*self
)
186 if (self
->args
&& self
->dict
)
187 return PyTuple_Pack(3, Py_TYPE(self
), self
->args
, self
->dict
);
189 return PyTuple_Pack(2, Py_TYPE(self
), self
->args
);
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.
198 BaseException_setstate(PyObject
*self
, PyObject
*state
)
200 PyObject
*d_key
, *d_value
;
203 if (state
!= Py_None
) {
204 if (!PyDict_Check(state
)) {
205 PyErr_SetString(PyExc_TypeError
, "state is not a dictionary");
208 while (PyDict_Next(state
, &i
, &d_key
, &d_value
)) {
209 if (PyObject_SetAttr(self
, d_key
, d_value
) < 0)
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
},
223 {NULL
, NULL
, 0, NULL
},
229 BaseException_getitem(PyBaseExceptionObject
*self
, Py_ssize_t index
)
231 if (PyErr_WarnPy3k("__getitem__ not supported for exception "
232 "classes in 3.x; use args attribute", 1) < 0)
234 return PySequence_GetItem(self
->args
, index
);
238 BaseException_getslice(PyBaseExceptionObject
*self
,
239 Py_ssize_t start
, Py_ssize_t stop
)
241 if (PyErr_WarnPy3k("__getslice__ not supported for exception "
242 "classes in 3.x; use args attribute", 1) < 0)
244 return PySequence_GetSlice(self
->args
, start
, stop
);
247 static PySequenceMethods BaseException_as_sequence
= {
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; */
261 BaseException_get_dict(PyBaseExceptionObject
*self
)
263 if (self
->dict
== NULL
) {
264 self
->dict
= PyDict_New();
268 Py_INCREF(self
->dict
);
273 BaseException_set_dict(PyBaseExceptionObject
*self
, PyObject
*val
)
276 PyErr_SetString(PyExc_TypeError
, "__dict__ may not be deleted");
279 if (!PyDict_Check(val
)) {
280 PyErr_SetString(PyExc_TypeError
, "__dict__ must be a dictionary");
283 Py_CLEAR(self
->dict
);
290 BaseException_get_args(PyBaseExceptionObject
*self
)
292 if (self
->args
== NULL
) {
296 Py_INCREF(self
->args
);
301 BaseException_set_args(PyBaseExceptionObject
*self
, PyObject
*val
)
305 PyErr_SetString(PyExc_TypeError
, "args may not be deleted");
308 seq
= PySequence_Tuple(val
);
311 Py_CLEAR(self
->args
);
317 BaseException_get_message(PyBaseExceptionObject
*self
)
321 /* if "message" is in self->dict, accessing a user-set message attribute */
323 (msg
= PyDict_GetItemString(self
->dict
, "message"))) {
328 if (self
->message
== NULL
) {
329 PyErr_SetString(PyExc_AttributeError
, "message attribute was deleted");
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)
339 Py_INCREF(self
->message
);
340 return self
->message
;
344 BaseException_set_message(PyBaseExceptionObject
*self
, PyObject
*val
)
346 /* if val is NULL, delete the message attribute */
348 if (self
->dict
&& PyDict_GetItemString(self
->dict
, "message")) {
349 if (PyDict_DelItemString(self
->dict
, "message") < 0)
352 Py_CLEAR(self
->message
);
356 /* else set it in __dict__, but may need to create the dict first */
357 if (self
->dict
== NULL
) {
358 self
->dict
= PyDict_New();
362 return PyDict_SetItemString(self
->dict
, "message", val
);
365 static PyGetSetDef BaseException_getset
[] = {
366 {"__dict__", (getter
)BaseException_get_dict
, (setter
)BaseException_set_dict
},
367 {"args", (getter
)BaseException_get_args
, (setter
)BaseException_set_args
},
368 {"message", (getter
)BaseException_get_message
,
369 (setter
)BaseException_set_message
},
374 static PyTypeObject _PyExc_BaseException
= {
375 PyObject_HEAD_INIT(NULL
)
377 EXC_MODULE_NAME
"BaseException", /*tp_name*/
378 sizeof(PyBaseExceptionObject
), /*tp_basicsize*/
380 (destructor
)BaseException_dealloc
, /*tp_dealloc*/
385 (reprfunc
)BaseException_repr
, /*tp_repr*/
387 &BaseException_as_sequence
, /*tp_as_sequence*/
391 (reprfunc
)BaseException_str
, /*tp_str*/
392 PyObject_GenericGetAttr
, /*tp_getattro*/
393 PyObject_GenericSetAttr
, /*tp_setattro*/
395 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
|
396 Py_TPFLAGS_BASE_EXC_SUBCLASS
, /*tp_flags*/
397 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
398 (traverseproc
)BaseException_traverse
, /* tp_traverse */
399 (inquiry
)BaseException_clear
, /* tp_clear */
400 0, /* tp_richcompare */
401 0, /* tp_weaklistoffset */
404 BaseException_methods
, /* tp_methods */
406 BaseException_getset
, /* tp_getset */
409 0, /* tp_descr_get */
410 0, /* tp_descr_set */
411 offsetof(PyBaseExceptionObject
, dict
), /* tp_dictoffset */
412 (initproc
)BaseException_init
, /* tp_init */
414 BaseException_new
, /* tp_new */
416 /* the CPython API expects exceptions to be (PyObject *) - both a hold-over
417 from the previous implmentation and also allowing Python objects to be used
419 PyObject
*PyExc_BaseException
= (PyObject
*)&_PyExc_BaseException
;
421 /* note these macros omit the last semicolon so the macro invocation may
422 * include it and not look strange.
424 #define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
425 static PyTypeObject _PyExc_ ## EXCNAME = { \
426 PyObject_HEAD_INIT(NULL) \
428 EXC_MODULE_NAME # EXCNAME, \
429 sizeof(PyBaseExceptionObject), \
430 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
431 0, 0, 0, 0, 0, 0, 0, \
432 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
433 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
434 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
435 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
436 (initproc)BaseException_init, 0, BaseException_new,\
438 PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
440 #define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
441 static PyTypeObject _PyExc_ ## EXCNAME = { \
442 PyObject_HEAD_INIT(NULL) \
444 EXC_MODULE_NAME # EXCNAME, \
445 sizeof(Py ## EXCSTORE ## Object), \
446 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
448 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
449 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
450 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
451 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
452 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
454 PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
456 #define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
457 static PyTypeObject _PyExc_ ## EXCNAME = { \
458 PyObject_HEAD_INIT(NULL) \
460 EXC_MODULE_NAME # EXCNAME, \
461 sizeof(Py ## EXCSTORE ## Object), 0, \
462 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
463 (reprfunc)EXCSTR, 0, 0, 0, \
464 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
465 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
466 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
467 EXCMEMBERS, 0, &_ ## EXCBASE, \
468 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
469 (initproc)EXCSTORE ## _init, 0, BaseException_new,\
471 PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
475 * Exception extends BaseException
477 SimpleExtendsException(PyExc_BaseException
, Exception
,
478 "Common base class for all non-exit exceptions.");
482 * StandardError extends Exception
484 SimpleExtendsException(PyExc_Exception
, StandardError
,
485 "Base class for all standard Python exceptions that do not represent\n"
486 "interpreter exiting.");
490 * TypeError extends StandardError
492 SimpleExtendsException(PyExc_StandardError
, TypeError
,
493 "Inappropriate argument type.");
497 * StopIteration extends Exception
499 SimpleExtendsException(PyExc_Exception
, StopIteration
,
500 "Signal the end from iterator.next().");
504 * GeneratorExit extends BaseException
506 SimpleExtendsException(PyExc_BaseException
, GeneratorExit
,
507 "Request that a generator exit.");
511 * SystemExit extends BaseException
515 SystemExit_init(PySystemExitObject
*self
, PyObject
*args
, PyObject
*kwds
)
517 Py_ssize_t size
= PyTuple_GET_SIZE(args
);
519 if (BaseException_init((PyBaseExceptionObject
*)self
, args
, kwds
) == -1)
524 Py_CLEAR(self
->code
);
526 self
->code
= PyTuple_GET_ITEM(args
, 0);
529 Py_INCREF(self
->code
);
534 SystemExit_clear(PySystemExitObject
*self
)
536 Py_CLEAR(self
->code
);
537 return BaseException_clear((PyBaseExceptionObject
*)self
);
541 SystemExit_dealloc(PySystemExitObject
*self
)
543 _PyObject_GC_UNTRACK(self
);
544 SystemExit_clear(self
);
545 Py_TYPE(self
)->tp_free((PyObject
*)self
);
549 SystemExit_traverse(PySystemExitObject
*self
, visitproc visit
, void *arg
)
551 Py_VISIT(self
->code
);
552 return BaseException_traverse((PyBaseExceptionObject
*)self
, visit
, arg
);
555 static PyMemberDef SystemExit_members
[] = {
556 {"code", T_OBJECT
, offsetof(PySystemExitObject
, code
), 0,
557 PyDoc_STR("exception code")},
558 {NULL
} /* Sentinel */
561 ComplexExtendsException(PyExc_BaseException
, SystemExit
, SystemExit
,
562 SystemExit_dealloc
, 0, SystemExit_members
, 0,
563 "Request to exit from the interpreter.");
566 * KeyboardInterrupt extends BaseException
568 SimpleExtendsException(PyExc_BaseException
, KeyboardInterrupt
,
569 "Program interrupted by user.");
573 * ImportError extends StandardError
575 SimpleExtendsException(PyExc_StandardError
, ImportError
,
576 "Import can't find module, or can't find name in module.");
580 * EnvironmentError extends StandardError
583 /* Where a function has a single filename, such as open() or some
584 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
585 * called, giving a third argument which is the filename. But, so
586 * that old code using in-place unpacking doesn't break, e.g.:
588 * except IOError, (errno, strerror):
590 * we hack args so that it only contains two items. This also
591 * means we need our own __str__() which prints out the filename
592 * when it was supplied.
595 EnvironmentError_init(PyEnvironmentErrorObject
*self
, PyObject
*args
,
598 PyObject
*myerrno
= NULL
, *strerror
= NULL
, *filename
= NULL
;
599 PyObject
*subslice
= NULL
;
601 if (BaseException_init((PyBaseExceptionObject
*)self
, args
, kwds
) == -1)
604 if (PyTuple_GET_SIZE(args
) <= 1 || PyTuple_GET_SIZE(args
) > 3) {
608 if (!PyArg_UnpackTuple(args
, "EnvironmentError", 2, 3,
609 &myerrno
, &strerror
, &filename
)) {
612 Py_CLEAR(self
->myerrno
); /* replacing */
613 self
->myerrno
= myerrno
;
614 Py_INCREF(self
->myerrno
);
616 Py_CLEAR(self
->strerror
); /* replacing */
617 self
->strerror
= strerror
;
618 Py_INCREF(self
->strerror
);
620 /* self->filename will remain Py_None otherwise */
621 if (filename
!= NULL
) {
622 Py_CLEAR(self
->filename
); /* replacing */
623 self
->filename
= filename
;
624 Py_INCREF(self
->filename
);
626 subslice
= PyTuple_GetSlice(args
, 0, 2);
630 Py_DECREF(self
->args
); /* replacing args */
631 self
->args
= subslice
;
637 EnvironmentError_clear(PyEnvironmentErrorObject
*self
)
639 Py_CLEAR(self
->myerrno
);
640 Py_CLEAR(self
->strerror
);
641 Py_CLEAR(self
->filename
);
642 return BaseException_clear((PyBaseExceptionObject
*)self
);
646 EnvironmentError_dealloc(PyEnvironmentErrorObject
*self
)
648 _PyObject_GC_UNTRACK(self
);
649 EnvironmentError_clear(self
);
650 Py_TYPE(self
)->tp_free((PyObject
*)self
);
654 EnvironmentError_traverse(PyEnvironmentErrorObject
*self
, visitproc visit
,
657 Py_VISIT(self
->myerrno
);
658 Py_VISIT(self
->strerror
);
659 Py_VISIT(self
->filename
);
660 return BaseException_traverse((PyBaseExceptionObject
*)self
, visit
, arg
);
664 EnvironmentError_str(PyEnvironmentErrorObject
*self
)
666 PyObject
*rtnval
= NULL
;
668 if (self
->filename
) {
673 fmt
= PyString_FromString("[Errno %s] %s: %s");
677 repr
= PyObject_Repr(self
->filename
);
682 tuple
= PyTuple_New(3);
690 Py_INCREF(self
->myerrno
);
691 PyTuple_SET_ITEM(tuple
, 0, self
->myerrno
);
695 PyTuple_SET_ITEM(tuple
, 0, Py_None
);
697 if (self
->strerror
) {
698 Py_INCREF(self
->strerror
);
699 PyTuple_SET_ITEM(tuple
, 1, self
->strerror
);
703 PyTuple_SET_ITEM(tuple
, 1, Py_None
);
706 PyTuple_SET_ITEM(tuple
, 2, repr
);
708 rtnval
= PyString_Format(fmt
, tuple
);
713 else if (self
->myerrno
&& self
->strerror
) {
717 fmt
= PyString_FromString("[Errno %s] %s");
721 tuple
= PyTuple_New(2);
728 Py_INCREF(self
->myerrno
);
729 PyTuple_SET_ITEM(tuple
, 0, self
->myerrno
);
733 PyTuple_SET_ITEM(tuple
, 0, Py_None
);
735 if (self
->strerror
) {
736 Py_INCREF(self
->strerror
);
737 PyTuple_SET_ITEM(tuple
, 1, self
->strerror
);
741 PyTuple_SET_ITEM(tuple
, 1, Py_None
);
744 rtnval
= PyString_Format(fmt
, tuple
);
750 rtnval
= BaseException_str((PyBaseExceptionObject
*)self
);
755 static PyMemberDef EnvironmentError_members
[] = {
756 {"errno", T_OBJECT
, offsetof(PyEnvironmentErrorObject
, myerrno
), 0,
757 PyDoc_STR("exception errno")},
758 {"strerror", T_OBJECT
, offsetof(PyEnvironmentErrorObject
, strerror
), 0,
759 PyDoc_STR("exception strerror")},
760 {"filename", T_OBJECT
, offsetof(PyEnvironmentErrorObject
, filename
), 0,
761 PyDoc_STR("exception filename")},
762 {NULL
} /* Sentinel */
767 EnvironmentError_reduce(PyEnvironmentErrorObject
*self
)
769 PyObject
*args
= self
->args
;
770 PyObject
*res
= NULL
, *tmp
;
772 /* self->args is only the first two real arguments if there was a
773 * file name given to EnvironmentError. */
774 if (PyTuple_GET_SIZE(args
) == 2 && self
->filename
) {
775 args
= PyTuple_New(3);
779 tmp
= PyTuple_GET_ITEM(self
->args
, 0);
781 PyTuple_SET_ITEM(args
, 0, tmp
);
783 tmp
= PyTuple_GET_ITEM(self
->args
, 1);
785 PyTuple_SET_ITEM(args
, 1, tmp
);
787 Py_INCREF(self
->filename
);
788 PyTuple_SET_ITEM(args
, 2, self
->filename
);
793 res
= PyTuple_Pack(3, Py_TYPE(self
), args
, self
->dict
);
795 res
= PyTuple_Pack(2, Py_TYPE(self
), args
);
801 static PyMethodDef EnvironmentError_methods
[] = {
802 {"__reduce__", (PyCFunction
)EnvironmentError_reduce
, METH_NOARGS
},
806 ComplexExtendsException(PyExc_StandardError
, EnvironmentError
,
807 EnvironmentError
, EnvironmentError_dealloc
,
808 EnvironmentError_methods
, EnvironmentError_members
,
809 EnvironmentError_str
,
810 "Base class for I/O related errors.");
814 * IOError extends EnvironmentError
816 MiddlingExtendsException(PyExc_EnvironmentError
, IOError
,
817 EnvironmentError
, "I/O operation failed.");
821 * OSError extends EnvironmentError
823 MiddlingExtendsException(PyExc_EnvironmentError
, OSError
,
824 EnvironmentError
, "OS system call failed.");
828 * WindowsError extends OSError
834 WindowsError_clear(PyWindowsErrorObject
*self
)
836 Py_CLEAR(self
->myerrno
);
837 Py_CLEAR(self
->strerror
);
838 Py_CLEAR(self
->filename
);
839 Py_CLEAR(self
->winerror
);
840 return BaseException_clear((PyBaseExceptionObject
*)self
);
844 WindowsError_dealloc(PyWindowsErrorObject
*self
)
846 _PyObject_GC_UNTRACK(self
);
847 WindowsError_clear(self
);
848 Py_TYPE(self
)->tp_free((PyObject
*)self
);
852 WindowsError_traverse(PyWindowsErrorObject
*self
, visitproc visit
, void *arg
)
854 Py_VISIT(self
->myerrno
);
855 Py_VISIT(self
->strerror
);
856 Py_VISIT(self
->filename
);
857 Py_VISIT(self
->winerror
);
858 return BaseException_traverse((PyBaseExceptionObject
*)self
, visit
, arg
);
862 WindowsError_init(PyWindowsErrorObject
*self
, PyObject
*args
, PyObject
*kwds
)
864 PyObject
*o_errcode
= NULL
;
868 if (EnvironmentError_init((PyEnvironmentErrorObject
*)self
, args
, kwds
)
872 if (self
->myerrno
== NULL
)
875 /* Set errno to the POSIX errno, and winerror to the Win32
877 errcode
= PyInt_AsLong(self
->myerrno
);
878 if (errcode
== -1 && PyErr_Occurred())
880 posix_errno
= winerror_to_errno(errcode
);
882 Py_CLEAR(self
->winerror
);
883 self
->winerror
= self
->myerrno
;
885 o_errcode
= PyInt_FromLong(posix_errno
);
889 self
->myerrno
= o_errcode
;
896 WindowsError_str(PyWindowsErrorObject
*self
)
898 PyObject
*rtnval
= NULL
;
900 if (self
->filename
) {
905 fmt
= PyString_FromString("[Error %s] %s: %s");
909 repr
= PyObject_Repr(self
->filename
);
914 tuple
= PyTuple_New(3);
921 if (self
->winerror
) {
922 Py_INCREF(self
->winerror
);
923 PyTuple_SET_ITEM(tuple
, 0, self
->winerror
);
927 PyTuple_SET_ITEM(tuple
, 0, Py_None
);
929 if (self
->strerror
) {
930 Py_INCREF(self
->strerror
);
931 PyTuple_SET_ITEM(tuple
, 1, self
->strerror
);
935 PyTuple_SET_ITEM(tuple
, 1, Py_None
);
938 PyTuple_SET_ITEM(tuple
, 2, repr
);
940 rtnval
= PyString_Format(fmt
, tuple
);
945 else if (self
->winerror
&& self
->strerror
) {
949 fmt
= PyString_FromString("[Error %s] %s");
953 tuple
= PyTuple_New(2);
959 if (self
->winerror
) {
960 Py_INCREF(self
->winerror
);
961 PyTuple_SET_ITEM(tuple
, 0, self
->winerror
);
965 PyTuple_SET_ITEM(tuple
, 0, Py_None
);
967 if (self
->strerror
) {
968 Py_INCREF(self
->strerror
);
969 PyTuple_SET_ITEM(tuple
, 1, self
->strerror
);
973 PyTuple_SET_ITEM(tuple
, 1, Py_None
);
976 rtnval
= PyString_Format(fmt
, tuple
);
982 rtnval
= EnvironmentError_str((PyEnvironmentErrorObject
*)self
);
987 static PyMemberDef WindowsError_members
[] = {
988 {"errno", T_OBJECT
, offsetof(PyWindowsErrorObject
, myerrno
), 0,
989 PyDoc_STR("POSIX exception code")},
990 {"strerror", T_OBJECT
, offsetof(PyWindowsErrorObject
, strerror
), 0,
991 PyDoc_STR("exception strerror")},
992 {"filename", T_OBJECT
, offsetof(PyWindowsErrorObject
, filename
), 0,
993 PyDoc_STR("exception filename")},
994 {"winerror", T_OBJECT
, offsetof(PyWindowsErrorObject
, winerror
), 0,
995 PyDoc_STR("Win32 exception code")},
996 {NULL
} /* Sentinel */
999 ComplexExtendsException(PyExc_OSError
, WindowsError
, WindowsError
,
1000 WindowsError_dealloc
, 0, WindowsError_members
,
1001 WindowsError_str
, "MS-Windows OS system call failed.");
1003 #endif /* MS_WINDOWS */
1007 * VMSError extends OSError (I think)
1010 MiddlingExtendsException(PyExc_OSError
, VMSError
, EnvironmentError
,
1011 "OpenVMS OS system call failed.");
1016 * EOFError extends StandardError
1018 SimpleExtendsException(PyExc_StandardError
, EOFError
,
1019 "Read beyond end of file.");
1023 * RuntimeError extends StandardError
1025 SimpleExtendsException(PyExc_StandardError
, RuntimeError
,
1026 "Unspecified run-time error.");
1030 * NotImplementedError extends RuntimeError
1032 SimpleExtendsException(PyExc_RuntimeError
, NotImplementedError
,
1033 "Method or function hasn't been implemented yet.");
1036 * NameError extends StandardError
1038 SimpleExtendsException(PyExc_StandardError
, NameError
,
1039 "Name not found globally.");
1042 * UnboundLocalError extends NameError
1044 SimpleExtendsException(PyExc_NameError
, UnboundLocalError
,
1045 "Local name referenced but not bound to a value.");
1048 * AttributeError extends StandardError
1050 SimpleExtendsException(PyExc_StandardError
, AttributeError
,
1051 "Attribute not found.");
1055 * SyntaxError extends StandardError
1059 SyntaxError_init(PySyntaxErrorObject
*self
, PyObject
*args
, PyObject
*kwds
)
1061 PyObject
*info
= NULL
;
1062 Py_ssize_t lenargs
= PyTuple_GET_SIZE(args
);
1064 if (BaseException_init((PyBaseExceptionObject
*)self
, args
, kwds
) == -1)
1068 Py_CLEAR(self
->msg
);
1069 self
->msg
= PyTuple_GET_ITEM(args
, 0);
1070 Py_INCREF(self
->msg
);
1073 info
= PyTuple_GET_ITEM(args
, 1);
1074 info
= PySequence_Tuple(info
);
1078 if (PyTuple_GET_SIZE(info
) != 4) {
1079 /* not a very good error message, but it's what Python 2.4 gives */
1080 PyErr_SetString(PyExc_IndexError
, "tuple index out of range");
1085 Py_CLEAR(self
->filename
);
1086 self
->filename
= PyTuple_GET_ITEM(info
, 0);
1087 Py_INCREF(self
->filename
);
1089 Py_CLEAR(self
->lineno
);
1090 self
->lineno
= PyTuple_GET_ITEM(info
, 1);
1091 Py_INCREF(self
->lineno
);
1093 Py_CLEAR(self
->offset
);
1094 self
->offset
= PyTuple_GET_ITEM(info
, 2);
1095 Py_INCREF(self
->offset
);
1097 Py_CLEAR(self
->text
);
1098 self
->text
= PyTuple_GET_ITEM(info
, 3);
1099 Py_INCREF(self
->text
);
1107 SyntaxError_clear(PySyntaxErrorObject
*self
)
1109 Py_CLEAR(self
->msg
);
1110 Py_CLEAR(self
->filename
);
1111 Py_CLEAR(self
->lineno
);
1112 Py_CLEAR(self
->offset
);
1113 Py_CLEAR(self
->text
);
1114 Py_CLEAR(self
->print_file_and_line
);
1115 return BaseException_clear((PyBaseExceptionObject
*)self
);
1119 SyntaxError_dealloc(PySyntaxErrorObject
*self
)
1121 _PyObject_GC_UNTRACK(self
);
1122 SyntaxError_clear(self
);
1123 Py_TYPE(self
)->tp_free((PyObject
*)self
);
1127 SyntaxError_traverse(PySyntaxErrorObject
*self
, visitproc visit
, void *arg
)
1129 Py_VISIT(self
->msg
);
1130 Py_VISIT(self
->filename
);
1131 Py_VISIT(self
->lineno
);
1132 Py_VISIT(self
->offset
);
1133 Py_VISIT(self
->text
);
1134 Py_VISIT(self
->print_file_and_line
);
1135 return BaseException_traverse((PyBaseExceptionObject
*)self
, visit
, arg
);
1138 /* This is called "my_basename" instead of just "basename" to avoid name
1139 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1140 defined, and Python does define that. */
1142 my_basename(char *name
)
1145 char *result
= name
;
1149 while (*cp
!= '\0') {
1159 SyntaxError_str(PySyntaxErrorObject
*self
)
1163 int have_filename
= 0;
1164 int have_lineno
= 0;
1165 char *buffer
= NULL
;
1169 str
= PyObject_Str(self
->msg
);
1171 str
= PyObject_Str(Py_None
);
1174 /* Don't fiddle with non-string return (shouldn't happen anyway) */
1175 if (!PyString_Check(str
))
1178 /* XXX -- do all the additional formatting with filename and
1181 have_filename
= (self
->filename
!= NULL
) &&
1182 PyString_Check(self
->filename
);
1183 have_lineno
= (self
->lineno
!= NULL
) && PyInt_Check(self
->lineno
);
1185 if (!have_filename
&& !have_lineno
)
1188 bufsize
= PyString_GET_SIZE(str
) + 64;
1190 bufsize
+= PyString_GET_SIZE(self
->filename
);
1192 buffer
= PyMem_MALLOC(bufsize
);
1196 if (have_filename
&& have_lineno
)
1197 PyOS_snprintf(buffer
, bufsize
, "%s (%s, line %ld)",
1198 PyString_AS_STRING(str
),
1199 my_basename(PyString_AS_STRING(self
->filename
)),
1200 PyInt_AsLong(self
->lineno
));
1201 else if (have_filename
)
1202 PyOS_snprintf(buffer
, bufsize
, "%s (%s)",
1203 PyString_AS_STRING(str
),
1204 my_basename(PyString_AS_STRING(self
->filename
)));
1205 else /* only have_lineno */
1206 PyOS_snprintf(buffer
, bufsize
, "%s (line %ld)",
1207 PyString_AS_STRING(str
),
1208 PyInt_AsLong(self
->lineno
));
1210 result
= PyString_FromString(buffer
);
1220 static PyMemberDef SyntaxError_members
[] = {
1221 {"msg", T_OBJECT
, offsetof(PySyntaxErrorObject
, msg
), 0,
1222 PyDoc_STR("exception msg")},
1223 {"filename", T_OBJECT
, offsetof(PySyntaxErrorObject
, filename
), 0,
1224 PyDoc_STR("exception filename")},
1225 {"lineno", T_OBJECT
, offsetof(PySyntaxErrorObject
, lineno
), 0,
1226 PyDoc_STR("exception lineno")},
1227 {"offset", T_OBJECT
, offsetof(PySyntaxErrorObject
, offset
), 0,
1228 PyDoc_STR("exception offset")},
1229 {"text", T_OBJECT
, offsetof(PySyntaxErrorObject
, text
), 0,
1230 PyDoc_STR("exception text")},
1231 {"print_file_and_line", T_OBJECT
,
1232 offsetof(PySyntaxErrorObject
, print_file_and_line
), 0,
1233 PyDoc_STR("exception print_file_and_line")},
1234 {NULL
} /* Sentinel */
1237 ComplexExtendsException(PyExc_StandardError
, SyntaxError
, SyntaxError
,
1238 SyntaxError_dealloc
, 0, SyntaxError_members
,
1239 SyntaxError_str
, "Invalid syntax.");
1243 * IndentationError extends SyntaxError
1245 MiddlingExtendsException(PyExc_SyntaxError
, IndentationError
, SyntaxError
,
1246 "Improper indentation.");
1250 * TabError extends IndentationError
1252 MiddlingExtendsException(PyExc_IndentationError
, TabError
, SyntaxError
,
1253 "Improper mixture of spaces and tabs.");
1257 * LookupError extends StandardError
1259 SimpleExtendsException(PyExc_StandardError
, LookupError
,
1260 "Base class for lookup errors.");
1264 * IndexError extends LookupError
1266 SimpleExtendsException(PyExc_LookupError
, IndexError
,
1267 "Sequence index out of range.");
1271 * KeyError extends LookupError
1274 KeyError_str(PyBaseExceptionObject
*self
)
1276 /* If args is a tuple of exactly one item, apply repr to args[0].
1277 This is done so that e.g. the exception raised by {}[''] prints
1279 rather than the confusing
1281 alone. The downside is that if KeyError is raised with an explanatory
1282 string, that string will be displayed in quotes. Too bad.
1283 If args is anything else, use the default BaseException__str__().
1285 if (PyTuple_GET_SIZE(self
->args
) == 1) {
1286 return PyObject_Repr(PyTuple_GET_ITEM(self
->args
, 0));
1288 return BaseException_str(self
);
1291 ComplexExtendsException(PyExc_LookupError
, KeyError
, BaseException
,
1292 0, 0, 0, KeyError_str
, "Mapping key not found.");
1296 * ValueError extends StandardError
1298 SimpleExtendsException(PyExc_StandardError
, ValueError
,
1299 "Inappropriate argument value (of correct type).");
1302 * UnicodeError extends ValueError
1305 SimpleExtendsException(PyExc_ValueError
, UnicodeError
,
1306 "Unicode related error.");
1308 #ifdef Py_USING_UNICODE
1310 get_string(PyObject
*attr
, const char *name
)
1313 PyErr_Format(PyExc_TypeError
, "%.200s attribute not set", name
);
1317 if (!PyString_Check(attr
)) {
1318 PyErr_Format(PyExc_TypeError
, "%.200s attribute must be str", name
);
1327 set_string(PyObject
**attr
, const char *value
)
1329 PyObject
*obj
= PyString_FromString(value
);
1339 get_unicode(PyObject
*attr
, const char *name
)
1342 PyErr_Format(PyExc_TypeError
, "%.200s attribute not set", name
);
1346 if (!PyUnicode_Check(attr
)) {
1347 PyErr_Format(PyExc_TypeError
,
1348 "%.200s attribute must be unicode", name
);
1356 PyUnicodeEncodeError_GetEncoding(PyObject
*exc
)
1358 return get_string(((PyUnicodeErrorObject
*)exc
)->encoding
, "encoding");
1362 PyUnicodeDecodeError_GetEncoding(PyObject
*exc
)
1364 return get_string(((PyUnicodeErrorObject
*)exc
)->encoding
, "encoding");
1368 PyUnicodeEncodeError_GetObject(PyObject
*exc
)
1370 return get_unicode(((PyUnicodeErrorObject
*)exc
)->object
, "object");
1374 PyUnicodeDecodeError_GetObject(PyObject
*exc
)
1376 return get_string(((PyUnicodeErrorObject
*)exc
)->object
, "object");
1380 PyUnicodeTranslateError_GetObject(PyObject
*exc
)
1382 return get_unicode(((PyUnicodeErrorObject
*)exc
)->object
, "object");
1386 PyUnicodeEncodeError_GetStart(PyObject
*exc
, Py_ssize_t
*start
)
1389 PyObject
*obj
= get_unicode(((PyUnicodeErrorObject
*)exc
)->object
,
1393 *start
= ((PyUnicodeErrorObject
*)exc
)->start
;
1394 size
= PyUnicode_GET_SIZE(obj
);
1396 *start
= 0; /*XXX check for values <0*/
1405 PyUnicodeDecodeError_GetStart(PyObject
*exc
, Py_ssize_t
*start
)
1408 PyObject
*obj
= get_string(((PyUnicodeErrorObject
*)exc
)->object
,
1412 size
= PyString_GET_SIZE(obj
);
1413 *start
= ((PyUnicodeErrorObject
*)exc
)->start
;
1424 PyUnicodeTranslateError_GetStart(PyObject
*exc
, Py_ssize_t
*start
)
1426 return PyUnicodeEncodeError_GetStart(exc
, start
);
1431 PyUnicodeEncodeError_SetStart(PyObject
*exc
, Py_ssize_t start
)
1433 ((PyUnicodeErrorObject
*)exc
)->start
= start
;
1439 PyUnicodeDecodeError_SetStart(PyObject
*exc
, Py_ssize_t start
)
1441 ((PyUnicodeErrorObject
*)exc
)->start
= start
;
1447 PyUnicodeTranslateError_SetStart(PyObject
*exc
, Py_ssize_t start
)
1449 ((PyUnicodeErrorObject
*)exc
)->start
= start
;
1455 PyUnicodeEncodeError_GetEnd(PyObject
*exc
, Py_ssize_t
*end
)
1458 PyObject
*obj
= get_unicode(((PyUnicodeErrorObject
*)exc
)->object
,
1462 *end
= ((PyUnicodeErrorObject
*)exc
)->end
;
1463 size
= PyUnicode_GET_SIZE(obj
);
1474 PyUnicodeDecodeError_GetEnd(PyObject
*exc
, Py_ssize_t
*end
)
1477 PyObject
*obj
= get_string(((PyUnicodeErrorObject
*)exc
)->object
,
1481 *end
= ((PyUnicodeErrorObject
*)exc
)->end
;
1482 size
= PyString_GET_SIZE(obj
);
1493 PyUnicodeTranslateError_GetEnd(PyObject
*exc
, Py_ssize_t
*start
)
1495 return PyUnicodeEncodeError_GetEnd(exc
, start
);
1500 PyUnicodeEncodeError_SetEnd(PyObject
*exc
, Py_ssize_t end
)
1502 ((PyUnicodeErrorObject
*)exc
)->end
= end
;
1508 PyUnicodeDecodeError_SetEnd(PyObject
*exc
, Py_ssize_t end
)
1510 ((PyUnicodeErrorObject
*)exc
)->end
= end
;
1516 PyUnicodeTranslateError_SetEnd(PyObject
*exc
, Py_ssize_t end
)
1518 ((PyUnicodeErrorObject
*)exc
)->end
= end
;
1523 PyUnicodeEncodeError_GetReason(PyObject
*exc
)
1525 return get_string(((PyUnicodeErrorObject
*)exc
)->reason
, "reason");
1530 PyUnicodeDecodeError_GetReason(PyObject
*exc
)
1532 return get_string(((PyUnicodeErrorObject
*)exc
)->reason
, "reason");
1537 PyUnicodeTranslateError_GetReason(PyObject
*exc
)
1539 return get_string(((PyUnicodeErrorObject
*)exc
)->reason
, "reason");
1544 PyUnicodeEncodeError_SetReason(PyObject
*exc
, const char *reason
)
1546 return set_string(&((PyUnicodeErrorObject
*)exc
)->reason
, reason
);
1551 PyUnicodeDecodeError_SetReason(PyObject
*exc
, const char *reason
)
1553 return set_string(&((PyUnicodeErrorObject
*)exc
)->reason
, reason
);
1558 PyUnicodeTranslateError_SetReason(PyObject
*exc
, const char *reason
)
1560 return set_string(&((PyUnicodeErrorObject
*)exc
)->reason
, reason
);
1565 UnicodeError_init(PyUnicodeErrorObject
*self
, PyObject
*args
, PyObject
*kwds
,
1566 PyTypeObject
*objecttype
)
1568 Py_CLEAR(self
->encoding
);
1569 Py_CLEAR(self
->object
);
1570 Py_CLEAR(self
->reason
);
1572 if (!PyArg_ParseTuple(args
, "O!O!nnO!",
1573 &PyString_Type
, &self
->encoding
,
1574 objecttype
, &self
->object
,
1577 &PyString_Type
, &self
->reason
)) {
1578 self
->encoding
= self
->object
= self
->reason
= NULL
;
1582 Py_INCREF(self
->encoding
);
1583 Py_INCREF(self
->object
);
1584 Py_INCREF(self
->reason
);
1590 UnicodeError_clear(PyUnicodeErrorObject
*self
)
1592 Py_CLEAR(self
->encoding
);
1593 Py_CLEAR(self
->object
);
1594 Py_CLEAR(self
->reason
);
1595 return BaseException_clear((PyBaseExceptionObject
*)self
);
1599 UnicodeError_dealloc(PyUnicodeErrorObject
*self
)
1601 _PyObject_GC_UNTRACK(self
);
1602 UnicodeError_clear(self
);
1603 Py_TYPE(self
)->tp_free((PyObject
*)self
);
1607 UnicodeError_traverse(PyUnicodeErrorObject
*self
, visitproc visit
, void *arg
)
1609 Py_VISIT(self
->encoding
);
1610 Py_VISIT(self
->object
);
1611 Py_VISIT(self
->reason
);
1612 return BaseException_traverse((PyBaseExceptionObject
*)self
, visit
, arg
);
1615 static PyMemberDef UnicodeError_members
[] = {
1616 {"encoding", T_OBJECT
, offsetof(PyUnicodeErrorObject
, encoding
), 0,
1617 PyDoc_STR("exception encoding")},
1618 {"object", T_OBJECT
, offsetof(PyUnicodeErrorObject
, object
), 0,
1619 PyDoc_STR("exception object")},
1620 {"start", T_PYSSIZET
, offsetof(PyUnicodeErrorObject
, start
), 0,
1621 PyDoc_STR("exception start")},
1622 {"end", T_PYSSIZET
, offsetof(PyUnicodeErrorObject
, end
), 0,
1623 PyDoc_STR("exception end")},
1624 {"reason", T_OBJECT
, offsetof(PyUnicodeErrorObject
, reason
), 0,
1625 PyDoc_STR("exception reason")},
1626 {NULL
} /* Sentinel */
1631 * UnicodeEncodeError extends UnicodeError
1635 UnicodeEncodeError_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1637 if (BaseException_init((PyBaseExceptionObject
*)self
, args
, kwds
) == -1)
1639 return UnicodeError_init((PyUnicodeErrorObject
*)self
, args
,
1640 kwds
, &PyUnicode_Type
);
1644 UnicodeEncodeError_str(PyObject
*self
)
1646 PyUnicodeErrorObject
*uself
= (PyUnicodeErrorObject
*)self
;
1647 PyObject
*result
= NULL
;
1648 PyObject
*reason_str
= NULL
;
1649 PyObject
*encoding_str
= NULL
;
1652 /* Not properly initialized. */
1653 return PyUnicode_FromString("");
1655 /* Get reason and encoding as strings, which they might not be if
1656 they've been modified after we were contructed. */
1657 reason_str
= PyObject_Str(uself
->reason
);
1658 if (reason_str
== NULL
)
1660 encoding_str
= PyObject_Str(uself
->encoding
);
1661 if (encoding_str
== NULL
)
1664 if (uself
->start
< PyUnicode_GET_SIZE(uself
->object
) && uself
->end
== uself
->start
+1) {
1665 int badchar
= (int)PyUnicode_AS_UNICODE(uself
->object
)[uself
->start
];
1666 char badchar_str
[20];
1667 if (badchar
<= 0xff)
1668 PyOS_snprintf(badchar_str
, sizeof(badchar_str
), "x%02x", badchar
);
1669 else if (badchar
<= 0xffff)
1670 PyOS_snprintf(badchar_str
, sizeof(badchar_str
), "u%04x", badchar
);
1672 PyOS_snprintf(badchar_str
, sizeof(badchar_str
), "U%08x", badchar
);
1673 result
= PyString_FromFormat(
1674 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
1675 PyString_AS_STRING(encoding_str
),
1678 PyString_AS_STRING(reason_str
));
1681 result
= PyString_FromFormat(
1682 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
1683 PyString_AS_STRING(encoding_str
),
1686 PyString_AS_STRING(reason_str
));
1689 Py_XDECREF(reason_str
);
1690 Py_XDECREF(encoding_str
);
1694 static PyTypeObject _PyExc_UnicodeEncodeError
= {
1695 PyObject_HEAD_INIT(NULL
)
1697 EXC_MODULE_NAME
"UnicodeEncodeError",
1698 sizeof(PyUnicodeErrorObject
), 0,
1699 (destructor
)UnicodeError_dealloc
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1700 (reprfunc
)UnicodeEncodeError_str
, 0, 0, 0,
1701 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
,
1702 PyDoc_STR("Unicode encoding error."), (traverseproc
)UnicodeError_traverse
,
1703 (inquiry
)UnicodeError_clear
, 0, 0, 0, 0, 0, UnicodeError_members
,
1704 0, &_PyExc_UnicodeError
, 0, 0, 0, offsetof(PyUnicodeErrorObject
, dict
),
1705 (initproc
)UnicodeEncodeError_init
, 0, BaseException_new
,
1707 PyObject
*PyExc_UnicodeEncodeError
= (PyObject
*)&_PyExc_UnicodeEncodeError
;
1710 PyUnicodeEncodeError_Create(
1711 const char *encoding
, const Py_UNICODE
*object
, Py_ssize_t length
,
1712 Py_ssize_t start
, Py_ssize_t end
, const char *reason
)
1714 return PyObject_CallFunction(PyExc_UnicodeEncodeError
, "su#nns",
1715 encoding
, object
, length
, start
, end
, reason
);
1720 * UnicodeDecodeError extends UnicodeError
1724 UnicodeDecodeError_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1726 if (BaseException_init((PyBaseExceptionObject
*)self
, args
, kwds
) == -1)
1728 return UnicodeError_init((PyUnicodeErrorObject
*)self
, args
,
1729 kwds
, &PyString_Type
);
1733 UnicodeDecodeError_str(PyObject
*self
)
1735 PyUnicodeErrorObject
*uself
= (PyUnicodeErrorObject
*)self
;
1736 PyObject
*result
= NULL
;
1737 PyObject
*reason_str
= NULL
;
1738 PyObject
*encoding_str
= NULL
;
1741 /* Not properly initialized. */
1742 return PyUnicode_FromString("");
1744 /* Get reason and encoding as strings, which they might not be if
1745 they've been modified after we were contructed. */
1746 reason_str
= PyObject_Str(uself
->reason
);
1747 if (reason_str
== NULL
)
1749 encoding_str
= PyObject_Str(uself
->encoding
);
1750 if (encoding_str
== NULL
)
1753 if (uself
->start
< PyUnicode_GET_SIZE(uself
->object
) && uself
->end
== uself
->start
+1) {
1754 /* FromFormat does not support %02x, so format that separately */
1756 PyOS_snprintf(byte
, sizeof(byte
), "%02x",
1757 ((int)PyString_AS_STRING(uself
->object
)[uself
->start
])&0xff);
1758 result
= PyString_FromFormat(
1759 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
1760 PyString_AS_STRING(encoding_str
),
1763 PyString_AS_STRING(reason_str
));
1766 result
= PyString_FromFormat(
1767 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
1768 PyString_AS_STRING(encoding_str
),
1771 PyString_AS_STRING(reason_str
));
1774 Py_XDECREF(reason_str
);
1775 Py_XDECREF(encoding_str
);
1779 static PyTypeObject _PyExc_UnicodeDecodeError
= {
1780 PyObject_HEAD_INIT(NULL
)
1782 EXC_MODULE_NAME
"UnicodeDecodeError",
1783 sizeof(PyUnicodeErrorObject
), 0,
1784 (destructor
)UnicodeError_dealloc
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1785 (reprfunc
)UnicodeDecodeError_str
, 0, 0, 0,
1786 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
,
1787 PyDoc_STR("Unicode decoding error."), (traverseproc
)UnicodeError_traverse
,
1788 (inquiry
)UnicodeError_clear
, 0, 0, 0, 0, 0, UnicodeError_members
,
1789 0, &_PyExc_UnicodeError
, 0, 0, 0, offsetof(PyUnicodeErrorObject
, dict
),
1790 (initproc
)UnicodeDecodeError_init
, 0, BaseException_new
,
1792 PyObject
*PyExc_UnicodeDecodeError
= (PyObject
*)&_PyExc_UnicodeDecodeError
;
1795 PyUnicodeDecodeError_Create(
1796 const char *encoding
, const char *object
, Py_ssize_t length
,
1797 Py_ssize_t start
, Py_ssize_t end
, const char *reason
)
1799 return PyObject_CallFunction(PyExc_UnicodeDecodeError
, "ss#nns",
1800 encoding
, object
, length
, start
, end
, reason
);
1805 * UnicodeTranslateError extends UnicodeError
1809 UnicodeTranslateError_init(PyUnicodeErrorObject
*self
, PyObject
*args
,
1812 if (BaseException_init((PyBaseExceptionObject
*)self
, args
, kwds
) == -1)
1815 Py_CLEAR(self
->object
);
1816 Py_CLEAR(self
->reason
);
1818 if (!PyArg_ParseTuple(args
, "O!nnO!",
1819 &PyUnicode_Type
, &self
->object
,
1822 &PyString_Type
, &self
->reason
)) {
1823 self
->object
= self
->reason
= NULL
;
1827 Py_INCREF(self
->object
);
1828 Py_INCREF(self
->reason
);
1835 UnicodeTranslateError_str(PyObject
*self
)
1837 PyUnicodeErrorObject
*uself
= (PyUnicodeErrorObject
*)self
;
1838 PyObject
*result
= NULL
;
1839 PyObject
*reason_str
= NULL
;
1842 /* Not properly initialized. */
1843 return PyUnicode_FromString("");
1845 /* Get reason as a string, which it might not be if it's been
1846 modified after we were contructed. */
1847 reason_str
= PyObject_Str(uself
->reason
);
1848 if (reason_str
== NULL
)
1851 if (uself
->start
< PyUnicode_GET_SIZE(uself
->object
) && uself
->end
== uself
->start
+1) {
1852 int badchar
= (int)PyUnicode_AS_UNICODE(uself
->object
)[uself
->start
];
1853 char badchar_str
[20];
1854 if (badchar
<= 0xff)
1855 PyOS_snprintf(badchar_str
, sizeof(badchar_str
), "x%02x", badchar
);
1856 else if (badchar
<= 0xffff)
1857 PyOS_snprintf(badchar_str
, sizeof(badchar_str
), "u%04x", badchar
);
1859 PyOS_snprintf(badchar_str
, sizeof(badchar_str
), "U%08x", badchar
);
1860 result
= PyString_FromFormat(
1861 "can't translate character u'\\%s' in position %zd: %.400s",
1864 PyString_AS_STRING(reason_str
));
1866 result
= PyString_FromFormat(
1867 "can't translate characters in position %zd-%zd: %.400s",
1870 PyString_AS_STRING(reason_str
));
1873 Py_XDECREF(reason_str
);
1877 static PyTypeObject _PyExc_UnicodeTranslateError
= {
1878 PyObject_HEAD_INIT(NULL
)
1880 EXC_MODULE_NAME
"UnicodeTranslateError",
1881 sizeof(PyUnicodeErrorObject
), 0,
1882 (destructor
)UnicodeError_dealloc
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1883 (reprfunc
)UnicodeTranslateError_str
, 0, 0, 0,
1884 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
,
1885 PyDoc_STR("Unicode translation error."), (traverseproc
)UnicodeError_traverse
,
1886 (inquiry
)UnicodeError_clear
, 0, 0, 0, 0, 0, UnicodeError_members
,
1887 0, &_PyExc_UnicodeError
, 0, 0, 0, offsetof(PyUnicodeErrorObject
, dict
),
1888 (initproc
)UnicodeTranslateError_init
, 0, BaseException_new
,
1890 PyObject
*PyExc_UnicodeTranslateError
= (PyObject
*)&_PyExc_UnicodeTranslateError
;
1893 PyUnicodeTranslateError_Create(
1894 const Py_UNICODE
*object
, Py_ssize_t length
,
1895 Py_ssize_t start
, Py_ssize_t end
, const char *reason
)
1897 return PyObject_CallFunction(PyExc_UnicodeTranslateError
, "u#nns",
1898 object
, length
, start
, end
, reason
);
1904 * AssertionError extends StandardError
1906 SimpleExtendsException(PyExc_StandardError
, AssertionError
,
1907 "Assertion failed.");
1911 * ArithmeticError extends StandardError
1913 SimpleExtendsException(PyExc_StandardError
, ArithmeticError
,
1914 "Base class for arithmetic errors.");
1918 * FloatingPointError extends ArithmeticError
1920 SimpleExtendsException(PyExc_ArithmeticError
, FloatingPointError
,
1921 "Floating point operation failed.");
1925 * OverflowError extends ArithmeticError
1927 SimpleExtendsException(PyExc_ArithmeticError
, OverflowError
,
1928 "Result too large to be represented.");
1932 * ZeroDivisionError extends ArithmeticError
1934 SimpleExtendsException(PyExc_ArithmeticError
, ZeroDivisionError
,
1935 "Second argument to a division or modulo operation was zero.");
1939 * SystemError extends StandardError
1941 SimpleExtendsException(PyExc_StandardError
, SystemError
,
1942 "Internal error in the Python interpreter.\n"
1944 "Please report this to the Python maintainer, along with the traceback,\n"
1945 "the Python version, and the hardware/OS platform and version.");
1949 * ReferenceError extends StandardError
1951 SimpleExtendsException(PyExc_StandardError
, ReferenceError
,
1952 "Weak ref proxy used after referent went away.");
1956 * MemoryError extends StandardError
1958 SimpleExtendsException(PyExc_StandardError
, MemoryError
, "Out of memory.");
1961 * BufferError extends StandardError
1963 SimpleExtendsException(PyExc_StandardError
, BufferError
, "Buffer error.");
1966 /* Warning category docstrings */
1969 * Warning extends Exception
1971 SimpleExtendsException(PyExc_Exception
, Warning
,
1972 "Base class for warning categories.");
1976 * UserWarning extends Warning
1978 SimpleExtendsException(PyExc_Warning
, UserWarning
,
1979 "Base class for warnings generated by user code.");
1983 * DeprecationWarning extends Warning
1985 SimpleExtendsException(PyExc_Warning
, DeprecationWarning
,
1986 "Base class for warnings about deprecated features.");
1990 * PendingDeprecationWarning extends Warning
1992 SimpleExtendsException(PyExc_Warning
, PendingDeprecationWarning
,
1993 "Base class for warnings about features which will be deprecated\n"
1998 * SyntaxWarning extends Warning
2000 SimpleExtendsException(PyExc_Warning
, SyntaxWarning
,
2001 "Base class for warnings about dubious syntax.");
2005 * RuntimeWarning extends Warning
2007 SimpleExtendsException(PyExc_Warning
, RuntimeWarning
,
2008 "Base class for warnings about dubious runtime behavior.");
2012 * FutureWarning extends Warning
2014 SimpleExtendsException(PyExc_Warning
, FutureWarning
,
2015 "Base class for warnings about constructs that will change semantically\n"
2020 * ImportWarning extends Warning
2022 SimpleExtendsException(PyExc_Warning
, ImportWarning
,
2023 "Base class for warnings about probable mistakes in module imports");
2027 * UnicodeWarning extends Warning
2029 SimpleExtendsException(PyExc_Warning
, UnicodeWarning
,
2030 "Base class for warnings about Unicode related problems, mostly\n"
2031 "related to conversion problems.");
2034 * BytesWarning extends Warning
2036 SimpleExtendsException(PyExc_Warning
, BytesWarning
,
2037 "Base class for warnings about bytes and buffer related problems, mostly\n"
2038 "related to conversion from str or comparing to str.");
2040 /* Pre-computed MemoryError instance. Best to create this as early as
2041 * possible and not wait until a MemoryError is actually raised!
2043 PyObject
*PyExc_MemoryErrorInst
=NULL
;
2045 /* Pre-computed RuntimeError instance for when recursion depth is reached.
2046 Meant to be used when normalizing the exception for exceeding the recursion
2047 depth will cause its own infinite recursion.
2049 PyObject
*PyExc_RecursionErrorInst
= NULL
;
2051 /* module global functions */
2052 static PyMethodDef functions
[] = {
2057 #define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
2058 Py_FatalError("exceptions bootstrapping error.");
2060 #define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
2061 PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \
2062 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
2063 Py_FatalError("Module dictionary insertion problem.");
2069 PyObject
*m
, *bltinmod
, *bdict
;
2071 PRE_INIT(BaseException
)
2073 PRE_INIT(StandardError
)
2075 PRE_INIT(StopIteration
)
2076 PRE_INIT(GeneratorExit
)
2077 PRE_INIT(SystemExit
)
2078 PRE_INIT(KeyboardInterrupt
)
2079 PRE_INIT(ImportError
)
2080 PRE_INIT(EnvironmentError
)
2084 PRE_INIT(WindowsError
)
2090 PRE_INIT(RuntimeError
)
2091 PRE_INIT(NotImplementedError
)
2093 PRE_INIT(UnboundLocalError
)
2094 PRE_INIT(AttributeError
)
2095 PRE_INIT(SyntaxError
)
2096 PRE_INIT(IndentationError
)
2098 PRE_INIT(LookupError
)
2099 PRE_INIT(IndexError
)
2101 PRE_INIT(ValueError
)
2102 PRE_INIT(UnicodeError
)
2103 #ifdef Py_USING_UNICODE
2104 PRE_INIT(UnicodeEncodeError
)
2105 PRE_INIT(UnicodeDecodeError
)
2106 PRE_INIT(UnicodeTranslateError
)
2108 PRE_INIT(AssertionError
)
2109 PRE_INIT(ArithmeticError
)
2110 PRE_INIT(FloatingPointError
)
2111 PRE_INIT(OverflowError
)
2112 PRE_INIT(ZeroDivisionError
)
2113 PRE_INIT(SystemError
)
2114 PRE_INIT(ReferenceError
)
2115 PRE_INIT(MemoryError
)
2116 PRE_INIT(BufferError
)
2118 PRE_INIT(UserWarning
)
2119 PRE_INIT(DeprecationWarning
)
2120 PRE_INIT(PendingDeprecationWarning
)
2121 PRE_INIT(SyntaxWarning
)
2122 PRE_INIT(RuntimeWarning
)
2123 PRE_INIT(FutureWarning
)
2124 PRE_INIT(ImportWarning
)
2125 PRE_INIT(UnicodeWarning
)
2126 PRE_INIT(BytesWarning
)
2128 m
= Py_InitModule4("exceptions", functions
, exceptions_doc
,
2129 (PyObject
*)NULL
, PYTHON_API_VERSION
);
2133 bltinmod
= PyImport_ImportModule("__builtin__");
2134 if (bltinmod
== NULL
)
2135 Py_FatalError("exceptions bootstrapping error.");
2136 bdict
= PyModule_GetDict(bltinmod
);
2138 Py_FatalError("exceptions bootstrapping error.");
2140 POST_INIT(BaseException
)
2141 POST_INIT(Exception
)
2142 POST_INIT(StandardError
)
2143 POST_INIT(TypeError
)
2144 POST_INIT(StopIteration
)
2145 POST_INIT(GeneratorExit
)
2146 POST_INIT(SystemExit
)
2147 POST_INIT(KeyboardInterrupt
)
2148 POST_INIT(ImportError
)
2149 POST_INIT(EnvironmentError
)
2153 POST_INIT(WindowsError
)
2159 POST_INIT(RuntimeError
)
2160 POST_INIT(NotImplementedError
)
2161 POST_INIT(NameError
)
2162 POST_INIT(UnboundLocalError
)
2163 POST_INIT(AttributeError
)
2164 POST_INIT(SyntaxError
)
2165 POST_INIT(IndentationError
)
2167 POST_INIT(LookupError
)
2168 POST_INIT(IndexError
)
2170 POST_INIT(ValueError
)
2171 POST_INIT(UnicodeError
)
2172 #ifdef Py_USING_UNICODE
2173 POST_INIT(UnicodeEncodeError
)
2174 POST_INIT(UnicodeDecodeError
)
2175 POST_INIT(UnicodeTranslateError
)
2177 POST_INIT(AssertionError
)
2178 POST_INIT(ArithmeticError
)
2179 POST_INIT(FloatingPointError
)
2180 POST_INIT(OverflowError
)
2181 POST_INIT(ZeroDivisionError
)
2182 POST_INIT(SystemError
)
2183 POST_INIT(ReferenceError
)
2184 POST_INIT(MemoryError
)
2185 POST_INIT(BufferError
)
2187 POST_INIT(UserWarning
)
2188 POST_INIT(DeprecationWarning
)
2189 POST_INIT(PendingDeprecationWarning
)
2190 POST_INIT(SyntaxWarning
)
2191 POST_INIT(RuntimeWarning
)
2192 POST_INIT(FutureWarning
)
2193 POST_INIT(ImportWarning
)
2194 POST_INIT(UnicodeWarning
)
2195 POST_INIT(BytesWarning
)
2197 PyExc_MemoryErrorInst
= BaseException_new(&_PyExc_MemoryError
, NULL
, NULL
);
2198 if (!PyExc_MemoryErrorInst
)
2199 Py_FatalError("Cannot pre-allocate MemoryError instance");
2201 PyExc_RecursionErrorInst
= BaseException_new(&_PyExc_RuntimeError
, NULL
, NULL
);
2202 if (!PyExc_RecursionErrorInst
)
2203 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2204 "recursion errors");
2206 PyBaseExceptionObject
*err_inst
=
2207 (PyBaseExceptionObject
*)PyExc_RecursionErrorInst
;
2208 PyObject
*args_tuple
;
2209 PyObject
*exc_message
;
2210 exc_message
= PyString_FromString("maximum recursion depth exceeded");
2212 Py_FatalError("cannot allocate argument for RuntimeError "
2214 args_tuple
= PyTuple_Pack(1, exc_message
);
2216 Py_FatalError("cannot allocate tuple for RuntimeError "
2218 Py_DECREF(exc_message
);
2219 if (BaseException_init(err_inst
, args_tuple
, NULL
))
2220 Py_FatalError("init of pre-allocated RuntimeError failed");
2221 Py_DECREF(args_tuple
);
2223 Py_DECREF(bltinmod
);
2229 Py_CLEAR(PyExc_MemoryErrorInst
);
2230 Py_CLEAR(PyExc_RecursionErrorInst
);