@@ -36,10 +36,20 @@ def supports_trampoline_profiling():
3636
3737class TestPerfTrampoline (unittest .TestCase ):
3838
39+ def setUp (self ):
40+ # Register cleanup for JIT files for all tests in this class
41+ self .addCleanup (self ._cleanup_jit_files )
42+
3943 def _cleanup_perf_map (self , pid ):
4044 """Helper to safely delete a specific perf map file."""
4145 unlink (f"/tmp/perf-{ pid } .map" )
4246
47+ def _cleanup_jit_files (self ):
48+ """Helper to safely delete JIT-related temp files."""
49+ for pattern in ("jit*.dump" , "jitted-*.so" ):
50+ for path in pathlib .Path ("/tmp/" ).glob (pattern ):
51+ unlink (path )
52+
4353 @unittest .skipIf (support .check_bolt_optimized (), "fails on BOLT instrumented binaries" )
4454 def test_trampoline_works (self ):
4555 code = """if 1:
@@ -57,7 +67,6 @@ def baz():
5767 with temp_dir () as script_dir :
5868 script = make_script (script_dir , "perftest" , code )
5969 env = {** os .environ , "PYTHON_JIT" : "0" }
60-
6170 with subprocess .Popen (
6271 [sys .executable , "-Xperf" , script ],
6372 text = True ,
@@ -276,6 +285,7 @@ def perf_command_works():
276285 if "perf.data" not in stdout :
277286 return False
278287
288+ # Check that we can run a simple perf run
279289 with temp_dir () as script_dir :
280290 try :
281291 output_file = script_dir + "/perf_output.perf"
@@ -372,6 +382,10 @@ def run_perf(cwd, *args, use_jit=False, **env_vars):
372382
373383
374384class TestPerfProfilerMixin :
385+ def setUp (self ):
386+ # This setUp is added to make super() calls in child classes work smoothly.
387+ pass
388+
375389 def run_perf (self , script_dir , perf_mode , script ):
376390 raise NotImplementedError ()
377391
@@ -431,11 +445,11 @@ def baz(n):
431445 is_unwinding_reliable_with_frame_pointers (),
432446 "Unwinding is unreliable with frame pointers" ,
433447)
434- class TestPerfProfiler (unittest . TestCase , TestPerfProfilerMixin ):
448+ class TestPerfProfiler (TestPerfTrampoline , TestPerfProfilerMixin ):
435449
436- def _cleanup_perf_map (self , pid ):
437- """Helper to safely delete a specific perf map file."""
438- unlink ( f"/tmp/perf- { pid } .map" )
450+ def setUp (self ):
451+ super (). setUp ()
452+ TestPerfProfilerMixin . setUp ( self )
439453
440454 def run_perf (self , script_dir , script , activate_trampoline = True ):
441455 if activate_trampoline :
@@ -495,7 +509,6 @@ def compile_trampolines_for_all_functions():
495509
496510 self .assertEqual (process .returncode , 0 )
497511 self .assertNotIn ("Error:" , stderr )
498-
499512 child_pid = int (stdout .strip ())
500513 self .addCleanup (self ._cleanup_perf_map , child_pid )
501514
@@ -544,7 +557,13 @@ def _is_perf_version_at_least(major, minor):
544557@unittest .skipUnless (
545558 _is_perf_version_at_least (6 , 6 ), "perf command may not work due to a perf bug"
546559)
547- class TestPerfProfilerWithDwarf (unittest .TestCase , TestPerfProfilerMixin ):
560+ class TestPerfProfilerWithDwarf (TestPerfTrampoline , TestPerfProfilerMixin ):
561+
562+ def setUp (self ):
563+ # This calls TestPerfTrampoline.setUp() which registers jit_cleanup
564+ super ().setUp ()
565+ # This calls TestPerfProfilerMixin.setUp()
566+ TestPerfProfilerMixin .setUp (self )
548567
549568 def run_perf (self , script_dir , script , activate_trampoline = True ):
550569 if activate_trampoline :
0 commit comments