From 14e10a19f783d1f896e74d563fe3ef17e7cd8435 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 18 May 2015 16:08:38 +0300 Subject: Issue #24102: Fixed exception type checking in standard error handlers. --- Lib/test/test_codeccallbacks.py | 20 ++++++++++++++++++++ Misc/NEWS | 2 ++ Python/codecs.c | 17 +++++++++-------- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_codeccallbacks.py b/Lib/test/test_codeccallbacks.py index b9cd9c2..c11affd 100644 --- a/Lib/test/test_codeccallbacks.py +++ b/Lib/test/test_codeccallbacks.py @@ -836,6 +836,26 @@ class CodecCallbackTest(unittest.TestCase): 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 --git a/Misc/NEWS b/Misc/NEWS index c9b0dbe..2726077 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,8 @@ What's New in Python 2.7.11? Core and Builtins ----------------- +- Issue #24102: Fixed exception type checking in standard error handlers. + Library ------- diff --git a/Python/codecs.c b/Python/codecs.c index a901d6d..184d147 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -472,15 +472,16 @@ PyObject *PyCodec_StrictErrors(PyObject *exc) 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 @@ PyObject *PyCodec_ReplaceErrors(PyObject *exc) 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 @@ PyObject *PyCodec_ReplaceErrors(PyObject *exc) 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_ReplaceErrors(PyObject *exc) 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 @@ static Py_UNICODE hexdigits[] = { 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; -- cgit v0.12