Skip to content

Commit 4ce1246

Browse files
[3.13] gh-120722: Set position on RETURN_VALUE in lambda (GH-120724) (#120738)
(cherry picked from commit d8f27cb) Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent c598e61 commit 4ce1246

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

Lib/test/test_compile.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import _testinternalcapi
1616

1717
from test import support
18-
from test.support import (script_helper, requires_debug_ranges,
18+
from test.support import (script_helper, requires_debug_ranges, run_code,
1919
requires_specialization, get_c_recursion_limit)
2020
from test.support.bytecode_helper import instructions_with_positions
2121
from test.support.os_helper import FakePath
@@ -2028,6 +2028,33 @@ def test_load_super_attr(self):
20282028
code, "LOAD_GLOBAL", line=3, end_line=3, column=4, end_column=9
20292029
)
20302030

2031+
def test_lambda_return_position(self):
2032+
snippets = [
2033+
"f = lambda: x",
2034+
"f = lambda: 42",
2035+
"f = lambda: 1 + 2",
2036+
"f = lambda: a + b",
2037+
]
2038+
for snippet in snippets:
2039+
with self.subTest(snippet=snippet):
2040+
lamb = run_code(snippet)["f"]
2041+
positions = lamb.__code__.co_positions()
2042+
# assert that all positions are within the lambda
2043+
for i, pos in enumerate(positions):
2044+
with self.subTest(i=i, pos=pos):
2045+
start_line, end_line, start_col, end_col = pos
2046+
if i == 0 and start_col == end_col == 0:
2047+
# ignore the RESUME in the beginning
2048+
continue
2049+
self.assertEqual(start_line, 1)
2050+
self.assertEqual(end_line, 1)
2051+
code_start = snippet.find(":") + 2
2052+
code_end = len(snippet)
2053+
self.assertGreaterEqual(start_col, code_start)
2054+
self.assertLessEqual(end_col, code_end)
2055+
self.assertGreaterEqual(end_col, start_col)
2056+
self.assertLessEqual(end_col, code_end)
2057+
20312058

20322059
class TestExpectedAttributes(unittest.TestCase):
20332060

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Correctly set the bytecode position on return instructions within lambdas.
2+
Patch by Jelle Zijlstra.

Python/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3043,7 +3043,7 @@ compiler_lambda(struct compiler *c, expr_ty e)
30433043
co = optimize_and_assemble(c, 0);
30443044
}
30453045
else {
3046-
location loc = LOCATION(e->lineno, e->lineno, 0, 0);
3046+
location loc = LOC(e->v.Lambda.body);
30473047
ADDOP_IN_SCOPE(c, loc, RETURN_VALUE);
30483048
co = optimize_and_assemble(c, 1);
30493049
}

0 commit comments

Comments
 (0)