@@ -277,12 +277,6 @@ struct type_info {
277
277
# endif
278
278
#endif
279
279
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
-
286
280
#define PYBIND11_INTERNALS_ID \
287
281
" __pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
288
282
PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
@@ -425,21 +419,30 @@ PYBIND11_NOINLINE internals &get_internals() {
425
419
} gil;
426
420
error_scope err_scope;
427
421
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
430
433
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
+ }
435
437
436
438
if (state_dict.contains (id_cstr)) {
437
439
object o = state_dict[id];
438
440
// May fail if 'capsule_obj' is not a capsule, or if it has a different
439
441
// name. We clear the error status below in that case
440
442
internals_pp = static_cast <internals **>(PyCapsule_GetPointer (o.ptr (), id_cstr));
441
- if (!internals_pp)
443
+ if (!internals_pp) {
442
444
PyErr_Clear ();
445
+ }
443
446
}
444
447
445
448
if (internals_pp) {
0 commit comments