Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 09:08:10

0001 /*
0002     pybind11/detail/internals.h: Internal data structure and related functions
0003 
0004     Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
0005 
0006     All rights reserved. Use of this source code is governed by a
0007     BSD-style license that can be found in the LICENSE file.
0008 */
0009 
0010 #pragma once
0011 
0012 #include "common.h"
0013 
0014 #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
0015 #    include <pybind11/gil.h>
0016 #endif
0017 
0018 #include <pybind11/pytypes.h>
0019 
0020 #include <exception>
0021 #include <mutex>
0022 #include <thread>
0023 
0024 /// Tracks the `internals` and `type_info` ABI version independent of the main library version.
0025 ///
0026 /// Some portions of the code use an ABI that is conditional depending on this
0027 /// version number.  That allows ABI-breaking changes to be "pre-implemented".
0028 /// Once the default version number is incremented, the conditional logic that
0029 /// no longer applies can be removed.  Additionally, users that need not
0030 /// maintain ABI compatibility can increase the version number in order to take
0031 /// advantage of any functionality/efficiency improvements that depend on the
0032 /// newer ABI.
0033 ///
0034 /// WARNING: If you choose to manually increase the ABI version, note that
0035 /// pybind11 may not be tested as thoroughly with a non-default ABI version, and
0036 /// further ABI-incompatible changes may be made before the ABI is officially
0037 /// changed to the new version.
0038 #ifndef PYBIND11_INTERNALS_VERSION
0039 #    if PY_VERSION_HEX >= 0x030C0000 || defined(_MSC_VER)
0040 // Version bump for Python 3.12+, before first 3.12 beta release.
0041 // Version bump for MSVC piggy-backed on PR #4779. See comments there.
0042 #        define PYBIND11_INTERNALS_VERSION 5
0043 #    else
0044 #        define PYBIND11_INTERNALS_VERSION 4
0045 #    endif
0046 #endif
0047 
0048 // This requirement is mainly to reduce the support burden (see PR #4570).
0049 static_assert(PY_VERSION_HEX < 0x030C0000 || PYBIND11_INTERNALS_VERSION >= 5,
0050               "pybind11 ABI version 5 is the minimum for Python 3.12+");
0051 
0052 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0053 
0054 using ExceptionTranslator = void (*)(std::exception_ptr);
0055 
0056 PYBIND11_NAMESPACE_BEGIN(detail)
0057 
0058 constexpr const char *internals_function_record_capsule_name = "pybind11_function_record_capsule";
0059 
0060 // Forward declarations
0061 inline PyTypeObject *make_static_property_type();
0062 inline PyTypeObject *make_default_metaclass();
0063 inline PyObject *make_object_base_type(PyTypeObject *metaclass);
0064 
0065 // The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new
0066 // Thread Specific Storage (TSS) API.
0067 // Avoid unnecessary allocation of `Py_tss_t`, since we cannot use
0068 // `Py_LIMITED_API` anyway.
0069 #if PYBIND11_INTERNALS_VERSION > 4
0070 #    define PYBIND11_TLS_KEY_REF Py_tss_t &
0071 #    if defined(__clang__)
0072 #        define PYBIND11_TLS_KEY_INIT(var)                                                        \
0073             _Pragma("clang diagnostic push")                                         /**/         \
0074                 _Pragma("clang diagnostic ignored \"-Wmissing-field-initializers\"") /**/         \
0075                 Py_tss_t var                                                                      \
0076                 = Py_tss_NEEDS_INIT;                                                              \
0077             _Pragma("clang diagnostic pop")
0078 #    elif defined(__GNUC__) && !defined(__INTEL_COMPILER)
0079 #        define PYBIND11_TLS_KEY_INIT(var)                                                        \
0080             _Pragma("GCC diagnostic push")                                         /**/           \
0081                 _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") /**/           \
0082                 Py_tss_t var                                                                      \
0083                 = Py_tss_NEEDS_INIT;                                                              \
0084             _Pragma("GCC diagnostic pop")
0085 #    else
0086 #        define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT;
0087 #    endif
0088 #    define PYBIND11_TLS_KEY_CREATE(var) (PyThread_tss_create(&(var)) == 0)
0089 #    define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get(&(key))
0090 #    define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value))
0091 #    define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr)
0092 #    define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key))
0093 #else
0094 #    define PYBIND11_TLS_KEY_REF Py_tss_t *
0095 #    define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr;
0096 #    define PYBIND11_TLS_KEY_CREATE(var)                                                          \
0097         (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0))
0098 #    define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
0099 #    define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value))
0100 #    define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
0101 #    define PYBIND11_TLS_FREE(key) PyThread_tss_free(key)
0102 #endif
0103 
0104 // Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
0105 // other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module
0106 // even when `A` is the same, non-hidden-visibility type (e.g. from a common include).  Under
0107 // libstdc++, this doesn't happen: equality and the type_index hash are based on the type name,
0108 // which works.  If not under a known-good stl, provide our own name-based hash and equality
0109 // functions that use the type name.
0110 #if (PYBIND11_INTERNALS_VERSION <= 4 && defined(__GLIBCXX__))                                     \
0111     || (PYBIND11_INTERNALS_VERSION >= 5 && !defined(_LIBCPP_VERSION))
0112 inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
0113 using type_hash = std::hash<std::type_index>;
0114 using type_equal_to = std::equal_to<std::type_index>;
0115 #else
0116 inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
0117     return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
0118 }
0119 
0120 struct type_hash {
0121     size_t operator()(const std::type_index &t) const {
0122         size_t hash = 5381;
0123         const char *ptr = t.name();
0124         while (auto c = static_cast<unsigned char>(*ptr++)) {
0125             hash = (hash * 33) ^ c;
0126         }
0127         return hash;
0128     }
0129 };
0130 
0131 struct type_equal_to {
0132     bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
0133         return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
0134     }
0135 };
0136 #endif
0137 
0138 template <typename value_type>
0139 using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
0140 
0141 struct override_hash {
0142     inline size_t operator()(const std::pair<const PyObject *, const char *> &v) const {
0143         size_t value = std::hash<const void *>()(v.first);
0144         value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value << 6) + (value >> 2);
0145         return value;
0146     }
0147 };
0148 
0149 using instance_map = std::unordered_multimap<const void *, instance *>;
0150 
0151 #ifdef Py_GIL_DISABLED
0152 // Wrapper around PyMutex to provide BasicLockable semantics
0153 class pymutex {
0154     PyMutex mutex;
0155 
0156 public:
0157     pymutex() : mutex({}) {}
0158     void lock() { PyMutex_Lock(&mutex); }
0159     void unlock() { PyMutex_Unlock(&mutex); }
0160 };
0161 
0162 // Instance map shards are used to reduce mutex contention in free-threaded Python.
0163 struct instance_map_shard {
0164     instance_map registered_instances;
0165     pymutex mutex;
0166     // alignas(64) would be better, but causes compile errors in macOS before 10.14 (see #5200)
0167     char padding[64 - (sizeof(instance_map) + sizeof(pymutex)) % 64];
0168 };
0169 
0170 static_assert(sizeof(instance_map_shard) % 64 == 0,
0171               "instance_map_shard size is not a multiple of 64 bytes");
0172 #endif
0173 
0174 /// Internal data structure used to track registered instances and types.
0175 /// Whenever binary incompatible changes are made to this structure,
0176 /// `PYBIND11_INTERNALS_VERSION` must be incremented.
0177 struct internals {
0178 #ifdef Py_GIL_DISABLED
0179     pymutex mutex;
0180 #endif
0181     // std::type_index -> pybind11's type information
0182     type_map<type_info *> registered_types_cpp;
0183     // PyTypeObject* -> base type_info(s)
0184     std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py;
0185 #ifdef Py_GIL_DISABLED
0186     std::unique_ptr<instance_map_shard[]> instance_shards; // void * -> instance*
0187     size_t instance_shards_mask;
0188 #else
0189     instance_map registered_instances; // void * -> instance*
0190 #endif
0191     std::unordered_set<std::pair<const PyObject *, const char *>, override_hash>
0192         inactive_override_cache;
0193     type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
0194     std::unordered_map<const PyObject *, std::vector<PyObject *>> patients;
0195     std::forward_list<ExceptionTranslator> registered_exception_translators;
0196     std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across
0197                                                          // extensions
0198 #if PYBIND11_INTERNALS_VERSION == 4
0199     std::vector<PyObject *> unused_loader_patient_stack_remove_at_v5;
0200 #endif
0201     std::forward_list<std::string> static_strings; // Stores the std::strings backing
0202                                                    // detail::c_str()
0203     PyTypeObject *static_property_type;
0204     PyTypeObject *default_metaclass;
0205     PyObject *instance_base;
0206     // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
0207     PYBIND11_TLS_KEY_INIT(tstate)
0208 #if PYBIND11_INTERNALS_VERSION > 4
0209     PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
0210 #endif // PYBIND11_INTERNALS_VERSION > 4
0211     // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
0212     PyInterpreterState *istate = nullptr;
0213 
0214 #if PYBIND11_INTERNALS_VERSION > 4
0215     // Note that we have to use a std::string to allocate memory to ensure a unique address
0216     // We want unique addresses since we use pointer equality to compare function records
0217     std::string function_record_capsule_name = internals_function_record_capsule_name;
0218 #endif
0219 
0220     internals() = default;
0221     internals(const internals &other) = delete;
0222     internals &operator=(const internals &other) = delete;
0223     ~internals() {
0224 #if PYBIND11_INTERNALS_VERSION > 4
0225         PYBIND11_TLS_FREE(loader_life_support_tls_key);
0226 #endif // PYBIND11_INTERNALS_VERSION > 4
0227 
0228         // This destructor is called *after* Py_Finalize() in finalize_interpreter().
0229         // That *SHOULD BE* fine. The following details what happens when PyThread_tss_free is
0230         // called. PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does
0231         // nothing. PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree.
0232         // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX).
0233         // Neither of those have anything to do with CPython internals. PyMem_RawFree *requires*
0234         // that the `tstate` be allocated with the CPython allocator.
0235         PYBIND11_TLS_FREE(tstate);
0236     }
0237 };
0238 
0239 /// Additional type information which does not fit into the PyTypeObject.
0240 /// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`.
0241 struct type_info {
0242     PyTypeObject *type;
0243     const std::type_info *cpptype;
0244     size_t type_size, type_align, holder_size_in_ptrs;
0245     void *(*operator_new)(size_t);
0246     void (*init_instance)(instance *, const void *);
0247     void (*dealloc)(value_and_holder &v_h);
0248     std::vector<PyObject *(*) (PyObject *, PyTypeObject *)> implicit_conversions;
0249     std::vector<std::pair<const std::type_info *, void *(*) (void *)>> implicit_casts;
0250     std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
0251     buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
0252     void *get_buffer_data = nullptr;
0253     void *(*module_local_load)(PyObject *, const type_info *) = nullptr;
0254     /* A simple type never occurs as a (direct or indirect) parent
0255      * of a class that makes use of multiple inheritance.
0256      * A type can be simple even if it has non-simple ancestors as long as it has no descendants.
0257      */
0258     bool simple_type : 1;
0259     /* True if there is no multiple inheritance in this type's inheritance tree */
0260     bool simple_ancestors : 1;
0261     /* for base vs derived holder_type checks */
0262     bool default_holder : 1;
0263     /* true if this is a type registered with py::module_local */
0264     bool module_local : 1;
0265 };
0266 
0267 /// On MSVC, debug and release builds are not ABI-compatible!
0268 #if defined(_MSC_VER) && defined(_DEBUG)
0269 #    define PYBIND11_BUILD_TYPE "_debug"
0270 #else
0271 #    define PYBIND11_BUILD_TYPE ""
0272 #endif
0273 
0274 /// Let's assume that different compilers are ABI-incompatible.
0275 /// A user can manually set this string if they know their
0276 /// compiler is compatible.
0277 #ifndef PYBIND11_COMPILER_TYPE
0278 #    if defined(_MSC_VER)
0279 #        define PYBIND11_COMPILER_TYPE "_msvc"
0280 #    elif defined(__INTEL_COMPILER)
0281 #        define PYBIND11_COMPILER_TYPE "_icc"
0282 #    elif defined(__clang__)
0283 #        define PYBIND11_COMPILER_TYPE "_clang"
0284 #    elif defined(__PGI)
0285 #        define PYBIND11_COMPILER_TYPE "_pgi"
0286 #    elif defined(__MINGW32__)
0287 #        define PYBIND11_COMPILER_TYPE "_mingw"
0288 #    elif defined(__CYGWIN__)
0289 #        define PYBIND11_COMPILER_TYPE "_gcc_cygwin"
0290 #    elif defined(__GNUC__)
0291 #        define PYBIND11_COMPILER_TYPE "_gcc"
0292 #    else
0293 #        define PYBIND11_COMPILER_TYPE "_unknown"
0294 #    endif
0295 #endif
0296 
0297 /// Also standard libs
0298 #ifndef PYBIND11_STDLIB
0299 #    if defined(_LIBCPP_VERSION)
0300 #        define PYBIND11_STDLIB "_libcpp"
0301 #    elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
0302 #        define PYBIND11_STDLIB "_libstdcpp"
0303 #    else
0304 #        define PYBIND11_STDLIB ""
0305 #    endif
0306 #endif
0307 
0308 /// On Linux/OSX, changes in __GXX_ABI_VERSION__ indicate ABI incompatibility.
0309 /// On MSVC, changes in _MSC_VER may indicate ABI incompatibility (#2898).
0310 #ifndef PYBIND11_BUILD_ABI
0311 #    if defined(__GXX_ABI_VERSION)
0312 #        define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION)
0313 #    elif defined(_MSC_VER)
0314 #        define PYBIND11_BUILD_ABI "_mscver" PYBIND11_TOSTRING(_MSC_VER)
0315 #    else
0316 #        define PYBIND11_BUILD_ABI ""
0317 #    endif
0318 #endif
0319 
0320 #ifndef PYBIND11_INTERNALS_KIND
0321 #    define PYBIND11_INTERNALS_KIND ""
0322 #endif
0323 
0324 #define PYBIND11_PLATFORM_ABI_ID                                                                  \
0325     PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI             \
0326         PYBIND11_BUILD_TYPE
0327 
0328 #define PYBIND11_INTERNALS_ID                                                                     \
0329     "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION)                        \
0330         PYBIND11_PLATFORM_ABI_ID "__"
0331 
0332 #define PYBIND11_MODULE_LOCAL_ID                                                                  \
0333     "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION)                     \
0334         PYBIND11_PLATFORM_ABI_ID "__"
0335 
0336 /// Each module locally stores a pointer to the `internals` data. The data
0337 /// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`.
0338 inline internals **&get_internals_pp() {
0339     static internals **internals_pp = nullptr;
0340     return internals_pp;
0341 }
0342 
0343 // forward decl
0344 inline void translate_exception(std::exception_ptr);
0345 
0346 template <class T,
0347           enable_if_t<std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
0348 bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
0349     std::exception_ptr nested = exc.nested_ptr();
0350     if (nested != nullptr && nested != p) {
0351         translate_exception(nested);
0352         return true;
0353     }
0354     return false;
0355 }
0356 
0357 template <class T,
0358           enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
0359 bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
0360     if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
0361         return handle_nested_exception(*nep, p);
0362     }
0363     return false;
0364 }
0365 
0366 inline bool raise_err(PyObject *exc_type, const char *msg) {
0367     if (PyErr_Occurred()) {
0368         raise_from(exc_type, msg);
0369         return true;
0370     }
0371     set_error(exc_type, msg);
0372     return false;
0373 }
0374 
0375 inline void translate_exception(std::exception_ptr p) {
0376     if (!p) {
0377         return;
0378     }
0379     try {
0380         std::rethrow_exception(p);
0381     } catch (error_already_set &e) {
0382         handle_nested_exception(e, p);
0383         e.restore();
0384         return;
0385     } catch (const builtin_exception &e) {
0386         // Could not use template since it's an abstract class.
0387         if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
0388             handle_nested_exception(*nep, p);
0389         }
0390         e.set_error();
0391         return;
0392     } catch (const std::bad_alloc &e) {
0393         handle_nested_exception(e, p);
0394         raise_err(PyExc_MemoryError, e.what());
0395         return;
0396     } catch (const std::domain_error &e) {
0397         handle_nested_exception(e, p);
0398         raise_err(PyExc_ValueError, e.what());
0399         return;
0400     } catch (const std::invalid_argument &e) {
0401         handle_nested_exception(e, p);
0402         raise_err(PyExc_ValueError, e.what());
0403         return;
0404     } catch (const std::length_error &e) {
0405         handle_nested_exception(e, p);
0406         raise_err(PyExc_ValueError, e.what());
0407         return;
0408     } catch (const std::out_of_range &e) {
0409         handle_nested_exception(e, p);
0410         raise_err(PyExc_IndexError, e.what());
0411         return;
0412     } catch (const std::range_error &e) {
0413         handle_nested_exception(e, p);
0414         raise_err(PyExc_ValueError, e.what());
0415         return;
0416     } catch (const std::overflow_error &e) {
0417         handle_nested_exception(e, p);
0418         raise_err(PyExc_OverflowError, e.what());
0419         return;
0420     } catch (const std::exception &e) {
0421         handle_nested_exception(e, p);
0422         raise_err(PyExc_RuntimeError, e.what());
0423         return;
0424     } catch (const std::nested_exception &e) {
0425         handle_nested_exception(e, p);
0426         raise_err(PyExc_RuntimeError, "Caught an unknown nested exception!");
0427         return;
0428     } catch (...) {
0429         raise_err(PyExc_RuntimeError, "Caught an unknown exception!");
0430         return;
0431     }
0432 }
0433 
0434 #if !defined(__GLIBCXX__)
0435 inline void translate_local_exception(std::exception_ptr p) {
0436     try {
0437         if (p) {
0438             std::rethrow_exception(p);
0439         }
0440     } catch (error_already_set &e) {
0441         e.restore();
0442         return;
0443     } catch (const builtin_exception &e) {
0444         e.set_error();
0445         return;
0446     }
0447 }
0448 #endif
0449 
0450 inline object get_python_state_dict() {
0451     object state_dict;
0452 #if PYBIND11_INTERNALS_VERSION <= 4 || PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION)
0453     state_dict = reinterpret_borrow<object>(PyEval_GetBuiltins());
0454 #else
0455 #    if PY_VERSION_HEX < 0x03090000
0456     PyInterpreterState *istate = _PyInterpreterState_Get();
0457 #    else
0458     PyInterpreterState *istate = PyInterpreterState_Get();
0459 #    endif
0460     if (istate) {
0461         state_dict = reinterpret_borrow<object>(PyInterpreterState_GetDict(istate));
0462     }
0463 #endif
0464     if (!state_dict) {
0465         raise_from(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED");
0466         throw error_already_set();
0467     }
0468     return state_dict;
0469 }
0470 
0471 inline object get_internals_obj_from_state_dict(handle state_dict) {
0472     return reinterpret_steal<object>(
0473         dict_getitemstringref(state_dict.ptr(), PYBIND11_INTERNALS_ID));
0474 }
0475 
0476 inline internals **get_internals_pp_from_capsule(handle obj) {
0477     void *raw_ptr = PyCapsule_GetPointer(obj.ptr(), /*name=*/nullptr);
0478     if (raw_ptr == nullptr) {
0479         raise_from(PyExc_SystemError, "pybind11::detail::get_internals_pp_from_capsule() FAILED");
0480         throw error_already_set();
0481     }
0482     return static_cast<internals **>(raw_ptr);
0483 }
0484 
0485 inline uint64_t round_up_to_next_pow2(uint64_t x) {
0486     // Round-up to the next power of two.
0487     // See https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
0488     x--;
0489     x |= (x >> 1);
0490     x |= (x >> 2);
0491     x |= (x >> 4);
0492     x |= (x >> 8);
0493     x |= (x >> 16);
0494     x |= (x >> 32);
0495     x++;
0496     return x;
0497 }
0498 
0499 /// Return a reference to the current `internals` data
0500 PYBIND11_NOINLINE internals &get_internals() {
0501     auto **&internals_pp = get_internals_pp();
0502     if (internals_pp && *internals_pp) {
0503         return **internals_pp;
0504     }
0505 
0506 #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
0507     gil_scoped_acquire gil;
0508 #else
0509     // Ensure that the GIL is held since we will need to make Python calls.
0510     // Cannot use py::gil_scoped_acquire here since that constructor calls get_internals.
0511     struct gil_scoped_acquire_local {
0512         gil_scoped_acquire_local() : state(PyGILState_Ensure()) {}
0513         gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete;
0514         gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete;
0515         ~gil_scoped_acquire_local() { PyGILState_Release(state); }
0516         const PyGILState_STATE state;
0517     } gil;
0518 #endif
0519     error_scope err_scope;
0520 
0521     dict state_dict = get_python_state_dict();
0522     if (object internals_obj = get_internals_obj_from_state_dict(state_dict)) {
0523         internals_pp = get_internals_pp_from_capsule(internals_obj);
0524     }
0525     if (internals_pp && *internals_pp) {
0526         // We loaded the internals through `state_dict`, which means that our `error_already_set`
0527         // and `builtin_exception` may be different local classes than the ones set up in the
0528         // initial exception translator, below, so add another for our local exception classes.
0529         //
0530         // libstdc++ doesn't require this (types there are identified only by name)
0531         // libc++ with CPython doesn't require this (types are explicitly exported)
0532         // libc++ with PyPy still need it, awaiting further investigation
0533 #if !defined(__GLIBCXX__)
0534         (*internals_pp)->registered_exception_translators.push_front(&translate_local_exception);
0535 #endif
0536     } else {
0537         if (!internals_pp) {
0538             internals_pp = new internals *();
0539         }
0540         auto *&internals_ptr = *internals_pp;
0541         internals_ptr = new internals();
0542 
0543         PyThreadState *tstate = PyThreadState_Get();
0544         // NOLINTNEXTLINE(bugprone-assignment-in-if-condition)
0545         if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->tstate)) {
0546             pybind11_fail("get_internals: could not successfully initialize the tstate TSS key!");
0547         }
0548         PYBIND11_TLS_REPLACE_VALUE(internals_ptr->tstate, tstate);
0549 
0550 #if PYBIND11_INTERNALS_VERSION > 4
0551         // NOLINTNEXTLINE(bugprone-assignment-in-if-condition)
0552         if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->loader_life_support_tls_key)) {
0553             pybind11_fail("get_internals: could not successfully initialize the "
0554                           "loader_life_support TSS key!");
0555         }
0556 #endif
0557         internals_ptr->istate = tstate->interp;
0558         state_dict[PYBIND11_INTERNALS_ID] = capsule(reinterpret_cast<void *>(internals_pp));
0559         internals_ptr->registered_exception_translators.push_front(&translate_exception);
0560         internals_ptr->static_property_type = make_static_property_type();
0561         internals_ptr->default_metaclass = make_default_metaclass();
0562         internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass);
0563 #ifdef Py_GIL_DISABLED
0564         // Scale proportional to the number of cores. 2x is a heuristic to reduce contention.
0565         auto num_shards
0566             = static_cast<size_t>(round_up_to_next_pow2(2 * std::thread::hardware_concurrency()));
0567         if (num_shards == 0) {
0568             num_shards = 1;
0569         }
0570         internals_ptr->instance_shards.reset(new instance_map_shard[num_shards]);
0571         internals_ptr->instance_shards_mask = num_shards - 1;
0572 #endif // Py_GIL_DISABLED
0573     }
0574     return **internals_pp;
0575 }
0576 
0577 // the internals struct (above) is shared between all the modules. local_internals are only
0578 // for a single module. Any changes made to internals may require an update to
0579 // PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design,
0580 // restricted to a single module. Whether a module has local internals or not should not
0581 // impact any other modules, because the only things accessing the local internals is the
0582 // module that contains them.
0583 struct local_internals {
0584     type_map<type_info *> registered_types_cpp;
0585     std::forward_list<ExceptionTranslator> registered_exception_translators;
0586 #if PYBIND11_INTERNALS_VERSION == 4
0587 
0588     // For ABI compatibility, we can't store the loader_life_support TLS key in
0589     // the `internals` struct directly.  Instead, we store it in `shared_data` and
0590     // cache a copy in `local_internals`.  If we allocated a separate TLS key for
0591     // each instance of `local_internals`, we could end up allocating hundreds of
0592     // TLS keys if hundreds of different pybind11 modules are loaded (which is a
0593     // plausible number).
0594     PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
0595 
0596     // Holds the shared TLS key for the loader_life_support stack.
0597     struct shared_loader_life_support_data {
0598         PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
0599         shared_loader_life_support_data() {
0600             // NOLINTNEXTLINE(bugprone-assignment-in-if-condition)
0601             if (!PYBIND11_TLS_KEY_CREATE(loader_life_support_tls_key)) {
0602                 pybind11_fail("local_internals: could not successfully initialize the "
0603                               "loader_life_support TLS key!");
0604             }
0605         }
0606         // We can't help but leak the TLS key, because Python never unloads extension modules.
0607     };
0608 
0609     local_internals() {
0610         auto &internals = get_internals();
0611         // Get or create the `loader_life_support_stack_key`.
0612         auto &ptr = internals.shared_data["_life_support"];
0613         if (!ptr) {
0614             ptr = new shared_loader_life_support_data;
0615         }
0616         loader_life_support_tls_key
0617             = static_cast<shared_loader_life_support_data *>(ptr)->loader_life_support_tls_key;
0618     }
0619 #endif //  PYBIND11_INTERNALS_VERSION == 4
0620 };
0621 
0622 /// Works like `get_internals`, but for things which are locally registered.
0623 inline local_internals &get_local_internals() {
0624     // Current static can be created in the interpreter finalization routine. If the later will be
0625     // destroyed in another static variable destructor, creation of this static there will cause
0626     // static deinitialization fiasco. In order to avoid it we avoid destruction of the
0627     // local_internals static. One can read more about the problem and current solution here:
0628     // https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
0629     static auto *locals = new local_internals();
0630     return *locals;
0631 }
0632 
0633 #ifdef Py_GIL_DISABLED
0634 #    define PYBIND11_LOCK_INTERNALS(internals) std::unique_lock<pymutex> lock((internals).mutex)
0635 #else
0636 #    define PYBIND11_LOCK_INTERNALS(internals)
0637 #endif
0638 
0639 template <typename F>
0640 inline auto with_internals(const F &cb) -> decltype(cb(get_internals())) {
0641     auto &internals = get_internals();
0642     PYBIND11_LOCK_INTERNALS(internals);
0643     return cb(internals);
0644 }
0645 
0646 inline std::uint64_t mix64(std::uint64_t z) {
0647     // David Stafford's variant 13 of the MurmurHash3 finalizer popularized
0648     // by the SplitMix PRNG.
0649     // https://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
0650     z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
0651     z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
0652     return z ^ (z >> 31);
0653 }
0654 
0655 template <typename F>
0656 inline auto with_instance_map(const void *ptr,
0657                               const F &cb) -> decltype(cb(std::declval<instance_map &>())) {
0658     auto &internals = get_internals();
0659 
0660 #ifdef Py_GIL_DISABLED
0661     // Hash address to compute shard, but ignore low bits. We'd like allocations
0662     // from the same thread/core to map to the same shard and allocations from
0663     // other threads/cores to map to other shards. Using the high bits is a good
0664     // heuristic because memory allocators often have a per-thread
0665     // arena/superblock/segment from which smaller allocations are served.
0666     auto addr = reinterpret_cast<std::uintptr_t>(ptr);
0667     auto hash = mix64(static_cast<std::uint64_t>(addr >> 20));
0668     auto idx = static_cast<size_t>(hash & internals.instance_shards_mask);
0669 
0670     auto &shard = internals.instance_shards[idx];
0671     std::unique_lock<pymutex> lock(shard.mutex);
0672     return cb(shard.registered_instances);
0673 #else
0674     (void) ptr;
0675     return cb(internals.registered_instances);
0676 #endif
0677 }
0678 
0679 // Returns the number of registered instances for testing purposes.  The result may not be
0680 // consistent if other threads are registering or unregistering instances concurrently.
0681 inline size_t num_registered_instances() {
0682     auto &internals = get_internals();
0683 #ifdef Py_GIL_DISABLED
0684     size_t count = 0;
0685     for (size_t i = 0; i <= internals.instance_shards_mask; ++i) {
0686         auto &shard = internals.instance_shards[i];
0687         std::unique_lock<pymutex> lock(shard.mutex);
0688         count += shard.registered_instances.size();
0689     }
0690     return count;
0691 #else
0692     return internals.registered_instances.size();
0693 #endif
0694 }
0695 
0696 /// Constructs a std::string with the given arguments, stores it in `internals`, and returns its
0697 /// `c_str()`.  Such strings objects have a long storage duration -- the internal strings are only
0698 /// cleared when the program exits or after interpreter shutdown (when embedding), and so are
0699 /// suitable for c-style strings needed by Python internals (such as PyTypeObject's tp_name).
0700 template <typename... Args>
0701 const char *c_str(Args &&...args) {
0702     // GCC 4.8 doesn't like parameter unpack within lambda capture, so use
0703     // PYBIND11_LOCK_INTERNALS.
0704     auto &internals = get_internals();
0705     PYBIND11_LOCK_INTERNALS(internals);
0706     auto &strings = internals.static_strings;
0707     strings.emplace_front(std::forward<Args>(args)...);
0708     return strings.front().c_str();
0709 }
0710 
0711 inline const char *get_function_record_capsule_name() {
0712 #if PYBIND11_INTERNALS_VERSION > 4
0713     return get_internals().function_record_capsule_name.c_str();
0714 #else
0715     return nullptr;
0716 #endif
0717 }
0718 
0719 // Determine whether or not the following capsule contains a pybind11 function record.
0720 // Note that we use `internals` to make sure that only ABI compatible records are touched.
0721 //
0722 // This check is currently used in two places:
0723 // - An important optimization in functional.h to avoid overhead in C++ -> Python -> C++
0724 // - The sibling feature of cpp_function to allow overloads
0725 inline bool is_function_record_capsule(const capsule &cap) {
0726     // Pointer equality as we rely on internals() to ensure unique pointers
0727     return cap.name() == get_function_record_capsule_name();
0728 }
0729 
0730 PYBIND11_NAMESPACE_END(detail)
0731 
0732 /// Returns a named pointer that is shared among all extension modules (using the same
0733 /// pybind11 version) running in the current interpreter. Names starting with underscores
0734 /// are reserved for internal usage. Returns `nullptr` if no matching entry was found.
0735 PYBIND11_NOINLINE void *get_shared_data(const std::string &name) {
0736     return detail::with_internals([&](detail::internals &internals) {
0737         auto it = internals.shared_data.find(name);
0738         return it != internals.shared_data.end() ? it->second : nullptr;
0739     });
0740 }
0741 
0742 /// Set the shared data that can be later recovered by `get_shared_data()`.
0743 PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) {
0744     return detail::with_internals([&](detail::internals &internals) {
0745         internals.shared_data[name] = data;
0746         return data;
0747     });
0748 }
0749 
0750 /// Returns a typed reference to a shared data entry (by using `get_shared_data()`) if
0751 /// such entry exists. Otherwise, a new object of default-constructible type `T` is
0752 /// added to the shared data under the given name and a reference to it is returned.
0753 template <typename T>
0754 T &get_or_create_shared_data(const std::string &name) {
0755     return *detail::with_internals([&](detail::internals &internals) {
0756         auto it = internals.shared_data.find(name);
0757         T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr);
0758         if (!ptr) {
0759             ptr = new T();
0760             internals.shared_data[name] = ptr;
0761         }
0762         return ptr;
0763     });
0764 }
0765 
0766 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)