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

Skip to content

Commit 2e0e18b

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.
2 parents 549edcf + f8152c6 commit 2e0e18b

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
@@ -748,6 +748,11 @@ def test_dedent_preserve_margin_tabs(self):
748748
expect = "hello there\n how are you?"
749749
self.assertEqual(expect, dedent(text))
750750

751+
# test margin is smaller than smallest indent
752+
text = " \thello there\n \thow are you?\n \tI'm fine, thanks"
753+
expect = " \thello there\n \thow are you?\n\tI'm fine, thanks"
754+
self.assertEqual(expect, dedent(text))
755+
751756

752757
# Test textwrap.indent
753758
class IndentTestCase(unittest.TestCase):

Lib/textwrap.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,15 @@ def dedent(text):
444444
elif margin.startswith(indent):
445445
margin = indent
446446

447-
# Current line and previous winner have no common whitespace:
448-
# there is no margin.
447+
# Find the largest common whitespace between current line and previous
448+
# winner.
449449
else:
450-
margin = ""
451-
break
450+
for i, (x, y) in enumerate(zip(margin, indent)):
451+
if x != y:
452+
margin = margin[:i]
453+
break
454+
else:
455+
margin = margin[:len(indent)]
452456

453457
# sanity check (testing/debugging only)
454458
if 0 and margin:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ Mark Levitt
848848
Ivan Levkivskyi
849849
William Lewis
850850
Akira Li
851+
Robert Li
851852
Xuanji Li
852853
Robert van Liere
853854
Ross Light

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ Core and Builtins
6363
Library
6464
-------
6565

66+
- Issue #21827: Fixed textwrap.dedent() for the case when largest common
67+
whitespace is a substring of smallest leading whitespace.
68+
Based on patch by Robert Li.
69+
6670
- Issue #25447: The lru_cache() wrapper objects now can be copied and pickled
6771
(by returning the original object unchanged).
6872

0 commit comments

Comments
 (0)