diff --git a/python/objToJSON.c b/python/objToJSON.c index 8f4be736..d7f6cb92 100644 --- a/python/objToJSON.c +++ b/python/objToJSON.c @@ -257,6 +257,11 @@ static int Dict_iterNext(JSOBJ obj, JSONTypeContext *tc) itemNameTmp = GET_TC(tc)->itemName; GET_TC(tc)->itemName = PyObject_Str(GET_TC(tc)->itemName); Py_DECREF(itemNameTmp); + if (PyErr_Occurred()) + { + PRINTMARK(); + return -1; + } itemNameTmp = GET_TC(tc)->itemName; GET_TC(tc)->itemName = PyUnicode_AsUTF8String (GET_TC(tc)->itemName); Py_DECREF(itemNameTmp); @@ -332,6 +337,10 @@ static int SortedDict_iterNext(JSOBJ obj, JSONTypeContext *tc) else if (!PyBytes_Check(key)) { key = PyObject_Str(key); + if (PyErr_Occurred()) + { + goto error; + } keyTmp = key; key = PyUnicode_AsUTF8String(key); Py_DECREF(keyTmp); diff --git a/tests/test_ujson.py b/tests/test_ujson.py index 8c8770e8..3e3f3f1b 100644 --- a/tests/test_ujson.py +++ b/tests/test_ujson.py @@ -997,6 +997,16 @@ def convert(self): ) +@pytest.mark.parametrize("sort_keys", [False, True]) +def test_obj_str_exception(sort_keys): + class Obj: + def __str__(self): + raise NotImplementedError + + with pytest.raises(NotImplementedError): + ujson.dumps({Obj(): 1}, sort_keys=sort_keys) + + """ def test_decode_numeric_int_frc_overflow(): input = "X.Y"