|
10 | 10 | import textwrap |
11 | 11 | import warnings |
12 | 12 | from test import support |
13 | | -from test.support import (script_helper, requires_debug_ranges, |
| 13 | +from test.support import (script_helper, requires_debug_ranges, run_code, |
14 | 14 | requires_specialization, C_RECURSION_LIMIT) |
15 | 15 | from test.support.os_helper import FakePath |
16 | 16 |
|
@@ -1829,6 +1829,33 @@ def test_load_super_attr(self): |
1829 | 1829 | code, "LOAD_GLOBAL", line=3, end_line=3, column=4, end_column=9 |
1830 | 1830 | ) |
1831 | 1831 |
|
| 1832 | + def test_lambda_return_position(self): |
| 1833 | + snippets = [ |
| 1834 | + "f = lambda: x", |
| 1835 | + "f = lambda: 42", |
| 1836 | + "f = lambda: 1 + 2", |
| 1837 | + "f = lambda: a + b", |
| 1838 | + ] |
| 1839 | + for snippet in snippets: |
| 1840 | + with self.subTest(snippet=snippet): |
| 1841 | + lamb = run_code(snippet)["f"] |
| 1842 | + positions = lamb.__code__.co_positions() |
| 1843 | + # assert that all positions are within the lambda |
| 1844 | + for i, pos in enumerate(positions): |
| 1845 | + with self.subTest(i=i, pos=pos): |
| 1846 | + start_line, end_line, start_col, end_col = pos |
| 1847 | + if i == 0 and start_col == end_col == 0: |
| 1848 | + # ignore the RESUME in the beginning |
| 1849 | + continue |
| 1850 | + self.assertEqual(start_line, 1) |
| 1851 | + self.assertEqual(end_line, 1) |
| 1852 | + code_start = snippet.find(":") + 2 |
| 1853 | + code_end = len(snippet) |
| 1854 | + self.assertGreaterEqual(start_col, code_start) |
| 1855 | + self.assertLessEqual(end_col, code_end) |
| 1856 | + self.assertGreaterEqual(end_col, start_col) |
| 1857 | + self.assertLessEqual(end_col, code_end) |
| 1858 | + |
1832 | 1859 |
|
1833 | 1860 | class TestExpressionStackSize(unittest.TestCase): |
1834 | 1861 | # These tests check that the computed stack size for a code object |
|
0 commit comments