[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 2b382dd612)
This commit is contained in:
Miss Islington (bot) 2017-09-26 22:21:47 -07:00 committed by Serhiy Storchaka
parent 90fe25a051
commit 7c24e99c99
3 changed files with 36 additions and 3 deletions

View file

@ -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