Skip to content

Commit

Permalink
Issue python#18682: Optimized pprint functions for builtin scalar types.
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed May 16, 2015
1 parent 6d90fd5 commit 8eb1f07
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
22 changes: 4 additions & 18 deletions Lib/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,24 +489,8 @@ def _pprint_user_string(self, object, stream, indent, allowance, context, level)

def _safe_repr(object, context, maxlevels, level):
typ = type(object)
if typ is str:
if 'locale' not in _sys.modules:
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
if typ in _builtin_scalars:
return repr(object), True, False

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

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

def _recursion(object):
return ("<Recursion on %s with id=%s>"
Expand Down
2 changes: 2 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Core and Builtins
Library
-------

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

- Issue #22027: smtplib now supports RFC 6531 (SMTPUTF8).

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

0 comments on commit 8eb1f07

Please sign in to comment.