diff -r f0666e56a552 Lib/json/tests/test_unicode.py --- a/Lib/json/tests/test_unicode.py Thu Jan 05 23:23:52 2012 -0800 +++ b/Lib/json/tests/test_unicode.py Thu Jan 12 11:14:01 2012 +0100 @@ -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 -r f0666e56a552 Modules/_json.c --- a/Modules/_json.c Thu Jan 05 23:23:52 2012 -0800 +++ b/Modules/_json.c Thu Jan 12 11:14:01 2012 +0100 @@ -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");