Skip to content

Commit 9afc9c4

Browse files
b-passhenryiii
andauthored
feat: change PYBIND11_EMBEDDED_MODULE to multiphase init (#5665)
* Move embedded modules to multiphase init So that they too can support multi-interpreter and nogil tags * Update the multiple interpreter test for embedded module changes * Add a note to embedded module docs about the new tags * Oops, missed a warning pop * Remove unused variable * Update ci.yml * Fix this embedded GIL test for free-threading * Oops, need to use ptr() here * This test created a subinterpreter when PYBIND11_SUBINTERPRETER_SUPPORT was off So the fix is really this test should not be run in these older versions at all. The hang was a GIL issue between the subinterpreters during pybind11::exception::what(). * fix: standard mutex for 3.13t Signed-off-by: Henry Schreiner <[email protected]> --------- Signed-off-by: Henry Schreiner <[email protected]> Co-authored-by: Henry Schreiner <[email protected]>
1 parent 094343c commit 9afc9c4

File tree

6 files changed

+69
-20
lines changed

6 files changed

+69
-20
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ jobs:
3434
python:
3535
- '3.8'
3636
- '3.13'
37+
- '3.13t'
3738
- '3.14'
39+
- '3.14t'
3840
- 'pypy-3.10'
3941
- 'pypy-3.11'
4042
- 'graalpy-24.2'

docs/advanced/embedding.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ naturally:
212212
assert(locals["message"].cast<std::string>() == "1 + 2 = 3");
213213
}
214214
215+
``PYBIND11_EMBEDDED_MODULE`` also accepts
216+
:func:`py::mod_gil_not_used()`,
217+
:func:`py::multiple_interpreters::per_interpreter_gil()`, and
218+
:func:`py::multiple_interpreters::shared_gil()` tags just like ``PYBIND11_MODULE``.
219+
See :ref:`misc_subinterp` and :ref:`misc_free_threading` for more information.
215220

216221
Interpreter lifetime
217222
====================

docs/advanced/misc.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ following checklist.
155155
within pybind11 that will throw exceptions on certain GIL handling errors
156156
(reference counting operations).
157157

158+
.. _misc_free_threading:
159+
158160
Free-threading support
159161
==================================================================
160162

@@ -178,6 +180,8 @@ your code is thread safe. Modules must still be built against the Python free-t
178180
enable free-threading, even if they specify this tag. Adding this tag does not break
179181
compatibility with non-free-threaded Python.
180182

183+
.. _misc_subinterp:
184+
181185
Sub-interpreter support
182186
==================================================================
183187

include/pybind11/embed.h

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,43 @@
3838
});
3939
}
4040
\endrst */
41-
#define PYBIND11_EMBEDDED_MODULE(name, variable) \
41+
PYBIND11_WARNING_PUSH
42+
PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")
43+
#define PYBIND11_EMBEDDED_MODULE(name, variable, ...) \
4244
static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name); \
45+
static ::pybind11::module_::slots_array PYBIND11_CONCAT(pybind11_module_slots_, name); \
46+
static int PYBIND11_CONCAT(pybind11_exec_, name)(PyObject *); \
4347
static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \
4448
static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
45-
auto m = ::pybind11::module_::create_extension_module( \
46-
PYBIND11_TOSTRING(name), nullptr, &PYBIND11_CONCAT(pybind11_module_def_, name)); \
49+
static auto result = []() { \
50+
auto &slots = PYBIND11_CONCAT(pybind11_module_slots_, name); \
51+
slots[0] = {Py_mod_exec, \
52+
reinterpret_cast<void *>(&PYBIND11_CONCAT(pybind11_exec_, name))}; \
53+
slots[1] = {0, nullptr}; \
54+
return ::pybind11::module_::initialize_multiphase_module_def( \
55+
PYBIND11_TOSTRING(name), \
56+
nullptr, \
57+
&PYBIND11_CONCAT(pybind11_module_def_, name), \
58+
slots, \
59+
##__VA_ARGS__); \
60+
}(); \
61+
return result.ptr(); \
62+
} \
63+
PYBIND11_EMBEDDED_MODULE_IMPL(name) \
64+
::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name)( \
65+
PYBIND11_TOSTRING(name), PYBIND11_CONCAT(pybind11_init_impl_, name)); \
66+
int PYBIND11_CONCAT(pybind11_exec_, name)(PyObject * pm) { \
4767
try { \
68+
auto m = pybind11::reinterpret_borrow<::pybind11::module_>(pm); \
4869
PYBIND11_CONCAT(pybind11_init_, name)(m); \
49-
return m.ptr(); \
70+
return 0; \
5071
} \
5172
PYBIND11_CATCH_INIT_EXCEPTIONS \
52-
return nullptr; \
73+
return -1; \
5374
} \
54-
PYBIND11_EMBEDDED_MODULE_IMPL(name) \
55-
::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name)( \
56-
PYBIND11_TOSTRING(name), PYBIND11_CONCAT(pybind11_init_impl_, name)); \
5775
void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ \
5876
& variable) // NOLINT(bugprone-macro-parentheses)
77+
PYBIND11_WARNING_POP
5978

