(self)
| 213 | |
| 214 | @unittest.skipIf(support.check_bolt_optimized(), "fails on BOLT instrumented binaries") |
| 215 | def test_sys_api(self): |
| 216 | for define_eval_hook in (False, True): |
| 217 | code = """if 1: |
| 218 | import sys |
| 219 | def foo(): |
| 220 | pass |
| 221 | |
| 222 | def spam(): |
| 223 | pass |
| 224 | |
| 225 | def bar(): |
| 226 | sys.deactivate_stack_trampoline() |
| 227 | foo() |
| 228 | sys.activate_stack_trampoline("perf") |
| 229 | spam() |
| 230 | |
| 231 | def baz(): |
| 232 | bar() |
| 233 | |
| 234 | sys.activate_stack_trampoline("perf") |
| 235 | baz() |
| 236 | """ |
| 237 | if define_eval_hook: |
| 238 | set_eval_hook = """if 1: |
| 239 | import _testinternalcapi |
| 240 | _testinternalcapi.set_eval_frame_record([]) |
| 241 | """ |
| 242 | code = set_eval_hook + code |
| 243 | with temp_dir() as script_dir: |
| 244 | script = make_script(script_dir, "perftest", code) |
| 245 | env = {**os.environ, "PYTHON_JIT": "0"} |
| 246 | with subprocess.Popen( |
| 247 | [sys.executable, script], |
| 248 | text=True, |
| 249 | stderr=subprocess.PIPE, |
| 250 | stdout=subprocess.PIPE, |
| 251 | env=env, |
| 252 | ) as process: |
| 253 | stdout, stderr = process.communicate() |
| 254 | |
| 255 | self.assertEqual(stderr, "") |
| 256 | self.assertEqual(stdout, "") |
| 257 | |
| 258 | perf_file = pathlib.Path(f"/tmp/perf-{process.pid}.map") |
| 259 | self.assertTrue(perf_file.exists()) |
| 260 | perf_file_contents = perf_file.read_text() |
| 261 | self.assertNotIn(f"py::foo:{script}", perf_file_contents) |
| 262 | self.assertIn(f"py::spam:{script}", perf_file_contents) |
| 263 | self.assertIn(f"py::bar:{script}", perf_file_contents) |
| 264 | self.assertIn(f"py::baz:{script}", perf_file_contents) |
| 265 | |
| 266 | def test_sys_api_with_existing_trampoline(self): |
| 267 | code = """if 1: |
nothing calls this directly
no test coverage detected