https://github.com/python/cpython/commit/4ce1246a9958d2d1452f83be961e369f7d050714
commit: 4ce1246a9958d2d1452f83be961e369f7d050714
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2024-06-19T13:22:21Z
summary:

[3.13] gh-120722: Set position on RETURN_VALUE in lambda (GH-120724) (#120738)


(cherry picked from commit d8f27cb1141fd3575de816438ed80a916c0560ed)

Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-06-18-22-41-05.gh-issue-120722.rS7tkE.rst
M Lib/test/test_compile.py
M Python/compile.c

diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index 6be6155f1db95a..e29c95e8bf263a 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -15,7 +15,7 @@
 import _testinternalcapi
 
 from test import support
-from test.support import (script_helper, requires_debug_ranges,
+from test.support import (script_helper, requires_debug_ranges, run_code,
                           requires_specialization, get_c_recursion_limit)
 from test.support.bytecode_helper import instructions_with_positions
 from test.support.os_helper import FakePath
@@ -2028,6 +2028,33 @@ def test_load_super_attr(self):
             code, "LOAD_GLOBAL", line=3, end_line=3, column=4, end_column=9
         )
 
+    def test_lambda_return_position(self):
+        snippets = [
+            "f = lambda: x",
+            "f = lambda: 42",
+            "f = lambda: 1 + 2",
+            "f = lambda: a + b",
+        ]
+        for snippet in snippets:
+            with self.subTest(snippet=snippet):
+                lamb = run_code(snippet)["f"]
+                positions = lamb.__code__.co_positions()
+                # assert that all positions are within the lambda
+                for i, pos in enumerate(positions):
+                    with self.subTest(i=i, pos=pos):
+                        start_line, end_line, start_col, end_col = pos
+                        if i == 0 and start_col == end_col == 0:
+                            # ignore the RESUME in the beginning
+                            continue
+                        self.assertEqual(start_line, 1)
+                        self.assertEqual(end_line, 1)
+                        code_start = snippet.find(":") + 2
+                        code_end = len(snippet)
+                        self.assertGreaterEqual(start_col, code_start)
+                        self.assertLessEqual(end_col, code_end)
+                        self.assertGreaterEqual(end_col, start_col)
+                        self.assertLessEqual(end_col, code_end)
+
 
 class TestExpectedAttributes(unittest.TestCase):
 
diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2024-06-18-22-41-05.gh-issue-120722.rS7tkE.rst b/Misc/NEWS.d/next/Core 
and Builtins/2024-06-18-22-41-05.gh-issue-120722.rS7tkE.rst
new file mode 100644
index 00000000000000..df83e69c601a32
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2024-06-18-22-41-05.gh-issue-120722.rS7tkE.rst 
@@ -0,0 +1,2 @@
+Correctly set the bytecode position on return instructions within lambdas.
+Patch by Jelle Zijlstra.
diff --git a/Python/compile.c b/Python/compile.c
index a24764c92fe5fb..78ed44e78d4a9d 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -3043,7 +3043,7 @@ compiler_lambda(struct compiler *c, expr_ty e)
         co = optimize_and_assemble(c, 0);
     }
     else {
-        location loc = LOCATION(e->lineno, e->lineno, 0, 0);
+        location loc = LOC(e->v.Lambda.body);
         ADDOP_IN_SCOPE(c, loc, RETURN_VALUE);
         co = optimize_and_assemble(c, 1);
     }

_______________________________________________
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]

Reply via email to