Skip to content

Commit c3dfaab

Browse files
committed
Implement expand=True for str
1 parent b1df0bb commit c3dfaab

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Lib/pprint.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,10 @@ def _pprint_str(self, object, stream, indent, allowance, context, level):
406406
chunks = []
407407
lines = object.splitlines(True)
408408
if level == 1:
409-
indent += 1
409+
if self._expand:
410+
indent += self._indent_per_level
411+
else:
412+
indent += 1
410413
allowance += 1
411414
max_width1 = max_width = self._width - indent
412415
for i, line in enumerate(lines):
@@ -442,13 +445,13 @@ def _pprint_str(self, object, stream, indent, allowance, context, level):
442445
write(rep)
443446
return
444447
if level == 1:
445-
write('(')
448+
write(self._format_block_start("(", indent))
446449
for i, rep in enumerate(chunks):
447450
if i > 0:
448451
write('\n' + ' '*indent)
449452
write(rep)
450453
if level == 1:
451-
write(')')
454+
write(self._format_block_end(")", indent - self._indent_per_level))
452455

453456
_dispatch[str.__repr__] = _pprint_str
454457

Lib/test/test_pprint.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,20 @@ def test_expand_dict_items(self):
19451945
])""",
19461946
)
19471947

1948+
def test_expand_str(self):
1949+
s = "The quick brown fox jumped over the lazy dog " * 3
1950+
self.assertEqual(
1951+
pprint.pformat(s, width=40, indent=4, expand=True),
1952+
"""\
1953+
(
1954+
'The quick brown fox jumped over '
1955+
'the lazy dog The quick brown fox '
1956+
'jumped over the lazy dog The '
1957+
'quick brown fox jumped over the '
1958+
'lazy dog '
1959+
)""",
1960+
)
1961+
19481962

19491963
class DottedPrettyPrinter(pprint.PrettyPrinter):
19501964

0 commit comments

Comments
 (0)