Bugs item #1361643, was opened at 2005-11-19 20:02 Message generated for change (Comment added) made by birkenfeld You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1361643&group_id=5470
Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: None Group: None Status: Open Resolution: None Priority: 6 Submitted By: Steven Bethard (bediviere) Assigned to: Greg Ward (gward) Summary: textwrap.dedent() expands tabs Initial Comment: I'm not sure whether this is a documentation bug or a code bug, but textwrap.dedent() expands tabs (and AFAICT doesn't give the user any way of stopping this): py> def test(): ... x = ('abcd efgh\n' ... 'ijkl mnop\n') ... y = textwrap.dedent('''... abcd efgh ... ijkl mnop ... ''') ... return x, y ... py> test() ('abcd\tefgh\nijkl\tmnop\n', 'abcd efgh\nijkl mnop\n') Looking at the code, I can see the culprit is the first line: lines = text.expandtabs().split('\n') If this is the intended behavior, I think the first sentence in the documentation[1] should be replaced with: """ Replace all tabs in string with spaces as per str.expandtabs() and then remove any whitespace that can be uniformly removed from the left of every line in text. """ and (I guess this part is an RFE) textwrap.dedent() should gain an optional expandtabs= keyword argument to disable this behavior. If it's not the intended behavior, I'd love to see that .expandtabs() call removed. [1]http://docs.python.org/lib/module-textwrap.html ---------------------------------------------------------------------- >Comment By: Reinhold Birkenfeld (birkenfeld) Date: 2005-12-15 09:45 Message: Logged In: YES user_id=1188172 Looks good! ---------------------------------------------------------------------- Comment By: Raymond Hettinger (rhettinger) Date: 2005-11-20 07:04 Message: Logged In: YES user_id=80475 Suggested code: import re as _re _emptylines_with_spaces = _re.compile('(?m)^[ \t]+$') _prefixes_on_nonempty_lines = _re.compile('(?m)(^[ \t]*)(?:[^ \t\n]+)') def dedent(text): 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 ---------------------------------------------------------------------- Comment By: Raymond Hettinger (rhettinger) Date: 2005-11-20 05:52 Message: Logged In: YES user_id=80475 After more thought, I think the expandtabs() is a bug since it expands content tabs as well as margin tabs: >>> textwrap.dedent('\tABC\t\tDEF') 'ABC DEF' This is especially problematic given that dedent() has to guess at the tab size. If this gets fixed, I recommend using regular expressions as a way to indentify common margin prefixes on non-empty lines. This will also mixes of spaces and tabs without altering content with embedded tabs and without making assumptions about the tab size. Also, it ought to run somewhat faster. ---------------------------------------------------------------------- Comment By: Raymond Hettinger (rhettinger) Date: 2005-11-19 21:18 Message: Logged In: YES user_id=80475 FWIW, the tab expansion would be more useful if the default tabsize could be changed. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1361643&group_id=5470 _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com