Skip to content

Commit cd5307d

Browse files
authored
Fix AsyncioDebug section detection on Python 3.14 (#804)
Fixes #717.
1 parent 45586d7 commit cd5307d

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

cpython-unix/build-cpython.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ if [ -n "${PYTHON_MEETS_MINIMUM_VERSION_3_12}" ]; then
319319
patch -p1 -i ${ROOT}/patch-test-embed-prevent-segfault.patch
320320
fi
321321

322+
# Cherry-pick an upstream change in Python 3.15 to build _asyncio as
323+
# static (which we do anyway in our own fashion) and more importantly to
324+
# take this into account when finding the AsyncioDebug section.
325+
if [ "${PYTHON_MAJMIN_VERSION}" = 3.14 ]; then
326+
patch -p1 -i ${ROOT}/patch-python-3.14-asyncio-static.patch
327+
fi
328+
322329
# Most bits look at CFLAGS. But setup.py only looks at CPPFLAGS.
323330
# So we need to set both.
324331
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC -I${TOOLS_PATH}/deps/include -I${TOOLS_PATH}/deps/include/ncursesw"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
From b7d722547bcc9e92dca4837b9fdbe7457788820b Mon Sep 17 00:00:00 2001
2+
From: Kumar Aditya <[email protected]>
3+
Date: Wed, 16 Jul 2025 22:09:08 +0530
4+
Subject: [PATCH 1/1] gh-136669: build `_asyncio` as static module (#136670)
5+
6+
`_asyncio` is now built as a static module so that thread states can be accessed directly via registers and avoids the overhead of function call.
7+
---
8+
.../Library/2025-07-15-16-37-34.gh-issue-136669.Yexwah.rst | 1 +
9+
Modules/Setup.stdlib.in | 7 ++++++-
10+
Modules/_remote_debugging_module.c | 6 +++---
11+
3 files changed, 10 insertions(+), 4 deletions(-)
12+
create mode 100644 Misc/NEWS.d/next/Library/2025-07-15-16-37-34.gh-issue-136669.Yexwah.rst
13+
14+
diff --git a/Misc/NEWS.d/next/Library/2025-07-15-16-37-34.gh-issue-136669.Yexwah.rst b/Misc/NEWS.d/next/Library/2025-07-15-16-37-34.gh-issue-136669.Yexwah.rst
15+
new file mode 100644
16+
index 00000000000..0d93397ff35
17+
--- /dev/null
18+
+++ b/Misc/NEWS.d/next/Library/2025-07-15-16-37-34.gh-issue-136669.Yexwah.rst
19+
@@ -0,0 +1 @@
20+
+:mod:`!_asyncio` is now statically linked for improved performance.
21+
diff --git a/Modules/Setup.stdlib.in b/Modules/Setup.stdlib.in
22+
index 3a38a60a152..86c8eb27c0a 100644
23+
--- a/Modules/Setup.stdlib.in
24+
+++ b/Modules/Setup.stdlib.in
25+
@@ -32,7 +32,6 @@
26+
############################################################################
27+
# Modules that should always be present (POSIX and Windows):
28+
@MODULE_ARRAY_TRUE@array arraymodule.c
29+
-@MODULE__ASYNCIO_TRUE@_asyncio _asynciomodule.c
30+
@MODULE__BISECT_TRUE@_bisect _bisectmodule.c
31+
@MODULE__CSV_TRUE@_csv _csv.c
32+
@MODULE__HEAPQ_TRUE@_heapq _heapqmodule.c
33+
@@ -193,3 +192,9 @@
34+
# Limited API template modules; must be built as shared modules.
35+
@MODULE_XXLIMITED_TRUE@xxlimited xxlimited.c
36+
@MODULE_XXLIMITED_35_TRUE@xxlimited_35 xxlimited_35.c
37+
+
38+
+
39+
+# for performance
40+
+*static*
41+
+
42+
+@MODULE__ASYNCIO_TRUE@_asyncio _asynciomodule.c
43+
diff --git a/Modules/_remote_debugging_module.c b/Modules/_remote_debugging_module.c
44+
index d72031137e0..b50e5e403a1 100644
45+
--- a/Modules/_remote_debugging_module.c
46+
+++ b/Modules/_remote_debugging_module.c
47+
@@ -811,7 +811,7 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle)
48+
}
49+
#elif defined(__linux__)
50+
// On Linux, search for asyncio debug in executable or DLL
51+
- address = search_linux_map_for_section(handle, "AsyncioDebug", "_asyncio.cpython");
52+
+ address = search_linux_map_for_section(handle, "AsyncioDebug", "python");
53+
if (address == 0) {
54+
// Error out: 'python' substring covers both executable and DLL
55+
PyObject *exc = PyErr_GetRaisedException();
56+
@@ -820,10 +820,10 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle)
57+
}
58+
#elif defined(__APPLE__) && TARGET_OS_OSX
59+
// On macOS, try libpython first, then fall back to python
60+
- address = search_map_for_section(handle, "AsyncioDebug", "_asyncio.cpython");
61+
+ address = search_map_for_section(handle, "AsyncioDebug", "libpython");
62+
if (address == 0) {
63+
PyErr_Clear();
64+
- address = search_map_for_section(handle, "AsyncioDebug", "_asyncio.cpython");
65+
+ address = search_map_for_section(handle, "AsyncioDebug", "python");
66+
}
67+
if (address == 0) {
68+
// Error out: 'python' substring covers both executable and DLL
69+
--
70+
2.39.5 (Apple Git-154)
71+

0 commit comments

Comments
 (0)