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.
This commit is contained in:
Serhiy Storchaka 2015-10-28 21:39:36 +02:00
parent 68b6874f59
commit ea4cb63e68
4 changed files with 18 additions and 4 deletions

View File

@ -732,6 +732,11 @@ def foo():
expect = "hello there\n how are you?" expect = "hello there\n how are you?"
self.assertEqual(expect, dedent(text)) self.assertEqual(expect, dedent(text))
# test margin is smaller than smallest indent
text = " \thello there\n \thow are you?\n \tI'm fine, thanks"
expect = " \thello there\n \thow are you?\n\tI'm fine, thanks"
self.assertEqual(expect, dedent(text))
# Test textwrap.indent # Test textwrap.indent
class IndentTestCase(unittest.TestCase): class IndentTestCase(unittest.TestCase):

View File

@ -429,11 +429,15 @@ def dedent(text):
elif margin.startswith(indent): elif margin.startswith(indent):
margin = indent margin = indent
# Current line and previous winner have no common whitespace: # Find the largest common whitespace between current line and previous
# there is no margin. # winner.
else: else:
margin = "" for i, (x, y) in enumerate(zip(margin, indent)):
break if x != y:
margin = margin[:i]
break
else:
margin = margin[:len(indent)]
# sanity check (testing/debugging only) # sanity check (testing/debugging only)
if 0 and margin: if 0 and margin:

View File

@ -823,6 +823,7 @@ Mark Levitt
Ivan Levkivskyi Ivan Levkivskyi
William Lewis William Lewis
Akira Li Akira Li
Robert Li
Xuanji Li Xuanji Li
Robert van Liere Robert van Liere
Ross Light Ross Light

View File

@ -96,6 +96,10 @@ Core and Builtins
Library Library
------- -------
- 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.
- Issue #25471: Sockets returned from accept() shouldn't appear to be - Issue #25471: Sockets returned from accept() shouldn't appear to be
nonblocking. nonblocking.