Skip to content

untokenize() round-trip fails for t-strings (with only type + string) #134582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
loic-simon opened this issue May 23, 2025 · 0 comments
Open

untokenize() round-trip fails for t-strings (with only type + string) #134582

loic-simon opened this issue May 23, 2025 · 0 comments
Labels
3.14 bugs and security fixes 3.15 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@loic-simon
Copy link
Contributor

loic-simon commented May 23, 2025

Bug report

Bug description:

Found when investigating test.test_tokenize.TestRoundtrip.test_random_files failing on #134577

def test(code):
    tokens = list(tokenize.tokenize(iter([code]).__next__))
    from5 = tokenize.untokenize(tokens)
    print("from5   ", from5, eval(from5))

    tokens2 = [tok[:2] for tok in tokens]
    from2 = tokenize.untokenize(tokens2)
    print("from2   ", from2, eval(from2))
>>> test(b't"{ {} }"')
from5    b't"{ {} }"' Template(strings=('', ''), interpolations=(Interpolation({}, ' {}', None, ''),))
from2    b't"{{}}"'   Template(strings=('{}',), interpolations=())

>>> test(b'f"{ {} }"')
from5    b'f"{ {} }"' {}
from2    b'f"{ {} }"' {}

From what I understand, untokenize should round-trip correctly even with only the type and string of tokens.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

@loic-simon loic-simon added the type-bug An unexpected behavior, bug, or error label May 23, 2025
@picnixz picnixz added interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.14 bugs and security fixes 3.15 new features, bugs and security fixes labels May 23, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 25, 2025
…between braces (pythonGH-134603)

(cherry picked from commit 52509cc94b1a18cb325dbfa7e5f830b32759a903)

Co-authored-by: Loïc Simon <loic.pano@gmail.com>
pablogsal pushed a commit that referenced this issue May 25, 2025
… between braces (GH-134603) (#134659)

gh-134582: Fix t-strings untokenize() roundtrip removing space between braces (GH-134603)
(cherry picked from commit 52509cc)

Co-authored-by: Loïc Simon <loic.pano@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.14 bugs and security fixes 3.15 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants