Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak dumping on non-string dict keys #521

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions python/objToJSON.c
Expand Up @@ -254,15 +254,13 @@ static int Dict_iterNext(JSOBJ obj, JSONTypeContext *tc)
return 1;
}

itemNameTmp = GET_TC(tc)->itemName;
GET_TC(tc)->itemName = PyObject_Str(GET_TC(tc)->itemName);
Py_DECREF(itemNameTmp);
itemNameTmp = GET_TC(tc)->itemName;
GET_TC(tc)->itemName = PyUnicode_AsUTF8String (GET_TC(tc)->itemName);
Py_DECREF(itemNameTmp);
}
else
{
Py_INCREF(GET_TC(tc)->itemName);
}
PRINTMARK();
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_ujson.py
Expand Up @@ -239,11 +239,11 @@ def test_encode_dict_values_ref_counting():
@pytest.mark.skipif(
hasattr(sys, "pypy_version_info"), reason="PyPy uses incompatible GC"
)
def test_encode_dict_key_ref_counting():
@pytest.mark.parametrize("key", ["key", b"key", 1, True, None])
def test_encode_dict_key_ref_counting(key):
import gc

gc.collect()
key = "key"
data = {key: "abc"}
ref_count = sys.getrefcount(key)
ujson.dumps(data)
Expand Down