]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-102856: Allow comments inside multi-line f-string expresions (#104006)
authorCristián Maureira-Fredes <cmaureir@users.noreply.github.com>
Mon, 22 May 2023 10:30:07 +0000 (12:30 +0200)
committerGitHub <noreply@github.com>
Mon, 22 May 2023 10:30:07 +0000 (10:30 +0000)
Lib/test/test_fstring.py
Parser/tokenizer.c

index fcb12d25ff9dd6728afadb7beb976dba3f4d233c..3ba2f943e6e9689147a659efbb6bbbe7c0c27766 100644 (file)
@@ -661,15 +661,50 @@ x = (
         self.assertEqual(f'{"#"}', '#')
         self.assertEqual(f'{d["#"]}', 'hash')
 
-        self.assertAllRaise(SyntaxError, "f-string expression part cannot include '#'",
-                            ["f'{1#}'",   # error because the expression becomes "(1#)"
-                             "f'{3(#)}'",
+        self.assertAllRaise(SyntaxError, "'{' was never closed",
+                            ["f'{1#}'",   # error because everything after '#' is a comment
                              "f'{#}'",
+                             "f'one: {1#}'",
+                             "f'{1# one} {2 this is a comment still#}'",
                              ])
         self.assertAllRaise(SyntaxError, r"f-string: unmatched '\)'",
                             ["f'{)#}'",   # When wrapped in parens, this becomes
                                           #  '()#)'.  Make sure that doesn't compile.
                              ])
+        self.assertEqual(f'''A complex trick: {
+2  # two
+}''', 'A complex trick: 2')
+        self.assertEqual(f'''
+{
+40 # fourty
++  # plus
+2  # two
+}''', '\n42')
+        self.assertEqual(f'''
+{
+40 # fourty
++  # plus
+2  # two
+}''', '\n42')
+
+        self.assertEqual(f'''
+# this is not a comment
+{ # the following operation it's
+3 # this is a number
+* 2}''', '\n# this is not a comment\n6')
+        self.assertEqual(f'''
+{# f'a {comment}'
+86 # constant
+# nothing more
+}''', '\n86')
+
+        self.assertAllRaise(SyntaxError, r"f-string: valid expression required before '}'",
+                            ["""f'''
+{
+# only a comment
+}'''
+""", # this is equivalent to f'{}'
+                             ])
 
     def test_many_expressions(self):
         # Create a string with many expressions in it. Note that
index fc4afccbfc40083d40cc5bdc4d43bf16fcbf58f7..472d417472635481199d7cb5d2d2a73009765aec 100644 (file)
@@ -1818,10 +1818,6 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
     /* Skip comment, unless it's a type comment */
     if (c == '#') {
 
-        if (INSIDE_FSTRING(tok)) {
-            return MAKE_TOKEN(syntaxerror(tok, "f-string expression part cannot include '#'"));
-        }
-
         const char* p = NULL;
         const char *prefix, *type_start;
         int current_starting_col_offset;