Repository navigation
Conversation
Raw f-strings (rf..., fr...) do not process backslash escape
sequences, so a backslash immediately before a '{' must not suppress
the opening of a replacement field, e.g. rfscript\temp\{c}.png is
valid syntax to CPython.
The f-string tokenizer regexes (fstring_string_single_line /
fstring_string_multi_line) unconditionally treated any backslash
followed by a non-newline/non-N character as a single escaped unit,
swallowing the following { as literal text. FStringNode was never
told whether the enclosing f-string was raw.
Track raw-ness on FStringNode (derived from the string prefix at the
point FSTRING_START is tokenized) and use dedicated regexes for raw
f-strings where a backslash is ordinary literal text (braces/{{ }}
still behave normally, and backslash-newline line continuation for
single-line raw f-strings is preserved). Non-raw f-string tokenization
is unchanged.
Fixes davidhalter#197
Owner
|
Hi, I'm sorry to say this, but I decided to not work at all with AI generated pull requests/content. There are multiple reasons for this:
Thank you anyway for trying to contribute to Open Source. I would really appreciate if you avoid the usage of LLMs in my projects. It is obviously fine to use LLMs as a search/brainstorming tool with my projects, just don't use it to interact with me. PS: As a side note I want to mention that I'm probably not the only maintainer that has a difficult relationship with LLM content. So you probably should ask first before you use LLMs for other repositories. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Added
rawfield to FStringNode, set from the FSTRING_START token's prefix ('r' in token.rstrip('"\'').lower()) when it is pushed onto fstring_stack. Added two new regexes, fstring_raw_string_single_line and fstring_raw_string_multi_line, used by find_fstring_string() instead of the normal fstring_string* regexes when tos.raw is True. These treat backslash as ordinary literal text (no\N{...}handling, no backslash+char escape pairing) while still: matching{{/}}doubled-brace literals correctly, stopping the match at a bare{/}so expressions still open normally, and preserving backslash-newline line continuation for single-line raw f-strings (verified against CPython's actual raw-string continuation behavior). Non-raw f-string regexes/behavior are untouched.Problem
davidhalter/parso issue reference: #197
Root Cause
Confirmed the discovery hypothesis. In parso/python/tokenize.py, fstring_string_single_line/fstring_string_multi_line (used by _find_fstring_string()) unconditionally matched a backslash followed by any non-newline/non-N character (
\\[^\r\nN]/\\[^N]) as a single escaped literal unit, so\{was swallowed as plain text instead of opening a replacement field. FStringNode (constructed at the FSTRING_START token) never tracked whether the f-string's prefix contained 'r'/'R', so this non-raw escaping behavior was wrongly applied to raw f-strings too, where backslashes have no escaping effect.Testing
PASS - all 78 test_fstring.py cases pass (including 8 new raw-f-string cases covering the reporter's exact case, fr/rf/Rf/fR prefix orderings, escaped backslash, doubled braces, triple-quoted raw f-strings with line continuation, and an invalid raw single-'}' case), and the full suite passes at 1994 passed, 0 failed.
Related Issue
#197