Skip to content

Commit bcbfbd5

Browse files
author
makiper
committed
Revert "pythonGH-141686: Break cycles created by JSONEncoder.iterencode (pythonGH-141687)"
This reverts commit 4cfa695.
1 parent ff4e507 commit bcbfbd5

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

Lib/json/encoder.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,17 @@ def floatstr(o, allow_nan=self.allow_nan,
264264

265265
def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
266266
_key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,
267+
## HACK: hand-optimized bytecode; turn globals into locals
268+
ValueError=ValueError,
269+
dict=dict,
270+
float=float,
271+
id=id,
272+
int=int,
273+
isinstance=isinstance,
274+
list=list,
275+
str=str,
276+
tuple=tuple,
277+
_intstr=int.__repr__,
267278
):
268279

269280
def _iterencode_list(lst, _current_indent_level):
@@ -300,7 +311,7 @@ def _iterencode_list(lst, _current_indent_level):
300311
# Subclasses of int/float may override __repr__, but we still
301312
# want to encode them as integers/floats in JSON. One example
302313
# within the standard library is IntEnum.
303-
yield buf + int.__repr__(value)
314+
yield buf + _intstr(value)
304315
elif isinstance(value, float):
305316
# see comment above for int
306317
yield buf + _floatstr(value)
@@ -363,7 +374,7 @@ def _iterencode_dict(dct, _current_indent_level):
363374
key = 'null'
364375
elif isinstance(key, int):
365376
# see comment for int/float in _make_iterencode
366-
key = int.__repr__(key)
377+
key = _intstr(key)
367378
elif _skipkeys:
368379
continue
369380
else:
@@ -388,7 +399,7 @@ def _iterencode_dict(dct, _current_indent_level):
388399
yield 'false'
389400
elif isinstance(value, int):
390401
# see comment for int/float in _make_iterencode
391-
yield int.__repr__(value)
402+
yield _intstr(value)
392403
elif isinstance(value, float):
393404
# see comment for int/float in _make_iterencode
394405
yield _floatstr(value)
@@ -423,7 +434,7 @@ def _iterencode(o, _current_indent_level):
423434
yield 'false'
424435
elif isinstance(o, int):
425436
# see comment for int/float in _make_iterencode
426-
yield int.__repr__(o)
437+
yield _intstr(o)
427438
elif isinstance(o, float):
428439
# see comment for int/float in _make_iterencode
429440
yield _floatstr(o)
@@ -447,13 +458,4 @@ def _iterencode(o, _current_indent_level):
447458
raise
448459
if markers is not None:
449460
del markers[markerid]
450-
451-
def _iterencode_once(o, _current_indent_level):
452-
nonlocal _iterencode, _iterencode_dict, _iterencode_list
453-
try:
454-
yield from _iterencode(o, _current_indent_level)
455-
finally:
456-
# Break reference cycles due to mutually recursive closures:
457-
del _iterencode, _iterencode_dict, _iterencode_list
458-
459-
return _iterencode_once
461+
return _iterencode

Misc/NEWS.d/next/Library/2025-11-17-16-53-49.gh-issue-141686.V-xaoI.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)