| 
									
										
										
										
											2013-05-07 08:28:21 +10: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 10:02:59 +01:00
										 |  |  |         """Returns instr if opname is found, otherwise throws AssertionError""" | 
					
						
							| 
									
										
										
										
											2013-05-07 08:28:21 +10: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 10:02:59 +01:00
										 |  |  |         """Throws AssertionError if opname is found""" | 
					
						
							| 
									
										
										
										
											2013-05-07 08:28:21 +10:00
										 |  |  |         for instr in dis.get_instructions(x): | 
					
						
							|  |  |  |             if instr.opname == opname: | 
					
						
							| 
									
										
										
										
											2016-01-19 08:48:48 +01:00
										 |  |  |                 disassembly = self.get_disassembly_as_string(x) | 
					
						
							|  |  |  |                 if argval is _UNSPECIFIED: | 
					
						
							| 
									
										
										
										
											2013-05-07 08:28:21 +10:00
										 |  |  |                     msg = '%s occurs in bytecode:\n%s' % (opname, disassembly) | 
					
						
							| 
									
										
										
										
											2020-12-17 16:30:29 -08:00
										 |  |  |                     self.fail(msg) | 
					
						
							| 
									
										
										
										
											2013-05-07 08:28:21 +10:00
										 |  |  |                 elif instr.argval == argval: | 
					
						
							|  |  |  |                     msg = '(%s,%r) occurs in bytecode:\n%s' | 
					
						
							|  |  |  |                     msg = msg % (opname, argval, disassembly) | 
					
						
							| 
									
										
										
										
											2020-12-17 16:30:29 -08:00
										 |  |  |                     self.fail(msg) |