Index: textwrap.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/textwrap.py,v retrieving revision 1.38 diff -u -r1.38 textwrap.py --- textwrap.py 15 Jul 2005 06:53:35 -0000 1.38 +++ textwrap.py 20 Nov 2005 12:28:47 -0000 @@ -317,15 +317,18 @@ # -- Loosely related functionality ------------------------------------- +_emptylines_with_spaces = re.compile('(?m)^[ \t]+$') +_prefixes_on_nonempty_lines = re.compile('(?m)(^[ \t]*)(?:[^ \t\n])') + def dedent(text): """dedent(text : string) -> string - Remove any whitespace than can be uniformly removed from the left - of every line in `text`. + Remove any whitespace than can be uniformly removed from the left of + every line in `text`. Also trims excess whitespace on emtpy lines. - This can be used e.g. to make triple-quoted strings line up with - the left edge of screen/whatever, while still presenting it in the - source code in indented form. + One use is to make triple-quoted strings line up with the left edge + of screen or whatever, while keeping the original indented inside + the source code. For example: @@ -338,20 +341,9 @@ print repr(s) # prints ' hello\n world\n ' print repr(dedent(s)) # prints 'hello\n world\n' """ - lines = text.expandtabs().split('\n') - margin = None - for line in lines: - content = line.lstrip() - if not content: - continue - indent = len(line) - len(content) - if margin is None: - margin = indent - else: - margin = min(margin, indent) - - if margin is not None and margin> 0: - for i in range(len(lines)): - lines[i] = lines[i][margin:] - - return '\n'.join(lines) + text = _emptylines_with_spaces.sub('', text) + prefixes = _prefixes_on_nonempty_lines.findall(text) + margin = min(prefixes or '') + if margin: + text = re.sub('(?m)^' + margin, '', text) + return text Index: test/test_textwrap.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_textwrap.py,v retrieving revision 1.29 diff -u -r1.29 test_textwrap.py --- test/test_textwrap.py 3 Aug 2005 17:09:02 -0000 1.29 +++ test/test_textwrap.py 20 Nov 2005 12:28:47 -0000 @@ -517,6 +517,10 @@ expect = "Foo\n Bar\n\n Baz\n" self.assertEquals(dedent(text), expect) + # SF 1361643: Don't expand tabs inside content + text = " Content\t\tTabbed" + expect = "Content\t\tTabbed" + self.assertEquals(dedent(text), expect) def test_main():