本文整理汇总了Python中dis.get_instructions方法的典型用法代码示例。如果您正苦于以下问题:Python dis.get_instructions方法的具体用法?Python dis.get_instructions怎么用?Python dis.get_instructions使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dis
的用法示例。
在下文中一共展示了dis.get_instructions方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: accessed_attributes_of_local
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def accessed_attributes_of_local(f, local_name):
"""
Get a list of attributes of ``local_name`` accessed by ``f``.
The analysis performed by this function is conservative, meaning that
it's not guaranteed to find **all** attributes used.
"""
try:
instrs = dis.get_instructions(f)
except TypeError:
# Got a default wrapping an object that's not a python function. Be
# conservative and assume this is safe.
return set()
used = set()
# Find sequences of the form: LOAD_FAST(local_name), LOAD_ATTR(<name>).
# This will find all usages of the form ``local_name.<name>``.
#
# It will **NOT** find usages in which ``local_name`` is aliased to
# another name.
for first, second in sliding_window(instrs, 2):
if first.opname == "LOAD_FAST" and first.argval == local_name:
if second.opname in ("LOAD_ATTR", "LOAD_METHOD", "STORE_ATTR"):
used.add(second.argval)
return used
示例2: co_code_findloadednames
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def co_code_findloadednames(co):
"""Find in the code of a code object, all loaded names.
(by LOAD_NAME, LOAD_GLOBAL or LOAD_FAST) """
import dis
from opcode import HAVE_ARGUMENT, opmap
hasloadname = (opmap['LOAD_NAME'],
opmap['LOAD_GLOBAL'], opmap['LOAD_FAST'])
insns = dis.get_instructions(co)
len_co_names = len(co.co_names)
indexset = {}
for insn in insns:
if insn.opcode >= HAVE_ARGUMENT:
if insn.opcode in hasloadname:
indexset[insn.argval] = 1
if len(indexset) >= len_co_names:
break
for name in co.co_varnames:
try:
del indexset[name]
except KeyError:
pass
return indexset
示例3: test_elim_jump_after_return1
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def test_elim_jump_after_return1(self):
# Eliminate dead code: jumps immediately after returns can't be reached
def f(cond1, cond2):
if cond1: return 1
if cond2: return 2
while 1:
return 3
while 1:
if cond1: return 4
return 5
return 6
self.assertNotInBytecode(f, 'JUMP_FORWARD')
self.assertNotInBytecode(f, 'JUMP_ABSOLUTE')
returns = [instr for instr in dis.get_instructions(f)
if instr.opname == 'RETURN_VALUE']
self.assertEqual(len(returns), 6)
示例4: test_constant_folding
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def test_constant_folding(self):
# Issue #11244: aggressive constant folding.
exprs = [
'3 * -5',
'-3 * 5',
'2 * (3 * 4)',
'(2 * 3) * 4',
'(-1, 2, 3)',
'(1, -2, 3)',
'(1, 2, -3)',
'(1, 2, -3) * 6',
'lambda x: x in {(3 * -5) + (-1 - 6), (1, -2, 3) * 2, None}',
]
for e in exprs:
code = compile(e, '', 'single')
for instr in dis.get_instructions(code):
self.assertFalse(instr.opname.startswith('UNARY_'))
self.assertFalse(instr.opname.startswith('BINARY_'))
self.assertFalse(instr.opname.startswith('BUILD_'))
示例5: get_instructions
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def get_instructions(co):
code = co.co_code
n = len(code)
i = 0
extended_arg = 0
while i < n:
offset = i
c = code[i]
op = ord(c)
argval = None
i = i + 1
if op >= HAVE_ARGUMENT:
oparg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
extended_arg = 0
i = i + 2
if op == EXTENDED_ARG:
extended_arg = oparg * 65536
if op in hasconst:
argval = co.co_consts[oparg]
yield Instruction(offset, argval, opname[op])
示例6: _walk_global_ops
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def _walk_global_ops(code):
"""
Yield (opcode, argument number) tuples for all
global-referencing instructions in *code*.
"""
for instr in dis.get_instructions(code):
op = instr.opcode
if op in GLOBAL_OPS:
yield op, instr.arg
示例7: _template_has_attr
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def _template_has_attr(self, name):
def get_class_init(klass):
import testslide.mock_constructor # Avoid cyclic dependencies
if testslide.mock_constructor._is_mocked_class(klass):
# If klass is the mocked subclass, pull the original version of
# __init__ so we can introspect into its implementation (and
# not the __init__ wrapper at the mocked class).
mocked_class = klass
original_class = mocked_class.mro()[1]
return testslide.mock_constructor._get_original_init(
original_class, instance=None, owner=mocked_class
)
else:
return klass.__init__
def is_runtime_attr():
if self._template:
for klass in self._template.mro():
template_init = get_class_init(klass)
if not inspect.isfunction(template_init):
continue
for instruction in dis.get_instructions(template_init):
if (
instruction.opname == "STORE_ATTR"
and name == instruction.argval
):
return True
return False
return (
hasattr(self._template, name)
or name in self._runtime_attrs
or name in getattr(self._template, "__slots__", [])
or is_runtime_attr()
)
示例8: can_solve_cn
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def can_solve_cn(self, conditions={}):
"""Check whether this model is compatible with Crank-Nicolson solver.
All bound functions which do not depend on time are compatible."""
# TODO in the future, instead of looking for parameters this
# way, we should use "t in [i.argrepr for i in
# dis.get_instructions(get_bound)]" to see if it is used in the
# function rather than looking to see if it is passed to the
# function.
boundfuncsig = inspect.signature(self.get_dependence("bound").get_bound)
if "t" in boundfuncsig.parameters:
return False
return True
示例9: test_default_first_line
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def test_default_first_line(self):
actual = dis.get_instructions(simple)
self.assertEqual(list(actual), expected_opinfo_simple)
示例10: test_first_line_set_to_None
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def test_first_line_set_to_None(self):
actual = dis.get_instructions(simple, first_line=None)
self.assertEqual(list(actual), expected_opinfo_simple)
示例11: test_outer
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def test_outer(self):
actual = dis.get_instructions(outer, first_line=expected_outer_line)
self.assertEqual(list(actual), expected_opinfo_outer)
示例12: test_nested
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def test_nested(self):
with captured_stdout():
f = outer()
actual = dis.get_instructions(f, first_line=expected_f_line)
self.assertEqual(list(actual), expected_opinfo_f)
示例13: test_doubly_nested
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def test_doubly_nested(self):
with captured_stdout():
inner = outer()()
actual = dis.get_instructions(inner, first_line=expected_inner_line)
self.assertEqual(list(actual), expected_opinfo_inner)
示例14: test_iteration
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def test_iteration(self):
for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
with self.subTest(obj=obj):
via_object = list(dis.Bytecode(obj))
via_generator = list(dis.get_instructions(obj))
self.assertEqual(via_object, via_generator)
示例15: test_folding_of_tuples_of_constants
# 需要导入模块: import dis [as 别名]
# 或者: from dis import get_instructions [as 别名]
def test_folding_of_tuples_of_constants(self):
for line, elem in (
('a = 1,2,3', (1, 2, 3)),
('("a","b","c")', ('a', 'b', 'c')),
('a,b,c = 1,2,3', (1, 2, 3)),
('(None, 1, None)', (None, 1, None)),
('((1, 2), 3, 4)', ((1, 2), 3, 4)),
):
code = compile(line,'','single')
self.assertInBytecode(code, 'LOAD_CONST', elem)
self.assertNotInBytecode(code, 'BUILD_TUPLE')
# Long tuples should be folded too.
code = compile(repr(tuple(range(10000))),'','single')
self.assertNotInBytecode(code, 'BUILD_TUPLE')
# One LOAD_CONST for the tuple, one for the None return value
load_consts = [instr for instr in dis.get_instructions(code)
if instr.opname == 'LOAD_CONST']
self.assertEqual(len(load_consts), 2)
# Bug 1053819: Tuple of constants misidentified when presented with:
# . . . opcode_with_arg 100 unary_opcode BUILD_TUPLE 1 . . .
# The following would segfault upon compilation
def crater():
(~[
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
],)