]> 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 7b72672376984dad5e5a8acc966c00d258a2c1b6..dccf095e53dd78caba1328170890a89ac3321653 100644 (file)
@@ -647,6 +647,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))
+
 
 def test_main():
     test_support.run_unittest(WrapTestCase,
index e75586082862563c9a42683ffe566ff0ccda2736..5c2e4fa5237c9def8c249f8de4d7e5b5e85d9ea5 100644 (file)
@@ -403,11 +403,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 0c7d7d9cd0d7a714f58c1376bdeab5e51b3535d7..35619ecb5e73f43b7f3736f8ada8c4f415fac171 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -808,6 +808,7 @@ Mark Levinson
 Mark Levitt
 William Lewis
 Akira Li
+Robert Li
 Xuanji Li
 Robert van Liere
 Ross Light
index c5e1b7fdfac9b5a5eef0fc9808ec99ffc3975bdf..b5d50f816ee61a8ae9c371cdac876edbd0de0887 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -46,6 +46,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 #21709: Fix the logging module to not depend upon __file__ being set
   properly to get the filename of its caller from the stack.  This allows it
   to work if run in a frozen or embedded environment where the module's