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 — Consider emitting buffered DEDENT tokens on the last line · Issue #104976 · python/cpython

Consider emitting buffered DEDENT tokens on the last line #104976

New issue
Copy link
New issue
Copy link
Closed
Closed
Consider emitting buffered DEDENT tokens on the last line#104976
Copy link
Assignees
pablogsal

Description

@pablogsal
pablogsal
opened on May 26, 2023
Issue body actions

In Python 3.12, porting the tokenizer to use the C tokenizer underneath to support PEP 701 has now a documented change in docs.python.org/3.12/whatsnew/3.12.html#changes-in-the-python-api:

Some final DEDENT tokens are now emitted within the bounds of the input. This means that for a file containing 3 lines, the old version of the tokenizer returned a DEDENT token in line 4 whilst the new version returns the token in line 3.

Apparently, this affects negatively some formatting tools (see PyCQA/pycodestyle#1142). Let's consider what options do we have and see if we can fix this without adding a lot of maintenance burden to the C tokenizer or slowing down everything.

Linked PRs

  • gh-104976: Ensure trailing dedent tokens are emitted as the previous tokenizer #104980
  • [3.12] gh-104976: Ensure trailing dedent tokens are emitted as the previous tokenizer (GH-104980) #105000

Activity

  1. pablogsal commented on May 26, 2023

    @pablogsal
    pablogsal
    on May 26, 2023
    MemberAuthor
    More actions

    We can do something like this:

    diff --git a/Lib/tokenize.py b/Lib/tokenize.py
    index 911f0f12f9..63dc44b0dc 100644
    --- a/Lib/tokenize.py
    +++ b/Lib/tokenize.py
    @@ -452,7 +452,9 @@ def _tokenize(rl_gen, encoding):
             yield token
         if token is not None:
             last_line, _ = token.start
    -        yield TokenInfo(ENDMARKER, '', (last_line + 1, 0), (last_line + 1, 0), '')
    +        if token.type != DEDENT:
    +            last_line != 1
    +        yield TokenInfo(ENDMARKER, '', (last_line, 0), (last_line, 0), '')
    
    
     def generate_tokens(readline):
    diff --git a/Python/Python-tokenize.c b/Python/Python-tokenize.c
    index 88087c1256..2a09dfd94a 100644
    --- a/Python/Python-tokenize.c
    +++ b/Python/Python-tokenize.c
    @@ -214,6 +214,10 @@ tokenizeriter_next(tokenizeriterobject *it)
         }
    
         if (it->tok->tok_extra_tokens) {
    +        if (type == DEDENT && it->tok->done == E_EOF) {
    +            lineno = end_lineno = lineno + 1;
    +            col_offset = end_col_offset = 0;
    +        }
             // Necessary adjustments to match the original Python tokenize
             // implementation
             if (type > DEDENT && type < OP) {

    but I think this forces us to somehow handle the ENDMARKER internally. Maybe that's a possible solution but I fear this still has some side effects.

  2. pablogsal commented on May 26, 2023

    @pablogsal
    pablogsal
    on May 26, 2023
    MemberAuthor
    More actions

    @lysnikolaou thoughts?

  3. pablogsal
    added a commit that references this issue on May 26, 2023

    pythongh-104976: Ensure trailing dedent tokens are emitted as the pre…

    3bdac97
  4. pablogsal commented on May 26, 2023

    @pablogsal
    pablogsal
    on May 26, 2023
    MemberAuthor
    More actions

    CC: @mgmacias95

  5. pablogsal
    self-assigned this
    on May 26, 2023
  6. pablogsal commented on May 26, 2023

    @pablogsal
    pablogsal
    on May 26, 2023
    MemberAuthor
    More actions

    Opened #104980 to test this idea

  7. lysnikolaou commented on May 26, 2023

    @lysnikolaou
    lysnikolaou
    on May 26, 2023
    Member
    More actions

    Yeah, I think that, if we want to support doing the same thing as 3.11, the only way is to special-case it Python-tokenize.c and not in the C tokenizer itself.

  8. pablogsal
    added 3 commits that reference this issue on May 26, 2023

    pythongh-104976: Ensure trailing dedent tokens are emitted as the pre…

    709f577

    pythongh-104976: Ensure trailing dedent tokens are emitted as the pre…

    7c0113e

    pythongh-104976: Ensure trailing dedent tokens are emitted as the pre…

    6db032a
  9. pablogsal commented on May 26, 2023

    @pablogsal
    pablogsal
    on May 26, 2023
    MemberAuthor
    More actions

    Yeah, I think that, if we want to support doing the same thing as 3.11, the only way is to special-case it Python-tokenize.c and not in the C tokenizer itself.

    Ok, then check if you like #104980

  10. pablogsal
    added 4 commits that reference this issue on May 26, 2023

    pythongh-104976: Ensure trailing dedent tokens are emitted as the pre…

    8a23a18

    pythongh-104976: Ensure trailing dedent tokens are emitted as the pre…

    19a58c5

    pythongh-104976: Ensure trailing dedent tokens are emitted as the pre…

    a1a0a48

    pythongh-104976: Ensure trailing dedent tokens are emitted as the pre…

    54fc1d5
  11. miss-islington
    added a commit that references this issue on May 26, 2023

    pythongh-104976: Ensure trailing dedent tokens are emitted as the pre…

    0d9e7ec
  12. pablogsal
    added 2 commits that reference this issue on May 26, 2023

    gh-104976: Ensure trailing dedent tokens are emitted as the previous …

    46b52e6

    [3.12] gh-104976: Ensure trailing dedent tokens are emitted as the pr…

    2c02c68
  13. pablogsal
    closed this as completedon May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @pablogsal
    pablogsal

Labels

No labels
No labels

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.