changeset: 98877:c307ae7b3d9f branch: 2.7 parent: 98872:aaf8a25133ff user: Serhiy Storchaka date: Wed Oct 28 21:39:36 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 aaf8a25133ff -r c307ae7b3d9f Lib/test/test_textwrap.py --- a/Lib/test/test_textwrap.py Wed Oct 28 03:14:46 2015 -0400 +++ b/Lib/test/test_textwrap.py Wed Oct 28 21:39:36 2015 +0200 @@ -647,6 +647,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)) + def test_main(): test_support.run_unittest(WrapTestCase, diff -r aaf8a25133ff -r c307ae7b3d9f Lib/textwrap.py --- a/Lib/textwrap.py Wed Oct 28 03:14:46 2015 -0400 +++ b/Lib/textwrap.py Wed Oct 28 21:39:36 2015 +0200 @@ -403,11 +403,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 aaf8a25133ff -r c307ae7b3d9f Misc/ACKS --- a/Misc/ACKS Wed Oct 28 03:14:46 2015 -0400 +++ b/Misc/ACKS Wed Oct 28 21:39:36 2015 +0200 @@ -808,6 +808,7 @@ Mark Levitt William Lewis Akira Li +Robert Li Xuanji Li Robert van Liere Ross Light diff -r aaf8a25133ff -r c307ae7b3d9f Misc/NEWS --- a/Misc/NEWS Wed Oct 28 03:14:46 2015 -0400 +++ b/Misc/NEWS Wed Oct 28 21:39:36 2015 +0200 @@ -46,6 +46,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 #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