Make changes specific to Python 3.8+, incorporate feedback

This commit is contained in:
Wenzel Jakob 2022-10-31 21:27:48 +01:00
parent 7a944063e1
commit 3a89f67107

View File

@ -277,12 +277,6 @@ struct type_info {
# endif # endif
#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 \ #define PYBIND11_INTERNALS_ID \
"__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \ "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \ PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
@ -425,22 +419,31 @@ PYBIND11_NOINLINE internals &get_internals() {
} gil; } gil;
error_scope err_scope; error_scope err_scope;
const char *id_cstr = PYBIND11_INTERNALS_ID; constexpr const char *id_cstr = PYBIND11_INTERNALS_ID;
PYBIND11_STR_TYPE id(id_cstr); str id(id_cstr);
dict state_dict dict state_dict;
= reinterpret_borrow<dict>(PyInterpreterState_GetDict(PYBIND11_INTERPRETER_STATE_GET())); #if PY_VERSION_HEX < 0x03080000
if (!state_dict) state_dict = reinterpret_borrow<dict>(PyEval_GetBuiltins());
pybind11_fail("get_internals(): PyInterpreterState_GetDict() failed!"); #elif PY_VERSION_HEX < 0x03090000
state_dict = reinterpret_borrow<dict>(PyInterpreterState_GetDict(_PyInterpreterState_Get()));
#else
state_dict = reinterpret_borrow<dict>(PyInterpreterState_GetDict(PyInterpreterState_Get()));
#endif
if (!state_dict) {
pybind11_fail("get_internals(): could not acquire state dictionary!");
}
if (state_dict.contains(id_cstr)) { if (state_dict.contains(id_cstr)) {
object o = state_dict[id]; object o = state_dict[id];
// May fail if 'capsule_obj' is not a capsule, or if it has a different // 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 // name. We clear the error status below in that case
internals_pp = static_cast<internals **>(PyCapsule_GetPointer(o.ptr(), id_cstr)); internals_pp = static_cast<internals **>(PyCapsule_GetPointer(o.ptr(), id_cstr));
if (!internals_pp) if (!internals_pp) {
PyErr_Clear(); PyErr_Clear();
} }
}
if (internals_pp) { if (internals_pp) {
// We loaded builtins through python's builtins, which means that our `error_already_set` // We loaded builtins through python's builtins, which means that our `error_already_set`