[Python-checkins] cpython (2.7): Issue #13774: json: Fix a SystemError when a bogus encoding is passed to

amaury.forgeotdarc python-checkins at python.org
Fri Jan 13 22:54:20 CET 2012


http://hg.python.org/cpython/rev/a9680746ae4a
changeset:   74369:a9680746ae4a
branch:      2.7
parent:      74362:7fcfbaad75ee
user:        Amaury Forgeot d'Arc <amauryfa at gmail.com>
date:        Fri Jan 13 22:53:25 2012 +0100
summary:
  Issue #13774: json: Fix a SystemError when a bogus encoding is passed to
json.loads().

files:
  Lib/json/tests/test_unicode.py |  4 ++++
  Misc/NEWS                      |  3 +++
  Modules/_json.c                |  9 ++++++++-
  3 files changed, 15 insertions(+), 1 deletions(-)


diff --git a/Lib/json/tests/test_unicode.py b/Lib/json/tests/test_unicode.py
--- a/Lib/json/tests/test_unicode.py
+++ b/Lib/json/tests/test_unicode.py
@@ -80,6 +80,10 @@
         # 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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -374,6 +374,9 @@
 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.
 
diff --git a/Modules/_json.c b/Modules/_json.c
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -1725,8 +1725,15 @@
         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");

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list