|
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 |
|
|
0 commit comments