Skip to content

Commit 6972597

Browse files
authored
docs: show nogil in most examples (#5770)
Created using [mini-swe-agent](https://mini-swe-agent.com) and the propmt: I'd like to find usages of PYBIND11_MODULE in the docs folder and add py::mod_gil_not_used() as a third argument if there ar e only two arguments. These are examples, and it's really a good idea to always include that now. I removed a few of the changes. Signed-off-by: Henry Schreiner <[email protected]>
1 parent 33533ff commit 6972597

File tree

9 files changed

+15
-16
lines changed

9 files changed

+15
-16
lines changed

docs/advanced/cast/custom.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ type is explicitly allowed.
108108
} // namespace pybind11
109109
110110
// Bind the negate function
111-
PYBIND11_MODULE(docs_advanced_cast_custom, m) { m.def("negate", user_space::negate); }
111+
PYBIND11_MODULE(docs_advanced_cast_custom, m, py::mod_gil_not_used()) { m.def("negate", user_space::negate); }
112112
113113
.. note::
114114

docs/advanced/cast/functional.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ trivial to generate binding code for all of these functions.
5656
5757
#include <pybind11/functional.h>
5858
59-
PYBIND11_MODULE(example, m) {
59+
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
6060
m.def("func_arg", &func_arg);
6161
m.def("func_ret", &func_ret);
6262
m.def("func_cpp", &func_cpp);

docs/advanced/classes.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Normally, the binding code for these classes would look as follows:
4545

4646
.. code-block:: cpp
4747
48-
PYBIND11_MODULE(example, m) {
48+
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
4949
py::class_<Animal>(m, "Animal")
5050
.def("go", &Animal::go);
5151
@@ -112,7 +112,7 @@ The binding code also needs a few minor adaptations (highlighted):
112112
.. code-block:: cpp
113113
:emphasize-lines: 2,3
114114
115-
PYBIND11_MODULE(example, m) {
115+
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
116116
py::class_<Animal, PyAnimal /* <--- trampoline */, py::smart_holder>(m, "Animal")
117117
.def(py::init<>())
118118
.def("go", &Animal::go);
@@ -774,7 +774,7 @@ to Python.
774774
775775
#include <pybind11/operators.h>
776776
777-
PYBIND11_MODULE(example, m) {
777+
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
778778
py::class_<Vector2>(m, "Vector2")
779779
.def(py::init<float, float>())
780780
.def(py::self + py::self)

docs/advanced/pycpp/numpy.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ expects the type followed by field names:
217217
};
218218
219219
// ...
220-
PYBIND11_MODULE(test, m) {
220+
PYBIND11_MODULE(test, m, py::mod_gil_not_used()) {
221221
// ...
222222
223223
PYBIND11_NUMPY_DTYPE(A, x, y);
@@ -351,7 +351,7 @@ simply using ``vectorize``).
351351
return result;
352352
}
353353
354-
PYBIND11_MODULE(test, m) {
354+
PYBIND11_MODULE(test, m, py::mod_gil_not_used()) {
355355
m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
356356
}
357357

docs/basics.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ a file named :file:`example.cpp` with the following contents:
112112
return i + j;
113113
}
114114
115-
PYBIND11_MODULE(example, m) {
115+
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
116116
m.doc() = "pybind11 example plugin"; // optional module docstring
117117
118118
m.def("add", &add, "A function that adds two numbers");
@@ -288,7 +288,7 @@ converted using the function ``py::cast``.
288288

289289
.. code-block:: cpp
290290
291-
PYBIND11_MODULE(example, m) {
291+
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
292292
m.attr("the_answer") = 42;
293293
py::object world = py::cast("World");
294294
m.attr("what") = world;

docs/benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def generate_dummy_code_pybind11(nclasses=10):
3333
result = "#include <pybind11/pybind11.h>\n\n"
3434
result += "namespace py = pybind11;\n\n"
3535
result += decl + "\n"
36-
result += "PYBIND11_MODULE(example, m) {\n"
36+
result += "PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {\n"
3737
result += bindings
3838
result += "}"
3939
return result

docs/benchmark.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Here is an example of the binding code for one class:
3131
};
3232
...
3333
34-
PYBIND11_MODULE(example, m) {
34+
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
3535
...
3636
py::class_<cl034>(m, "cl034")
3737
.def("fn_000", &cl034::fn_000)

docs/classes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The binding code for ``Pet`` looks as follows:
2727
2828
namespace py = pybind11;
2929
30-
PYBIND11_MODULE(example, m) {
30+
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
3131
py::class_<Pet>(m, "Pet")
3232
.def(py::init<const std::string &>())
3333
.def("setName", &Pet::setName)
@@ -480,7 +480,7 @@ management. For example, ownership is inadvertently transferred here:
480480
std::shared_ptr<Child> child;
481481
};
482482
483-
PYBIND11_MODULE(example, m) {
483+
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
484484
py::class_<Child, std::shared_ptr<Child>>(m, "Child");
485485
486486
py::class_<Parent, std::shared_ptr<Parent>>(m, "Parent")

docs/faq.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ following example:
9090
void init_ex2(py::module_ &);
9191
/* ... */
9292
93-
PYBIND11_MODULE(example, m) {
93+
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
9494
init_ex1(m);
9595
init_ex2(m);
9696
/* ... */
@@ -235,8 +235,7 @@ been received, you must either explicitly interrupt execution by throwing
235235

236236
.. code-block:: cpp
237237
238-
PYBIND11_MODULE(example, m)
239-
{
238+
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
240239
m.def("long running_func", []()
241240
{
242241
for (;;) {

0 commit comments

Comments
 (0)