Skip to content

Commit 4ed3f49

Browse files
committed
fix: allow subinterp support to be disabled
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 33fb533 commit 4ed3f49

File tree

8 files changed

+23
-11
lines changed

8 files changed

+23
-11
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ jobs:
9292
cmake-args: -DCMAKE_CXX_STANDARD=20 -DPYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION=ON
9393
- runs-on: ubuntu-latest
9494
python-version: '3.14'
95-
cmake-args: -DCMAKE_CXX_STANDARD=14
95+
cmake-args: -DCMAKE_CXX_STANDARD=14 -DCMAKE_CXX_FLAGS="-DPYBIND11_HAS_SUBINTERPRETER_SUPPORT=0"
9696
- runs-on: ubuntu-latest
9797
python-version: 'pypy-3.10'
9898
cmake-args: -DCMAKE_CXX_STANDARD=14

include/pybind11/detail/common.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,13 @@
256256

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,
259-
// defining it unnecessarily is not expected to break anything.
260-
#if PY_VERSION_HEX >= 0x030C0000 && !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
261-
# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT
259+
// defining it unnecessarily is not expected to break anything (other than old iOS targets).
260+
#ifndef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
261+
# if PY_VERSION_HEX >= 0x030C0000 && !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
262+
# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT 1
263+
# else
264+
# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT 0
265+
# endif
262266
#endif
263267

264268
// 3.12 Compatibility

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-
#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
327+
#if 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 !defined(PYBIND11_HAS_SUBINTERPRETER_SUPPORT)
18+
#if !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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ 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("DPYBIND11_HAS_SUBINTERPRETER_SUPPORT") = DPYBIND11_HAS_SUBINTERPRETER_SUPPORT;
1516
}

tests/mod_shared_interpreter_gil.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ 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("DPYBIND11_HAS_SUBINTERPRETER_SUPPORT") = DPYBIND11_HAS_SUBINTERPRETER_SUPPORT;
1213
}

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-
#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
2+
#if 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: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ def test_independent_subinterpreters():
2323
elif sys.version_info >= (3, 12):
2424
import _xxsubinterpreters as interpreters
2525
else:
26-
pytest.skip("Test requires a the interpreters stdlib module")
26+
pytest.skip("Test requires the interpreters stdlib module")
2727

28-
import mod_per_interpreter_gil as m
28+
m = pytest.importorskip("mod_per_interpreter_gil")
29+
30+
if not m.DPYBIND11_HAS_SUBINTERPRETER_SUPPORT:
31+
pytest.skip("Does not have subinterpreter support compiled in")
2932

3033
code = """
3134
import mod_per_interpreter_gil as m
@@ -94,9 +97,12 @@ def test_dependent_subinterpreters():
9497
elif sys.version_info >= (3, 12):
9598
import _xxsubinterpreters as interpreters
9699
else:
97-
pytest.skip("Test requires a the interpreters stdlib module")
100+
pytest.skip("Test requires the interpreters stdlib module")
101+
102+
m = pytest.importorskip("mod_shared_interpreter_gil")
98103

99-
import mod_shared_interpreter_gil as m
104+
if not m.DPYBIND11_HAS_SUBINTERPRETER_SUPPORT:
105+
pytest.skip("Does not have subinterpreter support compiled in")
100106

101107
code = """
102108
import mod_shared_interpreter_gil as m

0 commit comments

Comments
 (0)