Bug report
Bug description:
tokenize.untokenize() replaces the whitespace between tokens and after indents with spaces:
>>> import tokenize
>>> code = b"if False:\n\tprint(\n\t\t'hello')"
>>> readline = iter(code.splitlines(keepends=True)).__next__
>>> tokenize.untokenize(tokenize.tokenize(readline))
b"if False:\n\tprint(\n\t 'hello')"
Note the second tab before 'hello' becomes a space.
The culprit is Untokenizer.add_whitespace which is called from the loop inside Untokenizer.untokenize whenever a 5-tuple iterator is used. The fifth item of the tuple is the original line; therefore, the loop could pass it to add_whitespace to reconstruct the original whitespace.
CPython versions tested on:
3.10, CPython main branch
Operating systems tested on:
Windows
Linked PRs
Bug report
Bug description:
tokenize.untokenize()replaces the whitespace between tokens and after indents with spaces:Note the second tab before
'hello'becomes a space.The culprit is
Untokenizer.add_whitespacewhich is called from the loop insideUntokenizer.untokenizewhenever a 5-tuple iterator is used. The fifth item of the tuple is the original line; therefore, the loop could pass it toadd_whitespaceto reconstruct the original whitespace.CPython versions tested on:
3.10, CPython main branch
Operating systems tested on:
Windows
Linked PRs
tokenize.untokenizeto preserve the original line's whitespace #156073