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