GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Change in tokenize.generate_tokens behaviour with non-ASCII · Issue #112943 · python/cpython

Change in tokenize.generate_tokens behaviour with non-ASCII #112943

New issue
Copy link
New issue
Copy link
Closed
Closed
Change in tokenize.generate_tokens behaviour with non-ASCII#112943
Copy link
Labels
3.12only security fixesonly security fixes3.13only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-parsertype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@hugovk
hugovk
opened on Dec 10, 2023
Issue body actions

Bug report

Bug description:

This docstring has non-ASCII characters:

import io
import tokenize

src = '''\
def thing():
    """Autorzy, którzy tą jednostkę mają wpisani jako AKTUALNA -- czyli
    aktualni pracownicy, obecni pracownicy"""
    ...
'''
tokens = list(tokenize.generate_tokens(io.StringIO(src).readline))

for token in tokens:
    print(token)

assert tokens[7].end == (3, 45), tokens[7].end

And tokenize.generate_tokens has different behaviour between 3.11, and 3.12 (and 3.13).

Python 3.11

❯ python3.11 --version
Python 3.11.7

❯ python3.11 1.py
TokenInfo(type=1 (NAME), string='def', start=(1, 0), end=(1, 3), line='def thing():\n')
TokenInfo(type=1 (NAME), string='thing', start=(1, 4), end=(1, 9), line='def thing():\n')
TokenInfo(type=54 (OP), string='(', start=(1, 9), end=(1, 10), line='def thing():\n')
TokenInfo(type=54 (OP), string=')', start=(1, 10), end=(1, 11), line='def thing():\n')
TokenInfo(type=54 (OP), string=':', start=(1, 11), end=(1, 12), line='def thing():\n')
TokenInfo(type=4 (NEWLINE), string='\n', start=(1, 12), end=(1, 13), line='def thing():\n')
TokenInfo(type=5 (INDENT), string='    ', start=(2, 0), end=(2, 4), line='    """Autorzy, którzy tą jednostkę mają wpisani jako AKTUALNA -- czyli\n')
TokenInfo(type=3 (STRING), string='"""Autorzy, którzy tą jednostkę mają wpisani jako AKTUALNA -- czyli\n    aktualni pracownicy, obecni pracownicy"""', start=(2, 4), end=(3, 45), line='    """Autorzy, którzy tą jednostkę mają wpisani jako AKTUALNA -- czyli\n    aktualni pracownicy, obecni pracownicy"""\n')
TokenInfo(type=4 (NEWLINE), string='\n', start=(3, 45), end=(3, 46), line='    aktualni pracownicy, obecni pracownicy"""\n')
TokenInfo(type=54 (OP), string='...', start=(4, 4), end=(4, 7), line='    ...\n')
TokenInfo(type=4 (NEWLINE), string='\n', start=(4, 7), end=(4, 8), line='    ...\n')
TokenInfo(type=6 (DEDENT), string='', start=(5, 0), end=(5, 0), line='')
TokenInfo(type=0 (ENDMARKER), string='', start=(5, 0), end=(5, 0), line='')

Python 3.12

❯ python3.12 --version
Python 3.12.1

