Bugs item #1361643, was opened at 2005-11-19 14:02 Message generated for change (Comment added) made by gward 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: Python Library >Group: Python 2.5 >Status: Closed >Resolution: Fixed 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: Greg Ward (gward) Date: 2006-06-10 20:41 Message: Logged In: YES user_id=14422 I agree that the docs are (pretty) clear and the code is wrong. When determining common leading whitespace, tabs and spaces should *not* be treated as equivalent. Raymond's fix was close, but not quite there: considering only the length of leading whitespace still causes space/tab confusion. (This only became clear to me after I wrote several test cases.) My fix is based on Raymond's, i.e. it uses regexes for most of the heavy lifting rather than splitting the input string on newline and looping over the lines. The bit that's different is determining what exactly is the common leading whitespace string. Anyways, this ended up being a complete rewrite of dedent(). I also added a paragraph to the docs to clarify the distinction between tabs and spaces. Checked in under rev 46844 (trunk only). ---------------------------------------------------------------------- Comment By: Georg Brandl (birkenfeld) Date: 2005-12-15 03:45 Message: Logged In: YES user_id=1188172 Looks good! ---------------------------------------------------------------------- Comment By: Raymond Hettinger (rhettinger) Date: 2005-11-20 01: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-19 23: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 15: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