Issue #13774: json: Fix a SystemError when a bogus encoding is passed to

json.loads().
This commit is contained in:
Amaury Forgeot d'Arc 2012-01-13 22:53:25 +01:00
parent 44765e58b0
commit dee76e627d
3 changed files with 15 additions and 1 deletions

View File

@ -80,6 +80,10 @@ class TestUnicode(object):
# Issue 10038. # Issue 10038.
self.assertEqual(type(self.loads('"foo"')), unicode) self.assertEqual(type(self.loads('"foo"')), unicode)
def test_bad_encoding(self):
self.assertRaises(UnicodeEncodeError, self.loads, '"a"', u"rat\xe9")
self.assertRaises(TypeError, self.loads, '"a"', 1)
class TestPyUnicode(TestUnicode, PyTest): pass class TestPyUnicode(TestUnicode, PyTest): pass
class TestCUnicode(TestUnicode, CTest): pass class TestCUnicode(TestUnicode, CTest): pass

View File

@ -374,6 +374,9 @@ Library
Extension Modules Extension Modules
----------------- -----------------
- Issue #13774: json: Fix a SystemError when a bogus encoding is passed to
json.loads().
- Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by - Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
Vilmos Nebehaj. Vilmos Nebehaj.

View File

@ -1725,8 +1725,15 @@ scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
Py_DECREF(s->encoding); Py_DECREF(s->encoding);
s->encoding = tmp; s->encoding = tmp;
} }
if (s->encoding == NULL || !PyString_Check(s->encoding)) if (s->encoding == NULL)
goto bail; goto bail;
if (!PyString_Check(s->encoding)) {
PyErr_Format(PyExc_TypeError,
"encoding must be a string, not %.80s",
Py_TYPE(s->encoding)->tp_name);
goto bail;
}
/* All of these will fail "gracefully" so we don't need to verify them */ /* All of these will fail "gracefully" so we don't need to verify them */
s->strict = PyObject_GetAttrString(ctx, "strict"); s->strict = PyObject_GetAttrString(ctx, "strict");