Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Correctly set the bytecode position on return instructions within lambdas.
Patch by Jelle Zijlstra.
2 changes: 1 addition & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading