Skip to content

Commit 3a89f67

Browse files
committed
Make changes specific to Python 3.8+, incorporate feedback
1 parent 7a94406 commit 3a89f67

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

include/pybind11/detail/internals.h

+16-13
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,6 @@ struct type_info {
277277
# endif
278278
#endif
279279

280-
#if PY_VERSION_HEX < 0x03090000
281-
# define PYBIND11_INTERPRETER_STATE_GET _PyInterpreterState_Get
282-
#else
283-
# define PYBIND11_INTERPRETER_STATE_GET PyInterpreterState_Get
284-
#endif
285-
286280
#define PYBIND11_INTERNALS_ID \
287281
"__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
288282
PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
@@ -425,21 +419,30 @@ PYBIND11_NOINLINE internals &get_internals() {
425419
} gil;
426420
error_scope err_scope;
427421

428-
const char *id_cstr = PYBIND11_INTERNALS_ID;
429-
PYBIND11_STR_TYPE id(id_cstr);
422+
constexpr const char *id_cstr = PYBIND11_INTERNALS_ID;
423+
str id(id_cstr);
424+
425+
dict state_dict;
426+
#if PY_VERSION_HEX < 0x03080000
427+
state_dict = reinterpret_borrow<dict>(PyEval_GetBuiltins());
428+
#elif PY_VERSION_HEX < 0x03090000
429+
state_dict = reinterpret_borrow<dict>(PyInterpreterState_GetDict(_PyInterpreterState_Get()));
430+
#else
431+
state_dict = reinterpret_borrow<dict>(PyInterpreterState_GetDict(PyInterpreterState_Get()));
432+
#endif
430433

431-
dict state_dict
432-
= reinterpret_borrow<dict>(PyInterpreterState_GetDict(PYBIND11_INTERPRETER_STATE_GET()));
433-
if (!state_dict)
434-
pybind11_fail("get_internals(): PyInterpreterState_GetDict() failed!");
434+
if (!state_dict) {
435+
pybind11_fail("get_internals(): could not acquire state dictionary!");
436+
}
435437

436438
if (state_dict.contains(id_cstr)) {
437439
object o = state_dict[id];
438440
// May fail if 'capsule_obj' is not a capsule, or if it has a different
439441
// name. We clear the error status below in that case
440442
internals_pp = static_cast<internals **>(PyCapsule_GetPointer(o.ptr(), id_cstr));
441-
if (!internals_pp)
443+
if (!internals_pp) {
442444
PyErr_Clear();
445+
}
443446
}
444447

445448
if (internals_pp) {

0 commit comments

Comments
 (0)