Commit 8eb1f077 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #18682: Optimized pprint functions for builtin scalar types.

parent 6d90fd5f
...@@ -489,24 +489,8 @@ class PrettyPrinter: ...@@ -489,24 +489,8 @@ class PrettyPrinter:
def _safe_repr(object, context, maxlevels, level): def _safe_repr(object, context, maxlevels, level):
typ = type(object) typ = type(object)
if typ is str: if typ in _builtin_scalars:
if 'locale' not in _sys.modules: return repr(object), True, False
return repr(object), True, False
if "'" in object and '"' not in object:
closure = '"'
quotes = {'"': '\\"'}
else:
closure = "'"
quotes = {"'": "\\'"}
qget = quotes.get
sio = _StringIO()
write = sio.write
for char in object:
if char.isalpha():
write(char)
else:
write(qget(char, repr(char)[1:-1]))
return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
r = getattr(typ, "__repr__", None) r = getattr(typ, "__repr__", None)
if issubclass(typ, dict) and r is dict.__repr__: if issubclass(typ, dict) and r is dict.__repr__:
...@@ -571,6 +555,8 @@ def _safe_repr(object, context, maxlevels, level): ...@@ -571,6 +555,8 @@ def _safe_repr(object, context, maxlevels, level):
rep = repr(object) rep = repr(object)
return rep, (rep and not rep.startswith('<')), False return rep, (rep and not rep.startswith('<')), False
_builtin_scalars = frozenset({str, bytes, bytearray, int, float, complex,
bool, type(None)})
def _recursion(object): def _recursion(object):
return ("<Recursion on %s with id=%s>" return ("<Recursion on %s with id=%s>"
......
...@@ -47,6 +47,8 @@ Core and Builtins ...@@ -47,6 +47,8 @@ Core and Builtins
Library Library
------- -------
- Issue #18682: Optimized pprint functions for builtin scalar types.
- Issue #22027: smtplib now supports RFC 6531 (SMTPUTF8). - Issue #22027: smtplib now supports RFC 6531 (SMTPUTF8).
- Issue #23488: Random generator objects now consume 2x less memory on 64-bit. - Issue #23488: Random generator objects now consume 2x less memory on 64-bit.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment