def test_has_jump(self): label = Label() jump = Instr("JUMP_ABSOLUTE", label) self.assertTrue(jump.has_jump()) instr = Instr("LOAD_FAST", 'x') self.assertFalse(instr.has_jump())
def test_is_uncond_jump(self): label = Label() jump = Instr("JUMP_ABSOLUTE", label) self.assertTrue(jump.is_uncond_jump()) instr = Instr("POP_JUMP_IF_TRUE", label) self.assertFalse(instr.is_uncond_jump())
def test_is_cond_jump(self): label = Label() jump = Instr("POP_JUMP_IF_TRUE", label) self.assertTrue(jump.is_cond_jump()) instr = Instr("LOAD_FAST", 'x') self.assertFalse(instr.is_cond_jump())
def test_attr(self): instr = Instr("LOAD_CONST", 3, lineno=5) self.assertEqual(instr.name, 'LOAD_CONST') self.assertEqual(instr.opcode, 100) self.assertEqual(instr.arg, 3) self.assertEqual(instr.lineno, 5) # invalid values/types self.assertRaises(ValueError, setattr, instr, 'lineno', 0) self.assertRaises(TypeError, setattr, instr, 'lineno', 1.0) self.assertRaises(TypeError, setattr, instr, 'name', 5) self.assertRaises(TypeError, setattr, instr, 'opcode', 1.0) self.assertRaises(ValueError, setattr, instr, 'opcode', -1) self.assertRaises(ValueError, setattr, instr, 'opcode', 255) # arg can take any attribute but cannot be deleted instr.arg = -8 instr.arg = object() self.assertRaises(AttributeError, delattr, instr, 'arg') # no argument instr = Instr("ROT_TWO") self.assertIs(instr.arg, UNSET)
def LOAD_FAST(name: str, *, lineno=None): return Instr('LOAD_FAST', name, lineno=lineno)
def LOAD_GLOBAL(name: str, lineno=None): return Instr('LOAD_GLOBAL', name, lineno=lineno)
def POP_JUMP_IF_TRUE(label: Label, lineno=None): return Instr('POP_JUMP_IF_TRUE', label, lineno=lineno)
def RAISE_VARARGS(n: int, lineno=None): return Instr('RAISE_VARARGS', n, lineno=lineno)
def DELETE_ATTR(attr: str, *, lineno=None): return Instr('DELETE_ATTR', attr, lineno=lineno)
def POP_EXCEPT(*, lineno=None): return Instr("POP_EXCEPT", lineno=lineno)
def CALL_FUNCTION_EX(n: int, *, lineno=None): return Instr("CALL_FUNCTION_EX", n, lineno=lineno)
def POP_TOP(*, lineno=None): return Instr('POP_TOP', lineno=lineno)
def DUP_TOP_TWO(*, lineno=None): return Instr('DUP_TOP_TWO', lineno=lineno)
def test_modify_op(self): instr = Instr("LOAD_NAME", 'x') load_fast = opcode.opmap['LOAD_FAST'] instr.opcode = load_fast self.assertEqual(instr.name, 'LOAD_FAST') self.assertEqual(instr.opcode, load_fast)
def BUILD_STRING(n: int, *, lineno=None): return Instr('BUILD_STRING', n, lineno=lineno)
def STORE_GLOBAL(name: str, *, lineno=None): return Instr("STORE_GLOBAL", name, lineno=lineno)
def test_setlineno(self): # x = 7 # y = 8 # z = 9 code = Bytecode() code.first_lineno = 3 code.extend([ Instr("LOAD_CONST", 7), Instr("STORE_NAME", "x"), SetLineno(4), Instr("LOAD_CONST", 8), Instr("STORE_NAME", "y"), SetLineno(5), Instr("LOAD_CONST", 9), Instr("STORE_NAME", "z"), ]) blocks = ControlFlowGraph.from_bytecode(code) self.assertBlocksEqual( blocks, [ Instr("LOAD_CONST", 7), Instr("STORE_NAME", "x"), SetLineno(4), Instr("LOAD_CONST", 8), Instr("STORE_NAME", "y"), SetLineno(5), Instr("LOAD_CONST", 9), Instr("STORE_NAME", "z"), ], )
def SETUP_ANNOTATIONS(*, lineno=None): return Instr('SETUP_ANNOTATIONS', lineno=lineno)
def COMPARE_OP(arg: Compare, *, lineno=None): return Instr("COMPARE_OP", arg, lineno=lineno)
def STORE_ANNOTATION(n, *, lineno=None): return Instr('STORE_ANNOTATION', n, lineno=lineno)
def YIELD_FROM(*, lineno=None): return Instr("YIELD_FROM", lineno=lineno)
def YIELD_VALUE(*, lineno=None): return Instr("YIELD_VALUE", lineno=lineno)
def CALL_FUNCTION(n: int, *, lineno=None): return Instr('CALL_FUNCTION', n, lineno=lineno)
def BUILD_MAP(n: int, *, lineno=None): return Instr("BUILD_MAP", n, lineno=lineno)
def STORE_ATTR(attr: str, *, lineno=None): return Instr('STORE_ATTR', attr, lineno=lineno)
def LOAD_ATTR(attr: str, *, lineno=None): return Instr('LOAD_ATTR', attr, lineno=lineno)
def UNPACK_EX(arg: int, lineno=None): return Instr('UNPACK_EX', arg, lineno=lineno)
def BUILD_MAP_UNPACK(n: int, *, lineno=None): return Instr("BUILD_MAP_UNPACK", n, lineno=lineno)
def LOAD_CONST(var: object, *, lineno=None): return Instr('LOAD_CONST', var, lineno=lineno)
def GET_AITER(*, lineno=None): return Instr("GET_AITER", lineno=lineno)
def STORE_FAST(name: str, *, lineno=None): return Instr('STORE_FAST', name, lineno=lineno)
def GET_AWAITABLE(*, lineno=None): return Instr("GET_AWAITABLE", lineno=lineno)
def UNPACK_SEQUENCE(n: int, *, lineno=None): return Instr('UNPACK_SEQUENCE', n, lineno=lineno)
def END_FINALLY(*, lineno=None): return Instr("END_FINALLY", lineno=lineno)
def test_legalize(self): code = Bytecode() code.first_lineno = 3 code.extend([ Instr("LOAD_CONST", 7), Instr("STORE_NAME", "x"), Instr("LOAD_CONST", 8, lineno=4), Instr("STORE_NAME", "y"), SetLineno(5), Instr("LOAD_CONST", 9, lineno=6), Instr("STORE_NAME", "z"), ]) blocks = ControlFlowGraph.from_bytecode(code) blocks.legalize() self.assertBlocksEqual( blocks, [ Instr("LOAD_CONST", 7, lineno=3), Instr("STORE_NAME", "x", lineno=3), Instr("LOAD_CONST", 8, lineno=4), Instr("STORE_NAME", "y", lineno=4), Instr("LOAD_CONST", 9, lineno=5), Instr("STORE_NAME", "z", lineno=5), ], )
def test_slots(self): instr = Instr("NOP") with self.assertRaises(AttributeError): instr.myattr = 1