summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-05-18 13:10:40 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-05-18 13:10:40 (GMT)
commitc0937f79ec12fb46938416004fd1fd002ae75a12 (patch)
tree388599c3c5ebed30075666b789503ea845b20cda
parentf4e6030542eaf684a384f27a741d877c05b17b33 (diff)
parentca7fecb0389466ef1b995e69c92ea076d4c52498 (diff)
downloadcpython-c0937f79ec12fb46938416004fd1fd002ae75a12.zip
cpython-c0937f79ec12fb46938416004fd1fd002ae75a12.tar.gz
cpython-c0937f79ec12fb46938416004fd1fd002ae75a12.tar.bz2
Issue #24102: Fixed exception type checking in standard error handlers.
-rw-r--r--Lib/test/test_codeccallbacks.py24
-rw-r--r--Misc/NEWS2
-rw-r--r--Python/codecs.c48
3 files changed, 47 insertions, 27 deletions
diff --git a/Lib/test/test_codeccallbacks.py b/Lib/test/test_codeccallbacks.py
index 4cfb88e..ee1e28a 100644
--- a/Lib/test/test_codeccallbacks.py
+++ b/Lib/test/test_codeccallbacks.py
@@ -1046,6 +1046,30 @@ class CodecCallbackTest(unittest.TestCase):
with self.assertRaises(TypeError):
data.decode(encoding, "test.replacing")
+ def test_fake_error_class(self):
+ handlers = [
+ codecs.strict_errors,
+ codecs.ignore_errors,
+ codecs.replace_errors,
+ codecs.backslashreplace_errors,
+ codecs.namereplace_errors,
+ codecs.xmlcharrefreplace_errors,
+ codecs.lookup_error('surrogateescape'),
+ codecs.lookup_error('surrogatepass'),
+ ]
+ for cls in UnicodeEncodeError, UnicodeDecodeError, UnicodeTranslateError:
+ class FakeUnicodeError(str):
+ __class__ = cls
+ for handler in handlers:
+ with self.subTest(handler=handler, error_class=cls):
+ self.assertRaises(TypeError, handler, FakeUnicodeError())
+ class FakeUnicodeError(Exception):
+ __class__ = cls
+ for handler in handlers:
+ with self.subTest(handler=handler, error_class=cls):
+ with self.assertRaises((TypeError, FakeUnicodeError)):
+ handler(FakeUnicodeError())
+
if __name__ == "__main__":
unittest.main()
diff --git a/Misc/NEWS b/Misc/NEWS
index b51a9f2..141d2e0 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ Release date: 2015-05-24
Core and Builtins
-----------------
+- Issue #24102: Fixed exception type checking in standard error handlers.
+
- Issue #15027: The UTF-32 encoder is now 3x to 7x faster.
- Issue #23290: Optimize set_merge() for cases where the target is empty.
diff --git a/Python/codecs.c b/Python/codecs.c
index 64fc3d6..38b0c2c 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -662,18 +662,9 @@ PyObject *PyCodec_LookupError(const char *name)
static void wrong_exception_type(PyObject *exc)
{
- _Py_IDENTIFIER(__class__);
- _Py_IDENTIFIER(__name__);
- PyObject *type = _PyObject_GetAttrId(exc, &PyId___class__);
- if (type != NULL) {
- PyObject *name = _PyObject_GetAttrId(type, &PyId___name__);
- Py_DECREF(type);
- if (name != NULL) {
- PyErr_Format(PyExc_TypeError,
- "don't know how to handle %S in error callback", name);
- Py_DECREF(name);
- }
- }
+ PyErr_Format(PyExc_TypeError,
+ "don't know how to handle %.200s in error callback",
+ exc->ob_type->tp_name);
}
PyObject *PyCodec_StrictErrors(PyObject *exc)
@@ -689,15 +680,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;
}
@@ -713,7 +705,7 @@ PyObject *PyCodec_ReplaceErrors(PyObject *exc)
{
Py_ssize_t start, end, i, len;
- if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
+ if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
PyObject *res;
int kind;
void *data;
@@ -732,14 +724,14 @@ PyObject *PyCodec_ReplaceErrors(PyObject *exc)
assert(_PyUnicode_CheckConsistency(res, 1));
return Py_BuildValue("(Nn)", res, end);
}
- else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
+ else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
if (PyUnicodeDecodeError_GetEnd(exc, &end))
return NULL;
return Py_BuildValue("(Cn)",
(int)Py_UNICODE_REPLACEMENT_CHARACTER,
end);
}
- else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
+ else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) {
PyObject *res;
int kind;
void *data;
@@ -766,7 +758,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 i;
@@ -873,7 +865,7 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
int ressize;
Py_UCS4 c;
- if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
+ if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
unsigned char *p;
if (PyUnicodeDecodeError_GetStart(exc, &start))
return NULL;
@@ -903,7 +895,7 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
Py_DECREF(object);
return Py_BuildValue("(Nn)", res, end);
}
- if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
+ if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
if (PyUnicodeEncodeError_GetStart(exc, &start))
return NULL;
if (PyUnicodeEncodeError_GetEnd(exc, &end))
@@ -911,7 +903,7 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
if (!(object = PyUnicodeEncodeError_GetObject(exc)))
return NULL;
}
- else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
+ else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) {
if (PyUnicodeTranslateError_GetStart(exc, &start))
return NULL;
if (PyUnicodeTranslateError_GetEnd(exc, &end))
@@ -977,7 +969,7 @@ static int ucnhash_initialized = 0;
PyObject *PyCodec_NameReplaceErrors(PyObject *exc)
{
- if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
+ if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
PyObject *restuple;
PyObject *object;
Py_ssize_t i;
@@ -1150,7 +1142,8 @@ PyCodec_SurrogatePassErrors(PyObject *exc)
Py_ssize_t start;
Py_ssize_t end;
PyObject *res;
- if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
+
+ if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
unsigned char *outp;
if (PyUnicodeEncodeError_GetStart(exc, &start))
return NULL;
@@ -1227,7 +1220,7 @@ PyCodec_SurrogatePassErrors(PyObject *exc)
Py_DECREF(object);
return restuple;
}
- else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
+ else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
unsigned char *p;
Py_UCS4 ch = 0;
if (PyUnicodeDecodeError_GetStart(exc, &start))
@@ -1312,7 +1305,8 @@ PyCodec_SurrogateEscapeErrors(PyObject *exc)
Py_ssize_t start;
Py_ssize_t end;
PyObject *res;
- if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
+
+ if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
char *outp;
if (PyUnicodeEncodeError_GetStart(exc, &start))
return NULL;
@@ -1343,7 +1337,7 @@ PyCodec_SurrogateEscapeErrors(PyObject *exc)
Py_DECREF(object);
return restuple;
}
- else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
+ else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
PyObject *str;
unsigned char *p;
Py_UCS2 ch[4]; /* decode up to 4 bad bytes. */