Skip to content

Commit

Permalink
Fix ref counting on repeated default function calls
Browse files Browse the repository at this point in the history
Fixes #523
  • Loading branch information
JustAnotherArchivist authored and bwoodsend committed Apr 7, 2022
1 parent f6860f1 commit 2d1f088
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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

0 comments on commit 2d1f088

Please sign in to comment.