Skip to content

Commit 3e00945

Browse files
committed
🐛 fix free threaded detection in cmake
1 parent e9a145f commit 3e00945

6 files changed

Lines changed: 39 additions & 18 deletions

File tree

CMakeLists.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ if(DEFINED ENV{TOOLCHAIN})
1717
message(STATUS "TOOLCHAIN: $ENV{TOOLCHAIN}")
1818
endif()
1919

20+
list(APPEND c104_PRIVATE_DEFINITIONS VERSION_INFO=\"${C104_VERSION_INFO}\")
21+
2022
# ##############################################################################
2123
# pybind11 and Python3
2224
# ##############################################################################
@@ -31,6 +33,20 @@ if(MSVC)
3133
list(APPEND c104_tests_PRIVATE_LIBRARIES pybind11::windows_extras)
3234
endif()
3335
36+
# Run Python to check for GIL status
37+
execute_process(
38+
COMMAND
39+
${Python_EXECUTABLE} -c
40+
"import sysconfig; print(bool(sysconfig.get_config_var(\"Py_GIL_DISABLED\")) and 1 or 0)"
41+
OUTPUT_VARIABLE PYTHON_FREE_THREADED
42+
OUTPUT_STRIP_TRAILING_WHITESPACE)
43+
if(PYTHON_FREE_THREADED)
44+
list(APPEND c104_PRIVATE_DEFINITIONS Py_GIL_DISABLED=1)
45+
message(STATUS "Python is running without GIL (free threaded)")
46+
else()
47+
message(STATUS "Python is running with GIL (classic)")
48+
endif()
49+
3450
# ##############################################################################
3551
# atomic
3652
# ##############################################################################
@@ -150,7 +166,7 @@ if(c104_PRIVATE_LIBRARIES)
150166
target_link_libraries(_c104 PRIVATE ${c104_PRIVATE_LIBRARIES})
151167
endif()
152168

153-
target_compile_definitions(_c104 PRIVATE VERSION_INFO=\"${C104_VERSION_INFO}\")
169+
target_compile_definitions(_c104 PRIVATE ${c104_PRIVATE_DEFINITIONS})
154170

155171
# ##############################################################################
156172
# Tests with Catch2

bin/aarch64-build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ rm -rf /opt/c104/dist/*manylinux*.whl ; \
1212
/opt/python/cp311-cp311/bin/python3 -m pip wheel /opt/c104 ; \
1313
/opt/python/cp312-cp312/bin/python3 -m pip wheel /opt/c104 ; \
1414
/opt/python/cp313-cp313/bin/python3 -m pip wheel /opt/c104 ; \
15+
/opt/python/cp313-cp313t/bin/python3 -m pip wheel /opt/c104 ; \
1516
auditwheel repair ./c104-*-linux_aarch64.whl ; \
1617
mv ./wheelhouse/* /opt/c104/dist/
1718
"

bin/linux-build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ rm -rf /opt/c104/dist/*manylinux*.whl ; \
1111
/opt/python/cp311-cp311/bin/python3 -m pip wheel /opt/c104 ; \
1212
/opt/python/cp312-cp312/bin/python3 -m pip wheel /opt/c104 ; \
1313
/opt/python/cp313-cp313/bin/python3 -m pip wheel /opt/c104 ; \
14+
/opt/python/cp313-cp313t/bin/python3 -m pip wheel /opt/c104 ; \
1415
auditwheel repair ./c104-*-linux_x86_64.whl ; \
1516
mv ./wheelhouse/* /opt/c104/dist/
1617
"

bin/win-build.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
REM change working directory to repository directory
44
cd /D "%~dp0/.."
55

6+
py -3.13t -m pip wheel . -w dist
67
py -3.13 -m pip wheel . -w dist
78
py -3.12 -m pip wheel . -w dist
89
py -3.11 -m pip wheel . -w dist

c104/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
import sys
2+
import io
3+
import logging
24
from types import BuiltinFunctionType
35
from c104 import _c104
46

7+
# create logger instance
8+
logger = logging.getLogger("c104")
9+
logger.setLevel(logging.INFO)
10+
ch = logging.StreamHandler()
11+
formatter = logging.Formatter(
12+
fmt="[{asctime}] {levelname:<8} [{name}] {message}",
13+
datefmt="%Y-%m-%d %H:%M:%S",
14+
style="{"
15+
)
16+
ch.setFormatter(formatter)
17+
logger.addHandler(ch)
18+
19+
# test for unbuffered mode
20+
if isinstance(sys.stdout.buffer, io.BufferedWriter):
21+
logger.info("Python is running in buffered mode, messages will be printed with high delay! Set PYTHONUNBUFFERED=1 environment variable to enable unbuffered mode.")
22+
523
# Iterate over all attributes in the submodule
624
for attr_name, attr_value in vars(_c104).items():
725
# Check if the attribute is a class or a function

src/python.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,11 @@
4545
#else
4646
#define VERSION_INFO "embedded"
4747
#include <pybind11/embed.h>
48-
#define PY_MODULE(var) \
49-
PYBIND11_EMBEDDED_MODULE(c104, var, py::mod_gil_not_used())
48+
#define PY_MODULE(var) PYBIND11_EMBEDDED_MODULE(c104, var)
5049
#endif
5150

5251
using namespace pybind11::literals;
5352

54-
// set UNBUFFERED mode for correct order of stdout flush between python and c++
55-
struct EnvironmentInitializer {
56-
EnvironmentInitializer() {
57-
#if defined(_WIN32) || defined(_WIN64)
58-
_putenv("PYTHONUNBUFFERED=1");
59-
#else
60-
// Use setenv on Linux
61-
setenv("PYTHONUNBUFFERED", "1", 1);
62-
#endif
63-
}
64-
};
65-
66-
// Initialize the environment variable before main() is called
67-
static EnvironmentInitializer initializer;
68-
6953
// Bind Number with Template
7054
template <typename T>
7155
void bind_Number(py::module &m, const std::string &name,

0 commit comments

Comments
 (0)