summaryrefslogtreecommitdiff
path: root/Lib/pprint.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-05-16 21:38:05 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2015-05-16 21:38:05 +0300
commit8eb1f077c2be03f72ef31ddd2cfe805ffbfbd524 (patch)
tree088422d4680c76733272e64e703233a1b23af1a3 /Lib/pprint.py
parent6d90fd5fd90df5f84618d9125737b0df3b6c9a14 (diff)
downloadcpython-git-8eb1f077c2be03f72ef31ddd2cfe805ffbfbd524.tar.gz
Issue #18682: Optimized pprint functions for builtin scalar types.
Diffstat (limited to 'Lib/pprint.py')
-rw-r--r--Lib/pprint.py22
1 files changed, 4 insertions, 18 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py
index e084dc62fe..87649b48cd 100644
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -489,24 +489,8 @@ class PrettyPrinter:
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__:
@@ -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>"