]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-90117: Check for list and tuple before MappingView in pprint (GH-135779)
authorBrandt Bucher <brandtbucher@microsoft.com>
Tue, 24 Jun 2025 21:41:41 +0000 (14:41 -0700)
committerGitHub <noreply@github.com>
Tue, 24 Jun 2025 21:41:41 +0000 (14:41 -0700)
Lib/pprint.py
Misc/NEWS.d/next/Library/2025-06-20-17-06-59.gh-issue-90117.GYWVrn.rst [new file with mode: 0644]

index 1e611481b51ac00b9c5e4c45cdb73743a8a23d4f..92a2c543ac279cdbe92af1d7841c527929a500dc 100644 (file)
@@ -653,6 +653,40 @@ class PrettyPrinter:
             del context[objid]
             return "{%s}" % ", ".join(components), readable, recursive
 
+        if (issubclass(typ, list) and r is list.__repr__) or \
+           (issubclass(typ, tuple) and r is tuple.__repr__):
+            if issubclass(typ, list):
+                if not object:
+                    return "[]", True, False
+                format = "[%s]"
+            elif len(object) == 1:
+                format = "(%s,)"
+            else:
+                if not object:
+                    return "()", True, False
+                format = "(%s)"
+            objid = id(object)
+            if maxlevels and level >= maxlevels:
+                return format % "...", False, objid in context
+            if objid in context:
+                return _recursion(object), False, True
+            context[objid] = 1
+            readable = True
+            recursive = False
+            components = []
+            append = components.append
+            level += 1
+            for o in object:
+                orepr, oreadable, orecur = self.format(
+                    o, context, maxlevels, level)
+                append(orepr)
+                if not oreadable:
+                    readable = False
+                if orecur:
+                    recursive = True
+            del context[objid]
+            return format % ", ".join(components), readable, recursive
+
         if issubclass(typ, _collections.abc.MappingView) and r in self._view_reprs:
             objid = id(object)
             if maxlevels and level >= maxlevels:
@@ -689,40 +723,6 @@ class PrettyPrinter:
             del context[objid]
             return typ.__name__ + '([%s])' % ", ".join(components), readable, recursive
 
-        if (issubclass(typ, list) and r is list.__repr__) or \
-           (issubclass(typ, tuple) and r is tuple.__repr__):
-            if issubclass(typ, list):
-                if not object:
-                    return "[]", True, False
-                format = "[%s]"
-            elif len(object) == 1:
-                format = "(%s,)"
-            else:
-                if not object:
-                    return "()", True, False
-                format = "(%s)"
-            objid = id(object)
-            if maxlevels and level >= maxlevels:
-                return format % "...", False, objid in context
-            if objid in context:
-                return _recursion(object), False, True
-            context[objid] = 1
-            readable = True
-            recursive = False
-            components = []
-            append = components.append
-            level += 1
-            for o in object:
-                orepr, oreadable, orecur = self.format(
-                    o, context, maxlevels, level)
-                append(orepr)
-                if not oreadable:
-                    readable = False
-                if orecur:
-                    recursive = True
-            del context[objid]
-            return format % ", ".join(components), readable, recursive
-
         rep = repr(object)
         return rep, (rep and not rep.startswith('<')), False
 
diff --git a/Misc/NEWS.d/next/Library/2025-06-20-17-06-59.gh-issue-90117.GYWVrn.rst b/Misc/NEWS.d/next/Library/2025-06-20-17-06-59.gh-issue-90117.GYWVrn.rst
new file mode 100644 (file)
index 0000000..2bb15cb
--- /dev/null
@@ -0,0 +1 @@
+Speed up :mod:`pprint` for :class:`list` and :class:`tuple`.