Exemple #1
0
    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())
Exemple #2
0
    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())
Exemple #3
0
    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())
Exemple #4
0
    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)
Exemple #5
0
def LOAD_FAST(name: str, *, lineno=None):
    return Instr('LOAD_FAST', name, lineno=lineno)
Exemple #6
0
def LOAD_GLOBAL(name: str, lineno=None):
    return Instr('LOAD_GLOBAL', name, lineno=lineno)
Exemple #7
0
def POP_JUMP_IF_TRUE(label: Label, lineno=None):
    return Instr('POP_JUMP_IF_TRUE', label, lineno=lineno)
Exemple #8
0
def RAISE_VARARGS(n: int, lineno=None):
    return Instr('RAISE_VARARGS', n, lineno=lineno)
Exemple #9
0
def DELETE_ATTR(attr: str, *, lineno=None):
    return Instr('DELETE_ATTR', attr, lineno=lineno)
Exemple #10
0
def POP_EXCEPT(*, lineno=None):
    return Instr("POP_EXCEPT", lineno=lineno)
Exemple #11
0
def CALL_FUNCTION_EX(n: int, *, lineno=None):
    return Instr("CALL_FUNCTION_EX", n, lineno=lineno)
Exemple #12
0
def POP_TOP(*, lineno=None):
    return Instr('POP_TOP', lineno=lineno)
Exemple #13
0
def DUP_TOP_TWO(*, lineno=None):
    return Instr('DUP_TOP_TWO', lineno=lineno)
Exemple #14
0
 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)
Exemple #15
0
def BUILD_STRING(n: int, *, lineno=None):
    return Instr('BUILD_STRING', n, lineno=lineno)
Exemple #16
0
def STORE_GLOBAL(name: str, *, lineno=None):
    return Instr("STORE_GLOBAL", name, lineno=lineno)
Exemple #17
0
    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"),
            ],
        )
Exemple #18
0
def SETUP_ANNOTATIONS(*, lineno=None):
    return Instr('SETUP_ANNOTATIONS', lineno=lineno)
Exemple #19
0
def COMPARE_OP(arg: Compare, *, lineno=None):
    return Instr("COMPARE_OP", arg, lineno=lineno)
Exemple #20
0
def STORE_ANNOTATION(n, *, lineno=None):
    return Instr('STORE_ANNOTATION', n, lineno=lineno)
Exemple #21
0
def YIELD_FROM(*, lineno=None):
    return Instr("YIELD_FROM", lineno=lineno)
Exemple #22
0
def YIELD_VALUE(*, lineno=None):
    return Instr("YIELD_VALUE", lineno=lineno)
Exemple #23
0
def CALL_FUNCTION(n: int, *, lineno=None):
    return Instr('CALL_FUNCTION', n, lineno=lineno)
Exemple #24
0
def BUILD_MAP(n: int, *, lineno=None):
    return Instr("BUILD_MAP", n, lineno=lineno)
Exemple #25
0
def STORE_ATTR(attr: str, *, lineno=None):
    return Instr('STORE_ATTR', attr, lineno=lineno)
Exemple #26
0
def LOAD_ATTR(attr: str, *, lineno=None):
    return Instr('LOAD_ATTR', attr, lineno=lineno)
Exemple #27
0
def UNPACK_EX(arg: int, lineno=None):
    return Instr('UNPACK_EX', arg, lineno=lineno)
Exemple #28
0
def BUILD_MAP_UNPACK(n: int, *, lineno=None):
    return Instr("BUILD_MAP_UNPACK", n, lineno=lineno)
Exemple #29
0
def LOAD_CONST(var: object, *, lineno=None):
    return Instr('LOAD_CONST', var, lineno=lineno)
Exemple #30
0
def GET_AITER(*, lineno=None):
    return Instr("GET_AITER", lineno=lineno)
Exemple #31
0
def STORE_FAST(name: str, *, lineno=None):
    return Instr('STORE_FAST', name, lineno=lineno)
Exemple #32
0
def GET_AWAITABLE(*, lineno=None):
    return Instr("GET_AWAITABLE", lineno=lineno)
Exemple #33
0
def UNPACK_SEQUENCE(n: int, *, lineno=None):
    return Instr('UNPACK_SEQUENCE', n, lineno=lineno)
Exemple #34
0
def END_FINALLY(*, lineno=None):
    return Instr("END_FINALLY", lineno=lineno)
Exemple #35
0
    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),
            ],
        )
Exemple #36
0
 def test_slots(self):
     instr = Instr("NOP")
     with self.assertRaises(AttributeError):
         instr.myattr = 1