Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c6fe4d

Browse files
Issue #19104: pprint now produces evaluable output for wrapped strings.
2 parents ff90e35 + fe3dc37 commit 3c6fe4d

3 files changed

Lines changed: 47 additions & 36 deletions

File tree

Lib/pprint.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -235,35 +235,41 @@ def _format(self, object, stream, indent, allowance, context, level):
235235
return
236236

237237
if issubclass(typ, str) and len(object) > 0 and r is str.__repr__:
238-
def _str_parts(s):
239-
"""
240-
Return a list of string literals comprising the repr()
241-
of the given string using literal concatenation.
242-
"""
243-
lines = s.splitlines(True)
244-
for i, line in enumerate(lines):
245-
rep = repr(line)
246-
if len(rep) <= max_width:
247-
yield rep
248-
else:
249-
# A list of alternating (non-space, space) strings
250-
parts = re.split(r'(\s+)', line) + ['']
251-
current = ''
252-
for i in range(0, len(parts), 2):
253-
part = parts[i] + parts[i+1]
254-
candidate = current + part
255-
if len(repr(candidate)) > max_width:
256-
if current:
257-
yield repr(current)
258-
current = part
259-
else:
260-
current = candidate
261-
if current:
262-
yield repr(current)
263-
for i, rep in enumerate(_str_parts(object)):
238+
chunks = []
239+
lines = object.splitlines(True)
240+
if level == 1:
241+
indent += 1
242+
max_width -= 2
243+
for i, line in enumerate(lines):
244+
rep = repr(line)
245+
if len(rep) <= max_width:
246+
chunks.append(rep)
247+
else:
248+
# A list of alternating (non-space, space) strings
249+
parts = re.split(r'(\s+)', line) + ['']
250+
current = ''
251+
for i in range(0, len(parts), 2):
252+
part = parts[i] + parts[i+1]
253+
candidate = current + part
254+
if len(repr(candidate)) > max_width:
255+
if current:
256+
chunks.append(repr(current))
257+
current = part
258+
else:
259+
current = candidate
260+
if current:
261+
chunks.append(repr(current))
262+
if len(chunks) == 1:
263+
write(rep)
264+
return
265+
if level == 1:
266+
write('(')
267+
for i, rep in enumerate(chunks):
264268
if i > 0:
265269
write('\n' + ' '*indent)
266270
write(rep)
271+
if level == 1:
272+
write(')')
267273
return
268274
write(rep)
269275

Lib/test/test_pprint.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,10 @@ def test_str_wrap(self):
536536
# pprint tries to wrap strings intelligently
537537
fox = 'the quick brown fox jumped over a lazy dog'
538538
self.assertEqual(pprint.pformat(fox, width=20), """\
539-
'the quick brown '
540-
'fox jumped over '
541-
'a lazy dog'""")
539+
('the quick '
540+
'brown fox '
541+
'jumped over a '
542+
'lazy dog')""")
542543
self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2},
543544
width=26), """\
544545
{'a': 1,
@@ -552,12 +553,12 @@ def test_str_wrap(self):
552553
# - non-ASCII is allowed
553554
# - an apostrophe doesn't disrupt the pprint
554555
special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo"
555-
self.assertEqual(pprint.pformat(special, width=20), """\
556-
'Portons dix bons '
557-
'"whiskys"\\n'
558-
"à l'avocat "
559-
'goujat\\t qui '
560-
'fumait au zoo'""")
556+
self.assertEqual(pprint.pformat(special, width=21), """\
557+
('Portons dix '
558+
'bons "whiskys"\\n'
559+
"à l'avocat "
560+
'goujat\\t qui '
561+
'fumait au zoo')""")
561562
# An unwrappable string is formatted as its repr
562563
unwrappable = "x" * 100
563564
self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable))
@@ -566,7 +567,9 @@ def test_str_wrap(self):
566567
special *= 10
567568
for width in range(3, 40):
568569
formatted = pprint.pformat(special, width=width)
569-
self.assertEqual(eval("(" + formatted + ")"), special)
570+
self.assertEqual(eval(formatted), special)
571+
formatted = pprint.pformat([special] * 2, width=width)
572+
self.assertEqual(eval(formatted), [special] * 2)
570573

571574
def test_compact(self):
572575
o = ([list(range(i * i)) for i in range(5)] +

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ Core and Builtins
196196
Library
197197
-------
198198

199+
- Issue #19104: pprint now produces evaluable output for wrapped strings.
200+
199201
- Issue #23071: Added missing names to codecs.__all__. Patch by Martin Panter.
200202

201203
- Issue #22783: Pickling now uses the NEWOBJ opcode instead of the NEWOBJ_EX

0 commit comments

Comments
 (0)