From b0ab60054077e5d3e784f29bd2fb36deefe8c7ea Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 6 Nov 2024 23:03:54 +0900 Subject: [PATCH] gh-115999: Move specialier test to test_opcache --- Lib/test/test_dis.py | 42 ----------------------------------- Lib/test/test_opcache.py | 48 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 43 deletions(-) diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 337ee3bbb05136..211c0d92a08fd1 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -1261,27 +1261,6 @@ def test_super_instructions(self): got = self.get_disassembly(load_test, adaptive=True) self.do_disassembly_compare(got, dis_load_test_quickened_code) - @cpython_only - @requires_specialization_ft - def test_binary_specialize(self): - binary_op_quicken = """\ - 0 RESUME_CHECK 0 - - 1 LOAD_NAME 0 (a) - LOAD_NAME 1 (b) - %s - RETURN_VALUE -""" - co_int = compile('a + b', "", "eval") - self.code_quicken(lambda: exec(co_int, {}, {'a': 1, 'b': 2})) - got = self.get_disassembly(co_int, adaptive=True) - self.do_disassembly_compare(got, binary_op_quicken % "BINARY_OP_ADD_INT 0 (+)") - - co_unicode = compile('a + b', "", "eval") - self.code_quicken(lambda: exec(co_unicode, {}, {'a': 'a', 'b': 'b'})) - got = self.get_disassembly(co_unicode, adaptive=True) - self.do_disassembly_compare(got, binary_op_quicken % "BINARY_OP_ADD_UNICODE 0 (+)") - @cpython_only @requires_specialization def test_binary_subscr_specialize(self): @@ -1335,27 +1314,6 @@ def test_call_specialize(self): got = self.get_disassembly(co, adaptive=True) self.do_disassembly_compare(got, call_quicken) - @cpython_only - @requires_specialization_ft - def test_contains_specialize(self): - contains_op_quicken = """\ - 0 RESUME_CHECK 0 - - 1 LOAD_NAME 0 (a) - LOAD_NAME 1 (b) - %s - RETURN_VALUE -""" - co_dict = compile('a in b', "", "eval") - self.code_quicken(lambda: exec(co_dict, {}, {'a': 1, 'b': {1: 5}})) - got = self.get_disassembly(co_dict, adaptive=True) - self.do_disassembly_compare(got, contains_op_quicken % "CONTAINS_OP_DICT 0 (in)") - - co_set = compile('a in b', "", "eval") - self.code_quicken(lambda: exec(co_set, {}, {'a': 1.0, 'b': {1, 2, 3}})) - got = self.get_disassembly(co_set, adaptive=True) - self.do_disassembly_compare(got, contains_op_quicken % "CONTAINS_OP_SET 0 (in)") - @cpython_only @requires_specialization def test_loop_quicken(self): diff --git a/Lib/test/test_opcache.py b/Lib/test/test_opcache.py index cdcddb0d717f23..dd5aa1a3e4a5b0 100644 --- a/Lib/test/test_opcache.py +++ b/Lib/test/test_opcache.py @@ -4,7 +4,9 @@ import threading import types import unittest -from test.support import threading_helper, check_impl_detail, requires_specialization +from test.support import (threading_helper, check_impl_detail, + requires_specialization, requires_specialization_ft, + cpython_only) from test.support.import_helper import import_module # Skip this module on other interpreters, it is cpython specific: @@ -1200,5 +1202,49 @@ def f(o, n): self.assertEqual(test_obj.b, 0) +class TestSpecializer(TestBase): + + @cpython_only + @requires_specialization_ft + def test_binary_op(self): + def f(): + a, b = 1, 2 + for _ in range(100): + c = a + b + self.assertEqual(c, 3) + + f() + self.assert_specialized(f, "BINARY_OP_ADD_INT") + + def g(): + a, b = "foo", "bar" + for _ in range(100): + c = a + b + self.assertEqual(c, "foobar") + + g() + self.assert_specialized(g, "BINARY_OP_ADD_UNICODE") + + @cpython_only + @requires_specialization_ft + def test_contain_op(self): + def f(): + a, b = 1, {1: 2, 2: 5} + for _ in range(100): + self.assertTrue(a in b) + + f() + self.assert_specialized(f, "CONTAINS_OP_DICT") + + def g(): + a, b = 1, {1, 2} + for _ in range(100): + self.assertTrue(a in b) + + g() + self.assert_specialized(g, "CONTAINS_OP_SET") + + + if __name__ == "__main__": unittest.main()