]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #13774: json: Fix a SystemError when a bogus encoding is passed to
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 13 Jan 2012 21:53:25 +0000 (22:53 +0100)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 13 Jan 2012 21:53:25 +0000 (22:53 +0100)
json.loads().

Lib/json/tests/test_unicode.py
Misc/NEWS
Modules/_json.c

index 31cf38925ccbbe0ee4f6f9b259dcf8afedad5ab0..e90f15860631ca3bcae7ea8e99e91a25701d31c2 100644 (file)
@@ -80,6 +80,10 @@ class TestUnicode(object):
         # Issue 10038.
         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 TestCUnicode(TestUnicode, CTest): pass
index aa32dd75872ade9447668e72e362138087594d0e..ae2c1a3cd35cf15775da7a167511e7c69f3a6f56 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -374,6 +374,9 @@ Library
 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
   Vilmos Nebehaj.
 
index 6b321e57f584cdd08b790ddbb79aa3b90ca28f4b..434b83c735885d4b604c083b3fe4793a6a634c0b 100644 (file)
@@ -1725,8 +1725,15 @@ scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
         Py_DECREF(s->encoding);
         s->encoding = tmp;
     }
-    if (s->encoding == NULL || !PyString_Check(s->encoding))
+    if (s->encoding == NULL)
         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 */
     s->strict = PyObject_GetAttrString(ctx, "strict");