summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-10-28 19:39:36 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-10-28 19:39:36 (GMT)
commitfbe04b68af58ea513cf95cb45dc0d2656bd67cfc (patch)
tree447fd3b7c8d2050235d6e48210186d0aa659baae
parent0a1ba709493656cb6f450c0ec3d07566185104b4 (diff)
downloadcpython-fbe04b68af58ea513cf95cb45dc0d2656bd67cfc.zip
cpython-fbe04b68af58ea513cf95cb45dc0d2656bd67cfc.tar.gz
cpython-fbe04b68af58ea513cf95cb45dc0d2656bd67cfc.tar.bz2
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.
-rw-r--r--Lib/test/test_textwrap.py5
-rw-r--r--Lib/textwrap.py12
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS4
4 files changed, 18 insertions, 4 deletions
diff --git a/Lib/test/test_textwrap.py b/Lib/test/test_textwrap.py
index 7b72672..dccf095 100644
--- a/Lib/test/test_textwrap.py
+++ b/Lib/test/test_textwrap.py
@@ -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,
diff --git a/Lib/textwrap.py b/Lib/textwrap.py
index e755860..5c2e4fa 100644
--- a/Lib/textwrap.py
+++ b/Lib/textwrap.py
@@ -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:
diff --git a/Misc/ACKS b/Misc/ACKS
index 0c7d7d9..35619ec 100644
--- 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
diff --git a/Misc/NEWS b/Misc/NEWS
index c5e1b7f..b5d50f8 100644
--- 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