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

Expression before = in an f-string is interpreted like a normal string with escape sequences #124363

Closed
dscorbett opened this issue Sep 23, 2024 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-parser type-bug An unexpected behavior, bug, or error

Comments

@dscorbett
Copy link

dscorbett commented Sep 23, 2024

Bug report

Bug description:

When an f-string contains an = specifier, the characters before the equals sign are interpreted as if they were within a normal string. In particular, a backslash is always interpreted as starting an escape sequence, even when it doesn’t, which causes unexpected output.

>>> print(f'{br'\N{OX}'=}')
br'🐂'=b'\\N{OX}'
>>> print(f'{b'\N{OX}'=}')
<unknown>:1: SyntaxWarning: invalid escape sequence '\N'
b'🐂'=b'\\N{OX}'
>>> print(f'{r'\xff'=}')
r'ÿ'='\\xff'
>>> print(f'{r'\n'=}')
r'
'='\\n'
>>> print(f'{'\''=}')
'''="'"

Those results are misleading because the expressions printed before the equals signs are either syntax errors or do not match the values printed after the equals signs. I expected these results:

>>> print(f'{br'\N{OX}'=}')
br'\N{OX}'=b'\\N{OX}'
>>> print(f'{b'\N{OX}'=}')
<unknown>:1: SyntaxWarning: invalid escape sequence '\N'
b'\N{OX}'=b'\\N{OX}'
>>> print(f'{r'\xff'=}')
r'\xff'='\\xff'
>>> print(f'{r'\n'=}')
r'\n'='\\n'
>>> print(f'{'\''=}')
'\''="'"

Even when the result is not so misleading, it would still be more helpful for debugging if the output kept the expression unmodified, instead of interpreting the escape sequences. For example, this:

>>> print(f'{'\xc5'=}')
'Å'='Å'

would be better as this:

>>> print(f'{'\xc5'=}')
'\xc5'='Å'

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

macOS

Linked PRs

@dscorbett dscorbett added the type-bug An unexpected behavior, bug, or error label Sep 23, 2024
@sobolevn
Copy link
Member

cc @pablogsal @lysnikolaou

@pablogsal
Copy link
Member

Hummmm, I am not sure this is a bug as this wasn't specified at the moment we started to allow escape sequences so we should first decide if this makes sense. CC: @lysnikolaou @ericvsmith .

This is possible but unfortunately a bit annoying to implement because the current shape of the AST doesn't properly pass down what is debug and what is not. I think I can try to move stuff around to eliminate this problem and make it work. I will create a draft for evaluation

@pablogsal
Copy link
Member

#128399

@picnixz picnixz added interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-parser labels Jan 1, 2025
pablogsal added a commit that referenced this issue Jan 22, 2025
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 22, 2025
…ythonGH-128399)

(cherry picked from commit 60a3a0d)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
pablogsal added a commit that referenced this issue Jan 22, 2025
…H-128399) (#129187)

gh-124363: Treat debug expressions in f-string as raw strings (GH-128399)

(cherry picked from commit 60a3a0d)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
pablogsal added a commit to pablogsal/cpython that referenced this issue Jan 22, 2025
…rings (pythonGH-128399)

(cherry picked from commit 60a3a0d)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
pablogsal added a commit that referenced this issue Jan 22, 2025
…H-128399) (#129190)

(cherry picked from commit 60a3a0d)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-parser type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants