New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
textwrap.dedent() expands tabs #42613
Comments
I'm not sure whether this is a documentation bug or a py> def test(): Looking at the code, I can see the culprit is the first lines = text.expandtabs().split('\n') If this is the intended behavior, I think the first """ and (I guess this part is an RFE) textwrap.dedent() If it's not the intended behavior, I'd love to see that |
Logged In: YES FWIW, the tab expansion would be more useful if the default |
Logged In: YES After more thought, I think the expandtabs() is a bug since >>> textwrap.dedent('\tABC\t\tDEF')
'ABC DEF' This is especially problematic given that dedent() has to If this gets fixed, I recommend using regular expressions as |
Logged In: YES 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 |
Logged In: YES Looks good! |
Logged In: YES I agree that the docs are (pretty) clear and the code is Raymond's fix was close, but not quite there: considering My fix is based on Raymond's, i.e. it uses regexes for most Checked in under rev 46844 (trunk only). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: