Skip to content

Commit 8eb1f07

Browse files
Issue #18682: Optimized pprint functions for builtin scalar types.
1 parent 6d90fd5 commit 8eb1f07

2 files changed

Lines changed: 6 additions & 18 deletions

File tree

Lib/pprint.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -489,24 +489,8 @@ def _pprint_user_string(self, object, stream, indent, allowance, context, level)
489489

490490
def _safe_repr(object, context, maxlevels, level):
491491
typ = type(object)
492-
if typ is str:
493-
if 'locale' not in _sys.modules:
494-
return repr(object), True, False
495-
if "'" in object and '"' not in object:
496-
closure = '"'
497-
quotes = {'"': '\\"'}
498-
else:
499-
closure = "'"
500-
quotes = {"'": "\\'"}
501-
qget = quotes.get
502-
sio = _StringIO()
503-
write = sio.write
504-
for char in object:
505-
if char.isalpha():
506-
write(char)
507-
else:
508-
write(qget(char, repr(char)[1:-1]))
509-
return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
492+
if typ in _builtin_scalars:
493+
return repr(object), True, False
510494

511495
r = getattr(typ, "__repr__", None)
512496
if issubclass(typ, dict) and r is dict.__repr__:
@@ -571,6 +555,8 @@ def _safe_repr(object, context, maxlevels, level):
571555
rep = repr(object)
572556
return rep, (rep and not rep.startswith('<')), False
573557

558+
_builtin_scalars = frozenset({str, bytes, bytearray, int, float, complex,
559+
bool, type(None)})
574560

575561
def _recursion(object):
576562
return ("<Recursion on %s with id=%s>"

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Core and Builtins
4747
Library
4848
-------
4949

50+
- Issue #18682: Optimized pprint functions for builtin scalar types.
51+
5052
- Issue #22027: smtplib now supports RFC 6531 (SMTPUTF8).
5153

5254
- Issue #23488: Random generator objects now consume 2x less memory on 64-bit.

0 commit comments

Comments
 (0)