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:
Saran Tunyasuvunakool
2019-07-15 15:47:02 +01:00
committed by Wenzel Jakob
parent dffe869dba
commit b60fd233fa
5 changed files with 106 additions and 0 deletions

View File

@@ -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])) {