diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2017-09-27 05:21:47 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2017-09-27 05:21:47 (GMT) |
commit | 7c24e99c99c961e6633ac45390a211f65806c687 (patch) | |
tree | cb98bb49e763c9d57adecb7486d31989c6f926ef /Modules/_json.c | |
parent | 90fe25a051bd8cf64d52be533c9b60cad9bdd7fb (diff) | |
download | cpython-7c24e99c99c961e6633ac45390a211f65806c687.zip cpython-7c24e99c99c961e6633ac45390a211f65806c687.tar.gz cpython-7c24e99c99c961e6633ac45390a211f65806c687.tar.bz2 |
[3.6] bpo-31505: Fix an assertion failure in json, in case _json.make_encoder() received a bad encoder() argument. (GH-3643) (#3777)
(cherry picked from commit 2b382dd6121bb1e4b75470fb3ef8555665df3eb6)
Diffstat (limited to 'Modules/_json.c')
-rw-r--r-- | Modules/_json.c | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/Modules/_json.c b/Modules/_json.c index 59376a7..8f1743b 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1431,10 +1431,20 @@ 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 s->fast_encode(NULL, obj); - else - return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL); + } + encoded = PyObject_CallFunctionObjArgs(s->encoder, obj, NULL); + if (encoded != NULL && !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 |