[Python-checkins] cpython (merge 3.5 -> default): Issue #21827: Fixed textwrap.dedent() for the case when largest common
serhiy.storchaka
python-checkins at python.org
Wed Oct 28 15:45:50 EDT 2015
https://hg.python.org/cpython/rev/8d3932671e48
changeset: 98879:8d3932671e48
parent: 98875:13be42829771
parent: 98878:3f29be82c944
user: Serhiy Storchaka <storchaka at gmail.com>
date: Wed Oct 28 21:45:01 2015 +0200
summary:
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.
files:
Lib/test/test_textwrap.py | 5 +++++
Lib/textwrap.py | 12 ++++++++----
Misc/ACKS | 1 +
Misc/NEWS | 4 ++++
4 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/Lib/test/test_textwrap.py b/Lib/test/test_textwrap.py
--- a/Lib/test/test_textwrap.py
+++ b/Lib/test/test_textwrap.py
@@ -748,6 +748,11 @@
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
--- a/Lib/textwrap.py
+++ b/Lib/textwrap.py
@@ -444,11 +444,15 @@
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
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -848,6 +848,7 @@
Ivan Levkivskyi
William Lewis
Akira Li
+Robert Li
Xuanji Li
Robert van Liere
Ross Light
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -63,6 +63,10 @@
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 #25447: The lru_cache() wrapper objects now can be copied and pickled
(by returning the original object unchanged).
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list