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

Skip to content

Commit ea4cb63

Browse files
Issue #21827: Fixed textwrap.dedent() for the case when largest common
whitespace is a substring of smallest leading whitespace. Based on patch by Robert Li.
1 parent 68b6874 commit ea4cb63

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

Lib/test/test_textwrap.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,11 @@ def test_dedent_preserve_margin_tabs(self):
732732
expect = "hello there\n how are you?"
733733
self.assertEqual(expect, dedent(text))
734734

735+
# test margin is smaller than smallest indent
736+
text = " \thello there\n \thow are you?\n \tI'm fine, thanks"
737+
expect = " \thello there\n \thow are you?\n\tI'm fine, thanks"
738+
self.assertEqual(expect, dedent(text))
739+
735740

736741
# Test textwrap.indent
737742
class IndentTestCase(unittest.TestCase):

Lib/textwrap.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,15 @@ def dedent(text):
429429
elif margin.startswith(indent):
430430
margin = indent
431431

432-
# Current line and previous winner have no common whitespace:
433-
# there is no margin.
432+
# Find the largest common whitespace between current line and previous
433+
# winner.
434434
else:
435-
margin = ""
436-
break
435+
for i, (x, y) in enumerate(zip(margin, indent)):
436+
if x != y:
437+
margin = margin[:i]
438+
break
439+
else:
440+
margin = margin[:len(indent)]
437441

438442
# sanity check (testing/debugging only)
439443
if 0 and margin:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ Mark Levitt
823823
Ivan Levkivskyi
824824
William Lewis
825825
Akira Li
826+
Robert Li
826827
Xuanji Li
827828
Robert van Liere
828829
Ross Light

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ Core and Builtins
9696
Library
9797
-------
9898

99+
- Issue #21827: Fixed textwrap.dedent() for the case when largest common
100+
whitespace is a substring of smallest leading whitespace.
101+
Based on patch by Robert Li.
102+
99103
- Issue #25471: Sockets returned from accept() shouldn't appear to be
100104
nonblocking.
101105

0 commit comments

Comments
 (0)