ยปCore Development>Code coverage>Lib/test/bytecode_helper.py

Python code coverage for Lib/test/bytecode_helper.py

#countcontent
1n/a"""bytecode_helper - support tools for testing correct bytecode generation"""
2n/a
3n/aimport unittest
4n/aimport dis
5n/aimport io
6n/a
7n/a_UNSPECIFIED = object()
8n/a
9n/aclass BytecodeTestCase(unittest.TestCase):
10n/a """Custom assertion methods for inspecting bytecode."""
11n/a
12n/a def get_disassembly_as_string(self, co):
13n/a s = io.StringIO()
14n/a dis.dis(co, file=s)
15n/a return s.getvalue()
16n/a
17n/a def assertInBytecode(self, x, opname, argval=_UNSPECIFIED):
18n/a """Returns instr if op is found, otherwise throws AssertionError"""
19n/a for instr in dis.get_instructions(x):
20n/a if instr.opname == opname:
21n/a if argval is _UNSPECIFIED or instr.argval == argval:
22n/a return instr
23n/a disassembly = self.get_disassembly_as_string(x)
24n/a if argval is _UNSPECIFIED:
25n/a msg = '%s not found in bytecode:\n%s' % (opname, disassembly)
26n/a else:
27n/a msg = '(%s,%r) not found in bytecode:\n%s'
28n/a msg = msg % (opname, argval, disassembly)
29n/a self.fail(msg)
30n/a
31n/a def assertNotInBytecode(self, x, opname, argval=_UNSPECIFIED):
32n/a """Throws AssertionError if op is found"""
33n/a for instr in dis.get_instructions(x):
34n/a if instr.opname == opname:
35n/a disassembly = self.get_disassembly_as_string(x)
36n/a if argval is _UNSPECIFIED:
37n/a msg = '%s occurs in bytecode:\n%s' % (opname, disassembly)
38n/a elif instr.argval == argval:
39n/a msg = '(%s,%r) occurs in bytecode:\n%s'
40n/a msg = msg % (opname, argval, disassembly)
41n/a self.fail(msg)