本文整理汇总了Python中dis.Bytecode方法的典型用法代码示例。如果您正苦于以下问题:Python dis.Bytecode方法的具体用法?Python dis.Bytecode怎么用?Python dis.Bytecode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dis
的用法示例。
在下文中一共展示了dis.Bytecode方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: scan_opcodes_34
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def scan_opcodes_34(self, co):
i = 0
bytecode = list(dis.Bytecode(co))
while i < len(bytecode):
if (
bytecode[i].opname == "LOAD_CONST" and
bytecode[i + 1].opname == "LOAD_CONST" and
bytecode[i + 2].opname == "IMPORT_NAME"
):
level = bytecode[i].argval
fromlist = bytecode[i + 1].argval
import_name = bytecode[i + 2].argval
if level == 0:
yield "absolute_import", (fromlist, import_name)
else:
yield "relative_import", (level, fromlist, import_name)
i += 2
i += 1
示例2: test_async
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_async():
# Write a coroutine, add2, that accepts two parameters,
# adds them, then calls ``asyncio.sleep(0)``.
# Finally it returns the sum.
# ************************************
res = await add2(2, 3)
assert res == 5
code = dis.Bytecode(add2)
assert 'sleep' in code.dis()
# Write a coroutine, avg, that accepts two parameters,
# ``coroutines`` and ``size``.
# It loops over coroutines and
# gets values from them. When
# it has pulled out size values,
# it returns the average of those
# values.
# (Make sure you put an await call in it)
# ************************************
res3 = await avg([add2(1, 3),
add2(1, 4),
add2(1, 6),
], 2)
assert res3 == 4.5
示例3: _iter_instructions
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def _iter_instructions(co):
if sys.version_info[0] < 3:
iter_in = _iter_as_bytecode_as_instructions_py2(co)
else:
iter_in = dis.Bytecode(co)
iter_in = list(iter_in)
bytecode_to_instruction = {}
for instruction in iter_in:
bytecode_to_instruction[instruction.offset] = instruction
if iter_in:
for instruction in iter_in:
yield instruction
示例4: debug_test_iter_bytecode
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def debug_test_iter_bytecode(data_regression):
# Note: not run by default, only to help visualizing bytecode and comparing differences among versions.
import dis
basename = 'test_iter_bytecode.py%s%s' % (sys.version_info[:2])
method_to_info = {}
for key, method in sorted(dict(globals()).items()):
if key.startswith('_method'):
info = []
if sys.version_info[0] < 3:
from _pydevd_bundle.pydevd_collect_bytecode_info import _iter_as_bytecode_as_instructions_py2
iter_in = _iter_as_bytecode_as_instructions_py2(method.__code__)
else:
iter_in = dis.Bytecode(method)
for instruction in iter_in:
info.append(_create_entry(instruction))
msg = []
for d in info:
line = []
for k, v in sorted(d.items()):
line.append(u'%s=%s' % (k, v))
msg.append(u' '.join(line))
if isinstance(key, bytes):
key = key.decode('utf-8')
method_to_info[key] = msg
data_regression.check(method_to_info, basename=basename)
示例5: test_instantiation
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_instantiation(self):
# Test with function, method, code string and code object
for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
with self.subTest(obj=obj):
b = dis.Bytecode(obj)
self.assertIsInstance(b.codeobj, types.CodeType)
self.assertRaises(TypeError, dis.Bytecode, object())
示例6: test_iteration
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [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)
示例7: test_explicit_first_line
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_explicit_first_line(self):
actual = dis.Bytecode(outer, first_line=expected_outer_line)
self.assertEqual(list(actual), expected_opinfo_outer)
示例8: test_source_line_in_disassembly
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_source_line_in_disassembly(self):
# Use the line in the source code
actual = dis.Bytecode(simple).dis()[:3]
expected = "{:>3}".format(simple.__code__.co_firstlineno)
self.assertEqual(actual, expected)
# Use an explicit first line number
actual = dis.Bytecode(simple, first_line=350).dis()[:3]
self.assertEqual(actual, "350")
示例9: test_info
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_info(self):
self.maxDiff = 1000
for x, expected in CodeInfoTests.test_pairs:
b = dis.Bytecode(x)
self.assertRegex(b.info(), expected)
示例10: test_from_traceback
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_from_traceback(self):
tb = get_tb()
b = dis.Bytecode.from_traceback(tb)
while tb.tb_next: tb = tb.tb_next
self.assertEqual(b.current_offset, tb.tb_lasti)
示例11: test_from_traceback_dis
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_from_traceback_dis(self):
tb = get_tb()
b = dis.Bytecode.from_traceback(tb)
self.assertEqual(b.dis(), dis_traceback)
示例12: instructions
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def instructions(code_obj):
# easy for python 3.4+
if sys.version_info >= (3, 4):
for inst in dis.Bytecode(code_obj):
yield inst
else:
# otherwise we have to manually parse
code = code_obj.co_code
NewInstruction = namedtuple('Instruction', ('opcode', 'arg'))
if six.PY2:
code = map(ord, code)
i, L = 0, len(code)
extended_arg = 0
while i < L:
op = code[i]
i += 1
if op < opcode.HAVE_ARGUMENT:
yield NewInstruction(op, None)
continue
oparg = code[i] + (code[i + 1] << 8) + extended_arg
extended_arg = 0
i += 2
if op == opcode.EXTENDED_ARG:
extended_arg = oparg << 16
continue
yield NewInstruction(op, oparg)
示例13: instructions
# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def instructions(code_obj):
# easy for python 3.4+
if sys.version_info >= (3, 4):
for inst in dis.Bytecode(code_obj):
yield inst
else:
# otherwise we have to manually parse
code = code_obj.co_code
NewInstruction = namedtuple('Instruction', ('opcode', 'arg'))
if six.PY2:
code = map(ord, code)
i, L = 0, len(code)
extended_arg = 0
while i < L:
op = code[i]
i+= 1
if op < opcode.HAVE_ARGUMENT:
yield NewInstruction(op, None)
continue
oparg = code[i] + (code[i+1] << 8) + extended_arg
extended_arg = 0
i += 2
if op == opcode.EXTENDED_ARG:
extended_arg = oparg << 16
continue
yield NewInstruction(op, oparg)