Skip to content

Commit a1c0f8e

Browse files
committed
py::mod_gil_not_used() suggestion.
1 parent f53dcb5 commit a1c0f8e

File tree

6 files changed

+20
-22
lines changed

6 files changed

+20
-22
lines changed

include/pybind11/pybind11.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,15 @@ struct handle_type_name<cpp_function> {
12061206

12071207
PYBIND11_NAMESPACE_END(detail)
12081208

1209-
struct gil_not_used {};
1209+
// Use to activate Py_MOD_GIL_NOT_USED.
1210+
class mod_gil_not_used {
1211+
public:
1212+
mod_gil_not_used(bool flag = true) : flag_(flag) {}
1213+
bool flag() const { return flag_; }
1214+
1215+
private:
1216+
bool flag_;
1217+
};
12101218

12111219
/// Wrapper for Python extension modules
12121220
class module_ : public object {
@@ -1308,21 +1316,11 @@ class module_ : public object {
13081316
13091317
``def`` should point to a statically allocated module_def.
13101318
\endrst */
1311-
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-
bool gil_disabled) {
1325-
1319+
static module_ create_extension_module(const char *name,
1320+
const char *doc,
1321+
module_def *def,
1322+
mod_gil_not_used gil_not_used
1323+
= mod_gil_not_used(false)) {
13261324
// module_def is PyModuleDef
13271325
// Placement new (not an allocation).
13281326
def = new (def)
@@ -1342,7 +1340,7 @@ class module_ : public object {
13421340
}
13431341
pybind11_fail("Internal error in module_::create_extension_module()");
13441342
}
1345-
if (gil_disabled) {
1343+
if (gil_not_used.flag()) {
13461344
#ifdef Py_GIL_DISABLED
13471345
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
13481346
#endif

tests/eigen_tensor_avoid_stl_array.cpp

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

1212
#include "test_eigen_tensor.inl"
1313

14-
PYBIND11_MODULE(eigen_tensor_avoid_stl_array, m, pybind11::gil_not_used()) {
14+
PYBIND11_MODULE(eigen_tensor_avoid_stl_array, m, pybind11::mod_gil_not_used()) {
1515
eigen_tensor_test::test_module(m);
1616
}

tests/pybind11_cross_module_tests.cpp

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

19-
PYBIND11_MODULE(pybind11_cross_module_tests, m, py::gil_not_used()) {
19+
PYBIND11_MODULE(pybind11_cross_module_tests, m, py::mod_gil_not_used()) {
2020
m.doc() = "pybind11 cross-module test module";
2121

2222
// test_local_bindings.py tests:

tests/pybind11_tests.cpp

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

78-
PYBIND11_MODULE(pybind11_tests, m, py::gil_not_used()) {
78+
PYBIND11_MODULE(pybind11_tests, m, py::mod_gil_not_used()) {
7979
m.doc() = "pybind11 test module";
8080

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

tests/test_cmake_build/main.cpp

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

4-
PYBIND11_MODULE(test_cmake_build, m, py::gil_not_used()) {
4+
PYBIND11_MODULE(test_cmake_build, m, py::mod_gil_not_used()) {
55
m.def("add", [](int i, int j) { return i + j; });
66
}

tests/test_embed/external_module.cpp

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

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

0 commit comments

Comments
 (0)