Issue #24102: Fixed exception type checking in standard error handlers.
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 @@
text = u'abc<def>ghi'*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 @@
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_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;