❯ python3.12 1.py
TokenInfo(type=1 (NAME), string='def', start=(1, 0), end=(1, 3), line='def thing():\n')
TokenInfo(type=1 (NAME), string='thing', start=(1, 4), end=(1, 9), line='def thing():\n')
TokenInfo(type=55 (OP), string='(', start=(1, 9), end=(1, 10), line='def thing():\n')
TokenInfo(type=55 (OP), string=')', start=(1, 10), end=(1, 11), line='def thing():\n')
TokenInfo(type=55 (OP), string=':', start=(1, 11), end=(1, 12), line='def thing():\n')
TokenInfo(type=4 (NEWLINE), string='\n', start=(1, 12), end=(1, 13), line='def thing():\n')
TokenInfo(type=5 (INDENT), string='    ', start=(2, 0), end=(2, 4), line='    """Autorzy, którzy tą jednostkę mają wpisani jako AKTUALNA -- czyli\n')
TokenInfo(type=3 (STRING), string='"""Autorzy, którzy tą jednostkę mają wpisani jako AKTUALNA -- czyli\n    aktualni pracownicy, obecni pracownicy"""', start=(2, 4), end=(3, 41), line='    """Autorzy, którzy tą jednostkę mają wpisani jako AKTUALNA -- czyli\n    aktualni pracownicy, obecni pracownicy"""\n')
TokenInfo(type=4 (NEWLINE), string='\n', start=(3, 45), end=(3, 46), line='    aktualni pracownicy, obecni pracownicy"""\n')
TokenInfo(type=55 (OP), string='...', start=(4, 4), end=(4, 7), line='    ...\n')
TokenInfo(type=4 (NEWLINE), string='\n', start=(4, 7), end=(4, 8), line='    ...\n')
TokenInfo(type=6 (DEDENT), string='', start=(5, 0), end=(5, 0), line='')
TokenInfo(type=0 (ENDMARKER), string='', start=(5, 0), end=(5, 0), line='')
Traceback (most recent call last):
  File "/private/tmp/1.py", line 15, in <module>
    assert tokens[7].end == (3, 45), tokens[7].end
AssertionError: (3, 41)

git bisect

Points to PR #104323 (gh-102856: Python tokenizer implementation for PEP 701).

What’s New In Python 3.12 » Changes in the Python API says:

Additionally, there may be some minor behavioral changes as a consequence of the changes required to support PEP 701. Some of these changes include:

This change isn't listed here, but is this an acceptable behavioural change or something to fix?

cc @lysnikolaou @pablogsal

More info

Originally reported at asottile/pyupgrade#923 by @mpasternak with the minimal reproducer created by @asottile.

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

macOS

Linked PRs

  • gh-112943: Correctly compute end offsets for multiline tokens in the tokenize module #112949
  • [3.12] gh-112943: Correctly compute end offsets for multiline tokens in the tokenize module (GH-112949) #112957

Activity

  1. hugovk
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    3.12only security fixesonly security fixes
    3.13only security fixesonly security fixes
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    topic-parser
    on Dec 10, 2023
  2. pablogsal
    added 3 commits that reference this issue on Dec 11, 2023

    pythongh-112943: Correctly compute end offsets for multiline tokens i…

    de5cc3c

    gh-112943: Correctly compute end offsets for multiline tokens in the …

    a135a6d

    [3.12] pythongh-112943: Correctly compute end offsets for multiline t…

    2fe4bf7
  3. pablogsal commented on Dec 11, 2023

    @pablogsal
    pablogsal
    on Dec 11, 2023
    Member
    More actions

    Thanks for the report @hugovk

    Reacted by Hugo van Kemenade
  4. pablogsal
    closed this as completedon Dec 11, 2023
  5. pablogsal
    added a commit that references this issue on Dec 11, 2023

    [3.12] gh-112943: Correctly compute end offsets for multiline tokens …

    e4d2fb2
  6. hugovk commented on Dec 11, 2023

    @hugovk
    hugovk
    on Dec 11, 2023
    MemberAuthor
    More actions

    Thanks for the quick fix!

    @mpasternak This will be in Python 3.12.2, due in February 2024, and 3.13.0a3, due next week (Tuesday, 2023-12-19).

  7. mpasternak commented on Dec 11, 2023

    @mpasternak
    mpasternak
    on Dec 11, 2023
    More actions

    Probably the first case a comment in my software ever helped anybody. Thanks!

    Reacted by Hugo van Kemenade, Jürg Rast and thomas-nokleby-arkivverket
  8. aisk
    added a commit that references this issue on Feb 11, 2024

    pythongh-112943: Correctly compute end offsets for multiline tokens i…

    79e2905
  9. Glyphack
    added a commit that references this issue on Sep 2, 2024

    pythongh-112943: Correctly compute end offsets for multiline tokens i…

    5bc545c
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

    3.12only security fixesonly security fixes3.13only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-parsertype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.