Skip to content

Commit 31acc4d

Browse files
authored
gh-123165: correct tests for dis.dis(func, show_positions=True) (#123220)
1 parent a3d8c05 commit 31acc4d

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

Lib/test/test_dis.py

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Minimal tests for dis module
22

3+
import ast
34
import contextlib
45
import dis
56
import functools
@@ -976,44 +977,47 @@ def format_instr_positions(instr):
976977

977978
@requires_debug_ranges()
978979
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]
981984

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-
))
1011985
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',
10131001
'',
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',
10151017
'',
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)',
10171021
'',
10181022
])
10191023
self.do_disassembly_test(f, expect, show_positions=True)

0 commit comments

Comments
 (0)