https://github.com/python/cpython/commit/4192eadc82226f55f0fd1e12ab27aeb5c51631fc
commit: 4192eadc82226f55f0fd1e12ab27aeb5c51631fc
branch: 3.13
author: Arnesh <[email protected]>
committer: pablogsal <[email protected]>
date: 2026-09-29T23:45:21+01:00
summary:
[3.13] gh-154711: Strip comments after escaped quotes in f-string debug text
(#158310)
When a replacement field ended with a string that had an escaped quote and
was followed by a comment, the comment leaked into the debug f-string output.
The comment detection loop in set_fstring_expr skips escaped characters, but
the comment stripping loop did not. So an escaped quote left the scanner
thinking it was still inside a string, and the comment was never removed.
This makes the stripping loop skip escaped characters the same way the
detection loop does. 3.14 and later already have this fix from gh-154719.
files:
A
Misc/NEWS.d/next/Core_and_Builtins/2026-07-30-14-40-00.gh-issue-154711.nUxYXc.rst
M Lib/test/test_fstring.py
M Parser/lexer/lexer.c
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index b47e906ce351c74..2acd49f3584513a 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -1664,6 +1664,11 @@ def __repr__(self):
self.assertEqual(f'{" # nooo "=}', '" # nooo "=\' # nooo \'')
self.assertEqual(f'{" \" # nooo \" "=}', '" \\" # nooo \\" "=\' " #
nooo " \'')
+ # A real comment after a string that ends with an escaped quote must
+ # still be stripped (gh-154711).
+ self.assertEqual(f"{'\'' = # comment
+}", "'\\'' = \n" + repr("'"))
+
self.assertEqual(f'{ # some comment goes here
"""hello"""=}', ' \n """hello"""=\'hello\'')
self.assertEqual(f'{"""# this is not a comment
diff --git
a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-30-14-40-00.gh-issue-154711.nUxYXc.rst
b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-30-14-40-00.gh-issue-154711.nUxYXc.rst
new file mode 100644
index 000000000000000..58ad001d3befb78
--- /dev/null
+++
b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-30-14-40-00.gh-issue-154711.nUxYXc.rst
@@ -0,0 +1,3 @@
+Fix a bug where a comment placed after a string that ends with an escaped
+quote could leak into the output of a debug f-string. Now the comment is
+stripped as expected.
diff --git a/Parser/lexer/lexer.c b/Parser/lexer/lexer.c
index 38d3742f6e73d0f..110f312498bdd57 100644
--- a/Parser/lexer/lexer.c
+++ b/Parser/lexer/lexer.c
@@ -162,6 +162,19 @@ set_fstring_expr(struct tok_state* tok, struct token
*token, char c) {
while (i < expression_size) {
char ch = expression[i];
+ // Copy escaped characters as-is. This keeps an escaped quote from
+ // flipping the in_string state, which would otherwise stop a real
+ // comment from being detected (see the detection loop above).
+ if (ch == '\\') {
+ result[j++] = ch;
+ i++;
+ if (i < expression_size) {
+ result[j++] = expression[i];
+ i++;
+ }
+ continue;
+ }
+
// Handle string quotes
if (ch == '"' || ch == '\'') {
// See comment above to understand this part
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]