changeset: 96133:547bc11e3357 branch: 2.7 parent: 96123:b6121a4afad7 user: Serhiy Storchaka date: Mon May 18 16:08:38 2015 +0300 files: Lib/test/test_codeccallbacks.py Misc/NEWS Python/codecs.c description: Issue #24102: Fixed exception type checking in standard error handlers. diff -r b6121a4afad7 -r 547bc11e3357 Lib/test/test_codeccallbacks.py --- a/Lib/test/test_codeccallbacks.py Sun May 17 14:37:39 2015 -0700 +++ b/Lib/test/test_codeccallbacks.py Mon May 18 16:08:38 2015 +0300 @@ -836,6 +836,26 @@ text = u'abcghi'*n text.translate(charmap) + def test_fake_error_class(self): + handlers = [ + codecs.strict_errors, + codecs.ignore_errors, + codecs.replace_errors, + codecs.backslashreplace_errors, + codecs.xmlcharrefreplace_errors, + ] + for cls in UnicodeEncodeError, UnicodeDecodeError, UnicodeTranslateError: + class FakeUnicodeError(str): + __class__ = cls + for handler in handlers: + self.assertRaises(TypeError, handler, FakeUnicodeError()) + class FakeUnicodeError(Exception): + __class__ = cls + for handler in handlers: + with self.assertRaises((TypeError, FakeUnicodeError)): + handler(FakeUnicodeError()) + + def test_main(): test.test_support.run_unittest(CodecCallbackTest) diff -r b6121a4afad7 -r 547bc11e3357 Misc/NEWS --- a/Misc/NEWS Sun May 17 14:37:39 2015 -0700 +++ b/Misc/NEWS Mon May 18 16:08:38 2015 +0300 @@ -10,6 +10,8 @@ Core and Builtins ----------------- +- Issue #24102: Fixed exception type checking in standard error handlers. + Library ------- diff -r b6121a4afad7 -r 547bc11e3357 Python/codecs.c --- a/Python/codecs.c Sun May 17 14:37:39 2015 -0700 +++ b/Python/codecs.c Mon May 18 16:08:38 2015 +0300 @@ -472,15 +472,16 @@ PyObject *PyCodec_IgnoreErrors(PyObject *exc) { Py_ssize_t end; - if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { + + if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { if (PyUnicodeEncodeError_GetEnd(exc, &end)) return NULL; } - else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { + else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { if (PyUnicodeDecodeError_GetEnd(exc, &end)) return NULL; } - else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { + else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) { if (PyUnicodeTranslateError_GetEnd(exc, &end)) return NULL; } @@ -500,7 +501,7 @@ Py_ssize_t end; Py_ssize_t i; - if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { + if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { PyObject *res; Py_UNICODE *p; if (PyUnicodeEncodeError_GetStart(exc, &start)) @@ -517,13 +518,13 @@ Py_DECREF(res); return restuple; } - else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { + else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER; if (PyUnicodeDecodeError_GetEnd(exc, &end)) return NULL; return Py_BuildValue("(u#n)", &res, (Py_ssize_t)1, end); } - else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { + else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) { PyObject *res; Py_UNICODE *p; if (PyUnicodeTranslateError_GetStart(exc, &start)) @@ -548,7 +549,7 @@ PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc) { - if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { + if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { PyObject *restuple; PyObject *object; Py_ssize_t start; @@ -673,7 +674,7 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) { - if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { + if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { PyObject *restuple; PyObject *object; Py_ssize_t start;