internals.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. pybind11/detail/internals.h: Internal data structure and related functions
  3. Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #pragma once
  8. #include "common.h"
  9. #if defined(WITH_THREAD) && defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
  10. # include "../gil.h"
  11. #endif
  12. #include "../pytypes.h"
  13. #include <exception>
  14. /// Tracks the `internals` and `type_info` ABI version independent of the main library version.
  15. ///
  16. /// Some portions of the code use an ABI that is conditional depending on this
  17. /// version number. That allows ABI-breaking changes to be "pre-implemented".
  18. /// Once the default version number is incremented, the conditional logic that
  19. /// no longer applies can be removed. Additionally, users that need not
  20. /// maintain ABI compatibility can increase the version number in order to take
  21. /// advantage of any functionality/efficiency improvements that depend on the
  22. /// newer ABI.
  23. ///
  24. /// WARNING: If you choose to manually increase the ABI version, note that
  25. /// pybind11 may not be tested as thoroughly with a non-default ABI version, and
  26. /// further ABI-incompatible changes may be made before the ABI is officially
  27. /// changed to the new version.
  28. #ifndef PYBIND11_INTERNALS_VERSION
  29. # define PYBIND11_INTERNALS_VERSION 4
  30. #endif
  31. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  32. using ExceptionTranslator = void (*)(std::exception_ptr);
  33. PYBIND11_NAMESPACE_BEGIN(detail)
  34. // Forward declarations
  35. inline PyTypeObject *make_static_property_type();
  36. inline PyTypeObject *make_default_metaclass();
  37. inline PyObject *make_object_base_type(PyTypeObject *metaclass);
  38. // The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new
  39. // Thread Specific Storage (TSS) API.
  40. #if PY_VERSION_HEX >= 0x03070000
  41. // Avoid unnecessary allocation of `Py_tss_t`, since we cannot use
  42. // `Py_LIMITED_API` anyway.
  43. # if PYBIND11_INTERNALS_VERSION > 4
  44. # define PYBIND11_TLS_KEY_REF Py_tss_t &
  45. # if defined(__GNUC__) && !defined(__INTEL_COMPILER)
  46. // Clang on macOS warns due to `Py_tss_NEEDS_INIT` not specifying an initializer
  47. // for every field.
  48. # define PYBIND11_TLS_KEY_INIT(var) \
  49. _Pragma("GCC diagnostic push") /**/ \
  50. _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") /**/ \
  51. Py_tss_t var \
  52. = Py_tss_NEEDS_INIT; \
  53. _Pragma("GCC diagnostic pop")
  54. # else
  55. # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT;
  56. # endif
  57. # define PYBIND11_TLS_KEY_CREATE(var) (PyThread_tss_create(&(var)) == 0)
  58. # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get(&(key))
  59. # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value))
  60. # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr)
  61. # define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key))
  62. # else
  63. # define PYBIND11_TLS_KEY_REF Py_tss_t *
  64. # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr;
  65. # define PYBIND11_TLS_KEY_CREATE(var) \
  66. (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0))
  67. # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
  68. # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value))
  69. # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
  70. # define PYBIND11_TLS_FREE(key) PyThread_tss_free(key)
  71. # endif
  72. #else
  73. // Usually an int but a long on Cygwin64 with Python 3.x
  74. # define PYBIND11_TLS_KEY_REF decltype(PyThread_create_key())
  75. # define PYBIND11_TLS_KEY_INIT(var) PYBIND11_TLS_KEY_REF var = 0;
  76. # define PYBIND11_TLS_KEY_CREATE(var) (((var) = PyThread_create_key()) != -1)
  77. # define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key))
  78. # if defined(PYPY_VERSION)
  79. // On CPython < 3.4 and on PyPy, `PyThread_set_key_value` strangely does not set
  80. // the value if it has already been set. Instead, it must first be deleted and
  81. // then set again.
  82. inline void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value) {
  83. PyThread_delete_key_value(key);
  84. PyThread_set_key_value(key, value);
  85. }
  86. # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_delete_key_value(key)
  87. # define PYBIND11_TLS_REPLACE_VALUE(key, value) \
  88. ::pybind11::detail::tls_replace_value((key), (value))
  89. # else
  90. # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_set_key_value((key), nullptr)
  91. # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_set_key_value((key), (value))
  92. # endif
  93. # define PYBIND11_TLS_FREE(key) (void) key
  94. #endif
  95. // Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
  96. // other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module
  97. // even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under
  98. // libstdc++, this doesn't happen: equality and the type_index hash are based on the type name,
  99. // which works. If not under a known-good stl, provide our own name-based hash and equality
  100. // functions that use the type name.
  101. #if defined(__GLIBCXX__)
  102. inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
  103. using type_hash = std::hash<std::type_index>;
  104. using type_equal_to = std::equal_to<std::type_index>;
  105. #else
  106. inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
  107. return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
  108. }
  109. struct type_hash {
  110. size_t operator()(const std::type_index &t) const {
  111. size_t hash = 5381;
  112. const char *ptr = t.name();
  113. while (auto c = static_cast<unsigned char>(*ptr++)) {
  114. hash = (hash * 33) ^ c;
  115. }
  116. return hash;
  117. }
  118. };
  119. struct type_equal_to {
  120. bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
  121. return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
  122. }
  123. };
  124. #endif
  125. template <typename value_type>
  126. using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
  127. struct override_hash {
  128. inline size_t operator()(const std::pair<const PyObject *, const char *> &v) const {
  129. size_t value = std::hash<const void *>()(v.first);
  130. value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value << 6) + (value >> 2);
  131. return value;
  132. }
  133. };
  134. /// Internal data structure used to track registered instances and types.
  135. /// Whenever binary incompatible changes are made to this structure,
  136. /// `PYBIND11_INTERNALS_VERSION` must be incremented.
  137. struct internals {
  138. // std::type_index -> pybind11's type information
  139. type_map<type_info *> registered_types_cpp;
  140. // PyTypeObject* -> base type_info(s)
  141. std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py;
  142. std::unordered_multimap<const void *, instance *> registered_instances; // void * -> instance*
  143. std::unordered_set<std::pair<const PyObject *, const char *>, override_hash>
  144. inactive_override_cache;
  145. type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
  146. std::unordered_map<const PyObject *, std::vector<PyObject *>> patients;
  147. std::forward_list<ExceptionTranslator> registered_exception_translators;
  148. std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across
  149. // extensions
  150. #if PYBIND11_INTERNALS_VERSION == 4
  151. std::vector<PyObject *> unused_loader_patient_stack_remove_at_v5;
  152. #endif
  153. std::forward_list<std::string> static_strings; // Stores the std::strings backing
  154. // detail::c_str()
  155. PyTypeObject *static_property_type;
  156. PyTypeObject *default_metaclass;
  157. PyObject *instance_base;
  158. #if defined(WITH_THREAD)
  159. // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
  160. PYBIND11_TLS_KEY_INIT(tstate)
  161. # if PYBIND11_INTERNALS_VERSION > 4
  162. PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
  163. # endif // PYBIND11_INTERNALS_VERSION > 4
  164. // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
  165. PyInterpreterState *istate = nullptr;
  166. ~internals() {
  167. # if PYBIND11_INTERNALS_VERSION > 4
  168. PYBIND11_TLS_FREE(loader_life_support_tls_key);
  169. # endif // PYBIND11_INTERNALS_VERSION > 4
  170. // This destructor is called *after* Py_Finalize() in finalize_interpreter().
  171. // That *SHOULD BE* fine. The following details what happens when PyThread_tss_free is
  172. // called. PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does
  173. // nothing. PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree.
  174. // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX).
  175. // Neither of those have anything to do with CPython internals. PyMem_RawFree *requires*
  176. // that the `tstate` be allocated with the CPython allocator.
  177. PYBIND11_TLS_FREE(tstate);
  178. }
  179. #endif
  180. };
  181. /// Additional type information which does not fit into the PyTypeObject.
  182. /// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`.
  183. struct type_info {
  184. PyTypeObject *type;
  185. const std::type_info *cpptype;
  186. size_t type_size, type_align, holder_size_in_ptrs;
  187. void *(*operator_new)(size_t);
  188. void (*init_instance)(instance *, const void *);
  189. void (*dealloc)(value_and_holder &v_h);
  190. std::vector<PyObject *(*) (PyObject *, PyTypeObject *)> implicit_conversions;
  191. std::vector<std::pair<const std::type_info *, void *(*) (void *)>> implicit_casts;
  192. std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
  193. buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
  194. void *get_buffer_data = nullptr;
  195. void *(*module_local_load)(PyObject *, const type_info *) = nullptr;
  196. /* A simple type never occurs as a (direct or indirect) parent
  197. * of a class that makes use of multiple inheritance.
  198. * A type can be simple even if it has non-simple ancestors as long as it has no descendants.
  199. */
  200. bool simple_type : 1;
  201. /* True if there is no multiple inheritance in this type's inheritance tree */
  202. bool simple_ancestors : 1;
  203. /* for base vs derived holder_type checks */
  204. bool default_holder : 1;
  205. /* true if this is a type registered with py::module_local */
  206. bool module_local : 1;
  207. };
  208. /// On MSVC, debug and release builds are not ABI-compatible!
  209. #if defined(_MSC_VER) && defined(_DEBUG)
  210. # define PYBIND11_BUILD_TYPE "_debug"
  211. #else
  212. # define PYBIND11_BUILD_TYPE ""
  213. #endif
  214. /// Let's assume that different compilers are ABI-incompatible.
  215. /// A user can manually set this string if they know their
  216. /// compiler is compatible.
  217. #ifndef PYBIND11_COMPILER_TYPE
  218. # if defined(_MSC_VER)
  219. # define PYBIND11_COMPILER_TYPE "_msvc"
  220. # elif defined(__INTEL_COMPILER)
  221. # define PYBIND11_COMPILER_TYPE "_icc"
  222. # elif defined(__clang__)
  223. # define PYBIND11_COMPILER_TYPE "_clang"
  224. # elif defined(__PGI)
  225. # define PYBIND11_COMPILER_TYPE "_pgi"
  226. # elif defined(__MINGW32__)
  227. # define PYBIND11_COMPILER_TYPE "_mingw"
  228. # elif defined(__CYGWIN__)
  229. # define PYBIND11_COMPILER_TYPE "_gcc_cygwin"
  230. # elif defined(__GNUC__)
  231. # define PYBIND11_COMPILER_TYPE "_gcc"
  232. # else
  233. # define PYBIND11_COMPILER_TYPE "_unknown"
  234. # endif
  235. #endif
  236. /// Also standard libs
  237. #ifndef PYBIND11_STDLIB
  238. # if defined(_LIBCPP_VERSION)
  239. # define PYBIND11_STDLIB "_libcpp"
  240. # elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
  241. # define PYBIND11_STDLIB "_libstdcpp"
  242. # else
  243. # define PYBIND11_STDLIB ""
  244. # endif
  245. #endif
  246. /// On Linux/OSX, changes in __GXX_ABI_VERSION__ indicate ABI incompatibility.
  247. #ifndef PYBIND11_BUILD_ABI
  248. # if defined(__GXX_ABI_VERSION)
  249. # define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION)
  250. # else
  251. # define PYBIND11_BUILD_ABI ""
  252. # endif
  253. #endif
  254. #ifndef PYBIND11_INTERNALS_KIND
  255. # if defined(WITH_THREAD)
  256. # define PYBIND11_INTERNALS_KIND ""
  257. # else
  258. # define PYBIND11_INTERNALS_KIND "_without_thread"
  259. # endif
  260. #endif
  261. #define PYBIND11_INTERNALS_ID \
  262. "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
  263. PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
  264. PYBIND11_BUILD_TYPE "__"
  265. #define PYBIND11_MODULE_LOCAL_ID \
  266. "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
  267. PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
  268. PYBIND11_BUILD_TYPE "__"
  269. /// Each module locally stores a pointer to the `internals` data. The data
  270. /// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`.
  271. inline internals **&get_internals_pp() {
  272. static internals **internals_pp = nullptr;
  273. return internals_pp;
  274. }
  275. // forward decl
  276. inline void translate_exception(std::exception_ptr);
  277. template <class T,
  278. enable_if_t<std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
  279. bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
  280. std::exception_ptr nested = exc.nested_ptr();
  281. if (nested != nullptr && nested != p) {
  282. translate_exception(nested);
  283. return true;
  284. }
  285. return false;
  286. }
  287. template <class T,
  288. enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
  289. bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
  290. if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
  291. return handle_nested_exception(*nep, p);
  292. }
  293. return false;
  294. }
  295. inline bool raise_err(PyObject *exc_type, const char *msg) {
  296. if (PyErr_Occurred()) {
  297. raise_from(exc_type, msg);
  298. return true;
  299. }
  300. PyErr_SetString(exc_type, msg);
  301. return false;
  302. }
  303. inline void translate_exception(std::exception_ptr p) {
  304. if (!p) {
  305. return;
  306. }
  307. try {
  308. std::rethrow_exception(p);
  309. } catch (error_already_set &e) {
  310. handle_nested_exception(e, p);
  311. e.restore();
  312. return;
  313. } catch (const builtin_exception &e) {
  314. // Could not use template since it's an abstract class.
  315. if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
  316. handle_nested_exception(*nep, p);
  317. }
  318. e.set_error();
  319. return;
  320. } catch (const std::bad_alloc &e) {
  321. handle_nested_exception(e, p);
  322. raise_err(PyExc_MemoryError, e.what());
  323. return;
  324. } catch (const std::domain_error &e) {
  325. handle_nested_exception(e, p);
  326. raise_err(PyExc_ValueError, e.what());
  327. return;
  328. } catch (const std::invalid_argument &e) {
  329. handle_nested_exception(e, p);
  330. raise_err(PyExc_ValueError, e.what());
  331. return;
  332. } catch (const std::length_error &e) {
  333. handle_nested_exception(e, p);
  334. raise_err(PyExc_ValueError, e.what());
  335. return;
  336. } catch (const std::out_of_range &e) {
  337. handle_nested_exception(e, p);
  338. raise_err(PyExc_IndexError, e.what());
  339. return;
  340. } catch (const std::range_error &e) {
  341. handle_nested_exception(e, p);
  342. raise_err(PyExc_ValueError, e.what());
  343. return;
  344. } catch (const std::overflow_error &e) {
  345. handle_nested_exception(e, p);
  346. raise_err(PyExc_OverflowError, e.what());
  347. return;
  348. } catch (const std::exception &e) {
  349. handle_nested_exception(e, p);
  350. raise_err(PyExc_RuntimeError, e.what());
  351. return;
  352. } catch (const std::nested_exception &e) {
  353. handle_nested_exception(e, p);
  354. raise_err(PyExc_RuntimeError, "Caught an unknown nested exception!");
  355. return;
  356. } catch (...) {
  357. raise_err(PyExc_RuntimeError, "Caught an unknown exception!");
  358. return;
  359. }
  360. }
  361. #if !defined(__GLIBCXX__)
  362. inline void translate_local_exception(std::exception_ptr p) {
  363. try {
  364. if (p) {
  365. std::rethrow_exception(p);
  366. }
  367. } catch (error_already_set &e) {
  368. e.restore();
  369. return;
  370. } catch (const builtin_exception &e) {
  371. e.set_error();
  372. return;
  373. }
  374. }
  375. #endif
  376. /// Return a reference to the current `internals` data
  377. PYBIND11_NOINLINE internals &get_internals() {
  378. auto **&internals_pp = get_internals_pp();
  379. if (internals_pp && *internals_pp) {
  380. return **internals_pp;
  381. }
  382. #if defined(WITH_THREAD)
  383. # if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
  384. gil_scoped_acquire gil;
  385. # else
  386. // Ensure that the GIL is held since we will need to make Python calls.
  387. // Cannot use py::gil_scoped_acquire here since that constructor calls get_internals.
  388. struct gil_scoped_acquire_local {
  389. gil_scoped_acquire_local() : state(PyGILState_Ensure()) {}
  390. gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete;
  391. gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete;
  392. ~gil_scoped_acquire_local() { PyGILState_Release(state); }
  393. const PyGILState_STATE state;
  394. } gil;
  395. # endif
  396. #endif
  397. error_scope err_scope;
  398. PYBIND11_STR_TYPE id(PYBIND11_INTERNALS_ID);
  399. auto builtins = handle(PyEval_GetBuiltins());
  400. if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
  401. internals_pp = static_cast<internals **>(capsule(builtins[id]));
  402. // We loaded builtins through python's builtins, which means that our `error_already_set`
  403. // and `builtin_exception` may be different local classes than the ones set up in the
  404. // initial exception translator, below, so add another for our local exception classes.
  405. //
  406. // libstdc++ doesn't require this (types there are identified only by name)
  407. // libc++ with CPython doesn't require this (types are explicitly exported)
  408. // libc++ with PyPy still need it, awaiting further investigation
  409. #if !defined(__GLIBCXX__)
  410. (*internals_pp)->registered_exception_translators.push_front(&translate_local_exception);
  411. #endif
  412. } else {
  413. if (!internals_pp) {
  414. internals_pp = new internals *();
  415. }
  416. auto *&internals_ptr = *internals_pp;
  417. internals_ptr = new internals();
  418. #if defined(WITH_THREAD)
  419. # if PY_VERSION_HEX < 0x03090000
  420. PyEval_InitThreads();
  421. # endif
  422. PyThreadState *tstate = PyThreadState_Get();
  423. if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->tstate)) {
  424. pybind11_fail("get_internals: could not successfully initialize the tstate TSS key!");
  425. }
  426. PYBIND11_TLS_REPLACE_VALUE(internals_ptr->tstate, tstate);
  427. # if PYBIND11_INTERNALS_VERSION > 4
  428. if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->loader_life_support_tls_key)) {
  429. pybind11_fail("get_internals: could not successfully initialize the "
  430. "loader_life_support TSS key!");
  431. }
  432. # endif
  433. internals_ptr->istate = tstate->interp;
  434. #endif
  435. builtins[id] = capsule(internals_pp);
  436. internals_ptr->registered_exception_translators.push_front(&translate_exception);
  437. internals_ptr->static_property_type = make_static_property_type();
  438. internals_ptr->default_metaclass = make_default_metaclass();
  439. internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass);
  440. }
  441. return **internals_pp;
  442. }
  443. // the internals struct (above) is shared between all the modules. local_internals are only
  444. // for a single module. Any changes made to internals may require an update to
  445. // PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design,
  446. // restricted to a single module. Whether a module has local internals or not should not
  447. // impact any other modules, because the only things accessing the local internals is the
  448. // module that contains them.
  449. struct local_internals {
  450. type_map<type_info *> registered_types_cpp;
  451. std::forward_list<ExceptionTranslator> registered_exception_translators;
  452. #if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
  453. // For ABI compatibility, we can't store the loader_life_support TLS key in
  454. // the `internals` struct directly. Instead, we store it in `shared_data` and
  455. // cache a copy in `local_internals`. If we allocated a separate TLS key for
  456. // each instance of `local_internals`, we could end up allocating hundreds of
  457. // TLS keys if hundreds of different pybind11 modules are loaded (which is a
  458. // plausible number).
  459. PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
  460. // Holds the shared TLS key for the loader_life_support stack.
  461. struct shared_loader_life_support_data {
  462. PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
  463. shared_loader_life_support_data() {
  464. if (!PYBIND11_TLS_KEY_CREATE(loader_life_support_tls_key)) {
  465. pybind11_fail("local_internals: could not successfully initialize the "
  466. "loader_life_support TLS key!");
  467. }
  468. }
  469. // We can't help but leak the TLS key, because Python never unloads extension modules.
  470. };
  471. local_internals() {
  472. auto &internals = get_internals();
  473. // Get or create the `loader_life_support_stack_key`.
  474. auto &ptr = internals.shared_data["_life_support"];
  475. if (!ptr) {
  476. ptr = new shared_loader_life_support_data;
  477. }
  478. loader_life_support_tls_key
  479. = static_cast<shared_loader_life_support_data *>(ptr)->loader_life_support_tls_key;
  480. }
  481. #endif // defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
  482. };
  483. /// Works like `get_internals`, but for things which are locally registered.
  484. inline local_internals &get_local_internals() {
  485. // Current static can be created in the interpreter finalization routine. If the later will be
  486. // destroyed in another static variable destructor, creation of this static there will cause
  487. // static deinitialization fiasco. In order to avoid it we avoid destruction of the
  488. // local_internals static. One can read more about the problem and current solution here:
  489. // https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
  490. static auto *locals = new local_internals();
  491. return *locals;
  492. }
  493. /// Constructs a std::string with the given arguments, stores it in `internals`, and returns its
  494. /// `c_str()`. Such strings objects have a long storage duration -- the internal strings are only
  495. /// cleared when the program exits or after interpreter shutdown (when embedding), and so are
  496. /// suitable for c-style strings needed by Python internals (such as PyTypeObject's tp_name).
  497. template <typename... Args>
  498. const char *c_str(Args &&...args) {
  499. auto &strings = get_internals().static_strings;
  500. strings.emplace_front(std::forward<Args>(args)...);
  501. return strings.front().c_str();
  502. }
  503. PYBIND11_NAMESPACE_END(detail)
  504. /// Returns a named pointer that is shared among all extension modules (using the same
  505. /// pybind11 version) running in the current interpreter. Names starting with underscores
  506. /// are reserved for internal usage. Returns `nullptr` if no matching entry was found.
  507. PYBIND11_NOINLINE void *get_shared_data(const std::string &name) {
  508. auto &internals = detail::get_internals();
  509. auto it = internals.shared_data.find(name);
  510. return it != internals.shared_data.end() ? it->second : nullptr;
  511. }
  512. /// Set the shared data that can be later recovered by `get_shared_data()`.
  513. PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) {
  514. detail::get_internals().shared_data[name] = data;
  515. return data;
  516. }
  517. /// Returns a typed reference to a shared data entry (by using `get_shared_data()`) if
  518. /// such entry exists. Otherwise, a new object of default-constructible type `T` is
  519. /// added to the shared data under the given name and a reference to it is returned.
  520. template <typename T>
  521. T &get_or_create_shared_data(const std::string &name) {
  522. auto &internals = detail::get_internals();
  523. auto it = internals.shared_data.find(name);
  524. T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr);
  525. if (!ptr) {
  526. ptr = new T();
  527. internals.shared_data[name] = ptr;
  528. }
  529. return *ptr;
  530. }
  531. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)