Skip to content

Commit 163abc6

Browse files
brandtbuchertaegyunkim
authored andcommitted
pythonGH-90117: Check for list and tuple before MappingView in pprint (pythonGH-135779)
1 parent 1bcf576 commit 163abc6

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

Lib/pprint.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,40 @@ def _safe_repr(self, object, context, maxlevels, level):
653653
del context[objid]
654654
return "{%s}" % ", ".join(components), readable, recursive
655655

656+
if (issubclass(typ, list) and r is list.__repr__) or \
657+
(issubclass(typ, tuple) and r is tuple.__repr__):
658+
if issubclass(typ, list):
659+
if not object:
660+
return "[]", True, False
661+
format = "[%s]"
662+
elif len(object) == 1:
663+
format = "(%s,)"
664+
else:
665+
if not object:
666+
return "()", True, False
667+
format = "(%s)"
668+
objid = id(object)
669+
if maxlevels and level >= maxlevels:
670+
return format % "...", False, objid in context
671+
if objid in context:
672+
return _recursion(object), False, True
673+
context[objid] = 1
674+
readable = True
675+
recursive = False
676+
components = []
677+
append = components.append
678+
level += 1
679+
for o in object:
680+
orepr, oreadable, orecur = self.format(
681+
o, context, maxlevels, level)
682+
append(orepr)
683+
if not oreadable:
684+
readable = False
685+
if orecur:
686+
recursive = True
687+
del context[objid]
688+
return format % ", ".join(components), readable, recursive
689+
656690
if issubclass(typ, _collections.abc.MappingView) and r in self._view_reprs:
657691
objid = id(object)
658692
if maxlevels and level >= maxlevels:
@@ -689,40 +723,6 @@ def _safe_repr(self, object, context, maxlevels, level):
689723
del context[objid]
690724
return typ.__name__ + '([%s])' % ", ".join(components), readable, recursive
691725

692-
if (issubclass(typ, list) and r is list.__repr__) or \
693-
(issubclass(typ, tuple) and r is tuple.__repr__):
694-
if issubclass(typ, list):
695-
if not object:
696-
return "[]", True, False
697-
format = "[%s]"
698-
elif len(object) == 1:
699-
format = "(%s,)"
700-
else:
701-
if not object:
702-
return "()", True, False
703-
format = "(%s)"
704-
objid = id(object)
705-
if maxlevels and level >= maxlevels:
706-
return format % "...", False, objid in context
707-
if objid in context:
708-
return _recursion(object), False, True
709-
context[objid] = 1
710-
readable = True
711-
recursive = False
712-
components = []
713-
append = components.append
714-
level += 1
715-
for o in object:
716-
orepr, oreadable, orecur = self.format(
717-
o, context, maxlevels, level)
718-
append(orepr)
719-
if not oreadable:
720-
readable = False
721-
if orecur:
722-
recursive = True
723-
del context[objid]
724-
return format % ", ".join(components), readable, recursive
725-
726726
rep = repr(object)
727727
return rep, (rep and not rep.startswith('<')), False
728728

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Speed up :mod:`pprint` for :class:`list` and :class:`tuple`.

0 commit comments

Comments
 (0)