diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5f0638007..0778e244e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -92,7 +92,7 @@ jobs: cmake-args: -DCMAKE_CXX_STANDARD=20 -DPYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION=ON - runs-on: ubuntu-latest python-version: '3.14' - cmake-args: -DCMAKE_CXX_STANDARD=14 + cmake-args: -DCMAKE_CXX_STANDARD=14 -DCMAKE_CXX_FLAGS="-DPYBIND11_HAS_SUBINTERPRETER_SUPPORT=0" - runs-on: ubuntu-latest python-version: 'pypy-3.10' cmake-args: -DCMAKE_CXX_STANDARD=14 diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 096d301b3..7d90054ea 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -256,9 +256,13 @@ // Slightly faster code paths are available when PYBIND11_HAS_SUBINTERPRETER_SUPPORT is *not* // defined, so avoid defining it for implementations that do not support subinterpreters. However, -// defining it unnecessarily is not expected to break anything. -#if PY_VERSION_HEX >= 0x030C0000 && !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON) -# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT +// defining it unnecessarily is not expected to break anything (other than old iOS targets). +#ifndef PYBIND11_HAS_SUBINTERPRETER_SUPPORT +# if PY_VERSION_HEX >= 0x030C0000 && !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON) +# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT 1 +# else +# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT 0 +# endif #endif // 3.12 Compatibility diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 123460308..9ae4f08da 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -324,7 +324,7 @@ inline std::atomic &get_num_interpreters_seen() { template inline std::unique_ptr *&get_internals_pp() { -#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT +#if PYBIND11_HAS_SUBINTERPRETER_SUPPORT if (get_num_interpreters_seen() > 1) { // Internals is one per interpreter. When multiple interpreters are alive in different // threads we have to allow them to have different internals, so we need a thread_local. diff --git a/include/pybind11/subinterpreter.h b/include/pybind11/subinterpreter.h index 784e09c53..c326e32a0 100644 --- a/include/pybind11/subinterpreter.h +++ b/include/pybind11/subinterpreter.h @@ -15,7 +15,7 @@ #include -#if !defined(PYBIND11_HAS_SUBINTERPRETER_SUPPORT) +#if !PYBIND11_HAS_SUBINTERPRETER_SUPPORT # error "This platform does not support subinterpreters, do not include this file." #endif diff --git a/tests/mod_per_interpreter_gil.cpp b/tests/mod_per_interpreter_gil.cpp index 9c7bba875..0b7c79151 100644 --- a/tests/mod_per_interpreter_gil.cpp +++ b/tests/mod_per_interpreter_gil.cpp @@ -12,4 +12,5 @@ PYBIND11_MODULE(mod_per_interpreter_gil, py::multiple_interpreters::per_interpreter_gil()) { m.def("internals_at", []() { return reinterpret_cast(&py::detail::get_internals()); }); + m.attr("PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = PYBIND11_HAS_SUBINTERPRETER_SUPPORT; } diff --git a/tests/mod_shared_interpreter_gil.cpp b/tests/mod_shared_interpreter_gil.cpp index 4f5f91e29..6f0ab9283 100644 --- a/tests/mod_shared_interpreter_gil.cpp +++ b/tests/mod_shared_interpreter_gil.cpp @@ -9,4 +9,5 @@ namespace py = pybind11; PYBIND11_MODULE(mod_shared_interpreter_gil, m, py::multiple_interpreters::shared_gil()) { m.def("internals_at", []() { return reinterpret_cast(&py::detail::get_internals()); }); + m.attr("PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = PYBIND11_HAS_SUBINTERPRETER_SUPPORT; } diff --git a/tests/test_embed/test_subinterpreter.cpp b/tests/test_embed/test_subinterpreter.cpp index 9614a76cd..59cc5b44c 100644 --- a/tests/test_embed/test_subinterpreter.cpp +++ b/tests/test_embed/test_subinterpreter.cpp @@ -1,5 +1,5 @@ #include -#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT +#if PYBIND11_HAS_SUBINTERPRETER_SUPPORT # include // Silence MSVC C++17 deprecation warning from Catch regarding std::uncaught_exceptions (up to diff --git a/tests/test_multiple_interpreters.py b/tests/test_multiple_interpreters.py index 8394e9c77..6fb010e5f 100644 --- a/tests/test_multiple_interpreters.py +++ b/tests/test_multiple_interpreters.py @@ -23,9 +23,12 @@ def test_independent_subinterpreters(): elif sys.version_info >= (3, 12): import _xxsubinterpreters as interpreters else: - pytest.skip("Test requires a the interpreters stdlib module") + pytest.skip("Test requires the interpreters stdlib module") - import mod_per_interpreter_gil as m + m = pytest.importorskip("mod_per_interpreter_gil") + + if not m.PYBIND11_HAS_SUBINTERPRETER_SUPPORT: + pytest.skip("Does not have subinterpreter support compiled in") code = """ import mod_per_interpreter_gil as m @@ -94,9 +97,12 @@ def test_dependent_subinterpreters(): elif sys.version_info >= (3, 12): import _xxsubinterpreters as interpreters else: - pytest.skip("Test requires a the interpreters stdlib module") + pytest.skip("Test requires the interpreters stdlib module") - import mod_shared_interpreter_gil as m + m = pytest.importorskip("mod_shared_interpreter_gil") + + if not m.PYBIND11_HAS_SUBINTERPRETER_SUPPORT: + pytest.skip("Does not have subinterpreter support compiled in") code = """ import mod_shared_interpreter_gil as m