From 6972597c9b0249ddfc1634baa95c35155bdad79b Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sun, 27 Jul 2025 23:08:11 -0600 Subject: [PATCH] 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 --- docs/advanced/cast/custom.rst | 2 +- docs/advanced/cast/functional.rst | 2 +- docs/advanced/classes.rst | 6 +++--- docs/advanced/pycpp/numpy.rst | 4 ++-- docs/basics.rst | 4 ++-- docs/benchmark.py | 2 +- docs/benchmark.rst | 2 +- docs/classes.rst | 4 ++-- docs/faq.rst | 5 ++--- 9 files changed, 15 insertions(+), 16 deletions(-) diff --git a/docs/advanced/cast/custom.rst b/docs/advanced/cast/custom.rst index 1de0f0a21..786192bee 100644 --- a/docs/advanced/cast/custom.rst +++ b/docs/advanced/cast/custom.rst @@ -108,7 +108,7 @@ type is explicitly allowed. } // namespace pybind11 // Bind the negate function - PYBIND11_MODULE(docs_advanced_cast_custom, m) { m.def("negate", user_space::negate); } + PYBIND11_MODULE(docs_advanced_cast_custom, m, py::mod_gil_not_used()) { m.def("negate", user_space::negate); } .. note:: diff --git a/docs/advanced/cast/functional.rst b/docs/advanced/cast/functional.rst index d9b460575..c95aa074f 100644 --- a/docs/advanced/cast/functional.rst +++ b/docs/advanced/cast/functional.rst @@ -56,7 +56,7 @@ trivial to generate binding code for all of these functions. #include - PYBIND11_MODULE(example, m) { + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { m.def("func_arg", &func_arg); m.def("func_ret", &func_ret); m.def("func_cpp", &func_cpp); diff --git a/docs/advanced/classes.rst b/docs/advanced/classes.rst index 04559d038..14bfc0bcd 100644 --- a/docs/advanced/classes.rst +++ b/docs/advanced/classes.rst @@ -45,7 +45,7 @@ Normally, the binding code for these classes would look as follows: .. code-block:: cpp - PYBIND11_MODULE(example, m) { + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { py::class_(m, "Animal") .def("go", &Animal::go); @@ -112,7 +112,7 @@ The binding code also needs a few minor adaptations (highlighted): .. code-block:: cpp :emphasize-lines: 2,3 - PYBIND11_MODULE(example, m) { + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { py::class_(m, "Animal") .def(py::init<>()) .def("go", &Animal::go); @@ -774,7 +774,7 @@ to Python. #include - PYBIND11_MODULE(example, m) { + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { py::class_(m, "Vector2") .def(py::init()) .def(py::self + py::self) diff --git a/docs/advanced/pycpp/numpy.rst b/docs/advanced/pycpp/numpy.rst index 0c0447667..7369dea64 100644 --- a/docs/advanced/pycpp/numpy.rst +++ b/docs/advanced/pycpp/numpy.rst @@ -217,7 +217,7 @@ expects the type followed by field names: }; // ... - PYBIND11_MODULE(test, m) { + PYBIND11_MODULE(test, m, py::mod_gil_not_used()) { // ... PYBIND11_NUMPY_DTYPE(A, x, y); @@ -351,7 +351,7 @@ simply using ``vectorize``). return result; } - PYBIND11_MODULE(test, m) { + PYBIND11_MODULE(test, m, py::mod_gil_not_used()) { m.def("add_arrays", &add_arrays, "Add two NumPy arrays"); } diff --git a/docs/basics.rst b/docs/basics.rst index c7a0208c4..1e68869d4 100644 --- a/docs/basics.rst +++ b/docs/basics.rst @@ -112,7 +112,7 @@ a file named :file:`example.cpp` with the following contents: return i + j; } - PYBIND11_MODULE(example, m) { + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { m.doc() = "pybind11 example plugin"; // optional module docstring m.def("add", &add, "A function that adds two numbers"); @@ -288,7 +288,7 @@ converted using the function ``py::cast``. .. code-block:: cpp - PYBIND11_MODULE(example, m) { + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { m.attr("the_answer") = 42; py::object world = py::cast("World"); m.attr("what") = world; diff --git a/docs/benchmark.py b/docs/benchmark.py index 26e390eb4..0b9d08f84 100644 --- a/docs/benchmark.py +++ b/docs/benchmark.py @@ -33,7 +33,7 @@ def generate_dummy_code_pybind11(nclasses=10): result = "#include \n\n" result += "namespace py = pybind11;\n\n" result += decl + "\n" - result += "PYBIND11_MODULE(example, m) {\n" + result += "PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {\n" result += bindings result += "}" return result diff --git a/docs/benchmark.rst b/docs/benchmark.rst index 02c2ccde7..f1bf32163 100644 --- a/docs/benchmark.rst +++ b/docs/benchmark.rst @@ -31,7 +31,7 @@ Here is an example of the binding code for one class: }; ... - PYBIND11_MODULE(example, m) { + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { ... py::class_(m, "cl034") .def("fn_000", &cl034::fn_000) diff --git a/docs/classes.rst b/docs/classes.rst index b12625656..97ad72f3f 100644 --- a/docs/classes.rst +++ b/docs/classes.rst @@ -27,7 +27,7 @@ The binding code for ``Pet`` looks as follows: namespace py = pybind11; - PYBIND11_MODULE(example, m) { + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { py::class_(m, "Pet") .def(py::init()) .def("setName", &Pet::setName) @@ -480,7 +480,7 @@ management. For example, ownership is inadvertently transferred here: std::shared_ptr child; }; - PYBIND11_MODULE(example, m) { + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { py::class_>(m, "Child"); py::class_>(m, "Parent") diff --git a/docs/faq.rst b/docs/faq.rst index 31e33f8b5..2b89d203f 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -90,7 +90,7 @@ following example: void init_ex2(py::module_ &); /* ... */ - PYBIND11_MODULE(example, m) { + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { init_ex1(m); init_ex2(m); /* ... */ @@ -235,8 +235,7 @@ been received, you must either explicitly interrupt execution by throwing .. code-block:: cpp - PYBIND11_MODULE(example, m) - { + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { m.def("long running_func", []() { for (;;) {