mirror of
https://github.com/python/cpython.git
synced 2025-12-07 13:50:06 +00:00
GH-141686: Break cycles created by JSONEncoder.iterencode (GH-141687)
This commit is contained in:
parent
daafacf005
commit
4cfa695c95
2 changed files with 16 additions and 16 deletions
|
|
@ -264,17 +264,6 @@ def floatstr(o, allow_nan=self.allow_nan,
|
|||
|
||||
def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
|
||||
_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):
|
||||
|
|
@ -311,7 +300,7 @@ def _iterencode_list(lst, _current_indent_level):
|
|||
# Subclasses of int/float may override __repr__, but we still
|
||||
# want to encode them as integers/floats in JSON. One example
|
||||
# within the standard library is IntEnum.
|
||||
yield buf + _intstr(value)
|
||||
yield buf + int.__repr__(value)
|
||||
elif isinstance(value, float):
|
||||
# see comment above for int
|
||||
yield buf + _floatstr(value)
|
||||
|
|
@ -374,7 +363,7 @@ def _iterencode_dict(dct, _current_indent_level):
|
|||
key = 'null'
|
||||
elif isinstance(key, int):
|
||||
# see comment for int/float in _make_iterencode
|
||||
key = _intstr(key)
|
||||
key = int.__repr__(key)
|
||||
elif _skipkeys:
|
||||
continue
|
||||
else:
|
||||
|
|
@ -399,7 +388,7 @@ def _iterencode_dict(dct, _current_indent_level):
|
|||
yield 'false'
|
||||
elif isinstance(value, int):
|
||||
# see comment for int/float in _make_iterencode
|
||||
yield _intstr(value)
|
||||
yield int.__repr__(value)
|
||||
elif isinstance(value, float):
|
||||
# see comment for int/float in _make_iterencode
|
||||
yield _floatstr(value)
|
||||
|
|
@ -434,7 +423,7 @@ def _iterencode(o, _current_indent_level):
|
|||
yield 'false'
|
||||
elif isinstance(o, int):
|
||||
# see comment for int/float in _make_iterencode
|
||||
yield _intstr(o)
|
||||
yield int.__repr__(o)
|
||||
elif isinstance(o, float):
|
||||
# see comment for int/float in _make_iterencode
|
||||
yield _floatstr(o)
|
||||
|
|
@ -458,4 +447,13 @@ def _iterencode(o, _current_indent_level):
|
|||
raise
|
||||
if markers is not None:
|
||||
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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
Break reference cycles created by each call to :func:`json.dump` or
|
||||
:meth:`json.JSONEncoder.iterencode`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue