Skip to content

Commit 9295c4a

Browse files
fix: follow rest of pybind11 closer with PYBIND11_HAS_SUBINTERPRETER_SUPPORT (#5710)
* fix: follow rest of pybind11 closer with PYBIND11_HAS_SUBINTERPRETER_SUPPORT Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> * style: pre-commit fixes --------- Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 7da1d53 commit 9295c4a

File tree

7 files changed

+20
-9
lines changed

7 files changed

+20
-9
lines changed

include/pybind11/detail/common.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,14 @@
257257
// Slightly faster code paths are available when PYBIND11_HAS_SUBINTERPRETER_SUPPORT is *not*
258258
// defined, so avoid defining it for implementations that do not support subinterpreters. However,
259259
// defining it unnecessarily is not expected to break anything (other than old iOS targets).
260+
// This can be overridden by the user with -DPYBIND11_HAS_SUBINTERPRETER_SUPPORT=1 or 0
260261
#ifndef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
261262
# if PY_VERSION_HEX >= 0x030C0000 && !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
262263
# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT 1
263-
# else
264-
# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT 0
264+
# endif
265+
#else
266+
# if PYBIND11_HAS_SUBINTERPRETER_SUPPORT == 0
267+
# undef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
265268
# endif
266269
#endif
267270

include/pybind11/detail/internals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ inline std::atomic<int> &get_num_interpreters_seen() {
324324

325325
template <typename InternalsType>
326326
inline std::unique_ptr<InternalsType> *&get_internals_pp() {
327-
#if PYBIND11_HAS_SUBINTERPRETER_SUPPORT
327+
#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
328328
if (get_num_interpreters_seen() > 1) {
329329
// Internals is one per interpreter. When multiple interpreters are alive in different
330330
// threads we have to allow them to have different internals, so we need a thread_local.

include/pybind11/subinterpreter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include <stdexcept>
1717

18-
#if !PYBIND11_HAS_SUBINTERPRETER_SUPPORT
18+
#ifndef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
1919
# error "This platform does not support subinterpreters, do not include this file."
2020
#endif
2121

tests/mod_per_interpreter_gil.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ PYBIND11_MODULE(mod_per_interpreter_gil,
1212
py::multiple_interpreters::per_interpreter_gil()) {
1313
m.def("internals_at",
1414
[]() { return reinterpret_cast<uintptr_t>(&py::detail::get_internals()); });
15-
m.attr("PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = PYBIND11_HAS_SUBINTERPRETER_SUPPORT;
15+
#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
16+
m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = true;
17+
#else
18+
m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = false;
19+
#endif
1620
}

tests/mod_shared_interpreter_gil.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ namespace py = pybind11;
99
PYBIND11_MODULE(mod_shared_interpreter_gil, m, py::multiple_interpreters::shared_gil()) {
1010
m.def("internals_at",
1111
[]() { return reinterpret_cast<uintptr_t>(&py::detail::get_internals()); });
12-
m.attr("PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = PYBIND11_HAS_SUBINTERPRETER_SUPPORT;
12+
#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
13+
m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = true;
14+
#else
15+
m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = false;
16+
#endif
1317
}

tests/test_embed/test_subinterpreter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <pybind11/embed.h>
2-
#if PYBIND11_HAS_SUBINTERPRETER_SUPPORT
2+
#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
33
# include <pybind11/subinterpreter.h>
44

55
// Silence MSVC C++17 deprecation warning from Catch regarding std::uncaught_exceptions (up to

tests/test_multiple_interpreters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_independent_subinterpreters():
2727

2828
m = pytest.importorskip("mod_per_interpreter_gil")
2929

30-
if not m.PYBIND11_HAS_SUBINTERPRETER_SUPPORT:
30+
if not m.defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT:
3131
pytest.skip("Does not have subinterpreter support compiled in")
3232

3333
code = """
@@ -101,7 +101,7 @@ def test_dependent_subinterpreters():
101101

102102
m = pytest.importorskip("mod_shared_interpreter_gil")
103103

104-
if not m.PYBIND11_HAS_SUBINTERPRETER_SUPPORT:
104+
if not m.defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT:
105105
pytest.skip("Does not have subinterpreter support compiled in")
106106

107107
code = """

0 commit comments

Comments
 (0)