Skip to content

Commit 142dc97

Browse files
gh-154711: Add tests for comments after escaped quotes in f-strings and t-strings
Main is not affected by this bug after gh-153569, but nothing tested it. These tests cover a real comment placed after a string that ends with an escaped quote, in debug f-strings and in t-string interpolations.
1 parent ee1bbf0 commit 142dc97

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎Lib/test/test_fstring.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,11 @@ def __repr__(self):
17231723

17241724
self.assertEqual(f'{" # nooo "=}', '" # nooo "=\' # nooo \'')
17251725
self.assertEqual(f'{" \" # nooo \" "=}', '" \\" # nooo \\" "=\' " # nooo " \'')
1726+
1727+
# A real comment after a string that ends with an escaped quote must
1728+
# still be stripped (gh-154711).
1729+
self.assertEqual(f"{'\'' = # comment
1730+
}", "'\\'' = \n" + repr("'"))
17261731
self.assertEqual(f'{"""a" # inside"""=}',
17271732
'"""a" # inside"""=\'a" # inside\'')
17281733
self.assertEqual(f"{'''a' # inside'''=}",

‎Lib/test/test_tstring.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,18 @@ def test_interpolation_expression_with_reconstructed_metadata(self):
257257
self.assertEqual(regular.interpolations[0].expression, expected)
258258
self.assertEqual(debug.interpolations[0].expression, expected)
259259

260+
def test_comment_after_escaped_quote(self):
261+
# A comment after a string that ends with an escaped quote must be
262+
# stripped from the interpolation expression (gh-154711).
263+
t = t"{'a\'b' # comment
264+
}"
265+
self.assertEqual(t.interpolations[0].expression, "'a\\'b' \n")
266+
267+
t = t"{'a\'b' = # comment
268+
}"
269+
self.assertEqual(t.interpolations[0].expression, "'a\\'b' ")
270+
self.assertEqual(t.strings, ("'a\\'b' = \n", ""))
271+
260272
def test_raw_tstrings(self):
261273
path = r"C:\Users"
262274
t = rt"{path}\Documents"

0 commit comments

Comments
 (0)