Skip to content

tokenize.untokenize() does not preserve the whitespace of the original source #156072

Description

@kelly-eclipse

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions