Skip to content

Python 3.12 tokenize generates invalid locations for f'\N{unicode}' #115154

Closed

Description

@smartbomb

Bug report

Bug description:

from tokenize import untokenize, generate_tokens
from io import StringIO

untokenize(generate_tokens(StringIO("f'\\N{EXCLAMATION MARK}'").readline))

ValueError: start (1,22) precedes previous end (1,24)

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

Activity

  1. Eclips4 commented on Feb 8, 2024

    @Eclips4
    Member
  2. terryjreedy commented on Feb 8, 2024

    @terryjreedy
    Member

    The tokens parsing f'\\N{EXCLAMATION MARK}' and traceback unparsing the list (toks) are

    [TokenInfo(type=59 (FSTRING_START), string="f'", start=(1, 0), end=(1, 2), line="f'\\N{EXCLAMATION MARK}'"),
     TokenInfo(type=60 (FSTRING_MIDDLE), string='\\N{EXCLAMATION MARK}', start=(1, 2), end=(1, 22),
       line="f'\\N{EXCLAMATION MARK}'"), 
    TokenInfo(type=61 (FSTRING_END), string="'", start=(1, 22), end=(1, 23), line="f'\\N{EXCLAMATION MARK}'"),
     TokenInfo(type=4 (NEWLINE), string='', start=(1, 23), end=(1, 24), line="f'\\N{EXCLAMATION MARK}'"),
     TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='')]
    #
    Traceback (most recent call last):
      File "F:\dev\tem\tem2.py", line 6, in <module>
        print(untokenize(toks))
      File "C:\Programs\Python313\Lib\tokenize.py", line 294, in untokenize
        out = ut.untokenize(iterable)
      File "C:\Programs\Python313\Lib\tokenize.py", line 223, in untokenize
        self.add_whitespace(start)
      File "C:\Programs\Python313\Lib\tokenize.py", line 176, in add_whitespace
        raise ValueError("start ({},{}) precedes previous end ({},{})"
    ValueError: start (1,22) precedes previous end (1,24)
    

    The problem is that when Untokenizer.untokenize, line215 sees the FSTRING_MIDDLE token, it replaces '{' and '}' with '{{' and '}}' and bumps the end position by 2, making the end column 2 more than the next start column. This works when the presence of curly brackets results from the reverse process, but not when the lexer recognizes \N{name} as a unicode named literal without replacing it with the indicated character. Other escapes are resolved, as with '\ueeee' being tokenized as a single character. Unless the tokenizer replaces \N{name} with a character, the untokenizer must recognize it also and not do the replacement.

  3. pablogsal commented on Feb 8, 2024

    @pablogsal
    Member
  4. added 2 commits that reference this issue on Feb 8, 2024
  5. added a commit that references this issue on Feb 19, 2024
  6. added a commit that references this issue on Feb 19, 2024
  7. added a commit that references this issue on Feb 19, 2024
  8. added a commit that references this issue on Mar 4, 2024
  9. added a commit that references this issue on Apr 17, 2024
  10. added a commit that references this issue on Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions