Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions Doc/library/tokenize.rst

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the docs could be more explicit about whether the iterator may or may not mix 2- and 5-element sequences.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a para describing the behavior when they are mixed. :)

Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,27 @@ write back the modified script.

.. function:: untokenize(iterable)

Converts tokens back into Python source code. The *iterable* must return
sequences with at least two elements, the token type and the token string.
Any additional sequence elements are ignored.

The result is guaranteed to tokenize back to match the input so that the
conversion is lossless and round-trips are assured. The guarantee applies
only to the token type and token string as the spacing between tokens
(column positions) may change.
Converts tokens back into Python source code.

The *iterable* should return sequences with either two or five elements:

- If *iterable* returns sequences with two elements ``(type, string)``,
the result will tokenize back to the same token types and strings as
the input, but the resulting output is poor.

- If *iterable* returns sequences with five elements
``(type, string, start, end, line)``,
the result will tokenize back to match the input as closely as possible
(tab characters may be replaced with spaces).

Comment thread
ByteFlowing1337 marked this conversation as resolved.
If *iterable* mixes two- and five-element sequences,
as soon as a two-element sequence is encountered, the extra elements
``(start, end, line)`` of any later five-element sequences are ignored too.

The result is guaranteed to tokenize back to match the input so
that the conversion is lossless and round-trips are assured.
The guarantee applies only to the token type and token string as
the spacing between tokens (column positions) may change.

It returns bytes, encoded using the :data:`~token.ENCODING` token, which
is the first token sequence output by :func:`.tokenize`. If there is no
Expand Down
Loading