Skip to content

tokenize crashes on a mismatched closing bracket in an f-string replacement field #160268

Closed

Description

@lysnikolaou

Bug report

The following snippets crashes with an assertion error on the debug build:

import io, tokenize
list(tokenize.generate_tokens(io.StringIO("f'{]a'").readline))

The ] closes the replacement field's { without an error, because tokenize tolerates mismatched brackets (gh-105042). The f-string state still expects that { on the bracket stack. Release builds don't crash; they read a stale stack entry instead.

The parser rejects the same input with SyntaxError: f-string: unmatched ']', and tokenize mode should do the same.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added a commit that references this issue on Oct 22, 2026
  2. added 2 commits that reference this issue on Oct 22, 2026
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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-parsertype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions