diff options
author | Gregory P. Smith [Google LLC] <gps@google.com> | 2021-08-20 15:39:48 -0700 |
---|---|---|
committer | Gregory P. Smith [Google LLC] <gps@google.com> | 2021-08-20 15:39:48 -0700 |
commit | f53c2459e477fd81d2659bd8b64e1a10dae5e8cd (patch) | |
tree | 9d355d221a8dd3eb89db583b70b5dc1b40e6e61f /simplejson/_speedups.c | |
parent | 2b75ded9dbaf6768c0d74e887ea84b8e9124aa86 (diff) | |
download | simplejson-f53c2459e477fd81d2659bd8b64e1a10dae5e8cd.tar.gz |
Move the PyDict_Check after the _asdict call.
Add a unittest.
Diffstat (limited to 'simplejson/_speedups.c')
-rw-r--r-- | simplejson/_speedups.c | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/simplejson/_speedups.c b/simplejson/_speedups.c index 7c3b6b0..ef0dc6b 100644 --- a/simplejson/_speedups.c +++ b/simplejson/_speedups.c @@ -386,9 +386,8 @@ static int _is_namedtuple(PyObject *obj) { int rval = 0; - if (!PyTuple_Check(obj)) { - return 0; - } + /* We intentionally accept anything with a duck typed _asdict method rather + * than requiring it to pass PyTuple_Check(obj). */ PyObject *_asdict = PyObject_GetAttrString(obj, "_asdict"); if (_asdict == NULL) { PyErr_Clear(); @@ -2856,6 +2855,10 @@ encoder_listencode_obj(PyEncoderObject *s, JSON_Accu *rval, PyObject *obj, Py_ss return rv; newobj = PyObject_CallMethod(obj, "_asdict", NULL); if (newobj != NULL) { + if (!PyDict_Check(newobj)) { + Py_DECREF(newobj); + return -1; + } rv = encoder_listencode_dict(s, rval, newobj, indent_level); Py_DECREF(newobj); } @@ -2956,9 +2959,6 @@ encoder_listencode_dict(PyEncoderObject *s, JSON_Accu *rval, PyObject *dct, Py_s PyObject *encoded = NULL; Py_ssize_t idx; - if (!PyDict_Check(dct)) { - return -1; - } if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) { open_dict = JSON_InternFromString("{"); close_dict = JSON_InternFromString("}"); |