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 ref counting on repeated default function calls #524

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
1 change: 1 addition & 0 deletions python/objToJSON.c
Expand Up @@ -648,6 +648,7 @@ static void Object_beginTypeContext (JSOBJ _obj, JSONTypeContext *tc, JSONObject
if (newObj)
{
PRINTMARK();
Py_XDECREF(pc->newObj);
obj = pc->newObj = newObj;
level += 1;
goto BEGIN;
Expand Down
22 changes: 22 additions & 0 deletions tests/test_ujson.py
Expand Up @@ -974,6 +974,28 @@ def test_issue_334(indent):
ujson.dumps(a, indent=indent)


@pytest.mark.skipif(
hasattr(sys, "pypy_version_info"), reason="PyPy uses incompatible GC"
)
def test_default_ref_counting():
class DefaultRefCountingClass:
def __init__(self, value):
self._value = value

def convert(self):
if self._value > 1:
return type(self)(self._value - 1)
return 0

import gc

gc.collect()
ujson.dumps(DefaultRefCountingClass(3), default=lambda x: x.convert())
assert not any(
type(o).__name__ == "DefaultRefCountingClass" for o in gc.get_objects()
)


"""
def test_decode_numeric_int_frc_overflow():
input = "X.Y"
Expand Down