Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /jana2/src/python/externals/pybind11-2.10.3/include/pybind11/detail/internals.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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(WITH_THREAD) && defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
0015 #    include "../gil.h"
0016 #endif
0017 
0018 #include "../pytypes.h"
0019 
0020 #include <exception>
0021 
0022 /// Tracks the `internals` and `type_info` ABI version independent of the main library version.
0023 ///
0024 /// Some portions of the code use an ABI that is conditional depending on this
0025 /// version number.  That allows ABI-breaking changes to be "pre-implemented".
0026 /// Once the default version number is incremented, the conditional logic that
0027 /// no longer applies can be removed.  Additionally, users that need not
0028 /// maintain ABI compatibility can increase the version number in order to take
0029 /// advantage of any functionality/efficiency improvements that depend on the
0030 /// newer ABI.
0031 ///
0032 /// WARNING: If you choose to manually increase the ABI version, note that
0033 /// pybind11 may not be tested as thoroughly with a non-default ABI version, and
0034 /// further ABI-incompatible changes may be made before the ABI is officially
0035 /// changed to the new version.
0036 #ifndef PYBIND11_INTERNALS_VERSION
0037 #    define PYBIND11_INTERNALS_VERSION 4
0038 #endif
0039 
0040 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0041 
0042 using ExceptionTranslator = void (*)(std::exception_ptr);
0043 
0044 PYBIND11_NAMESPACE_BEGIN(detail)
0045 
0046 constexpr const char *internals_function_record_capsule_name = "pybind11_function_record_capsule";
0047 
0048 // Forward declarations
0049 inline PyTypeObject *make_static_property_type();
0050 inline PyTypeObject *make_default_metaclass();
0051 inline PyObject *make_object_base_type(PyTypeObject *metaclass);
0052 
0053 // The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new
0054 // Thread Specific Storage (TSS) API.
0055 #if PY_VERSION_HEX >= 0x03070000
0056 // Avoid unnecessary allocation of `Py_tss_t`, since we cannot use
0057 // `Py_LIMITED_API` anyway.
0058 #    if PYBIND11_INTERNALS_VERSION > 4
0059 #        define PYBIND11_TLS_KEY_REF Py_tss_t &
0060 #        if defined(__GNUC__) && !defined(__INTEL_COMPILER)
0061 // Clang on macOS warns due to `Py_tss_NEEDS_INIT` not specifying an initializer
0062 // for every field.
0063 #            define PYBIND11_TLS_KEY_INIT(var)                                                    \
0064                 _Pragma("GCC diagnostic push")                                         /**/       \
0065                     _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") /**/       \
0066                     Py_tss_t var                                                                  \
0067                     = Py_tss_NEEDS_INIT;                                                          \
0068                 _Pragma("GCC diagnostic pop")
0069 #        else
0070 #            define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT;
0071 #        endif
0072 #        define PYBIND11_TLS_KEY_CREATE(var) (PyThread_tss_create(&(var)) == 0)
0073 #        define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get(&(key))
0074 #        define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value))
0075 #        define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr)
0076 #        define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key))
0077 #    else
0078 #        define PYBIND11_TLS_KEY_REF Py_tss_t *
0079 #        define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr;
0080 #        define PYBIND11_TLS_KEY_CREATE(var)                                                      \
0081             (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0))
0082 #        define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
0083 #        define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value))
0084 #        define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
0085 #        define PYBIND11_TLS_FREE(key) PyThread_tss_free(key)
0086 #    endif
0087 #else
0088 // Usually an int but a long on Cygwin64 with Python 3.x
0089 #    define PYBIND11_TLS_KEY_REF decltype(PyThread_create_key())
0090 #    define PYBIND11_TLS_KEY_INIT(var) PYBIND11_TLS_KEY_REF var = 0;
0091 #    define PYBIND11_TLS_KEY_CREATE(var) (((var) = PyThread_create_key()) != -1)
0092 #    define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key))
0093 #    if defined(PYPY_VERSION)
0094 // On CPython < 3.4 and on PyPy, `PyThread_set_key_value` strangely does not set
0095 // the value if it has already been set.  Instead, it must first be deleted and
0096 // then set again.
0097 inline void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value) {
0098     PyThread_delete_key_value(key);
0099     PyThread_set_key_value(key, value);
0100 }
0101 #        define PYBIND11_TLS_DELETE_VALUE(key) PyThread_delete_key_value(key)
0102 #        define PYBIND11_TLS_REPLACE_VALUE(key, value)                                            \
0103             ::pybind11::detail::tls_replace_value((key), (value))
0104 #    else
0105 #        define PYBIND11_TLS_DELETE_VALUE(key) PyThread_set_key_value((key), nullptr)
0106 #        define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_set_key_value((key), (value))
0107 #    endif
0108 #    define PYBIND11_TLS_FREE(key) (void) key
0109 #endif
0110 
0111 // Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
0112 // other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module
0113 // even when `A` is the same, non-hidden-visibility type (e.g. from a common include).  Under
0114 // libstdc++, this doesn't happen: equality and the type_index hash are based on the type name,
0115 // which works.  If not under a known-good stl, provide our own name-based hash and equality
0116 // functions that use the type name.
0117 #if defined(__GLIBCXX__)
0118 inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
0119 using type_hash = std::hash<std::type_index>;
0120 using type_equal_to = std::equal_to<std::type_index>;
0121 #else
0122 inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
0123     return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
0124 }
0125 
0126 struct type_hash {
0127     size_t operator()(const std::type_index &t) const {
0128         size_t hash = 5381;
0129         const char *ptr = t.name();
0130         while (auto c = static_cast<unsigned char>(*ptr++)) {
0131             hash = (hash * 33) ^ c;
0132         }
0133         return hash;
0134     }
0135 };
0136 
0137 struct type_equal_to {
0138     bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
0139         return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
0140     }
0141 };
0142 #endif
0143 
0144 template <typename value_type>
0145 using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
0146 
0147 struct override_hash {
0148     inline size_t operator()(const std::pair<const PyObject *, const char *> &v) const {
0149         size_t value = std::hash<const void *>()(v.first);
0150         value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value << 6) + (value >> 2);
0151         return value;
0152     }
0153 };
0154 
0155 /// Internal data structure used to track registered instances and types.
0156 /// Whenever binary incompatible changes are made to this structure,
0157 /// `PYBIND11_INTERNALS_VERSION` must be incremented.
0158 struct internals {
0159     // std::type_index -> pybind11's type information
0160     type_map<type_info *> registered_types_cpp;
0161     // PyTypeObject* -> base type_info(s)
0162     std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py;
0163     std::unordered_multimap<const void *, instance *> registered_instances; // void * -> instance*
0164     std::unordered_set<std::pair<const PyObject *, const char *>, override_hash>
0165         inactive_override_cache;
0166     type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
0167     std::unordered_map<const PyObject *, std::vector<PyObject *>> patients;
0168     std::forward_list<ExceptionTranslator> registered_exception_translators;
0169     std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across
0170                                                          // extensions
0171 #if PYBIND11_INTERNALS_VERSION == 4
0172     std::vector<PyObject *> unused_loader_patient_stack_remove_at_v5;
0173 #endif
0174     std::forward_list<std::string> static_strings; // Stores the std::strings backing
0175                                                    // detail::c_str()
0176     PyTypeObject *static_property_type;
0177     PyTypeObject *default_metaclass;
0178     PyObject *instance_base;
0179 #if defined(WITH_THREAD)
0180     // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
0181     PYBIND11_TLS_KEY_INIT(tstate)
0182 #    if PYBIND11_INTERNALS_VERSION > 4
0183     PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
0184 #    endif // PYBIND11_INTERNALS_VERSION > 4
0185     // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
0186     PyInterpreterState *istate = nullptr;
0187 
0188 #    if PYBIND11_INTERNALS_VERSION > 4
0189     // Note that we have to use a std::string to allocate memory to ensure a unique address
0190     // We want unique addresses since we use pointer equality to compare function records
0191     std::string function_record_capsule_name = internals_function_record_capsule_name;
0192 #    endif
0193 
0194     internals() = default;
0195     internals(const internals &other) = delete;
0196     internals &operator=(const internals &other) = delete;
0197     ~internals() {
0198 #    if PYBIND11_INTERNALS_VERSION > 4
0199         PYBIND11_TLS_FREE(loader_life_support_tls_key);
0200 #    endif // PYBIND11_INTERNALS_VERSION > 4
0201 
0202         // This destructor is called *after* Py_Finalize() in finalize_interpreter().
0203         // That *SHOULD BE* fine. The following details what happens when PyThread_tss_free is
0204         // called. PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does
0205         // nothing. PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree.
0206         // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX).
0207         // Neither of those have anything to do with CPython internals. PyMem_RawFree *requires*
0208         // that the `tstate` be allocated with the CPython allocator.
0209         PYBIND11_TLS_FREE(tstate);
0210     }
0211 #endif
0212 };
0213 
0214 /// Additional type information which does not fit into the PyTypeObject.
0215 /// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`.
0216 struct type_info {
0217     PyTypeObject *type;
0218     const std::type_info *cpptype;
0219     size_t type_size, type_align, holder_size_in_ptrs;
0220     void *(*operator_new)(size_t);
0221     void (*init_instance)(instance *, const void *);
0222     void (*dealloc)(value_and_holder &v_h);
0223     std::vector<PyObject *(*) (PyObject *, PyTypeObject *)> implicit_conversions;
0224     std::vector<std::pair<const std::type_info *, void *(*) (void *)>> implicit_casts;
0225     std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
0226     buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
0227     void *get_buffer_data = nullptr;
0228     void *(*module_local_load)(PyObject *, const type_info *) = nullptr;
0229     /* A simple type never occurs as a (direct or indirect) parent
0230      * of a class that makes use of multiple inheritance.
0231      * A type can be simple even if it has non-simple ancestors as long as it has no descendants.
0232      */
0233     bool simple_type : 1;
0234     /* True if there is no multiple inheritance in this type's inheritance tree */
0235     bool simple_ancestors : 1;
0236     /* for base vs derived holder_type checks */
0237     bool default_holder : 1;
0238     /* true if this is a type registered with py::module_local */
0239     bool module_local : 1;
0240 };
0241 
0242 /// On MSVC, debug and release builds are not ABI-compatible!
0243 #if defined(_MSC_VER) && defined(_DEBUG)
0244 #    define PYBIND11_BUILD_TYPE "_debug"
0245 #else
0246 #    define PYBIND11_BUILD_TYPE ""
0247 #endif
0248 
0249 /// Let's assume that different compilers are ABI-incompatible.
0250 /// A user can manually set this string if they know their
0251 /// compiler is compatible.
0252 #ifndef PYBIND11_COMPILER_TYPE
0253 #    if defined(_MSC_VER)
0254 #        define PYBIND11_COMPILER_TYPE "_msvc"
0255 #    elif defined(__INTEL_COMPILER)
0256 #        define PYBIND11_COMPILER_TYPE "_icc"
0257 #    elif defined(__clang__)
0258 #        define PYBIND11_COMPILER_TYPE "_clang"
0259 #    elif defined(__PGI)
0260 #        define PYBIND11_COMPILER_TYPE "_pgi"
0261 #    elif defined(__MINGW32__)
0262 #        define PYBIND11_COMPILER_TYPE "_mingw"
0263 #    elif defined(__CYGWIN__)
0264 #        define PYBIND11_COMPILER_TYPE "_gcc_cygwin"
0265 #    elif defined(__GNUC__)
0266 #        define PYBIND11_COMPILER_TYPE "_gcc"
0267 #    else
0268 #        define PYBIND11_COMPILER_TYPE "_unknown"
0269 #    endif
0270 #endif
0271 
0272 /// Also standard libs
0273 #ifndef PYBIND11_STDLIB
0274 #    if defined(_LIBCPP_VERSION)
0275 #        define PYBIND11_STDLIB "_libcpp"
0276 #    elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
0277 #        define PYBIND11_STDLIB "_libstdcpp"
0278 #    else
0279 #        define PYBIND11_STDLIB ""
0280 #    endif
0281 #endif
0282 
0283 /// On Linux/OSX, changes in __GXX_ABI_VERSION__ indicate ABI incompatibility.
0284 #ifndef PYBIND11_BUILD_ABI
0285 #    if defined(__GXX_ABI_VERSION)
0286 #        define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION)
0287 #    else
0288 #        define PYBIND11_BUILD_ABI ""
0289 #    endif
0290 #endif
0291 
0292 #ifndef PYBIND11_INTERNALS_KIND
0293 #    if defined(WITH_THREAD)
0294 #        define PYBIND11_INTERNALS_KIND ""
0295 #    else
0296 #        define PYBIND11_INTERNALS_KIND "_without_thread"
0297 #    endif
0298 #endif
0299 
0300 #define PYBIND11_INTERNALS_ID                                                                     \
0301     "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION)                        \
0302         PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI         \
0303             PYBIND11_BUILD_TYPE "__"
0304 
0305 #define PYBIND11_MODULE_LOCAL_ID                                                                  \
0306     "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION)                     \
0307         PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI         \
0308             PYBIND11_BUILD_TYPE "__"
0309 
0310 /// Each module locally stores a pointer to the `internals` data. The data
0311 /// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`.
0312 inline internals **&get_internals_pp() {
0313     static internals **internals_pp = nullptr;
0314     return internals_pp;
0315 }
0316 
0317 // forward decl
0318 inline void translate_exception(std::exception_ptr);
0319 
0320 template <class T,
0321           enable_if_t<std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
0322 bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
0323     std::exception_ptr nested = exc.nested_ptr();
0324     if (nested != nullptr && nested != p) {
0325         translate_exception(nested);
0326         return true;
0327     }
0328     return false;
0329 }
0330 
0331 template <class T,
0332           enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
0333 bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
0334     if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
0335         return handle_nested_exception(*nep, p);
0336     }
0337     return false;
0338 }
0339 
0340 inline bool raise_err(PyObject *exc_type, const char *msg) {
0341     if (PyErr_Occurred()) {
0342         raise_from(exc_type, msg);
0343         return true;
0344     }
0345     PyErr_SetString(exc_type, msg);
0346     return false;
0347 }
0348 
0349 inline void translate_exception(std::exception_ptr p) {
0350     if (!p) {
0351         return;
0352     }
0353     try {
0354         std::rethrow_exception(p);
0355     } catch (error_already_set &e) {
0356         handle_nested_exception(e, p);
0357         e.restore();
0358         return;
0359     } catch (const builtin_exception &e) {
0360         // Could not use template since it's an abstract class.
0361         if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
0362             handle_nested_exception(*nep, p);
0363         }
0364         e.set_error();
0365         return;
0366     } catch (const std::bad_alloc &e) {
0367         handle_nested_exception(e, p);
0368         raise_err(PyExc_MemoryError, e.what());
0369         return;
0370     } catch (const std::domain_error &e) {
0371         handle_nested_exception(e, p);
0372         raise_err(PyExc_ValueError, e.what());
0373         return;
0374     } catch (const std::invalid_argument &e) {
0375         handle_nested_exception(e, p);
0376         raise_err(PyExc_ValueError, e.what());
0377         return;
0378     } catch (const std::length_error &e) {
0379         handle_nested_exception(e, p);
0380         raise_err(PyExc_ValueError, e.what());
0381         return;
0382     } catch (const std::out_of_range &e) {
0383         handle_nested_exception(e, p);
0384         raise_err(PyExc_IndexError, e.what());
0385         return;
0386     } catch (const std::range_error &e) {
0387         handle_nested_exception(e, p);
0388         raise_err(PyExc_ValueError, e.what());
0389         return;
0390     } catch (const std::overflow_error &e) {
0391         handle_nested_exception(e, p);
0392         raise_err(PyExc_OverflowError, e.what());
0393         return;
0394     } catch (const std::exception &e) {
0395         handle_nested_exception(e, p);
0396         raise_err(PyExc_RuntimeError, e.what());
0397         return;
0398     } catch (const std::nested_exception &e) {
0399         handle_nested_exception(e, p);
0400         raise_err(PyExc_RuntimeError, "Caught an unknown nested exception!");
0401         return;
0402     } catch (...) {
0403         raise_err(PyExc_RuntimeError, "Caught an unknown exception!");
0404         return;
0405     }
0406 }
0407 
0408 #if !defined(__GLIBCXX__)
0409 inline void translate_local_exception(std::exception_ptr p) {
0410     try {
0411         if (p) {
0412             std::rethrow_exception(p);
0413         }
0414     } catch (error_already_set &e) {
0415         e.restore();
0416         return;
0417     } catch (const builtin_exception &e) {
0418         e.set_error();
0419         return;
0420     }
0421 }
0422 #endif
0423 
0424 /// Return a reference to the current `internals` data
0425 PYBIND11_NOINLINE internals &get_internals() {
0426     auto **&internals_pp = get_internals_pp();
0427     if (internals_pp && *internals_pp) {
0428         return **internals_pp;
0429     }
0430 
0431 #if defined(WITH_THREAD)
0432 #    if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
0433     gil_scoped_acquire gil;
0434 #    else
0435     // Ensure that the GIL is held since we will need to make Python calls.
0436     // Cannot use py::gil_scoped_acquire here since that constructor calls get_internals.
0437     struct gil_scoped_acquire_local {
0438         gil_scoped_acquire_local() : state(PyGILState_Ensure()) {}
0439         gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete;
0440         gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete;
0441         ~gil_scoped_acquire_local() { PyGILState_Release(state); }
0442         const PyGILState_STATE state;
0443     } gil;
0444 #    endif
0445 #endif
0446     error_scope err_scope;
0447 
0448     PYBIND11_STR_TYPE id(PYBIND11_INTERNALS_ID);
0449     auto builtins = handle(PyEval_GetBuiltins());
0450     if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
0451         internals_pp = static_cast<internals **>(capsule(builtins[id]));
0452 
0453         // We loaded builtins through python's builtins, which means that our `error_already_set`
0454         // and `builtin_exception` may be different local classes than the ones set up in the
0455         // initial exception translator, below, so add another for our local exception classes.
0456         //
0457         // libstdc++ doesn't require this (types there are identified only by name)
0458         // libc++ with CPython doesn't require this (types are explicitly exported)
0459         // libc++ with PyPy still need it, awaiting further investigation
0460 #if !defined(__GLIBCXX__)
0461         (*internals_pp)->registered_exception_translators.push_front(&translate_local_exception);
0462 #endif
0463     } else {
0464         if (!internals_pp) {
0465             internals_pp = new internals *();
0466         }
0467         auto *&internals_ptr = *internals_pp;
0468         internals_ptr = new internals();
0469 #if defined(WITH_THREAD)
0470 
0471         PyThreadState *tstate = PyThreadState_Get();
0472         if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->tstate)) {
0473             pybind11_fail("get_internals: could not successfully initialize the tstate TSS key!");
0474         }
0475         PYBIND11_TLS_REPLACE_VALUE(internals_ptr->tstate, tstate);
0476 
0477 #    if PYBIND11_INTERNALS_VERSION > 4
0478         if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->loader_life_support_tls_key)) {
0479             pybind11_fail("get_internals: could not successfully initialize the "
0480                           "loader_life_support TSS key!");
0481         }
0482 #    endif
0483         internals_ptr->istate = tstate->interp;
0484 #endif
0485         builtins[id] = capsule(internals_pp);
0486         internals_ptr->registered_exception_translators.push_front(&translate_exception);
0487         internals_ptr->static_property_type = make_static_property_type();
0488         internals_ptr->default_metaclass = make_default_metaclass();
0489         internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass);
0490     }
0491     return **internals_pp;
0492 }
0493 
0494 // the internals struct (above) is shared between all the modules. local_internals are only
0495 // for a single module. Any changes made to internals may require an update to
0496 // PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design,
0497 // restricted to a single module. Whether a module has local internals or not should not
0498 // impact any other modules, because the only things accessing the local internals is the
0499 // module that contains them.
0500 struct local_internals {
0501     type_map<type_info *> registered_types_cpp;
0502     std::forward_list<ExceptionTranslator> registered_exception_translators;
0503 #if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
0504 
0505     // For ABI compatibility, we can't store the loader_life_support TLS key in
0506     // the `internals` struct directly.  Instead, we store it in `shared_data` and
0507     // cache a copy in `local_internals`.  If we allocated a separate TLS key for
0508     // each instance of `local_internals`, we could end up allocating hundreds of
0509     // TLS keys if hundreds of different pybind11 modules are loaded (which is a
0510     // plausible number).
0511     PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
0512 
0513     // Holds the shared TLS key for the loader_life_support stack.
0514     struct shared_loader_life_support_data {
0515         PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
0516         shared_loader_life_support_data() {
0517             if (!PYBIND11_TLS_KEY_CREATE(loader_life_support_tls_key)) {
0518                 pybind11_fail("local_internals: could not successfully initialize the "
0519                               "loader_life_support TLS key!");
0520             }
0521         }
0522         // We can't help but leak the TLS key, because Python never unloads extension modules.
0523     };
0524 
0525     local_internals() {
0526         auto &internals = get_internals();
0527         // Get or create the `loader_life_support_stack_key`.
0528         auto &ptr = internals.shared_data["_life_support"];
0529         if (!ptr) {
0530             ptr = new shared_loader_life_support_data;
0531         }
0532         loader_life_support_tls_key
0533             = static_cast<shared_loader_life_support_data *>(ptr)->loader_life_support_tls_key;
0534     }
0535 #endif //  defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
0536 };
0537 
0538 /// Works like `get_internals`, but for things which are locally registered.
0539 inline local_internals &get_local_internals() {
0540     // Current static can be created in the interpreter finalization routine. If the later will be
0541     // destroyed in another static variable destructor, creation of this static there will cause
0542     // static deinitialization fiasco. In order to avoid it we avoid destruction of the
0543     // local_internals static. One can read more about the problem and current solution here:
0544     // https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
0545     static auto *locals = new local_internals();
0546     return *locals;
0547 }
0548 
0549 /// Constructs a std::string with the given arguments, stores it in `internals`, and returns its
0550 /// `c_str()`.  Such strings objects have a long storage duration -- the internal strings are only
0551 /// cleared when the program exits or after interpreter shutdown (when embedding), and so are
0552 /// suitable for c-style strings needed by Python internals (such as PyTypeObject's tp_name).
0553 template <typename... Args>
0554 const char *c_str(Args &&...args) {
0555     auto &strings = get_internals().static_strings;
0556     strings.emplace_front(std::forward<Args>(args)...);
0557     return strings.front().c_str();
0558 }
0559 
0560 inline const char *get_function_record_capsule_name() {
0561 #if PYBIND11_INTERNALS_VERSION > 4
0562     return get_internals().function_record_capsule_name.c_str();
0563 #else
0564     return nullptr;
0565 #endif
0566 }
0567 
0568 // Determine whether or not the following capsule contains a pybind11 function record.
0569 // Note that we use `internals` to make sure that only ABI compatible records are touched.
0570 //
0571 // This check is currently used in two places:
0572 // - An important optimization in functional.h to avoid overhead in C++ -> Python -> C++
0573 // - The sibling feature of cpp_function to allow overloads
0574 inline bool is_function_record_capsule(const capsule &cap) {
0575     // Pointer equality as we rely on internals() to ensure unique pointers
0576     return cap.name() == get_function_record_capsule_name();
0577 }
0578 
0579 PYBIND11_NAMESPACE_END(detail)
0580 
0581 /// Returns a named pointer that is shared among all extension modules (using the same
0582 /// pybind11 version) running in the current interpreter. Names starting with underscores
0583 /// are reserved for internal usage. Returns `nullptr` if no matching entry was found.
0584 PYBIND11_NOINLINE void *get_shared_data(const std::string &name) {
0585     auto &internals = detail::get_internals();
0586     auto it = internals.shared_data.find(name);
0587     return it != internals.shared_data.end() ? it->second : nullptr;
0588 }
0589 
0590 /// Set the shared data that can be later recovered by `get_shared_data()`.
0591 PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) {
0592     detail::get_internals().shared_data[name] = data;
0593     return data;
0594 }
0595 
0596 /// Returns a typed reference to a shared data entry (by using `get_shared_data()`) if
0597 /// such entry exists. Otherwise, a new object of default-constructible type `T` is
0598 /// added to the shared data under the given name and a reference to it is returned.
0599 template <typename T>
0600 T &get_or_create_shared_data(const std::string &name) {
0601     auto &internals = detail::get_internals();
0602     auto it = internals.shared_data.find(name);
0603     T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr);
0604     if (!ptr) {
0605         ptr = new T();
0606         internals.shared_data[name] = ptr;
0607     }
0608     return *ptr;
0609 }
0610 
0611 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)