Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _flat(self):
instructions = []
labels = {}
jumps = []
offset = 0
for index, instr in enumerate(self):
if isinstance(instr, Label):
instructions.append("label_instr%s" % index)
labels[instr] = offset
else:
if isinstance(instr, Instr) and isinstance(instr.arg, Label):
target_label = instr.arg
instr = _bytecode.ConcreteInstr(instr.name, 0, lineno=instr.lineno)
jumps.append((target_label, instr))
instructions.append(instr)
offset += 1
for target_label, instr in jumps:
instr.arg = labels[target_label]
return instructions
def from_bytecode(bytecode):
# 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 = {}
def _check_arg(self, name, opcode, arg):
if opcode >= _opcode.HAVE_ARGUMENT:
if arg is UNSET:
raise ValueError("operation %s requires an argument" % name)
else:
if arg is not UNSET:
raise ValueError("operation %s has no argument" % name)
if self._has_jump(opcode):
if not isinstance(arg, (Label, _bytecode.BasicBlock)):
raise TypeError(
"operation %s argument type must be "
"Label or BasicBlock, got %s" % (name, type(arg).__name__)
)
elif opcode in _opcode.hasfree:
if not isinstance(arg, (CellVar, FreeVar)):
raise TypeError(
"operation %s argument must be CellVar "
"or FreeVar, got %s" % (name, type(arg).__name__)
)
elif opcode in _opcode.haslocal or opcode in _opcode.hasname:
if not isinstance(arg, str):
raise TypeError(
"operation %s argument must be a str, "
def _cmp_key(self, labels=None):
arg = self._arg
if self._opcode in _opcode.hasconst:
arg = const_key(arg)
elif isinstance(arg, Label) and labels is not None:
arg = labels[arg]
return (self._lineno, self._name, arg)
def concrete_instructions(self):
ncells = len(self.bytecode.cellvars)
lineno = self.bytecode.first_lineno
for instr in self.bytecode:
if isinstance(instr, Label):
self.labels[instr] = len(self.instructions)
continue
if isinstance(instr, SetLineno):
lineno = instr.lineno
continue
if isinstance(instr, ConcreteInstr):
instr = instr.copy()
else:
assert isinstance(instr, Instr)
if instr.lineno is not None:
lineno = instr.lineno
arg = instr.arg
if instr.lineno is not None:
cur_lineno = instr.lineno
if lineno:
fields.append(format_instr(instr))
line = "".join(fields)
line = format_line(offset, line)
else:
fields.append("% 3s %s" % (offset, format_instr(instr)))
line = "".join(fields)
print(line)
offset += instr.size
elif isinstance(bytecode, Bytecode):
labels = {}
for index, instr in enumerate(bytecode):
if isinstance(instr, Label):
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:
print()
else:
if instr.lineno is not None:
cur_lineno = instr.lineno
line = format_instr(instr, labels)
line = indent + format_line(index, line)
print(line)
print()
elif isinstance(bytecode, ControlFlowGraph):
continue
target = instr.get_jump_target(offset)
if target is not None:
jump_targets.add(target)
offset += instr.size
# create labels
jumps = []
instructions = []
labels = {}
offset = 0
ncells = len(self.cellvars)
for lineno, instr in self._normalize_lineno():
if offset in jump_targets:
label = Label()
labels[offset] = label
instructions.append(label)
jump_target = instr.get_jump_target(offset)
size = instr.size
arg = instr.arg
# FIXME: better error reporting
if instr.opcode in _opcode.hasconst:
arg = self.consts[arg]
elif instr.opcode in _opcode.haslocal:
arg = self.varnames[arg]
elif instr.opcode in _opcode.hasname:
arg = self.names[arg]
elif instr.opcode in _opcode.hasfree:
if arg < ncells:
def to_bytecode(self):
"""Convert to Bytecode."""
used_blocks = set()
for block in self:
target_block = block.get_jump()
if target_block is not None:
used_blocks.add(id(target_block))
labels = {}
jumps = []
instructions = []
for block in self:
if id(block) in used_blocks:
new_label = Label()
labels[id(block)] = new_label
instructions.append(new_label)
for instr in block:
# don't copy SetLineno objects
if isinstance(instr, (Instr, ConcreteInstr)):
instr = instr.copy()
if isinstance(instr.arg, BasicBlock):
jumps.append(instr)
instructions.append(instr)
# Map to new labels
for instr in jumps:
instr.arg = labels[id(instr.arg)]
bytecode = _bytecode.Bytecode()
def to_bytecode(self):
"""Convert to Bytecode."""
used_blocks = set()
for block in self:
target_block = block.get_jump()
if target_block is not None:
used_blocks.add(id(target_block))
labels = {}
jumps = []
instructions = []
for block in self:
if id(block) in used_blocks:
new_label = Label()
labels[id(block)] = new_label
instructions.append(new_label)
for instr in block:
# don't copy SetLineno objects
if isinstance(instr, (Instr, ConcreteInstr)):
instr = instr.copy()
if isinstance(instr.arg, BasicBlock):
jumps.append(instr)
instructions.append(instr)
# Map to new labels
for instr in jumps:
instr.arg = labels[id(instr.arg)]
bytecode = _bytecode.Bytecode()