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):
# 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)
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:
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)
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)
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