summaryrefslogtreecommitdiff
path: root/simplejson
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-11-06 22:01:29 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2017-11-06 22:01:29 +0200
commit5decd38112c02dbd646c2d13b9563460ee15eb4b (patch)
tree8efd2e4141838cd41ce5ac5737e3ce67bbcb625e /simplejson
parentef4015d1d997c234d2bb604b4d858e04cdbceeb3 (diff)
downloadsimplejson-5decd38112c02dbd646c2d13b9563460ee15eb4b.tar.gz
bpo-31505: Fix an assertion failure in json, in case _json.make_encoder() received a bad encoder() argument.
Original patch by Oren Milman.
Diffstat (limited to 'simplejson')
-rw-r--r--simplejson/_speedups.c21
-rw-r--r--simplejson/tests/test_speedups.py26
2 files changed, 44 insertions, 3 deletions
diff --git a/simplejson/_speedups.c b/simplejson/_speedups.c
index 7b46d8a..60f15e1 100644
--- a/simplejson/_speedups.c
+++ b/simplejson/_speedups.c
@@ -2807,10 +2807,25 @@ static PyObject *
encoder_encode_string(PyEncoderObject *s, PyObject *obj)
{
/* Return the JSON representation of a string */
- if (s->fast_encode)
+ PyObject *encoded;
+
+ if (s->fast_encode) {
return py_encode_basestring_ascii(NULL, obj);
- else
- return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
+ }
+ encoded = PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
+ if (encoded != NULL &&
+#if PY_MAJOR_VERSION < 3
+ !JSON_ASCII_Check(unicode) &&
+#endif /* PY_MAJOR_VERSION < 3 */
+ !PyUnicode_Check(encoded))
+ {
+ PyErr_Format(PyExc_TypeError,
+ "encoder() must return a string, not %.80s",
+ Py_TYPE(encoded)->tp_name);
+ Py_DECREF(encoded);
+ return NULL;
+ }
+ return encoded;
}
static int
diff --git a/simplejson/tests/test_speedups.py b/simplejson/tests/test_speedups.py
index b59eeca..75242fd 100644
--- a/simplejson/tests/test_speedups.py
+++ b/simplejson/tests/test_speedups.py
@@ -60,6 +60,32 @@ class TestEncode(TestCase):
)
@skip_if_speedups_missing
+ def test_bad_str_encoder(self):
+ # Issue #31505: There shouldn't be an assertion failure in case
+ # c_make_encoder() receives a bad encoder() argument.
+ def bad_encoder1(*args):
+ return None
+ enc = encoder.c_make_encoder(
+ None, lambda obj: str(obj),
+ bad_encoder1, None, ': ', ', ',
+ False, False, False, {}, False, False, False,
+ None, None, 'utf-8', False, False, decimal.Decimal, False)
+ self.assertRaises(TypeError, enc, 'spam', 4)
+ self.assertRaises(TypeError, enc, {'spam': 42}, 4)
+
+ def bad_encoder2(*args):
+ 1/0
+ enc = encoder.c_make_encoder(
+ None, lambda obj: str(obj),
+ bad_encoder2, None, ': ', ', ',
+ False, False, False, {}, False, False, False,
+ None, None, 'utf-8', False, False, decimal.Decimal, False)
+ enc = self.json.encoder.c_make_encoder(None, lambda obj: str(obj),
+ bad_encoder2, None, ': ', ', ',
+ False, False, False)
+ self.assertRaises(ZeroDivisionError, enc, 'spam', 4)
+
+ @skip_if_speedups_missing
def test_bad_bool_args(self):
def test(name):
encoder.JSONEncoder(**{name: BadBool()}).encode({})