2013-05-06 19:28:21 -03:00
|
|
|
"""bytecode_helper - support tools for testing correct bytecode generation"""
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import dis
|
|
|
|
import io
|
|
|
|
|
|
|
|
_UNSPECIFIED = object()
|
|
|
|
|
|
|
|
class BytecodeTestCase(unittest.TestCase):
|
|
|
|
"""Custom assertion methods for inspecting bytecode."""
|
|
|
|
|
|
|
|
def get_disassembly_as_string(self, co):
|
|
|
|
s = io.StringIO()
|
|
|
|
dis.dis(co, file=s)
|
|
|
|
return s.getvalue()
|
|
|
|
|
|
|
|
def assertInBytecode(self, x, opname, argval=_UNSPECIFIED):
|
2019-09-12 06:02:59 -03:00
|
|
|
"""Returns instr if opname is found, otherwise throws AssertionError"""
|
2013-05-06 19:28:21 -03:00
|
|
|
for instr in dis.get_instructions(x):
|
|
|
|
if instr.opname == opname:
|
|
|
|
if argval is _UNSPECIFIED or instr.argval == argval:
|
|
|
|
return instr
|
|
|
|
disassembly = self.get_disassembly_as_string(x)
|
|
|
|
if argval is _UNSPECIFIED:
|
|
|
|
msg = '%s not found in bytecode:\n%s' % (opname, disassembly)
|
|
|
|
else:
|
|
|
|
msg = '(%s,%r) not found in bytecode:\n%s'
|
|
|
|
msg = msg % (opname, argval, disassembly)
|
|
|
|
self.fail(msg)
|
|
|
|
|
|
|
|
def assertNotInBytecode(self, x, opname, argval=_UNSPECIFIED):
|
2019-09-12 06:02:59 -03:00
|
|
|
"""Throws AssertionError if opname is found"""
|
2013-05-06 19:28:21 -03:00
|
|
|
for instr in dis.get_instructions(x):
|
|
|
|
if instr.opname == opname:
|
2016-01-19 03:48:48 -04:00
|
|
|
disassembly = self.get_disassembly_as_string(x)
|
|
|
|
if argval is _UNSPECIFIED:
|
2013-05-06 19:28:21 -03:00
|
|
|
msg = '%s occurs in bytecode:\n%s' % (opname, disassembly)
|
|
|
|
elif instr.argval == argval:
|
|
|
|
msg = '(%s,%r) occurs in bytecode:\n%s'
|
|
|
|
msg = msg % (opname, argval, disassembly)
|
|
|
|
self.fail(msg)
|