Skip to content

Commit a165d28

Browse files
committed
refactor: use py::gil_not_used()
Signed-off-by: Henry Schreiner <[email protected]>
1 parent e5470eb commit a165d28

File tree

7 files changed

+31
-24
lines changed

7 files changed

+31
-24
lines changed

include/pybind11/detail/common.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ PYBIND11_WARNING_POP
464464
});
465465
}
466466
\endrst */
467-
#define PYBIND11_MODULE(name, variable) \
467+
#define PYBIND11_MODULE(name, variable, ...) \
468468
static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name) \
469469
PYBIND11_MAYBE_UNUSED; \
470470
PYBIND11_MAYBE_UNUSED \
@@ -473,7 +473,10 @@ PYBIND11_WARNING_POP
473473
PYBIND11_CHECK_PYTHON_VERSION \
474474
PYBIND11_ENSURE_INTERNALS_READY \
475475
auto m = ::pybind11::module_::create_extension_module( \
476-
PYBIND11_TOSTRING(name), nullptr, &PYBIND11_CONCAT(pybind11_module_def_, name)); \
476+
PYBIND11_TOSTRING(name), \
477+
nullptr, \
478+
&PYBIND11_CONCAT(pybind11_module_def_, name), \
479+
__VA_ARGS__); \
477480
try { \
478481
PYBIND11_CONCAT(pybind11_init_, name)(m); \
479482
return m.ptr(); \

include/pybind11/pybind11.h

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,8 @@ struct handle_type_name<cpp_function> {
12061206

12071207
PYBIND11_NAMESPACE_END(detail)
12081208

1209+
struct gil_not_used {};
1210+
12091211
/// Wrapper for Python extension modules
12101212
class module_ : public object {
12111213
public:
@@ -1299,15 +1301,6 @@ class module_ : public object {
12991301
PyModule_AddObject(ptr(), name, obj.inc_ref().ptr() /* steals a reference */);
13001302
}
13011303

1302-
/** \rst
1303-
Mark the module as not requiring the GIL in free-threaded Python builds.
1304-
\endrst */
1305-
void set_gil_not_used() {
1306-
#ifdef Py_GIL_DISABLED
1307-
PyUnstable_Module_SetGIL(m_ptr, Py_MOD_GIL_NOT_USED);
1308-
#endif
1309-
}
1310-
13111304
using module_def = PyModuleDef; // TODO: Can this be removed (it was needed only for Python 2)?
13121305

13131306
/** \rst
@@ -1316,6 +1309,20 @@ class module_ : public object {
13161309
``def`` should point to a statically allocated module_def.
13171310
\endrst */
13181311
static module_ create_extension_module(const char *name, const char *doc, module_def *def) {
1312+
return _create_extension_module(name, doc, def, false);
1313+
}
1314+
1315+
static module_
1316+
create_extension_module(const char *name, const char *doc, module_def *def, gil_not_used) {
1317+
return _create_extension_module(name, doc, def, true);
1318+
}
1319+
1320+
private:
1321+
static module_ _create_extension_module(const char *name,
1322+
const char *doc,
1323+
module_def *def,
1324+
PYBIND11_MAYBE_UNUSED bool gil_disabled) {
1325+
13191326
// module_def is PyModuleDef
13201327
// Placement new (not an allocation).
13211328
def = new (def)
@@ -1335,6 +1342,11 @@ class module_ : public object {
13351342
}
13361343
pybind11_fail("Internal error in module_::create_extension_module()");
13371344
}
1345+
#ifdef Py_GIL_DISABLED
1346+
if (gil_disabled) {
1347+
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
1348+
}
1349+
#endif
13381350
// TODO: Should be reinterpret_steal for Python 3, but Python also steals it again when
13391351
// returned from PyInit_...
13401352
// For Python 2, reinterpret_borrow was correct.

tests/eigen_tensor_avoid_stl_array.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "test_eigen_tensor.inl"
1313

14-
PYBIND11_MODULE(eigen_tensor_avoid_stl_array, m) {
15-
m.set_gil_not_used();
14+
PYBIND11_MODULE(eigen_tensor_avoid_stl_array, m, pybind11::gil_not_used()) {
1615
eigen_tensor_test::test_module(m);
1716
}

tests/pybind11_cross_module_tests.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
#include <numeric>
1717
#include <utility>
1818

19-
PYBIND11_MODULE(pybind11_cross_module_tests, m) {
20-
m.set_gil_not_used();
21-
19+
PYBIND11_MODULE(pybind11_cross_module_tests, m, py::gil_not_used()) {
2220
m.doc() = "pybind11 cross-module test module";
2321

2422
// test_local_bindings.py tests:

tests/pybind11_tests.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ const char *cpp_std() {
7575
#endif
7676
}
7777

78-
PYBIND11_MODULE(pybind11_tests, m) {
79-
m.set_gil_not_used();
80-
78+
PYBIND11_MODULE(pybind11_tests, m, py::gil_not_used()) {
8179
m.doc() = "pybind11 test module";
8280

8381
// Intentionally kept minimal to not create a maintenance chore

tests/test_cmake_build/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <pybind11/pybind11.h>
22
namespace py = pybind11;
33

4-
PYBIND11_MODULE(test_cmake_build, m) {
5-
m.set_gil_not_used();
4+
PYBIND11_MODULE(test_cmake_build, m, py::gil_not_used()) {
65
m.def("add", [](int i, int j) { return i + j; });
76
}

tests/test_embed/external_module.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ namespace py = pybind11;
66
* modules aren't preserved over a finalize/initialize.
77
*/
88

9-
PYBIND11_MODULE(external_module, m) {
9+
PYBIND11_MODULE(external_module, m, py::gil_not_used()) {
1010
class A {
1111
public:
1212
explicit A(int value) : v{value} {};
1313
int v;
1414
};
1515

16-
m.set_gil_not_used();
17-
1816
py::class_<A>(m, "A").def(py::init<int>()).def_readwrite("value", &A::v);
1917

2018
m.def("internals_at",

0 commit comments

Comments
 (0)