]> granicus.if.org Git - python/commitdiff
Issue #21827: Fixed textwrap.dedent() for the case when largest common
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 28 Oct 2015 19:39:36 +0000 (21:39 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 28 Oct 2015 19:39:36 +0000 (21:39 +0200)
whitespace is a substring of smallest leading whitespace.
Based on patch by Robert Li.

Lib/test/test_textwrap.py
Lib/textwrap.py
Misc/ACKS
Misc/NEWS

index 1bba77eb9e08b52369325f0480ee035ce6d8c741..ea7c0895c1616cdbb348586854919a38d54a18f4 100644 (file)
@@ -732,6 +732,11 @@ def foo():
         expect = "hello there\n  how are you?"
         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
 class IndentTestCase(unittest.TestCase):
index 58867f93bb3350c6519e6365747815d29178446b..1759b0d7cfe1cafdba79c3fd175519c57ac90bd6 100644 (file)
@@ -429,11 +429,15 @@ def dedent(text):
         elif margin.startswith(indent):
             margin = indent
 
-        # Current line and previous winner have no common whitespace:
-        # there is no margin.
+        # Find the largest common whitespace between current line and previous
+        # winner.
         else:
-            margin = ""
-            break
+            for i, (x, y) in enumerate(zip(margin, indent)):
+                if x != y:
+                    margin = margin[:i]
+                    break
+            else:
+                margin = margin[:len(indent)]
 
     # sanity check (testing/debugging only)
     if 0 and margin:
index 893fb2d25344767af646d68455beef56c1a0590f..abf73045c0288da69c1af5ae793bfd3e25d84871 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -823,6 +823,7 @@ Mark Levitt
 Ivan Levkivskyi
 William Lewis
 Akira Li
+Robert Li
 Xuanji Li
 Robert van Liere
 Ross Light
index 7d673b2cc29df66c43c79a5b41960c3dc0953238..3d4cbcb578bc4b123649762cc8642ea944e49322 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -96,6 +96,10 @@ Core and Builtins
 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
   nonblocking.