FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-123165: correct tests for `dis.dis(func, show_positions=True)` by picnixz · Pull Request #123220 · python/cpython · GitHub

/ cpython Public
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
72 changes: 38 additions & 34 deletions Lib/test/test_dis.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Minimal tests for dis module

import ast
import contextlib
import dis
import functools
Expand Down Expand Up @@ -976,44 +977,47 @@ def format_instr_positions(instr):

@requires_debug_ranges()
def test_dis_with_some_positions(self):
def f():
pass
code = ("def f():\n"
" try: pass\n"
" finally:pass")
f = compile(ast.parse(code), "?", "exec").co_consts[0]

PY_CODE_LOCATION_INFO_NO_COLUMNS = 13
PY_CODE_LOCATION_INFO_WITH_COLUMNS = 14
PY_CODE_LOCATION_INFO_NO_LOCATION = 15

f.__code__ = f.__code__.replace(
co_stacksize=1,
co_firstlineno=42,
co_code=bytes([
dis.opmap["RESUME"], 0,
dis.opmap["NOP"], 0,
dis.opmap["RETURN_CONST"], 0,
]),
co_linetable=bytes([
(1 << 7)
| (PY_CODE_LOCATION_INFO_NO_COLUMNS << 3)
| (1 - 1), # 1 code unit (RESUME)
(1 << 1), # start line offset is 0 (encoded as an svarint)
(1 << 7)
| (PY_CODE_LOCATION_INFO_NO_LOCATION << 3)
| (1 - 1), # 1 code unit (NOP)
(1 << 7)
| (PY_CODE_LOCATION_INFO_WITH_COLUMNS << 3)
| (1 - 1), # 1 code unit (RETURN CONST)
(2 << 1), # start line offset is 0 (encoded as an svarint)
3, # end line offset is 0 (varint encoded)
1, # 1-based start column (reported as COL - 1)
5, # 1-based end column (reported as ENDCOL - 1)
]
))
expect = '\n'.join([
'43:?-43:? RESUME 0',
'1:0-1:0 RESUME 0',
'',
'2:3-3:15 NOP',
'',
'3:11-3:15 RETURN_CONST 0 (None)',
'',
' -- L1: PUSH_EXC_INFO',
'',
'3:11-3:15 RERAISE 0',
'',
' -- L2: COPY 3',
' -- POP_EXCEPT',
' -- RERAISE 1',
'ExceptionTable:',
' L1 to L2 -> L2 [1] lasti',
'',
' -- NOP',
])
self.do_disassembly_test(f, expect, show_positions=True)

@requires_debug_ranges()
def test_dis_with_linenos_but_no_columns(self):
code = "def f():\n\tx = 1"
tree = ast.parse(code)
func = tree.body[0]
ass_x = func.body[0].targets[0]
# remove columns information but keep line information
ass_x.col_offset = ass_x.end_col_offset = -1
f = compile(tree, "?", "exec").co_consts[0]

expect = '\n'.join([
'1:0-1:0 RESUME 0',
'',
'45:0-48:4 RETURN_CONST 0 (None)',
'2:5-2:6 LOAD_CONST 1 (1)',
'2:?-2:? STORE_FAST 0 (x)',
'2:?-2:? RETURN_CONST 0 (None)',
'',
])
self.do_disassembly_test(f, expect, show_positions=True)
Expand Down

Back | FazBrowse Home | New Git URL