def test_self_match_all(self): self.check_instrs([], [SkipAny()]) self.check_instrs([Instruction("STORE_NAME", "foo")], [SkipAny()]) self.check_instrs( [Instruction("STORE_NAME", "foo"), Instruction("STORE_NAME", "foo")], [SkipAny()], )
def test_self_neg_wildcard_multimatch(self): with self.assertRaisesRegex( AssertionError, "unexpected match CALL_FUNCTION 0, got CALL_FUNCTION 0" ): self.check_instrs( [Instruction("LOAD_NAME", "foo"), Instruction("CALL_FUNCTION", 0)], [SkipBut(Op("LOAD_NAME", "bar"), Op("CALL_FUNCTION", 0))], )
def test_self_neg_wildcard_match(self): with self.assertRaisesRegex( AssertionError, "unexpected match LOAD_NAME foo, got LOAD_NAME 'foo'" ): self.check_instrs( [Instruction("LOAD_NAME", "foo"), Instruction("CALL_FUNCTION", 0)], [~Op("LOAD_NAME", "foo")], )
def test_self_trailing_script_wildcard(self): with self.assertRaisesRegex( AssertionError, escape("Trailing script elements: [Op(STORE_NAME, 'bar')]") ): self.check_instrs( [Instruction("STORE_NAME", "foo"), Instruction("STORE_NAME", "foo")], [SkipAny(), Op("STORE_NAME", "bar")], )
def test_self_trailing_instrs(self): with self.assertRaisesRegex(AssertionError, "Extra bytecodes not expected"): self.check_instrs( [ Instruction("STORE_NAME", "foo"), Instruction("STORE_NAME", "foo") ], [Op("STORE_NAME", "foo")], )
def test_self_match_block(self): block = pyassem.Block() block.bid = 1 self.check_instrs( [ Instruction("JUMP_ABSOLUTE", None, target=block), Instruction("LOAD_CONST", None), Instruction("RETURN_VALUE", 0), ], [Op("JUMP_ABSOLUTE", Block(1)), Op("LOAD_CONST", None), SkipAny()], )
def test_self_match_wrong_block(self): block = pyassem.Block() block.bid = 1 with self.assertRaisesRegex( AssertionError, escape( "mismatch, expected JUMP_ABSOLUTE Block(2), got JUMP_ABSOLUTE Block(1)" ), ): self.check_instrs( [ Instruction("JUMP_ABSOLUTE", None, target=block), Instruction("LOAD_CONST", None), Instruction("RETURN_VALUE", 0), ], [Op("JUMP_ABSOLUTE", Block(2)), Op("LOAD_CONST", None), SkipAny()], )
def test_self_bad_oparg(self): return with self.assertRaisesRegex( AssertionError, "Failed to eval expected oparg: foo" ): self.check_instrs( [Instruction("STORE_NAME", "foo")], [Op("STORE_NAME", "foo")] )
def graph_instrs(graph, name=None): if name: yield Instruction(SCRIPT_OPCODE_CODE, name) for block in graph.getBlocks(): for instr in block.getInstructions(): yield instr
def test_self_neg_wildcard_no_multimatch(self): self.check_instrs( [Instruction("LOAD_NAME", "foo"), Instruction("CALL_FUNCTION", 0)], [SkipBut(Op("LOAD_NAME", "bar"), Op("CALL_FUNCTION", 1))], )
def test_self_match_after_wildcard(self): self.check_instrs( [Instruction("LOAD_NAME", "foo"), Instruction("CALL_FUNCTION", 0)], [SkipAny(), Op("CALL_FUNCTION", 0)], )
def test_self_wildcard_opname(self): self.check_instrs([Instruction("LOAD_NAME", "foo")], [Op(Any, "foo")])
def test_self_wildcard_oparg(self): self.check_instrs([Instruction("LOAD_NAME", "foo")], [Op("LOAD_NAME", Any)])
def test_self_mismatch_opname(self): with self.assertRaises(AssertionError): self.check_instrs( [Instruction("LOAD_NAME", "foo")], [Op("STORE_NAME", "foo")] )