Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def code_info(cls, code: Bytecode, *,
debug_passes=()) -> PyCodeInfo[Repr]:
cfg = ControlFlowGraph.from_bytecode(code)
current = cls.empty()
run_machine(
Interpreter(code.first_lineno).abs_i_cfg(cfg), current)
glob_deps = tuple(current.globals)
instrs = current.instrs
instrs = cls.pass_push_pop_inline(instrs)
instrs = list(relabel(instrs))
if Options.get('log-stack-vm'):
print('DEBUG: stack-vm'.center(20, '='))
show_instrs(instrs)
phi_pass_name = Options['phi-pass']
e = None
try:
phi_pass = {
'phi-elim-by-move': phi_elim,
def optimize(self, code_obj):
bytecode = Bytecode.from_code(code_obj)
cfg = ControlFlowGraph.from_bytecode(bytecode)
self._optimize(cfg)
bytecode = cfg.to_bytecode()
code = bytecode.to_code()
return code
def compute_stacksize(self):
bytecode = self.to_bytecode()
cfg = _bytecode.ControlFlowGraph.from_bytecode(bytecode)
return cfg.compute_stacksize()
def compute_stacksize(self):
cfg = _bytecode.ControlFlowGraph.from_bytecode(self)
return cfg.compute_stacksize()
def optimize(self, code_obj):
bytecode = Bytecode.from_code(code_obj)
cfg = ControlFlowGraph.from_bytecode(bytecode)
self.optimize_cfg(cfg)
bytecode = cfg.to_bytecode()
code = bytecode.to_code()
return code
# label => instruction index
label_to_block_index = {}
jumps = []
block_starts = {}
for index, instr in enumerate(bytecode):
if isinstance(instr, Label):
label_to_block_index[instr] = index
else:
if isinstance(instr, Instr) and isinstance(instr.arg, Label):
jumps.append((index, instr.arg))
for target_index, target_label in jumps:
target_index = label_to_block_index[target_label]
block_starts[target_index] = target_label
bytecode_blocks = _bytecode.ControlFlowGraph()
bytecode_blocks._copy_attr_from(bytecode)
bytecode_blocks.argnames = list(bytecode.argnames)
# copy instructions, convert labels to block labels
block = bytecode_blocks[0]
labels = {}
jumps = []
for index, instr in enumerate(bytecode):
if index in block_starts:
old_label = block_starts[index]
if index != 0:
new_block = bytecode_blocks.add_block()
if not block[-1].is_final():
block.next_block = new_block
block = new_block
if old_label is not None:
# label => instruction index
label_to_block_index = {}
jumps = []
block_starts = {}
for index, instr in enumerate(bytecode):
if isinstance(instr, Label):
label_to_block_index[instr] = index
else:
if isinstance(instr, Instr) and isinstance(instr.arg, Label):
jumps.append((index, instr.arg))
for target_index, target_label in jumps:
target_index = label_to_block_index[target_label]
block_starts[target_index] = target_label
bytecode_blocks = _bytecode.ControlFlowGraph()
bytecode_blocks._copy_attr_from(bytecode)
bytecode_blocks.argnames = list(bytecode.argnames)
# copy instructions, convert labels to block labels
block = bytecode_blocks[0]
labels = {}
jumps = []
for index, instr in enumerate(bytecode):
if index in block_starts:
old_label = block_starts[index]
if index != 0:
new_block = bytecode_blocks.add_block()
if not block[-1].is_final():
block.next_block = new_block
block = new_block
if old_label is not None:
a previously async code into a sync one.
Parameters
----------
bytecode : Bytecode | ConcreteBytecode | ControlFlowGraph
Bytecode for which to infer the proper flags
is_async : bool | None, optional
Force the code to be marked as asynchronous if True, prevent it from
being marked as asynchronous if False and simply infer the best
solution based on the opcode and the existing flag if None.
"""
flags = CompilerFlags(0)
if not isinstance(
bytecode,
(_bytecode.Bytecode, _bytecode.ConcreteBytecode, _bytecode.ControlFlowGraph),
):
msg = (
"Expected a Bytecode, ConcreteBytecode or ControlFlowGraph "
"instance not %s"
)
raise ValueError(msg % bytecode)
instructions = (
bytecode.get_instructions()
if isinstance(bytecode, _bytecode.ControlFlowGraph)
else bytecode
)
instr_names = {
i.name
for i in instructions
if not isinstance(i, (_bytecode.SetLineno, _bytecode.Label))
def __init__(self, path: str, module_name='', to_import=['*']):
self.path = path
self.to_import = to_import
self.module_name = module_name
self._local_methods = []
source = open(path, 'rb')
compiled_source = compile(source.read(), path, 'exec')
self.bc = Bytecode.from_code(compiled_source)
self.cfg = ControlFlowGraph.from_bytecode(self.bc)
source.close()
self.build()
labels[instr] = 'label_instr%s' % index
for index, instr in enumerate(bytecode):
if isinstance(instr, Label):
label = labels[instr]
line = "%s:" % label
if index != 0:
io()
else:
if instr.lineno is not None:
cur_lineno = instr.lineno
line = format_instr(instr, labels)
line = indent + format_line(index, line)
io(line)
io()
elif isinstance(bytecode, ControlFlowGraph):
labels = {}
for block_index, block in enumerate(bytecode, 1):
labels[id(block)] = 'block%s' % block_index
for block_index, block in enumerate(bytecode, 1):
io('%s:' % labels[id(block)])
prev_lineno = None
for index, instr in enumerate(block):
if instr.lineno is not None:
cur_lineno = instr.lineno
line = format_instr(instr, labels)
line = indent + format_line(index, line)
io(line)
if block.next_block is not None:
io(indent + "-> %s" % labels[id(block.next_block)])
io()