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