Skip to content

fix: follow rest of pybind11 closer with PYBIND11_HAS_SUBINTERPRETER_SUPPORT #5710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ inline std::atomic<int> &get_num_interpreters_seen() {

template <typename InternalsType>
inline std::unique_ptr<InternalsType> *&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.
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/subinterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#include <stdexcept>

#if !PYBIND11_HAS_SUBINTERPRETER_SUPPORT
#ifndef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
# error "This platform does not support subinterpreters, do not include this file."
#endif

Expand Down
6 changes: 5 additions & 1 deletion tests/mod_per_interpreter_gil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ PYBIND11_MODULE(mod_per_interpreter_gil,
py::multiple_interpreters::per_interpreter_gil()) {
m.def("internals_at",
[]() { return reinterpret_cast<uintptr_t>(&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
}
6 changes: 5 additions & 1 deletion tests/mod_shared_interpreter_gil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uintptr_t>(&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
}
2 changes: 1 addition & 1 deletion tests/test_embed/test_subinterpreter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <pybind11/embed.h>
#if PYBIND11_HAS_SUBINTERPRETER_SUPPORT
#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
# include <pybind11/subinterpreter.h>

// Silence MSVC C++17 deprecation warning from Catch regarding std::uncaught_exceptions (up to
Expand Down
4 changes: 2 additions & 2 deletions tests/test_multiple_interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down Expand Up @@ -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 = """
Expand Down
Loading