comparison Python/exceptions.c @ 25110:0401a0ead1eb legacy-trunk

Add a custom __str__ method to KeyError that applies repr() to the missing key. (Also added a guard to SyntaxError__str__ to prevent calling PyString_Check(NULL).)
author Guido van Rossum <guido@python.org>
date Tue, 03 Sep 2002 20:24:09 +0000
parents 6a85a1c9d33d
children 14ad560f4a97
comparison
equal deleted inserted replaced
25109:00b6dd414b80 25110:0401a0ead1eb
783 result = str; 783 result = str;
784 784
785 /* XXX -- do all the additional formatting with filename and 785 /* XXX -- do all the additional formatting with filename and
786 lineno here */ 786 lineno here */
787 787
788 if (PyString_Check(str)) { 788 if (str != NULL && PyString_Check(str)) {
789 int have_filename = 0; 789 int have_filename = 0;
790 int have_lineno = 0; 790 int have_lineno = 0;
791 char *buffer = NULL; 791 char *buffer = NULL;
792 792
793 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL) 793 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
838 838
839 839
840 static PyMethodDef SyntaxError_methods[] = { 840 static PyMethodDef SyntaxError_methods[] = {
841 {"__init__", SyntaxError__init__, METH_VARARGS}, 841 {"__init__", SyntaxError__init__, METH_VARARGS},
842 {"__str__", SyntaxError__str__, METH_VARARGS}, 842 {"__str__", SyntaxError__str__, METH_VARARGS},
843 {NULL, NULL}
844 };
845
846
847 static PyObject *
848 KeyError__str__(PyObject *self, PyObject *args)
849 {
850 PyObject *argsattr;
851 PyObject *result;
852
853 if (!PyArg_ParseTuple(args, "O:__str__", &self))
854 return NULL;
855
856 if (!(argsattr = PyObject_GetAttrString(self, "args")))
857 return NULL;
858
859 /* If args is a tuple of exactly one item, apply repr to args[0].
860 This is done so that e.g. the exception raised by {}[''] prints
861 KeyError: ''
862 rather than the confusing
863 KeyError
864 alone. The downside is that if KeyError is raised with an explanatory
865 string, that string will be displayed in quotes. Too bad.
866 If args is anything else, use the default Exception__str__().
867 */
868 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
869 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
870 result = PyObject_Repr(key);
871 }
872 else
873 result = Exception__str__(self, args);
874
875 Py_DECREF(argsattr);
876 return result;
877 }
878
879 static PyMethodDef KeyError_methods[] = {
880 {"__str__", KeyError__str__, METH_VARARGS},
843 {NULL, NULL} 881 {NULL, NULL}
844 }; 882 };
845 883
846 884
847 static 885 static
1615 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__}, 1653 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1616 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__}, 1654 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1617 {"IndexError", &PyExc_IndexError, &PyExc_LookupError, 1655 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1618 IndexError__doc__}, 1656 IndexError__doc__},
1619 {"KeyError", &PyExc_KeyError, &PyExc_LookupError, 1657 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
1620 KeyError__doc__}, 1658 KeyError__doc__, KeyError_methods},
1621 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__}, 1659 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1622 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError, 1660 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1623 OverflowError__doc__}, 1661 OverflowError__doc__},
1624 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError, 1662 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1625 ZeroDivisionError__doc__}, 1663 ZeroDivisionError__doc__},