]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-139516: Fix lambda colon start format spec in f-string in t… (#139726)
authorTomasz Pytel <tompytel@gmail.com>
Tue, 7 Oct 2025 22:15:57 +0000 (18:15 -0400)
committerGitHub <noreply@github.com>
Tue, 7 Oct 2025 22:15:57 +0000 (23:15 +0100)
[3.13] gh-139516: Fix lambda colon start format spec in f-string in tokenizer (GH-139657)
(cherry picked from commit 539461d9ec8e5322ead638f7be733fd196aa6c79)

Lib/test/test_fstring.py
Lib/test/test_tokenize.py
Misc/NEWS.d/next/Core and Builtins/2025-10-06-13-15-26.gh-issue-139516.d9Pkur.rst [new file with mode: 0644]
Parser/lexer/lexer.c
Parser/lexer/state.h

index 5e743d34d2a5732d643ce808aca2b71095c98022..d86977c55c3b90416492a810bb38f7e0e617f97a 100644 (file)
@@ -1849,6 +1849,13 @@ print(f'''{{
         # Test multiple format specs in same raw f-string
         self.assertEqual(rf"{UnchangedFormat():\xFF} {UnchangedFormat():\n}", '\\xFF \\n')
 
+    def test_gh139516(self):
+        with temp_cwd():
+            script = 'script.py'
+            with open(script, 'wb') as f:
+                f.write('''def f(a): pass\nf"{f(a=lambda: 'à'\n)}"'''.encode())
+            assert_python_ok(script)
+
 
 if __name__ == '__main__':
     unittest.main()
index e9a9ee4469d8771238cf38288eb6a5ce5c003c47..9aff15eb530845812fb6b7da8024068384c5b51b 100644 (file)
@@ -1212,6 +1212,23 @@ f'''
     FSTRING_END "\'\'\'"         (3, 1) (3, 4)
     """)
 
+        # gh-139516, the '\n' is explicit to ensure no trailing whitespace which would invalidate the test
+        self.check_tokenize('''f"{f(a=lambda: 'à'\n)}"''', """\
+    FSTRING_START \'f"\'          (1, 0) (1, 2)
+    OP         '{'           (1, 2) (1, 3)
+    NAME       'f'           (1, 3) (1, 4)
+    OP         '('           (1, 4) (1, 5)
+    NAME       'a'           (1, 5) (1, 6)
+    OP         '='           (1, 6) (1, 7)
+    NAME       'lambda'      (1, 7) (1, 13)
+    OP         ':'           (1, 13) (1, 14)
+    STRING     "\'à\'"         (1, 15) (1, 18)
+    NL         '\\n'          (1, 18) (1, 19)
+    OP         ')'           (2, 0) (2, 1)
+    OP         '}'           (2, 1) (2, 2)
+    FSTRING_END \'"\'           (2, 2) (2, 3)
+    """)
+
 class GenerateTokensTest(TokenizeTest):
     def check_tokenize(self, s, expected):
         # Format the tokens in s in a table format.
diff --git a/Misc/NEWS.d/next/Core and Builtins/2025-10-06-13-15-26.gh-issue-139516.d9Pkur.rst b/Misc/NEWS.d/next/Core and Builtins/2025-10-06-13-15-26.gh-issue-139516.d9Pkur.rst
new file mode 100644 (file)
index 0000000..a709112
--- /dev/null
@@ -0,0 +1 @@
+Fix lambda colon erroneously start format spec in f-string in tokenizer.
index 384239bd414c384546ad998ba9648aeac7cd6c2d..66a7cbb769aa4653796cba7d1a9e42c8b56520b7 100644 (file)
@@ -1291,7 +1291,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
         return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, "invalid non-printable character U+%04X", c));
     }
 
-    if( c == '=' && INSIDE_FSTRING_EXPR(current_tok)) {
+    if( c == '=' && INSIDE_FSTRING_EXPR_AT_TOP(current_tok)) {
         current_tok->f_string_debug = 1;
     }
 
index d8791d89dd02db9254df2ec9bcc84631308b635c..7df24d571d558e690aac0c32bac0cf5bef985fc1 100644 (file)
@@ -10,6 +10,8 @@
 
 #define INSIDE_FSTRING(tok) (tok->tok_mode_stack_index > 0)
 #define INSIDE_FSTRING_EXPR(tok) (tok->curly_bracket_expr_start_depth >= 0)
+#define INSIDE_FSTRING_EXPR_AT_TOP(tok) \
+    (tok->curly_bracket_depth - tok->curly_bracket_expr_start_depth == 1)
 
 enum decoding_state {
     STATE_INIT,