|
1 | 1 | # Minimal tests for dis module
|
2 | 2 |
|
| 3 | +import ast |
3 | 4 | import contextlib
|
4 | 5 | import dis
|
5 | 6 | import functools
|
@@ -976,44 +977,47 @@ def format_instr_positions(instr):
|
976 | 977 |
|
977 | 978 | @requires_debug_ranges()
|
978 | 979 | def test_dis_with_some_positions(self):
|
979 |
| - def f(): |
980 |
| - pass |
| 980 | + code = ("def f():\n" |
| 981 | + " try: pass\n" |
| 982 | + " finally:pass") |
| 983 | + f = compile(ast.parse(code), "?", "exec").co_consts[0] |
981 | 984 |
|
982 |
| - PY_CODE_LOCATION_INFO_NO_COLUMNS = 13 |
983 |
| - PY_CODE_LOCATION_INFO_WITH_COLUMNS = 14 |
984 |
| - PY_CODE_LOCATION_INFO_NO_LOCATION = 15 |
985 |
| - |
986 |
| - f.__code__ = f.__code__.replace( |
987 |
| - co_stacksize=1, |
988 |
| - co_firstlineno=42, |
989 |
| - co_code=bytes([ |
990 |
| - dis.opmap["RESUME"], 0, |
991 |
| - dis.opmap["NOP"], 0, |
992 |
| - dis.opmap["RETURN_CONST"], 0, |
993 |
| - ]), |
994 |
| - co_linetable=bytes([ |
995 |
| - (1 << 7) |
996 |
| - | (PY_CODE_LOCATION_INFO_NO_COLUMNS << 3) |
997 |
| - | (1 - 1), # 1 code unit (RESUME) |
998 |
| - (1 << 1), # start line offset is 0 (encoded as an svarint) |
999 |
| - (1 << 7) |
1000 |
| - | (PY_CODE_LOCATION_INFO_NO_LOCATION << 3) |
1001 |
| - | (1 - 1), # 1 code unit (NOP) |
1002 |
| - (1 << 7) |
1003 |
| - | (PY_CODE_LOCATION_INFO_WITH_COLUMNS << 3) |
1004 |
| - | (1 - 1), # 1 code unit (RETURN CONST) |
1005 |
| - (2 << 1), # start line offset is 0 (encoded as an svarint) |
1006 |
| - 3, # end line offset is 0 (varint encoded) |
1007 |
| - 1, # 1-based start column (reported as COL - 1) |
1008 |
| - 5, # 1-based end column (reported as ENDCOL - 1) |
1009 |
| - ] |
1010 |
| - )) |
1011 | 985 | expect = '\n'.join([
|
1012 |
| - '43:?-43:? RESUME 0', |
| 986 | + '1:0-1:0 RESUME 0', |
| 987 | + '', |
| 988 | + '2:3-3:15 NOP', |
| 989 | + '', |
| 990 | + '3:11-3:15 RETURN_CONST 0 (None)', |
| 991 | + '', |
| 992 | + ' -- L1: PUSH_EXC_INFO', |
| 993 | + '', |
| 994 | + '3:11-3:15 RERAISE 0', |
| 995 | + '', |
| 996 | + ' -- L2: COPY 3', |
| 997 | + ' -- POP_EXCEPT', |
| 998 | + ' -- RERAISE 1', |
| 999 | + 'ExceptionTable:', |
| 1000 | + ' L1 to L2 -> L2 [1] lasti', |
1013 | 1001 | '',
|
1014 |
| - ' -- NOP', |
| 1002 | + ]) |
| 1003 | + self.do_disassembly_test(f, expect, show_positions=True) |
| 1004 | + |
| 1005 | + @requires_debug_ranges() |
| 1006 | + def test_dis_with_linenos_but_no_columns(self): |
| 1007 | + code = "def f():\n\tx = 1" |
| 1008 | + tree = ast.parse(code) |
| 1009 | + func = tree.body[0] |
| 1010 | + ass_x = func.body[0].targets[0] |
| 1011 | + # remove columns information but keep line information |
| 1012 | + ass_x.col_offset = ass_x.end_col_offset = -1 |
| 1013 | + f = compile(tree, "?", "exec").co_consts[0] |
| 1014 | + |
| 1015 | + expect = '\n'.join([ |
| 1016 | + '1:0-1:0 RESUME 0', |
1015 | 1017 | '',
|
1016 |
| - '45:0-48:4 RETURN_CONST 0 (None)', |
| 1018 | + '2:5-2:6 LOAD_CONST 1 (1)', |
| 1019 | + '2:?-2:? STORE_FAST 0 (x)', |
| 1020 | + '2:?-2:? RETURN_CONST 0 (None)', |
1017 | 1021 | '',
|
1018 | 1022 | ])
|
1019 | 1023 | self.do_disassembly_test(f, expect, show_positions=True)
|
|
0 commit comments