@@ -34,91 +34,6 @@ def clear_executors(func):
3434 func .__code__ = func .__code__ .replace ()
3535
3636
37- @requires_specialization
38- @unittest .skipIf (Py_GIL_DISABLED , "optimizer not yet supported in free-threaded builds" )
39- @unittest .skipUnless (hasattr (_testinternalcapi , "get_optimizer" ),
40- "Requires optimizer infrastructure" )
41- class TestOptimizerAPI (unittest .TestCase ):
42-
43- def test_new_counter_optimizer_dealloc (self ):
44- # See gh-108727
45- def f ():
46- _testinternalcapi .new_counter_optimizer ()
47-
48- f ()
49-
50- def test_get_set_optimizer (self ):
51- old = _testinternalcapi .get_optimizer ()
52- opt = _testinternalcapi .new_counter_optimizer ()
53- try :
54- _testinternalcapi .set_optimizer (opt )
55- self .assertEqual (_testinternalcapi .get_optimizer (), opt )
56- _testinternalcapi .set_optimizer (None )
57- self .assertEqual (_testinternalcapi .get_optimizer (), None )
58- finally :
59- _testinternalcapi .set_optimizer (old )
60-
61-
62- def test_counter_optimizer (self ):
63- # Generate a new function at each call
64- ns = {}
65- exec (textwrap .dedent ("""
66- def loop():
67- for _ in range(1000):
68- pass
69- """ ), ns , ns )
70- loop = ns ['loop' ]
71-
72- for repeat in range (5 ):
73- opt = _testinternalcapi .new_counter_optimizer ()
74- with temporary_optimizer (opt ):
75- self .assertEqual (opt .get_count (), 0 )
76- with clear_executors (loop ):
77- loop ()
78- # Subtract because optimizer doesn't kick in sooner
79- self .assertEqual (opt .get_count (), 1000 - TIER2_THRESHOLD )
80-
81- def test_long_loop (self ):
82- "Check that we aren't confused by EXTENDED_ARG"
83-
84- # Generate a new function at each call
85- ns = {}
86- exec (textwrap .dedent ("""
87- def nop():
88- pass
89-
90- def long_loop():
91- for _ in range(20):
92- nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
93- nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
94- nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
95- nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
96- nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
97- nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
98- nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
99- """ ), ns , ns )
100- long_loop = ns ['long_loop' ]
101-
102- opt = _testinternalcapi .new_counter_optimizer ()
103- with temporary_optimizer (opt ):
104- self .assertEqual (opt .get_count (), 0 )
105- long_loop ()
106- self .assertEqual (opt .get_count (), 20 - TIER2_THRESHOLD ) # Need iterations to warm up
107-
108- def test_code_restore_for_ENTER_EXECUTOR (self ):
109- def testfunc (x ):
110- i = 0
111- while i < x :
112- i += 1
113-
114- opt = _testinternalcapi .new_counter_optimizer ()
115- with temporary_optimizer (opt ):
116- testfunc (1000 )
117- code , replace_code = testfunc .__code__ , testfunc .__code__ .replace ()
118- self .assertEqual (code , replace_code )
119- self .assertEqual (hash (code ), hash (replace_code ))
120-
121-
12237def get_first_executor (func ):
12338 code = func .__code__
12439 co_code = code .co_code
@@ -139,88 +54,6 @@ def get_opnames(ex):
13954 return list (iter_opnames (ex ))
14055
14156
142- @requires_specialization
143- @unittest .skipIf (Py_GIL_DISABLED , "optimizer not yet supported in free-threaded builds" )
144- @unittest .skipUnless (hasattr (_testinternalcapi , "get_optimizer" ),
145- "Requires optimizer infrastructure" )
146- class TestExecutorInvalidation (unittest .TestCase ):
147-
148- def setUp (self ):
149- self .old = _testinternalcapi .get_optimizer ()
150- self .opt = _testinternalcapi .new_counter_optimizer ()
151- _testinternalcapi .set_optimizer (self .opt )
152-
153- def tearDown (self ):
154- _testinternalcapi .set_optimizer (self .old )
155-
156- def test_invalidate_object (self ):
157- # Generate a new set of functions at each call
158- ns = {}
159- func_src = "\n " .join (
160- f"""
161- def f{ n } ():
162- for _ in range(1000):
163- pass
164- """ for n in range (5 )
165- )
166- exec (textwrap .dedent (func_src ), ns , ns )
167- funcs = [ ns [f'f{ n } ' ] for n in range (5 )]
168- objects = [object () for _ in range (5 )]
169-
170- for f in funcs :
171- f ()
172- executors = [get_first_executor (f ) for f in funcs ]
173- # Set things up so each executor depends on the objects
174- # with an equal or lower index.
175- for i , exe in enumerate (executors ):
176- self .assertTrue (exe .is_valid ())
177- for obj in objects [:i + 1 ]:
178- _testinternalcapi .add_executor_dependency (exe , obj )
179- self .assertTrue (exe .is_valid ())
180- # Assert that the correct executors are invalidated
181- # and check that nothing crashes when we invalidate
182- # an executor multiple times.
183- for i in (4 ,3 ,2 ,1 ,0 ):
184- _testinternalcapi .invalidate_executors (objects [i ])
185- for exe in executors [i :]:
186- self .assertFalse (exe .is_valid ())
187- for exe in executors [:i ]:
188- self .assertTrue (exe .is_valid ())
189-
190- def test_uop_optimizer_invalidation (self ):
191- # Generate a new function at each call
192- ns = {}
193- exec (textwrap .dedent ("""
194- def f():
195- for i in range(1000):
196- pass
197- """ ), ns , ns )
198- f = ns ['f' ]
199- opt = _testinternalcapi .new_uop_optimizer ()
200- with temporary_optimizer (opt ):
201- f ()
202- exe = get_first_executor (f )
203- self .assertIsNotNone (exe )
204- self .assertTrue (exe .is_valid ())
205- _testinternalcapi .invalidate_executors (f .__code__ )
206- self .assertFalse (exe .is_valid ())
207-
208- def test_sys__clear_internal_caches (self ):
209- def f ():
210- for _ in range (1000 ):
211- pass
212- opt = _testinternalcapi .new_uop_optimizer ()
213- with temporary_optimizer (opt ):
214- f ()
215- exe = get_first_executor (f )
216- self .assertIsNotNone (exe )
217- self .assertTrue (exe .is_valid ())
218- sys ._clear_internal_caches ()
219- self .assertFalse (exe .is_valid ())
220- exe = get_first_executor (f )
221- self .assertIsNone (exe )
222-
223-
22457@requires_specialization
22558@unittest .skipIf (Py_GIL_DISABLED , "optimizer not yet supported in free-threaded builds" )
22659@unittest .skipUnless (hasattr (_testinternalcapi , "get_optimizer" ),
0 commit comments