| 
									
										
										
										
											2013-11-23 14:49:22 -08:00
										 |  |  | import dis | 
					
						
							| 
									
										
										
										
											2020-08-04 00:41:24 +08:00
										 |  |  | from test.support.import_helper import import_module | 
					
						
							| 
									
										
										
										
											2013-11-23 14:49:22 -08:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2021-07-29 17:26:53 +01:00
										 |  |  | import opcode | 
					
						
							| 
									
										
										
										
											2013-11-23 14:49:22 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 16:11:17 -08:00
										 |  |  | _opcode = import_module("_opcode") | 
					
						
							| 
									
										
										
										
											2018-09-18 09:54:26 +03:00
										 |  |  | from _opcode import stack_effect | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 16:11:17 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-18 19:42:44 +01:00
										 |  |  | class OpListTests(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2023-10-16 14:30:35 +01:00
										 |  |  |     def check_bool_function_result(self, func, ops, expected): | 
					
						
							|  |  |  |         for op in ops: | 
					
						
							|  |  |  |             if isinstance(op, str): | 
					
						
							|  |  |  |                 op = dis.opmap[op] | 
					
						
							|  |  |  |             with self.subTest(opcode=op, func=func): | 
					
						
							|  |  |  |                 self.assertIsInstance(func(op), bool) | 
					
						
							|  |  |  |                 self.assertEqual(func(op), expected) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 18:41:52 +01:00
										 |  |  |     def test_invalid_opcodes(self): | 
					
						
							| 
									
										
										
										
											2024-08-13 14:22:57 +01:00
										 |  |  |         invalid = [-100, -1, 512, 513, 1000] | 
					
						
							| 
									
										
										
										
											2023-07-14 18:41:52 +01:00
										 |  |  |         self.check_bool_function_result(_opcode.is_valid, invalid, False) | 
					
						
							|  |  |  |         self.check_bool_function_result(_opcode.has_arg, invalid, False) | 
					
						
							|  |  |  |         self.check_bool_function_result(_opcode.has_const, invalid, False) | 
					
						
							|  |  |  |         self.check_bool_function_result(_opcode.has_name, invalid, False) | 
					
						
							|  |  |  |         self.check_bool_function_result(_opcode.has_jump, invalid, False) | 
					
						
							| 
									
										
										
										
											2023-07-18 19:42:44 +01:00
										 |  |  |         self.check_bool_function_result(_opcode.has_free, invalid, False) | 
					
						
							|  |  |  |         self.check_bool_function_result(_opcode.has_local, invalid, False) | 
					
						
							|  |  |  |         self.check_bool_function_result(_opcode.has_exc, invalid, False) | 
					
						
							| 
									
										
										
										
											2023-07-14 18:41:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_is_valid(self): | 
					
						
							|  |  |  |         names = [ | 
					
						
							|  |  |  |             'CACHE', | 
					
						
							|  |  |  |             'POP_TOP', | 
					
						
							|  |  |  |             'IMPORT_NAME', | 
					
						
							|  |  |  |             'JUMP', | 
					
						
							|  |  |  |             'INSTRUMENTED_RETURN_VALUE', | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         opcodes = [dis.opmap[opname] for opname in names] | 
					
						
							|  |  |  |         self.check_bool_function_result(_opcode.is_valid, opcodes, True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-15 21:02:32 +00:00
										 |  |  |     def test_opmaps(self): | 
					
						
							|  |  |  |         def check_roundtrip(name, map): | 
					
						
							|  |  |  |             return self.assertEqual(opcode.opname[map[name]], name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         check_roundtrip('BINARY_OP', opcode.opmap) | 
					
						
							|  |  |  |         check_roundtrip('BINARY_OP_ADD_INT', opcode._specialized_opmap) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-18 19:42:44 +01:00
										 |  |  |     def test_oplists(self): | 
					
						
							|  |  |  |         def check_function(self, func, expected): | 
					
						
							|  |  |  |             for op in [-10, 520]: | 
					
						
							|  |  |  |                 with self.subTest(opcode=op, func=func): | 
					
						
							|  |  |  |                     res = func(op) | 
					
						
							|  |  |  |                     self.assertIsInstance(res, bool) | 
					
						
							|  |  |  |                     self.assertEqual(res, op in expected) | 
					
						
							| 
									
										
										
										
											2023-07-14 18:41:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-18 19:42:44 +01:00
										 |  |  |         check_function(self, _opcode.has_arg, dis.hasarg) | 
					
						
							|  |  |  |         check_function(self, _opcode.has_const, dis.hasconst) | 
					
						
							|  |  |  |         check_function(self, _opcode.has_name, dis.hasname) | 
					
						
							|  |  |  |         check_function(self, _opcode.has_jump, dis.hasjump) | 
					
						
							|  |  |  |         check_function(self, _opcode.has_free, dis.hasfree) | 
					
						
							|  |  |  |         check_function(self, _opcode.has_local, dis.haslocal) | 
					
						
							|  |  |  |         check_function(self, _opcode.has_exc, dis.hasexc) | 
					
						
							| 
									
										
										
										
											2023-07-14 18:41:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-16 14:30:35 +01:00
										 |  |  | class StackEffectTests(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2013-11-23 14:49:22 -08:00
										 |  |  |     def test_stack_effect(self): | 
					
						
							| 
									
										
										
										
											2018-09-18 09:54:26 +03:00
										 |  |  |         self.assertEqual(stack_effect(dis.opmap['POP_TOP']), -1) | 
					
						
							| 
									
										
										
										
											2025-01-27 16:24:48 +00:00
										 |  |  |         self.assertEqual(stack_effect(dis.opmap['BUILD_SLICE'], 2), -1) | 
					
						
							| 
									
										
										
										
											2018-09-18 09:54:26 +03:00
										 |  |  |         self.assertEqual(stack_effect(dis.opmap['BUILD_SLICE'], 3), -2) | 
					
						
							|  |  |  |         self.assertRaises(ValueError, stack_effect, 30000) | 
					
						
							| 
									
										
										
										
											2018-04-25 22:04:06 +03:00
										 |  |  |         # All defined opcodes | 
					
						
							| 
									
										
										
										
											2022-07-01 15:33:35 +01:00
										 |  |  |         has_arg = dis.hasarg | 
					
						
							| 
									
										
										
										
											2022-04-19 16:45:08 +08:00
										 |  |  |         for name, code in filter(lambda item: item[0] not in dis.deoptmap, dis.opmap.items()): | 
					
						
							| 
									
										
										
										
											2023-04-12 12:04:55 +01:00
										 |  |  |             if code >= opcode.MIN_INSTRUMENTED_OPCODE: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2018-04-25 22:04:06 +03:00
										 |  |  |             with self.subTest(opname=name): | 
					
						
							| 
									
										
										
										
											2023-06-17 17:00:16 +01:00
										 |  |  |                 stack_effect(code) | 
					
						
							|  |  |  |                 stack_effect(code, 0) | 
					
						
							| 
									
										
										
										
											2018-04-25 22:04:06 +03:00
										 |  |  |         # All not defined opcodes | 
					
						
							|  |  |  |         for code in set(range(256)) - set(dis.opmap.values()): | 
					
						
							|  |  |  |             with self.subTest(opcode=code): | 
					
						
							| 
									
										
										
										
											2018-09-18 09:54:26 +03:00
										 |  |  |                 self.assertRaises(ValueError, stack_effect, code) | 
					
						
							|  |  |  |                 self.assertRaises(ValueError, stack_effect, code, 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_stack_effect_jump(self): | 
					
						
							|  |  |  |         FOR_ITER = dis.opmap['FOR_ITER'] | 
					
						
							|  |  |  |         self.assertEqual(stack_effect(FOR_ITER, 0), 1) | 
					
						
							| 
									
										
										
										
											2022-10-27 03:55:03 -07:00
										 |  |  |         self.assertEqual(stack_effect(FOR_ITER, 0, jump=True), 1) | 
					
						
							| 
									
										
										
										
											2018-09-18 09:54:26 +03:00
										 |  |  |         self.assertEqual(stack_effect(FOR_ITER, 0, jump=False), 1) | 
					
						
							|  |  |  |         JUMP_FORWARD = dis.opmap['JUMP_FORWARD'] | 
					
						
							|  |  |  |         self.assertEqual(stack_effect(JUMP_FORWARD, 0), 0) | 
					
						
							|  |  |  |         self.assertEqual(stack_effect(JUMP_FORWARD, 0, jump=True), 0) | 
					
						
							|  |  |  |         self.assertEqual(stack_effect(JUMP_FORWARD, 0, jump=False), 0) | 
					
						
							|  |  |  |         # All defined opcodes | 
					
						
							| 
									
										
										
										
											2022-07-01 15:33:35 +01:00
										 |  |  |         has_arg = dis.hasarg | 
					
						
							|  |  |  |         has_exc = dis.hasexc | 
					
						
							| 
									
										
										
										
											2018-09-18 09:54:26 +03:00
										 |  |  |         has_jump = dis.hasjabs + dis.hasjrel | 
					
						
							| 
									
										
										
										
											2022-04-19 16:45:08 +08:00
										 |  |  |         for name, code in filter(lambda item: item[0] not in dis.deoptmap, dis.opmap.items()): | 
					
						
							| 
									
										
										
										
											2023-04-12 12:04:55 +01:00
										 |  |  |             if code >= opcode.MIN_INSTRUMENTED_OPCODE: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2018-09-18 09:54:26 +03:00
										 |  |  |             with self.subTest(opname=name): | 
					
						
							| 
									
										
										
										
											2022-07-01 15:33:35 +01:00
										 |  |  |                 if code not in has_arg: | 
					
						
							| 
									
										
										
										
											2018-09-18 09:54:26 +03:00
										 |  |  |                     common = stack_effect(code) | 
					
						
							|  |  |  |                     jump = stack_effect(code, jump=True) | 
					
						
							|  |  |  |                     nojump = stack_effect(code, jump=False) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     common = stack_effect(code, 0) | 
					
						
							|  |  |  |                     jump = stack_effect(code, 0, jump=True) | 
					
						
							|  |  |  |                     nojump = stack_effect(code, 0, jump=False) | 
					
						
							| 
									
										
										
										
											2022-07-01 15:33:35 +01:00
										 |  |  |                 if code in has_jump or code in has_exc: | 
					
						
							| 
									
										
										
										
											2018-09-18 09:54:26 +03:00
										 |  |  |                     self.assertEqual(common, max(jump, nojump)) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     self.assertEqual(jump, common) | 
					
						
							|  |  |  |                     self.assertEqual(nojump, common) | 
					
						
							| 
									
										
										
										
											2018-04-25 22:04:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 14:49:22 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-29 17:26:53 +01:00
										 |  |  | class SpecializationStatsTests(unittest.TestCase): | 
					
						
							|  |  |  |     def test_specialization_stats(self): | 
					
						
							| 
									
										
										
										
											2023-03-15 05:20:14 +09:00
										 |  |  |         stat_names = ["success", "failure", "hit", "deferred", "miss", "deopt"] | 
					
						
							| 
									
										
										
										
											2021-07-29 17:26:53 +01:00
										 |  |  |         specialized_opcodes = [ | 
					
						
							| 
									
										
										
										
											2022-11-09 10:50:09 -08:00
										 |  |  |             op.lower() | 
					
						
							|  |  |  |             for op in opcode._specializations | 
					
						
							| 
									
										
										
										
											2023-07-27 14:15:25 +01:00
										 |  |  |             if opcode._inline_cache_entries.get(op, 0) | 
					
						
							| 
									
										
										
										
											2022-11-09 10:50:09 -08:00
										 |  |  |         ] | 
					
						
							| 
									
										
										
										
											2021-07-29 17:26:53 +01:00
										 |  |  |         self.assertIn('load_attr', specialized_opcodes) | 
					
						
							| 
									
										
										
										
											2025-02-07 22:39:54 +00:00
										 |  |  |         self.assertIn('binary_op', specialized_opcodes) | 
					
						
							| 
									
										
										
										
											2021-07-29 17:26:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         stats = _opcode.get_specialization_stats() | 
					
						
							|  |  |  |         if stats is not None: | 
					
						
							|  |  |  |             self.assertIsInstance(stats, dict) | 
					
						
							|  |  |  |             self.assertCountEqual(stats.keys(), specialized_opcodes) | 
					
						
							|  |  |  |             self.assertCountEqual( | 
					
						
							|  |  |  |                 stats['load_attr'].keys(), | 
					
						
							| 
									
										
										
										
											2021-12-17 14:48:01 +00:00
										 |  |  |                 stat_names + ['failure_kinds']) | 
					
						
							| 
									
										
										
										
											2021-07-29 17:26:53 +01:00
										 |  |  |             for sn in stat_names: | 
					
						
							|  |  |  |                 self.assertIsInstance(stats['load_attr'][sn], int) | 
					
						
							| 
									
										
										
										
											2021-08-11 17:34:01 +01:00
										 |  |  |             self.assertIsInstance( | 
					
						
							| 
									
										
										
										
											2021-12-17 14:48:01 +00:00
										 |  |  |                 stats['load_attr']['failure_kinds'], | 
					
						
							| 
									
										
										
										
											2021-08-11 17:34:01 +01:00
										 |  |  |                 tuple) | 
					
						
							| 
									
										
										
										
											2021-12-17 14:48:01 +00:00
										 |  |  |             for v in stats['load_attr']['failure_kinds']: | 
					
						
							| 
									
										
										
										
											2021-07-29 17:26:53 +01:00
										 |  |  |                 self.assertIsInstance(v, int) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 14:49:22 -08:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2015-04-13 15:00:43 -05:00
										 |  |  |     unittest.main() |