changeset: 98878:3f29be82c944 branch: 3.5 parent: 98874:fea528e2dc26 parent: 98876:faeeb8dbe432 user: Serhiy Storchaka date: Wed Oct 28 21:43:12 2015 +0200 files: Lib/test/test_textwrap.py Lib/textwrap.py Misc/ACKS Misc/NEWS description: 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. diff -r fea528e2dc26 -r 3f29be82c944 Lib/test/test_textwrap.py --- a/Lib/test/test_textwrap.py Wed Oct 28 03:15:20 2015 -0400 +++ b/Lib/test/test_textwrap.py Wed Oct 28 21:43:12 2015 +0200 @@ -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 -r fea528e2dc26 -r 3f29be82c944 Lib/textwrap.py --- a/Lib/textwrap.py Wed Oct 28 03:15:20 2015 -0400 +++ b/Lib/textwrap.py Wed Oct 28 21:43:12 2015 +0200 @@ -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 -r fea528e2dc26 -r 3f29be82c944 Misc/ACKS --- a/Misc/ACKS Wed Oct 28 03:15:20 2015 -0400 +++ b/Misc/ACKS Wed Oct 28 21:43:12 2015 +0200 @@ -847,6 +847,7 @@ Ivan Levkivskyi William Lewis Akira Li +Robert Li Xuanji Li Robert van Liere Ross Light diff -r fea528e2dc26 -r 3f29be82c944 Misc/NEWS --- a/Misc/NEWS Wed Oct 28 03:15:20 2015 -0400 +++ b/Misc/NEWS Wed Oct 28 21:43:12 2015 +0200 @@ -45,6 +45,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).