GH-141686: Break cycles created by JSONEncoder.iterencode (GH-141687)

This commit is contained in:
Brandt Bucher 2025-11-18 09:51:18 -08:00 committed by GitHub
parent daafacf005
commit 4cfa695c95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 16 deletions

View file

@ -264,17 +264,6 @@ def floatstr(o, allow_nan=self.allow_nan,
def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
_key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot, _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,
## HACK: hand-optimized bytecode; turn globals into locals
ValueError=ValueError,
dict=dict,
float=float,
id=id,
int=int,
isinstance=isinstance,
list=list,
str=str,
tuple=tuple,
_intstr=int.__repr__,
): ):
def _iterencode_list(lst, _current_indent_level): def _iterencode_list(lst, _current_indent_level):
@ -311,7 +300,7 @@ def _iterencode_list(lst, _current_indent_level):
# Subclasses of int/float may override __repr__, but we still # Subclasses of int/float may override __repr__, but we still
# want to encode them as integers/floats in JSON. One example # want to encode them as integers/floats in JSON. One example
# within the standard library is IntEnum. # within the standard library is IntEnum.
yield buf + _intstr(value) yield buf + int.__repr__(value)
elif isinstance(value, float): elif isinstance(value, float):
# see comment above for int # see comment above for int
yield buf + _floatstr(value) yield buf + _floatstr(value)
@ -374,7 +363,7 @@ def _iterencode_dict(dct, _current_indent_level):
key = 'null' key = 'null'
elif isinstance(key, int): elif isinstance(key, int):
# see comment for int/float in _make_iterencode # see comment for int/float in _make_iterencode
key = _intstr(key) key = int.__repr__(key)
elif _skipkeys: elif _skipkeys:
continue continue
else: else:
@ -399,7 +388,7 @@ def _iterencode_dict(dct, _current_indent_level):
yield 'false' yield 'false'
elif isinstance(value, int): elif isinstance(value, int):
# see comment for int/float in _make_iterencode # see comment for int/float in _make_iterencode
yield _intstr(value) yield int.__repr__(value)
elif isinstance(value, float): elif isinstance(value, float):
# see comment for int/float in _make_iterencode # see comment for int/float in _make_iterencode
yield _floatstr(value) yield _floatstr(value)
@ -434,7 +423,7 @@ def _iterencode(o, _current_indent_level):
yield 'false' yield 'false'
elif isinstance(o, int): elif isinstance(o, int):
# see comment for int/float in _make_iterencode # see comment for int/float in _make_iterencode
yield _intstr(o) yield int.__repr__(o)
elif isinstance(o, float): elif isinstance(o, float):
# see comment for int/float in _make_iterencode # see comment for int/float in _make_iterencode
yield _floatstr(o) yield _floatstr(o)
@ -458,4 +447,13 @@ def _iterencode(o, _current_indent_level):
raise raise
if markers is not None: if markers is not None:
del markers[markerid] del markers[markerid]
return _iterencode
def _iterencode_once(o, _current_indent_level):
nonlocal _iterencode, _iterencode_dict, _iterencode_list
try:
yield from _iterencode(o, _current_indent_level)
finally:
# Break reference cycles due to mutually recursive closures:
del _iterencode, _iterencode_dict, _iterencode_list
return _iterencode_once

View file

@ -0,0 +1,2 @@
Break reference cycles created by each call to :func:`json.dump` or
:meth:`json.JSONEncoder.iterencode`.