| 
									
										
										
										
											2013-11-23 14:49:22 -08:00
										 |  |  | import dis | 
					
						
							| 
									
										
										
										
											2015-04-13 15:00:43 -05:00
										 |  |  | from test.support import import_module | 
					
						
							| 
									
										
										
										
											2013-11-23 14:49:22 -08:00
										 |  |  | import unittest | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 16:11:17 -08:00
										 |  |  | _opcode = import_module("_opcode") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 14:49:22 -08:00
										 |  |  | class OpcodeTests(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_stack_effect(self): | 
					
						
							|  |  |  |         self.assertEqual(_opcode.stack_effect(dis.opmap['POP_TOP']), -1) | 
					
						
							|  |  |  |         self.assertEqual(_opcode.stack_effect(dis.opmap['DUP_TOP_TWO']), 2) | 
					
						
							|  |  |  |         self.assertEqual(_opcode.stack_effect(dis.opmap['BUILD_SLICE'], 0), -1) | 
					
						
							|  |  |  |         self.assertEqual(_opcode.stack_effect(dis.opmap['BUILD_SLICE'], 1), -1) | 
					
						
							|  |  |  |         self.assertEqual(_opcode.stack_effect(dis.opmap['BUILD_SLICE'], 3), -2) | 
					
						
							|  |  |  |         self.assertRaises(ValueError, _opcode.stack_effect, 30000) | 
					
						
							|  |  |  |         self.assertRaises(ValueError, _opcode.stack_effect, dis.opmap['BUILD_SLICE']) | 
					
						
							|  |  |  |         self.assertRaises(ValueError, _opcode.stack_effect, dis.opmap['POP_TOP'], 0) | 
					
						
							| 
									
										
										
										
											2018-04-25 22:04:06 +03:00
										 |  |  |         # All defined opcodes | 
					
						
							|  |  |  |         for name, code in dis.opmap.items(): | 
					
						
							|  |  |  |             with self.subTest(opname=name): | 
					
						
							|  |  |  |                 if code < dis.HAVE_ARGUMENT: | 
					
						
							|  |  |  |                     _opcode.stack_effect(code) | 
					
						
							|  |  |  |                     self.assertRaises(ValueError, _opcode.stack_effect, code, 0) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     _opcode.stack_effect(code, 0) | 
					
						
							|  |  |  |                     self.assertRaises(ValueError, _opcode.stack_effect, code) | 
					
						
							|  |  |  |         # All not defined opcodes | 
					
						
							|  |  |  |         for code in set(range(256)) - set(dis.opmap.values()): | 
					
						
							|  |  |  |             with self.subTest(opcode=code): | 
					
						
							|  |  |  |                 self.assertRaises(ValueError, _opcode.stack_effect, code) | 
					
						
							|  |  |  |                 self.assertRaises(ValueError, _opcode.stack_effect, code, 0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 14:49:22 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2015-04-13 15:00:43 -05:00
										 |  |  |     unittest.main() |