[ python-Bugs-1361643 ] textwrap.dedent() expands tabs

SourceForge.net noreply at sourceforge.net
Sun Nov 20 05:52:00 CET 2005


Bugs item #1361643, was opened at 2005-11-19 14:02
Message generated for change (Comment added) made by rhettinger
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: 5
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: 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


More information about the Python-bugs-list mailing list