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