Skip to content
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

Fix incorrect f-string tokenization #4332

Merged
merged 8 commits into from
Apr 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify a bit
JelleZijlstra committed Apr 25, 2024
commit 9f939af9400dc86a1d7a0b985520158f3483e297
19 changes: 7 additions & 12 deletions src/blib2to3/pgen2/tokenize.py
Original file line number Diff line number Diff line change
@@ -480,25 +480,20 @@ def _split_fstring_start_and_middle(token: str) -> Tuple[str, str]:
raise ValueError(f"Token {token!r} is not a valid f-string start")


STATE_NOT_FSTRING: Final = -1
STATE_MIDDLE: Final = 0
STATE_IN_BRACES: Final = 1
STATE_IN_COLON: Final = 2
STATE_NOT_FSTRING: Final = 0
STATE_MIDDLE: Final = 1
STATE_IN_BRACES: Final = 2
STATE_IN_COLON: Final = 3


class FStringState:
def __init__(self) -> None:
self.stack: List[int] = []

def is_in_fstring(self) -> bool:
return bool(self.stack)
self.stack: List[int] = [STATE_NOT_FSTRING]

def is_in_fstring_expression(self) -> bool:
return bool(self.stack) and self.stack[-1] != STATE_MIDDLE
return self.stack[-1] not in (STATE_MIDDLE, STATE_NOT_FSTRING)

def current(self) -> int:
if not self.stack:
return STATE_NOT_FSTRING
return self.stack[-1]

def enter_fstring(self) -> None:
@@ -590,7 +585,7 @@ def generate_tokens(
spos = strstart
epos = (lnum, end)
tokenline = contline + line
if not fstring_state.is_in_fstring() and not is_fstring_start(token):
if fstring_state.current() == STATE_NOT_FSTRING and not is_fstring_start(token):
yield (STRING, token, spos, epos, tokenline)
endprog_stack.pop()
parenlev = parenlev_stack.pop()