cpython: 4d3066d4a5df (original) (raw)
Mercurial > cpython
changeset 93941:4d3066d4a5df
Issue #19104: pprint now produces evaluable output for wrapped strings. [#19104]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Sat, 20 Dec 2014 20:58:28 +0200 |
parents | 7cb79c494abc(current diff)872f048f0403(diff) |
children | 75ede5bec8db |
files | Misc/NEWS |
diffstat | 3 files changed, 47 insertions(+), 36 deletions(-)[+] [-] Lib/pprint.py 58 Lib/test/test_pprint.py 23 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -235,35 +235,41 @@ class PrettyPrinter: return if issubclass(typ, str) and len(object) > 0 and r is str.repr:
def _str_parts(s):[](#l1.7)
"""[](#l1.8)
Return a list of string literals comprising the repr()[](#l1.9)
of the given string using literal concatenation.[](#l1.10)
"""[](#l1.11)
lines = s.splitlines(True)[](#l1.12)
for i, line in enumerate(lines):[](#l1.13)
rep = repr(line)[](#l1.14)
if len(rep) <= max_width:[](#l1.15)
yield rep[](#l1.16)
else:[](#l1.17)
# A list of alternating (non-space, space) strings[](#l1.18)
parts = re.split(r'(\s+)', line) + [''][](#l1.19)
current = ''[](#l1.20)
for i in range(0, len(parts), 2):[](#l1.21)
part = parts[i] + parts[i+1][](#l1.22)
candidate = current + part[](#l1.23)
if len(repr(candidate)) > max_width:[](#l1.24)
if current:[](#l1.25)
yield repr(current)[](#l1.26)
current = part[](#l1.27)
else:[](#l1.28)
current = candidate[](#l1.29)
if current:[](#l1.30)
yield repr(current)[](#l1.31)
for i, rep in enumerate(_str_parts(object)):[](#l1.32)
chunks = [][](#l1.33)
lines = object.splitlines(True)[](#l1.34)
if level == 1:[](#l1.35)
indent += 1[](#l1.36)
max_width -= 2[](#l1.37)
for i, line in enumerate(lines):[](#l1.38)
rep = repr(line)[](#l1.39)
if len(rep) <= max_width:[](#l1.40)
chunks.append(rep)[](#l1.41)
else:[](#l1.42)
# A list of alternating (non-space, space) strings[](#l1.43)
parts = re.split(r'(\s+)', line) + [''][](#l1.44)
current = ''[](#l1.45)
for i in range(0, len(parts), 2):[](#l1.46)
part = parts[i] + parts[i+1][](#l1.47)
candidate = current + part[](#l1.48)
if len(repr(candidate)) > max_width:[](#l1.49)
if current:[](#l1.50)
chunks.append(repr(current))[](#l1.51)
current = part[](#l1.52)
else:[](#l1.53)
current = candidate[](#l1.54)
if current:[](#l1.55)
chunks.append(repr(current))[](#l1.56)
if len(chunks) == 1:[](#l1.57)
write(rep)[](#l1.58)
return[](#l1.59)
if level == 1:[](#l1.60)
write('(')[](#l1.61)
for i, rep in enumerate(chunks):[](#l1.62) if i > 0:[](#l1.63) write('\n' + ' '*indent)[](#l1.64) write(rep)[](#l1.65)
if level == 1:[](#l1.66)
write(')')[](#l1.67) return[](#l1.68) write(rep)[](#l1.69)
--- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -536,9 +536,10 @@ frozenset2({0, # pprint tries to wrap strings intelligently fox = 'the quick brown fox jumped over a lazy dog' self.assertEqual(pprint.pformat(fox, width=20), """[](#l2.6) -'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), """[](#l2.15)
{'a': 1, @@ -552,12 +553,12 @@ frozenset2({0, # - 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), """\[](#l2.21)
-'Portons dix bons ' -'"whiskys"\n' -"à l'avocat " -'goujat\t qui ' -'fumait au zoo'""")
self.assertEqual(pprint.pformat(special, width=21), """\[](#l2.27)
- '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 @@ frozenset2({0, special *= 10 for width in range(3, 40): formatted = pprint.pformat(special, width=width)
self.assertEqual(eval("(" + formatted + ")"), special)[](#l2.40)
self.assertEqual(eval(formatted), special)[](#l2.41)
formatted = pprint.pformat([special] * 2, width=width)[](#l2.42)
self.assertEqual(eval(formatted), [special] * 2)[](#l2.43)
def test_compact(self): o = ([list(range(i * i)) for i in range(5)] +
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -196,6 +196,8 @@ Core and Builtins Library ------- +- Issue #19104: pprint now produces evaluable output for wrapped strings. +