Skip to content

gh-146448: Add regression test for != followed by a format spec in t-strings - #146614

Open
joshuaswanson wants to merge 2 commits into
python:mainfrom
joshuaswanson:fix/tstring-exclamation-parser
Open

joshuaswanson wants to merge 2 commits into
python:mainfrom
joshuaswanson:fix/tstring-exclamation-parser

Conversation

@joshuaswanson

@joshuaswanson joshuaswanson commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

t'{0!=0:}' used to produce an interpolation whose expression was 0, because the lexer treated the ! of != as the start of a conversion specifier.

The lexer refactor on main already handles this case. _PyLexer_ftstring_punctuation peeks at the character after ! and leaves != alone.

This PR originally added the same lookahead in Parser/lexer/lexer.c. After merging main that change is redundant, so the PR now only adds a regression test in Lib/test/test_tstring.py for t"{0!=0:}" and t"{0!=0:s}".

The branch still contains the NEWS entry from the original fix. I can remove it if a test-only change should skip news.

@Shrey-N

Shrey-N commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor

Hiya @joshuaswanson, Great fix! just a small detail,

Shouldn't we peek at the buffer directly here?
I believe this avoids the overhead and potential lineno desync of tok_nextc/tok_backup if a buffer refill happens between ! and =:

if (c == '!' && (tok->cur >= tok->inp || *tok->cur != '=')) {
    is_punctuation = 1;
}

@github-actions

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 9, 2026
@joshuaswanson joshuaswanson changed the title gh-146448: fix t-string parser truncating expression at != with format spec gh-146448: Add regression test for != followed by a format spec in t-strings Oct 9, 2026
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants