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)
commitea4cb63e68d96697ff8eb9ea86da6158f27b95a2 (patch)
tree017471658ea2343cce55ac59a16926db4c0b60e1
parent68b6874f59d80c41785a4d4a6874be89ad08fcd5 (diff)
downloadcpython-ea4cb63e68d96697ff8eb9ea86da6158f27b95a2.zip
cpython-ea4cb63e68d96697ff8eb9ea86da6158f27b95a2.tar.gz
cpython-ea4cb63e68d96697ff8eb9ea86da6158f27b95a2.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 1bba77e..ea7c089 100644
--- a/Lib/test/test_textwrap.py
+++ b/Lib/test/test_textwrap.py
@@ -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):
diff --git a/Lib/textwrap.py b/Lib/textwrap.py
index 58867f9..1759b0d 100644
--- a/Lib/textwrap.py
+++ b/Lib/textwrap.py
@@ -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:
diff --git a/Misc/ACKS b/Misc/ACKS
index 893fb2d..abf7304 100644
--- 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
diff --git a/Misc/NEWS b/Misc/NEWS
index 7d673b2..3d4cbcb 100644
--- 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.