From 3a89f671078831d7388cc89285a28e1def8c8432 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Mon, 31 Oct 2022 21:27:48 +0100 Subject: [PATCH] Make changes specific to Python 3.8+, incorporate feedback --- include/pybind11/detail/internals.h | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 645109ed7..4929a68ce 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -277,12 +277,6 @@ struct type_info { # endif #endif -#if PY_VERSION_HEX < 0x03090000 -# define PYBIND11_INTERPRETER_STATE_GET _PyInterpreterState_Get -#else -# define PYBIND11_INTERPRETER_STATE_GET PyInterpreterState_Get -#endif - #define PYBIND11_INTERNALS_ID \ "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \ PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \ @@ -425,21 +419,30 @@ PYBIND11_NOINLINE internals &get_internals() { } gil; error_scope err_scope; - const char *id_cstr = PYBIND11_INTERNALS_ID; - PYBIND11_STR_TYPE id(id_cstr); + constexpr const char *id_cstr = PYBIND11_INTERNALS_ID; + str id(id_cstr); - dict state_dict - = reinterpret_borrow(PyInterpreterState_GetDict(PYBIND11_INTERPRETER_STATE_GET())); - if (!state_dict) - pybind11_fail("get_internals(): PyInterpreterState_GetDict() failed!"); + dict state_dict; +#if PY_VERSION_HEX < 0x03080000 + state_dict = reinterpret_borrow(PyEval_GetBuiltins()); +#elif PY_VERSION_HEX < 0x03090000 + state_dict = reinterpret_borrow(PyInterpreterState_GetDict(_PyInterpreterState_Get())); +#else + state_dict = reinterpret_borrow(PyInterpreterState_GetDict(PyInterpreterState_Get())); +#endif + + if (!state_dict) { + pybind11_fail("get_internals(): could not acquire state dictionary!"); + } if (state_dict.contains(id_cstr)) { object o = state_dict[id]; // May fail if 'capsule_obj' is not a capsule, or if it has a different // name. We clear the error status below in that case internals_pp = static_cast(PyCapsule_GetPointer(o.ptr(), id_cstr)); - if (!internals_pp) + if (!internals_pp) { PyErr_Clear(); + } } if (internals_pp) {