python / python/cpython

certain f/t-string expressions not copied correctly to debug str or interpolations

Open
#141,271 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.14 3.15 interpreter-core topic-parser type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

The function set_ftstring_expr() in Parser/lexer/lexer.c does not currently copy certain f/t-string expressions correctly. It has problems with non-coding quotes (escaped or in comments) and nested f/t-strings. This applies to both self-documenting debug expressions and more importantly to t-string interpolation expressions.

For example, the # comment below should not print:

>>> print(f'''{'\'' # comment
... =}''')
'\'' # comment
="'"

Copied expression terminates early:

>>> print(f'{(f'''
... # not comment A
... { # comment B '
... 3 # comment C
... * 2}''', '\n# not comment D\n6')=}')
(f'''
# not comment A
{ # comment B '
3
* 2}''', '\n('\n# not comment A\n6', '\n# not comment D\n6')

It does not appear the function was written to handle nested f/t-strings and the string quote handling does not take into account escaped quotes.

CPython versions tested on:

3.14, CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-141274

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Parser/lexer/lexer.c at set_ftstring_expr() and reproduce the escaped-quote, comment, and nested f/t-string examples from the report. Trace how expressions are copied for debug output and t-string interpolation, then verify that quoted text and comments do not terminate copying early and that the shown outputs are corrected.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.