@@ -35,90 +35,6 @@ def clear_executors(func):
3535 func .__code__ = func .__code__ .replace ()
3636
3737
38- @requires_specialization
39- @unittest .skipIf (Py_GIL_DISABLED , "optimizer not yet supported in free-threaded builds" )
40- @unittest .skipUnless (hasattr (_testinternalcapi , "get_optimizer" ),
41- "Requires optimizer infrastructure" )
42- class TestOptimizerAPI (unittest .TestCase ):
43-
44- def test_new_counter_optimizer_dealloc (self ):
45- # See gh-108727
46- def f ():
47- _testinternalcapi .new_counter_optimizer ()
48-
49- f ()
50-
51- def test_get_set_optimizer (self ):
52- old = _testinternalcapi .get_optimizer ()
53- opt = _testinternalcapi .new_counter_optimizer ()
54- try :
55- _testinternalcapi .set_optimizer (opt )
56- self .assertEqual (_testinternalcapi .get_optimizer (), opt )
57- _testinternalcapi .set_optimizer (None )
58- self .assertEqual (_testinternalcapi .get_optimizer (), None )
59- finally :
60- _testinternalcapi .set_optimizer (old )
61-
62-
63- def test_counter_optimizer (self ):
64- # Generate a new function at each call
65- ns = {}
66- exec (textwrap .dedent (f"""
67- def loop():
68- for _ in range({ TIER2_THRESHOLD + 1000 } ):
69- pass
70- """ ), ns , ns )
71- loop = ns ['loop' ]
72-
73- for repeat in range (5 ):
74- opt = _testinternalcapi .new_counter_optimizer ()
75- with temporary_optimizer (opt ):
76- self .assertEqual (opt .get_count (), 0 )
77- with clear_executors (loop ):
78- loop ()
79- self .assertEqual (opt .get_count (), 1001 )
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 (f"""
87- def nop():
88- pass
89-
90- def long_loop():
91- for _ in range({ TIER2_THRESHOLD + 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 (), 21 ) # 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-
12238def get_first_executor (func ):
12339 code = func .__code__
12440 co_code = code .co_code
@@ -139,88 +55,6 @@ def get_opnames(ex):
13955 return list (iter_opnames (ex ))
14056
14157
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({ TIER2_THRESHOLD } ):
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 (f"""
194- def f():
195- for i in range({ TIER2_THRESHOLD } ):
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 (TIER2_THRESHOLD ):
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-
22458@requires_specialization
22559@unittest .skipIf (Py_GIL_DISABLED , "optimizer not yet supported in free-threaded builds" )
22660@unittest .skipUnless (hasattr (_testinternalcapi , "get_optimizer" ),
0 commit comments