This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: textwrap.wrap expand_tabs does not behave as expected
Type: Stage: resolved
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: chris.jerdonek, r.david.murray
Priority: normal Keywords:

Created on 2012-07-29 21:11 by chris.jerdonek, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg166815 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-07-29 21:11
While working on issue 1859, I noticed that textwrap.wrap()'s tab expansion does not seem to behave sensibly.

In particular, the documentation says, "If expand_tabs is true, then all tab characters in text will be expanded to zero or more spaces, *depending on the current column* and the given tab size."

The problem is that tab expansion is done as the first stage (and in particular, before wrapping), which means that "the current column" is no longer current after wrapping.  This leads to results like--

from textwrap import wrap

text = ("a\tb "
        "a\tb")

lines = wrap(text, width=5, tabsize=4)
for line in lines:
    print(repr(line))

Output:
'a   b'
'a b'

One would expect tab expansion to occur after (or while) wrapping, so that tab stops line up in the wrapped output.
msg167334 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-03 14:52
I think that expanding them before wrapping is correct.  Any lining up would be true only for the original unwrapped input text.
msg167506 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-08-05 16:16
I didn't have a chance to respond before you closed this issue.  I will add more information later if that is okay. :)
msg167530 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-06 01:43
Sure.  If you convince me we can reopen the issue :)
History
Date User Action Args
2022-04-11 14:57:33adminsetgithub: 59697
2012-08-06 01:43:54r.david.murraysetmessages: + msg167530
2012-08-05 16:16:57chris.jerdoneksetmessages: + msg167506
2012-08-04 19:24:10r.david.murraysetstatus: open -> closed
resolution: not a bug
stage: resolved
2012-08-03 14:52:09r.david.murraysetnosy: + r.david.murray
messages: + msg167334
2012-07-29 21:11:56chris.jerdonekcreate