[Python-checkins] cpython (3.4): Issue #19104: pprint now produces evaluable output for wrapped strings.

serhiy.storchaka python-checkins at python.org
Sat Dec 20 20:02:23 CET 2014


https://hg.python.org/cpython/rev/872f048f0403
changeset:   93940:872f048f0403
branch:      3.4
parent:      93938:1ab04bcd03bf
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Dec 20 20:57:15 2014 +0200
summary:
  Issue #19104: pprint now produces evaluable output for wrapped strings.

files:
  Lib/pprint.py           |  58 ++++++++++++++++-------------
  Lib/test/test_pprint.py |  23 ++++++-----
  Misc/NEWS               |   2 +
  3 files changed, 47 insertions(+), 36 deletions(-)


diff --git a/Lib/pprint.py b/Lib/pprint.py
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -235,35 +235,41 @@
                 return
 
             if issubclass(typ, str) and len(object) > 0 and r is str.__repr__:
-                def _str_parts(s):
-                    """
-                    Return a list of string literals comprising the repr()
-                    of the given string using literal concatenation.
-                    """
-                    lines = s.splitlines(True)
-                    for i, line in enumerate(lines):
-                        rep = repr(line)
-                        if len(rep) <= max_width:
-                            yield rep
-                        else:
-                            # A list of alternating (non-space, space) strings
-                            parts = re.split(r'(\s+)', line) + ['']
-                            current = ''
-                            for i in range(0, len(parts), 2):
-                                part = parts[i] + parts[i+1]
-                                candidate = current + part
-                                if len(repr(candidate)) > max_width:
-                                    if current:
-                                        yield repr(current)
-                                    current = part
-                                else:
-                                    current = candidate
-                            if current:
-                                yield repr(current)
-                for i, rep in enumerate(_str_parts(object)):
+                chunks = []
+                lines = object.splitlines(True)
+                if level == 1:
+                    indent += 1
+                    max_width -= 2
+                for i, line in enumerate(lines):
+                    rep = repr(line)
+                    if len(rep) <= max_width:
+                        chunks.append(rep)
+                    else:
+                        # A list of alternating (non-space, space) strings
+                        parts = re.split(r'(\s+)', line) + ['']
+                        current = ''
+                        for i in range(0, len(parts), 2):
+                            part = parts[i] + parts[i+1]
+                            candidate = current + part
+                            if len(repr(candidate)) > max_width:
+                                if current:
+                                    chunks.append(repr(current))
+                                current = part
+                            else:
+                                current = candidate
+                        if current:
+                            chunks.append(repr(current))
+                if len(chunks) == 1:
+                    write(rep)
+                    return
+                if level == 1:
+                    write('(')
+                for i, rep in enumerate(chunks):
                     if i > 0:
                         write('\n' + ' '*indent)
                     write(rep)
+                if level == 1:
+                    write(')')
                 return
         write(rep)
 
diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py
--- a/Lib/test/test_pprint.py
+++ b/Lib/test/test_pprint.py
@@ -536,9 +536,10 @@
         # pprint tries to wrap strings intelligently
         fox = 'the quick brown fox jumped over a lazy dog'
         self.assertEqual(pprint.pformat(fox, width=20), """\
-'the quick brown '
-'fox jumped over '
-'a lazy dog'""")
+('the quick '
+ 'brown fox '
+ 'jumped over a '
+ 'lazy dog')""")
         self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2},
                                         width=26), """\
 {'a': 1,
@@ -552,12 +553,12 @@
         # - non-ASCII is allowed
         # - an apostrophe doesn't disrupt the pprint
         special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo"
-        self.assertEqual(pprint.pformat(special, width=20), """\
-'Portons dix bons '
-'"whiskys"\\n'
-"à l'avocat "
-'goujat\\t qui '
-'fumait au zoo'""")
+        self.assertEqual(pprint.pformat(special, width=21), """\
+('Portons dix '
+ 'bons "whiskys"\\n'
+ "à l'avocat "
+ 'goujat\\t qui '
+ 'fumait au zoo')""")
         # An unwrappable string is formatted as its repr
         unwrappable = "x" * 100
         self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable))
@@ -566,7 +567,9 @@
         special *= 10
         for width in range(3, 40):
             formatted = pprint.pformat(special, width=width)
-            self.assertEqual(eval("(" + formatted + ")"), special)
+            self.assertEqual(eval(formatted), special)
+            formatted = pprint.pformat([special] * 2, width=width)
+            self.assertEqual(eval(formatted), [special] * 2)
 
     def test_compact(self):
         o = ([list(range(i * i)) for i in range(5)] +
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -41,6 +41,8 @@
 Library
 -------
 
+- Issue #19104: pprint now produces evaluable output for wrapped strings.
+
 - Issue #23071: Added missing names to codecs.__all__.  Patch by Martin Panter.
 
 - Issue #15513: Added a __sizeof__ implementation for pickle classes.

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


More information about the Python-checkins mailing list