diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 4bb39c8014..0661f209e3 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -257,11 +257,14 @@ // 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 (other than old iOS targets). +// This can be overridden by the user with -DPYBIND11_HAS_SUBINTERPRETER_SUPPORT=1 or 0 #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 +#else +# if PYBIND11_HAS_SUBINTERPRETER_SUPPORT == 0 +# undef PYBIND11_HAS_SUBINTERPRETER_SUPPORT # endif #endif diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 9ae4f08da2..123460308d 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() { -#if PYBIND11_HAS_SUBINTERPRETER_SUPPORT +#ifdef 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 c326e32a07..a1bef5a124 100644 --- a/include/pybind11/subinterpreter.h +++ b/include/pybind11/subinterpreter.h @@ -15,7 +15,7 @@ #include -#if !PYBIND11_HAS_SUBINTERPRETER_SUPPORT +#ifndef 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 0b7c79151a..614967c54d 100644 --- a/tests/mod_per_interpreter_gil.cpp +++ b/tests/mod_per_interpreter_gil.cpp @@ -12,5 +12,9 @@ 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; +#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT + m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = true; +#else + m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = false; +#endif } diff --git a/tests/mod_shared_interpreter_gil.cpp b/tests/mod_shared_interpreter_gil.cpp index 6f0ab92838..c6ae1725fe 100644 --- a/tests/mod_shared_interpreter_gil.cpp +++ b/tests/mod_shared_interpreter_gil.cpp @@ -9,5 +9,9 @@ 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; +#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT + m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = true; +#else + m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = false; +#endif } diff --git a/tests/test_embed/test_subinterpreter.cpp b/tests/test_embed/test_subinterpreter.cpp index 59cc5b44ca..9614a76cd5 100644 --- a/tests/test_embed/test_subinterpreter.cpp +++ b/tests/test_embed/test_subinterpreter.cpp @@ -1,5 +1,5 @@ #include -#if PYBIND11_HAS_SUBINTERPRETER_SUPPORT +#ifdef 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 6fb010e5f3..a02a27547b 100644 --- a/tests/test_multiple_interpreters.py +++ b/tests/test_multiple_interpreters.py @@ -27,7 +27,7 @@ def test_independent_subinterpreters(): m = pytest.importorskip("mod_per_interpreter_gil") - if not m.PYBIND11_HAS_SUBINTERPRETER_SUPPORT: + if not m.defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT: pytest.skip("Does not have subinterpreter support compiled in") code = """ @@ -101,7 +101,7 @@ def test_dependent_subinterpreters(): m = pytest.importorskip("mod_shared_interpreter_gil") - if not m.PYBIND11_HAS_SUBINTERPRETER_SUPPORT: + if not m.defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT: pytest.skip("Does not have subinterpreter support compiled in") code = """