mirror of
https://github.com/pybind/pybind11.git
synced 2026-06-05 20:54:53 +00:00
Make sure detail::get_internals acquires the GIL before making Python calls. (#1836)
This is only necessary if `get_internals` is called for the first time in a given module when the running thread is in a GIL-released state. Fixes #1364
This commit is contained in:
committed by
Wenzel Jakob
parent
dffe869dba
commit
b60fd233fa
@@ -171,6 +171,14 @@ PYBIND11_NOINLINE inline internals &get_internals() {
|
||||
if (internals_pp && *internals_pp)
|
||||
return **internals_pp;
|
||||
|
||||
// Ensure that the GIL is held since we will need to make Python calls.
|
||||
// Cannot use py::gil_scoped_acquire here since that constructor calls get_internals.
|
||||
struct gil_scoped_acquire {
|
||||
gil_scoped_acquire() : state (PyGILState_Ensure()) {}
|
||||
~gil_scoped_acquire() { PyGILState_Release(state); }
|
||||
const PyGILState_STATE state;
|
||||
} gil;
|
||||
|
||||
constexpr auto *id = PYBIND11_INTERNALS_ID;
|
||||
auto builtins = handle(PyEval_GetBuiltins());
|
||||
if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
|
||||
|
||||
Reference in New Issue
Block a user