Skip to content

Commit

Permalink
pythongh-115999: Move specialier test to test_opcache
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Nov 6, 2024
1 parent a1c57bc commit b0ab600
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 43 deletions.
42 changes: 0 additions & 42 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', "<int>", "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', "<unicode>", "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):
Expand Down Expand Up @@ -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', "<dict>", "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', "<set>", "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):
Expand Down
48 changes: 47 additions & 1 deletion Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()

0 comments on commit b0ab600

Please sign in to comment.