Title: Escaped quotes cause source comments to leak into debug f-string output and t-string metadata · Issue #154711 · python/cpython · GitHub
Open Graph Title: Escaped quotes cause source comments to leak into debug f-string output and t-string metadata · Issue #154711 · python/cpython
X Title: Escaped quotes cause source comments to leak into debug f-string output and t-string metadata · Issue #154711 · python/cpython
Description: Bug report Bug description: When an f-string or t-string replacement expression contains a backslash-escaped quote, a following source comment can be copied into runtime output or t-string metadata. Observed effects: A debug f-string inc...
Open Graph Description: Bug report Bug description: When an f-string or t-string replacement expression contains a backslash-escaped quote, a following source comment can be copied into runtime output or t-string metadata...
X Description: Bug report Bug description: When an f-string or t-string replacement expression contains a backslash-escaped quote, a following source comment can be copied into runtime output or t-string metadata...
Opengraph URL: https://github.com/python/cpython/issues/154711
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Escaped quotes cause source comments to leak into debug f-string output and t-string metadata","articleBody":"# Bug report\n\n### Bug description:\n\nWhen an f-string or t-string replacement expression contains a backslash-escaped quote, a following source comment can be copied into runtime output or t-string metadata.\n\nObserved effects:\n\n- A debug f-string includes the source comment in the resulting str.\n- A debug t-string includes the comment in Template.strings.\n- A debug t-string includes both the debug = and the comment inInterpolation.expression.\n- A non-debug t-string also retains the comment inInterpolation.expression.\n\nThe replacement expression itself evaluates to the correct value. The problemappears to be in reconstruction of the replacement-field source text.\n\n## Reproducer\n```python\nf_result = f\"{'\\'' = # c\n}\"\n\nt_debug = t\"{'a\\'b' = # c\n}\"\nt_plain = t\"{'a\\'b' # c\n}\"\n\nprint(\"debug f-string result:\", repr(f_result))\nprint(\"\\ndebug t-string strings:\", repr(t_debug.strings))\nprint(\"\\ndebug t-string expression:\", repr(t_debug.interpolations[0].expression))\nprint(\"\\nnon-debug t-string expression:\", repr(t_plain.interpolations[0].expression))\n```\n\n## Actual behavior\n\nOn CPython main commit b86a41c, the output contains the source text # c in all four observed values.\n```text\ndebug f-string result: '\\'\\\\\\'\\' = # c\\n\"\\'\"'\n\ndebug t-string strings: (\"'a\\\\'b' = # c\\n\", '')\n\ndebug t-string expression: \"'a\\\\'b' = # c\"\n\nnon-debug t-string expression: \"'a\\\\'b' # c\"\n```\n\n## Possible cause\n\nThe behavior appears to be caused by inconsistent escaped-character handling between two scans in `_PyLexer_set_ftstring_expr() ` in `Parser/lexer/string.c`.\n\nThe initial scan that detects a comment outside a string literal explicitly skips a backslash and the character following it:\n```c\nif (ch == '\\\\') {\n i++;\n continue;\n}\n```\nHowever, after a comment has been detected, the subsequent reconstruction pass that removes comments appears not to perform the equivalent skip. It changes `in_string` whenever it encounters a quote:\n```c\nif (ch == '\"' || ch == '\\'') {\n if (!in_string) {\n in_string = 1;\n quote_char = ch;\n }\n else if (ch == quote_char) {\n in_string = 0;\n }\n}\nelse if (ch == '#' && !in_string) {\n /* skip the comment */\n}\n```\nFor:\n```python\n'a\\'b' = # c\n```\nthe second pass may interpret the escaped quote as the end of the string and the real closing quote as the beginning of another string:\n```text\n' begin string\n\\ treated as an ordinary character\n' incorrectly treated as end of string\nb\n' incorrectly treated as beginning of another string\n# incorrectly considered to be inside a string\n```\nConsequently, the # is not recognized by the comment-removal branch and the comment is copied into the reconstructed text.\nThis also explains why the same input pattern affects both f-strings and t-strings: both use the same replacement-expression reconstruction logic.\nThis is a proposed root cause based on the observed behavior and source review, not a confirmed diagnosis.\n\n\n### CPython versions tested on:\n\nCPython main branch, 3.16\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-154914\n* gh-158220\n* gh-158310\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/kid-lxy","@type":"Person","name":"kid-lxy"},"datePublished":"2026-07-26T07:01:48Z","url":"https://github.com/python/cpython/issues/154711"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:01f1f52c-fb19-a780-f354-1572968aec36 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8C88:CE438:125C2BD:18824E2:6B01FE9D |
| html-safe-nonce | 3f4f44cef20f7e2101c40eede0fbe8709eb17be36ca668637a823a046c55c2d3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Qzg4OkNFNDM4OjEyNUMyQkQ6MTg4MjRFMjo2QjAxRkU5RCIsInZpc2l0b3JfaWQiOiI2ODY0ODUzODYyNTk2MDgzMzU3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 58d99edfad01e9af70be393ddb0e161bd2e322a069000b00bad81e2a7591d262 |
| hovercard-subject-tag | issue:4979512543 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/154711/issue_layout |
| twitter:image | https://opengraph.githubassets.com/13112e19c1c953ebb9f2a6e9be3a574f20afad6457bbd1854804da32ec092c19/python/cpython/issues/154711 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/13112e19c1c953ebb9f2a6e9be3a574f20afad6457bbd1854804da32ec092c19/python/cpython/issues/154711 |
| og:image:alt | Bug report Bug description: When an f-string or t-string replacement expression contains a backslash-escaped quote, a following source comment can be copied into runtime output or t-string metadata... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kid-lxy |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width