-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Crash on f-string with \{
#4421
Comments
>>> print(rf'{1}\{{[\}}')
1\{[\} small tweak to the example as it shows expected formatting output |
Minimal repro: |
Here's another example that doesn't yield a Black crash but that we parse incorrectly:
The
|
And another one:
|
Normal case: $ cat foo.py
f'foo \{{'
$ python -m blib2to3.pgen2.tokenize foo.py
1,0-1,2: FSTRING_START "f'"
1,2-1,9: FSTRING_MIDDLE 'foo \\{{'
1,9-1,10: FSTRING_END "'"
1,10-1,11: NEWLINE '\n'
2,0-2,0: ENDMARKER '' Abnormal case: $ cat foo.py
f'{1} \{{'
$ python -m blib2to3.pgen2.tokenize foo.py
1,0-1,2: FSTRING_START "f'"
1,2-1,2: FSTRING_MIDDLE ''
1,2-1,3: LBRACE '{'
1,3-1,4: NUMBER '1'
1,4-1,5: RBRACE '}'
1,5-1,8: FSTRING_MIDDLE ' \\{'
1,8-1,9: LBRACE '{'
1,9-1,10: ERRORTOKEN "'"
1,10-1,11: NL '\n'
Traceback (most recent call last):
[...] It deviates at parsing |
Yeah this is interesting too. $ python -m tokenize <<<"rf'\{2+3}'"
1,0-1,3: FSTRING_START "rf'"
1,3-1,4: FSTRING_MIDDLE '\\'
1,4-1,5: OP '{'
1,5-1,6: NUMBER '2'
1,6-1,7: OP '+'
1,7-1,8: NUMBER '3'
1,8-1,9: OP '}'
1,9-1,10: FSTRING_END "'"
1,10-1,11: NEWLINE '\n'
2,0-2,0: ENDMARKER ''
$ python -m blib2to3.pgen2.tokenize <<<"rf'\{2+3}'"
1,0-1,3: FSTRING_START "rf'"
1,3-1,9: FSTRING_MIDDLE '\\{2+3}'
1,9-1,10: FSTRING_END "'"
1,10-1,11: NEWLINE '\n'
2,0-2,0: ENDMARKER '' |
Since we don't format expressions inside f-strings right now, this wouldn't have been caught for a long time, but thanks to this bug we know of this regression :D |
You can also construct crashes from these bad parses, if the replacement field contains nested strings relying on PEP 701:
|
Black fails to format a valid Python f-string containing
\{
. I minimized this from a file in Pygments,pygments/lexers/int_fiction.py
.cc @tusharsadhwani for f-strings.
The text was updated successfully, but these errors were encountered: