Only release the GIL if we own an object *and* the Python runtime is valid *and* the GIL is held.
| 267 | // Only release the GIL if we own an object *and* the Python runtime is |
| 268 | // valid *and* the GIL is held. |
| 269 | std::optional<PyReleaseGIL> optional_gil_release() const { |
| 270 | if (this->get() != nullptr && Py_IsInitialized() && PyGILState_Check()) { |
| 271 | return PyReleaseGIL(); |
| 272 | } |
| 273 | return {}; |
| 274 | } |
| 275 | }; |
| 276 | |
| 277 | /// \brief A std::shared_ptr<T, ...> subclass that releases the GIL when destroying T |