[Python-checkins] cpython: Issue #18682: Optimized pprint functions for builtin scalar types.

serhiy.storchaka python-checkins at python.org
Sat May 16 20:39:40 CEST 2015


https://hg.python.org/cpython/rev/c77a42c234d6
changeset:   96100:c77a42c234d6
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat May 16 21:38:05 2015 +0300
summary:
  Issue #18682: Optimized pprint functions for builtin scalar types.

files:
  Lib/pprint.py |  22 ++++------------------
  Misc/NEWS     |   2 ++
  2 files changed, 6 insertions(+), 18 deletions(-)


diff --git a/Lib/pprint.py b/Lib/pprint.py
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -489,24 +489,8 @@
 
 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 @@
     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>"
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -47,6 +47,8 @@
 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.

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list