Skip to content

Commit 4f9313e

Browse files
committed
pythongh-141579: Fix perf_jit backend in sys.activate_stack_trampoline()
The perf_jit backend was unreachable due to incorrect if-else nesting in the backend selection logic. The else-if checking for "perf_jit" was nested inside the if statement checking for "perf", making it impossible to activate the perf_jit trampoline.
1 parent 46f11b3 commit 4f9313e

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

Lib/test/test_perf_profiler.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,24 @@ def test_sys_api_get_status(self):
238238
"""
239239
assert_python_ok("-c", code, PYTHON_JIT="0")
240240

241+
def test_sys_api_perf_jit_backend(self):
242+
code = """if 1:
243+
import sys
244+
sys.activate_stack_trampoline("perf_jit")
245+
assert sys.is_stack_trampoline_active() is True
246+
sys.deactivate_stack_trampoline()
247+
assert sys.is_stack_trampoline_active() is False
248+
"""
249+
assert_python_ok("-c", code, PYTHON_JIT="0")
250+
251+
def test_sys_api_with_existing_perf_jit_trampoline(self):
252+
code = """if 1:
253+
import sys
254+
sys.activate_stack_trampoline("perf_jit")
255+
sys.activate_stack_trampoline("perf_jit")
256+
"""
257+
assert_python_ok("-c", code, PYTHON_JIT="0")
258+
241259

242260
def is_unwinding_reliable_with_frame_pointers():
243261
cflags = sysconfig.get_config_var("PY_CORE_CFLAGS")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`sys.activate_stack_trampoline` to properly support the
2+
``perf_jit`` backend. Patch by Pablo Galindo.

Python/sysmodule.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,14 +2378,14 @@ sys_activate_stack_trampoline_impl(PyObject *module, const char *backend)
23782378
return NULL;
23792379
}
23802380
}
2381-
else if (strcmp(backend, "perf_jit") == 0) {
2382-
_PyPerf_Callbacks cur_cb;
2383-
_PyPerfTrampoline_GetCallbacks(&cur_cb);
2384-
if (cur_cb.write_state != _Py_perfmap_jit_callbacks.write_state) {
2385-
if (_PyPerfTrampoline_SetCallbacks(&_Py_perfmap_jit_callbacks) < 0 ) {
2386-
PyErr_SetString(PyExc_ValueError, "can't activate perf jit trampoline");
2387-
return NULL;
2388-
}
2381+
}
2382+
else if (strcmp(backend, "perf_jit") == 0) {
2383+
_PyPerf_Callbacks cur_cb;
2384+
_PyPerfTrampoline_GetCallbacks(&cur_cb);
2385+
if (cur_cb.write_state != _Py_perfmap_jit_callbacks.write_state) {
2386+
if (_PyPerfTrampoline_SetCallbacks(&_Py_perfmap_jit_callbacks) < 0 ) {
2387+
PyErr_SetString(PyExc_ValueError, "can't activate perf jit trampoline");
2388+
return NULL;
23892389
}
23902390
}
23912391
}

0 commit comments

Comments
 (0)