6079
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
6180
PYBIND11_NAMESPACE_BEGIN(detail)

include/pybind11/pybind11.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ class module_ : public object {
14841484
using slots_array = std::array<PyModuleDef_Slot, 4>;
14851485

14861486
/** \rst
1487-
Initialized a module def for use with multi-phase module initialization.
1487+
Initialize a module def for use with multi-phase module initialization.
14881488
14891489
``def`` should point to a statically allocated module_def.
14901490
``slots`` must already contain a Py_mod_exec or Py_mod_create slot and will be filled with

tests/test_embed/test_interpreter.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class test_override_cache_helper_trampoline : public test_override_cache_helper
5555
int func() override { PYBIND11_OVERRIDE(int, test_override_cache_helper, func); }
5656
};
5757

58-
PYBIND11_EMBEDDED_MODULE(widget_module, m) {
58+
PYBIND11_EMBEDDED_MODULE(widget_module, m, py::multiple_interpreters::per_interpreter_gil()) {
5959
py::class_<Widget, PyWidget>(m, "Widget")
6060
.def(py::init<std::string>())
6161
.def_property_readonly("the_message", &Widget::the_message);
@@ -336,6 +336,7 @@ TEST_CASE("Restart the interpreter") {
336336
REQUIRE(py_widget.attr("the_message").cast<std::string>() == "Hello after restart");
337337
}
338338

339+
#if defined(PYBIND11_SUBINTERPRETER_SUPPORT)
339340
TEST_CASE("Subinterpreter") {
340341
py::module_::import("external_module"); // in the main interpreter
341342

@@ -347,6 +348,10 @@ TEST_CASE("Subinterpreter") {
347348

348349
REQUIRE(m.attr("add")(1, 2).cast<int>() == 3);
349350
}
351+
352+
auto main_int
353+
= py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>();
354+
350355
REQUIRE(has_state_dict_internals_obj());
351356
REQUIRE(has_pybind11_internals_static());
352357

@@ -359,7 +364,6 @@ TEST_CASE("Subinterpreter") {
359364
// Subinterpreters get their own copy of builtins.
360365
REQUIRE_FALSE(has_state_dict_internals_obj());
361366

362-
#if defined(PYBIND11_SUBINTERPRETER_SUPPORT) && PY_VERSION_HEX >= 0x030C0000
363367
// internals hasn't been populated yet, but will be different for the subinterpreter
364368
REQUIRE_FALSE(has_pybind11_internals_static());
365369

@@ -369,14 +373,12 @@ TEST_CASE("Subinterpreter") {
369373
py::detail::get_internals();
370374
REQUIRE(has_pybind11_internals_static());
371375
REQUIRE(get_details_as_uintptr() == ext_int);
372-
#else
373-
// This static is still defined
374-
REQUIRE(has_pybind11_internals_static());
375-
#endif
376+
REQUIRE(main_int != ext_int);
376377

377378
// Modules tags should be gone.
378379
REQUIRE_FALSE(py::hasattr(py::module_::import("__main__"), "tag"));
379380
{
381+
REQUIRE_NOTHROW(py::module_::import("widget_module"));
380382
auto m = py::module_::import("widget_module");
381383
REQUIRE_FALSE(py::hasattr(m, "extension_module_tag"));
382384

@@ -397,7 +399,6 @@ TEST_CASE("Subinterpreter") {
397399
REQUIRE(has_state_dict_internals_obj());
398400
}
399401

400-
#if defined(PYBIND11_SUBINTERPRETER_SUPPORT)
401402
TEST_CASE("Multiple Subinterpreters") {
402403
// Make sure the module is in the main interpreter and save its pointer
403404
auto *main_ext = py::module_::import("external_module").ptr();
@@ -512,10 +513,11 @@ TEST_CASE("Per-Subinterpreter GIL") {
512513

513514
// we have switched to the new interpreter and released the main gil
514515

515-
// widget_module did not provide the mod_per_interpreter_gil tag, so it cannot be imported
516+
// trampoline_module did not provide the per_interpreter_gil tag, so it cannot be
517+
// imported
516518
bool caught = false;
517519
try {
518-
py::module_::import("widget_module");
520+
py::module_::import("trampoline_module");
519521
} catch (pybind11::error_already_set &pe) {
520522
T_REQUIRE(pe.matches(PyExc_ImportError));
521523
std::string msg(pe.what());
@@ -525,6 +527,9 @@ TEST_CASE("Per-Subinterpreter GIL") {
525527
}
526528
T_REQUIRE(caught);
527529

530+
// widget_module did provide the per_interpreter_gil tag, so it this does not throw
531+
py::module_::import("widget_module");
532+
528533
T_REQUIRE(!py::hasattr(py::module_::import("external_module"), "multi_interp"));
529534
py::module_::import("external_module").attr("multi_interp") = std::to_string(num);
530535

@@ -547,8 +552,8 @@ TEST_CASE("Per-Subinterpreter GIL") {
547552

548553
Py_EndInterpreter(sub);
549554

550-
PyThreadState_Swap(
551-
main_tstate); // switch back so the scoped_acquire can release the GIL properly
555+
// switch back so the scoped_acquire can release the GIL properly
556+
PyThreadState_Swap(main_tstate);
552557
};
553558

554559
std::thread t1(thread_main, 1);
@@ -622,12 +627,26 @@ TEST_CASE("Threads") {
622627

623628
{
624629
py::gil_scoped_release gil_release{};
630+
#if defined(Py_GIL_DISABLED) && PY_VERSION_HEX < 0x030E0000
631+
std::mutex mutex;
632+
#endif
625633

626634
auto threads = std::vector<std::thread>();
627635
for (auto i = 0; i < num_threads; ++i) {
628636
threads.emplace_back([&]() {
629637
py::gil_scoped_acquire gil{};
638+
#ifdef Py_GIL_DISABLED
639+
# if PY_VERSION_HEX < 0x030E0000
640+
std::lock_guard<std::mutex> lock(mutex);
641+
locals["count"] = locals["count"].cast<int>() + 1;
642+
# else
643+
Py_BEGIN_CRITICAL_SECTION(locals.ptr());
630644
locals["count"] = locals["count"].cast<int>() + 1;
645+
Py_END_CRITICAL_SECTION();
646+
# endif
647+
#else
648+
locals["count"] = locals["count"].cast<int>() + 1;
649+
#endif
631650
});
632651
}
633652

0 commit comments

Comments
 (0)