Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_inst_size():
if (sys.version_info == (3,6)):
variant = 'pypy' if IS_PYPY else None
opc = get_opcode_module(sys.version_info, variant)
bytecode_obj = Bytecode(extended_arg_fn36, opc)
instructions = list(bytecode_obj.get_instructions(extended_arg_fn36))
inst1 = instructions[1]
assert inst1.opname == 'EXTENDED_ARG'
assert inst1.argval == 0
inst2 = instructions[2]
assert inst2.opname == 'POP_JUMP_IF_FALSE'
assert inst2.has_extended_arg == True
assert inst2.inst_size == 4
# for inst in instructions:
# print(inst)
else:
assert True
def test_inst_jumps():
if (sys.version_info >= (2,7)):
variant = 'pypy' if IS_PYPY else None
opc = get_opcode_module(sys.version_info, variant)
bytecode_obj = Bytecode(extended_arg_fn36, opc)
instructions = list(bytecode_obj.get_instructions(loop))
seen_pjif = False
seen_ja = False
for inst in instructions:
if inst.opname == "POP_JUMP_IF_FALSE":
assert inst.is_jump()
seen_pjif = True
elif inst.opname == "JUMP_ABSOLUTE":
assert inst.is_jump()
assert not inst.jumps_forward()
seen_ja = True
pass
pass
assert seen_pjif
assert seen_ja
section("Disassembly of %s: %s" % (x, mess))
frame = x.gi_frame
lasti = frame.f_last_i
x = x.gi_code
sectioned = True
elif inspect.isframe(x):
section("Disassembly of %s: %s" % (x, mess))
sectioned = True
if hasattr(x, 'f_lasti'):
lasti = x.f_lasti
if lasti == -1: lasti = 0
pass
opc = get_opcode(PYTHON_VERSION, IS_PYPY)
x = x.f_code
if include_header:
header_lines = Bytecode(x, opc).info().split("\n")
header = '\n'.join([format_token(Mformat.Comment, h) for h in header_lines])
msg(header)
pass
elif inspect.iscode(x):
pass
if hasattr(x, '__dict__'): # Class or module
items = sorted(x.__dict__.items())
for name, x1 in items:
if isinstance(x1, _have_code):
if not sectioned:
section("Disassembly of %s: " % x)
try:
dis(msg, msg_nocr, section, errmsg, x1,
start_line=start_line, end_line=end_line,
relative_pos = relative_pos)
"""Disassembles a queue of code objects. If we discover
another code object which will be found in co_consts, we add
the new code to the list. Note that the order of code discovery
is in the order of first encountered which is not amenable for
the format used by a disassembler where code objects should
be defined before using them in other functions.
However this is not recursive and will overall lead to less
memory consumption at run time.
"""
while len(queue) > 0:
co = queue.popleft()
if co.co_name != '':
real_out.write("\n" + format_code_info(co, version) + "\n")
bytecode = Bytecode(co, opc)
real_out.write(bytecode.dis() + "\n")
for c in co.co_consts:
if iscode(c):
queue.append(c)
pass
pass
def number_loop(queue, mappings, opc):
while len(queue) > 0:
code1 = queue.popleft()
code2 = queue.popleft()
assert code1.co_name == code2.co_name
linestarts_orig = findlinestarts(code1)
linestarts_uncompiled = list(findlinestarts(code2))
mappings += [[line, offset2line(offset, linestarts_uncompiled)] for offset, line in linestarts_orig]
bytecode1 = Bytecode(code1, opc)
bytecode2 = Bytecode(code2, opc)
instr2s = bytecode2.get_instructions(code2)
seen = set([code1.co_name])
for instr in bytecode1.get_instructions(code1):
next_code1 = None
if iscode(instr.argval):
next_code1 = instr.argval
if next_code1:
next_code2 = None
while not next_code2:
try:
instr2 = next(instr2s)
if iscode(instr2.argval):
next_code2 = instr2.argval
pass
except StopIteration:
break
import xdis.wordcode as xcode
else:
import xdis.bytecode as xcode
self.xcode = xcode
self.opc = opc = get_opcode_module(python_version, variant)
self.python_version = opc.version
self.is_pypy = variant == PYPY
self.hasconst = opc.hasconst
self.hasname = opc.hasname
self.opmap = opc.opmap
self.opname = opc.opname
self.EXTENDED_ARG = opc.EXTENDED_ARG
self.HAVE_ARGUMENT = opc.HAVE_ARGUMENT
class Bytecode(_Bytecode):
"""The bytecode operations of a piece of code
Instantiate this with a function, method, string of code, or a code object
(as returned by compile()).
Iterating over this yields the bytecode operations as Instruction instances.
"""
def __init__(self, x, first_line=None, current_offset=None):
super(Bytecode, self).__init__(x, opc=opc, first_line=first_line,
current_offset=current_offset)
self.Bytecode = Bytecode
class Instruction(_Instruction):
"""Details for a bytecode operation
Defined fields:
fn_name_map[mapped_name] = basename
co.co_name = mapped_name
pass
elif co_name in fn_name_map:
# FIXME: better would be a hash of the co_code
mapped_name = code_uniquify(co_name, co.co_code)
fn_name_map[mapped_name] = co_name
co.co_name = mapped_name
pass
co = co.freeze()
all_fns.add(co_name)
if co.co_name != "" or co.co_filename:
real_out.write("\n" + format_code_info(co, version, mapped_name) + "\n")
bytecode = Bytecode(co, opc, dup_lines=True)
real_out.write(bytecode.dis(asm_format=True) + "\n")
def disco_loop_asm_format(opc, version, co, real_out):
"""Produces disassembly in a format more conducive to
automatic assembly by producing inner modules before they are
used by outer ones. Since this is recusive, we'll
use more stack space at runtime.
"""
for c in co.co_consts:
if iscode(c):
disco_loop_asm_format(opc, version, c, real_out)
pass
if co.co_name != '' or co.co_filename:
real_out.write("\n" + format_code_info(co, version) + "\n")
bytecode = Bytecode(co, opc)
real_out.write(bytecode.dis(asm_format=True) + "\n")
"""Disassembles a queue of code objects. If we discover
another code object which will be found in co_consts, we add
the new code to the list. Note that the order of code discovery
is in the order of first encountered which is not amenable for
the format used by a disassembler where code objects should
be defined before using them in other functions.
However this is not recursive and will overall lead to less
memory consumption at run time.
"""
while len(queue) > 0:
co = queue.popleft()
if co.co_name not in ("", "?"):
real_out.write("\n" + format_code_info(co, version) + "\n")
bytecode = Bytecode(co, opc, dup_lines=dup_lines)
real_out.write(bytecode.dis(asm_format=asm_format) + "\n")
for c in co.co_consts:
if iscode(c):
queue.append(c)
pass
pass