| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,7 @@ | |||
| 15 | 15 | import _testinternalcapi | |
| 16 | 16 | ||
| 17 | 17 | 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, | ||
| 19 | 19 | requires_specialization, get_c_recursion_limit) | |
| 20 | 20 | from test.support.bytecode_helper import instructions_with_positions | |
| 21 | 21 | from test.support.os_helper import FakePath | |
@@ -2028,6 +2028,33 @@ def test_load_super_attr(self): | |||
| 2028 | 2028 | code, "LOAD_GLOBAL", line=3, end_line=3, column=4, end_column=9 | |
| 2029 | 2029 | ) | |
| 2030 | 2030 | ||
| 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 | + | ||
| 2031 | 2058 | ||
| 2032 | 2059 | class TestExpectedAttributes(unittest.TestCase): | |
| 2033 | 2060 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Correctly set the bytecode position on return instructions within lambdas. | ||
| 2 | + Patch by Jelle Zijlstra. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2968,7 +2968,7 @@ compiler_lambda(struct compiler *c, expr_ty e) | |||
| 2968 | 2968 | co = optimize_and_assemble(c, 0); | |
| 2969 | 2969 | } | |
| 2970 | 2970 | else { | |
| 2971 | - location loc = LOCATION(e->lineno, e->lineno, 0, 0); | ||
| 2971 | + location loc = LOC(e->v.Lambda.body); | ||
| 2972 | 2972 | ADDOP_IN_SCOPE(c, loc, RETURN_VALUE); | |
| 2973 | 2973 | co = optimize_and_assemble(c, 1); | |
| 2974 | 2974 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments