internals.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "../pytypes.h"
  9. NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  10. NAMESPACE_BEGIN(detail)
  11. // Forward declarations
  12. inline PyTypeObject *make_static_property_type();
  13. inline PyTypeObject *make_default_metaclass();
  14. inline PyObject *make_object_base_type(PyTypeObject *metaclass);
  15. // The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new
  16. // Thread Specific Storage (TSS) API.
  17. #if PY_VERSION_HEX >= 0x03070000
  18. # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr
  19. # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
  20. # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (tstate))
  21. # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
  22. #else
  23. // Usually an int but a long on Cygwin64 with Python 3.x
  24. # define PYBIND11_TLS_KEY_INIT(var) decltype(PyThread_create_key()) var = 0
  25. # define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key))
  26. # if PY_MAJOR_VERSION < 3
  27. # define PYBIND11_TLS_DELETE_VALUE(key) \
  28. PyThread_delete_key_value(key)
  29. # define PYBIND11_TLS_REPLACE_VALUE(key, value) \
  30. do { \
  31. PyThread_delete_key_value((key)); \
  32. PyThread_set_key_value((key), (value)); \
  33. } while (false)
  34. # else
  35. # define PYBIND11_TLS_DELETE_VALUE(key) \
  36. PyThread_set_key_value((key), nullptr)
  37. # define PYBIND11_TLS_REPLACE_VALUE(key, value) \
  38. PyThread_set_key_value((key), (value))
  39. # endif
  40. #endif
  41. // Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
  42. // other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module
  43. // even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under
  44. // libstdc++, this doesn't happen: equality and the type_index hash are based on the type name,
  45. // which works. If not under a known-good stl, provide our own name-based hash and equality
  46. // functions that use the type name.
  47. #if defined(__GLIBCXX__)
  48. inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
  49. using type_hash = std::hash<std::type_index>;
  50. using type_equal_to = std::equal_to<std::type_index>;
  51. #else
  52. inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
  53. return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
  54. }
  55. struct type_hash {
  56. size_t operator()(const std::type_index &t) const {
  57. size_t hash = 5381;
  58. const char *ptr = t.name();
  59. while (auto c = static_cast<unsigned char>(*ptr++))
  60. hash = (hash * 33) ^ c;
  61. return hash;
  62. }
  63. };
  64. struct type_equal_to {
  65. bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
  66. return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
  67. }
  68. };
  69. #endif
  70. template <typename value_type>
  71. using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
  72. struct overload_hash {
  73. inline size_t operator()(const std::pair<const PyObject *, const char *>& v) const {
  74. size_t value = std::hash<const void *>()(v.first);
  75. value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2);
  76. return value;
  77. }
  78. };
  79. /// Internal data structure used to track registered instances and types.
  80. /// Whenever binary incompatible changes are made to this structure,
  81. /// `PYBIND11_INTERNALS_VERSION` must be incremented.
  82. struct internals {
  83. type_map<type_info *> registered_types_cpp; // std::type_index -> pybind11's type information
  84. std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py; // PyTypeObject* -> base type_info(s)
  85. std::unordered_multimap<const void *, instance*> registered_instances; // void * -> instance*
  86. std::unordered_set<std::pair<const PyObject *, const char *>, overload_hash> inactive_overload_cache;
  87. type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
  88. std::unordered_map<const PyObject *, std::vector<PyObject *>> patients;
  89. std::forward_list<void (*) (std::exception_ptr)> registered_exception_translators;
  90. std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across extensions
  91. std::vector<PyObject *> loader_patient_stack; // Used by `loader_life_support`
  92. std::forward_list<std::string> static_strings; // Stores the std::strings backing detail::c_str()
  93. PyTypeObject *static_property_type;
  94. PyTypeObject *default_metaclass;
  95. PyObject *instance_base;
  96. #if defined(WITH_THREAD)
  97. PYBIND11_TLS_KEY_INIT(tstate);
  98. PyInterpreterState *istate = nullptr;
  99. #endif
  100. };
  101. /// Additional type information which does not fit into the PyTypeObject.
  102. /// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`.
  103. struct type_info {
  104. PyTypeObject *type;
  105. const std::type_info *cpptype;
  106. size_t type_size, type_align, holder_size_in_ptrs;
  107. void *(*operator_new)(size_t);
  108. void (*init_instance)(instance *, const void *);
  109. void (*dealloc)(value_and_holder &v_h);
  110. std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions;
  111. std::vector<std::pair<const std::type_info *, void *(*)(void *)>> implicit_casts;
  112. std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
  113. buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
  114. void *get_buffer_data = nullptr;
  115. void *(*module_local_load)(PyObject *, const type_info *) = nullptr;
  116. /* A simple type never occurs as a (direct or indirect) parent
  117. * of a class that makes use of multiple inheritance */
  118. bool simple_type : 1;
  119. /* True if there is no multiple inheritance in this type's inheritance tree */
  120. bool simple_ancestors : 1;
  121. /* for base vs derived holder_type checks */
  122. bool default_holder : 1;
  123. /* true if this is a type registered with py::module_local */
  124. bool module_local : 1;
  125. };
  126. /// Tracks the `internals` and `type_info` ABI version independent of the main library version
  127. #define PYBIND11_INTERNALS_VERSION 3
  128. #if defined(_DEBUG)
  129. # define PYBIND11_BUILD_TYPE "_debug"
  130. #else
  131. # define PYBIND11_BUILD_TYPE ""
  132. #endif
  133. #if defined(WITH_THREAD)
  134. # define PYBIND11_INTERNALS_KIND ""
  135. #else
  136. # define PYBIND11_INTERNALS_KIND "_without_thread"
  137. #endif
  138. #define PYBIND11_INTERNALS_ID "__pybind11_internals_v" \
  139. PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_BUILD_TYPE "__"
  140. #define PYBIND11_MODULE_LOCAL_ID "__pybind11_module_local_v" \
  141. PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_BUILD_TYPE "__"
  142. /// Each module locally stores a pointer to the `internals` data. The data
  143. /// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`.
  144. inline internals **&get_internals_pp() {
  145. static internals **internals_pp = nullptr;
  146. return internals_pp;
  147. }
  148. /// Return a reference to the current `internals` data
  149. PYBIND11_NOINLINE inline internals &get_internals() {
  150. auto **&internals_pp = get_internals_pp();
  151. if (internals_pp && *internals_pp)
  152. return **internals_pp;
  153. constexpr auto *id = PYBIND11_INTERNALS_ID;
  154. auto builtins = handle(PyEval_GetBuiltins());
  155. if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
  156. internals_pp = static_cast<internals **>(capsule(builtins[id]));
  157. // We loaded builtins through python's builtins, which means that our `error_already_set`
  158. // and `builtin_exception` may be different local classes than the ones set up in the
  159. // initial exception translator, below, so add another for our local exception classes.
  160. //
  161. // libstdc++ doesn't require this (types there are identified only by name)
  162. #if !defined(__GLIBCXX__)
  163. (*internals_pp)->registered_exception_translators.push_front(
  164. [](std::exception_ptr p) -> void {
  165. try {
  166. if (p) std::rethrow_exception(p);
  167. } catch (error_already_set &e) { e.restore(); return;
  168. } catch (const builtin_exception &e) { e.set_error(); return;
  169. }
  170. }
  171. );
  172. #endif
  173. } else {
  174. if (!internals_pp) internals_pp = new internals*();
  175. auto *&internals_ptr = *internals_pp;
  176. internals_ptr = new internals();
  177. #if defined(WITH_THREAD)
  178. PyEval_InitThreads();
  179. PyThreadState *tstate = PyThreadState_Get();
  180. #if PY_VERSION_HEX >= 0x03070000
  181. internals_ptr->tstate = PyThread_tss_alloc();
  182. if (!internals_ptr->tstate || PyThread_tss_create(internals_ptr->tstate))
  183. pybind11_fail("get_internals: could not successfully initialize the TSS key!");
  184. PyThread_tss_set(internals_ptr->tstate, tstate);
  185. #else
  186. internals_ptr->tstate = PyThread_create_key();
  187. if (internals_ptr->tstate == -1)
  188. pybind11_fail("get_internals: could not successfully initialize the TLS key!");
  189. PyThread_set_key_value(internals_ptr->tstate, tstate);
  190. #endif
  191. internals_ptr->istate = tstate->interp;
  192. #endif
  193. builtins[id] = capsule(internals_pp);
  194. internals_ptr->registered_exception_translators.push_front(
  195. [](std::exception_ptr p) -> void {
  196. try {
  197. if (p) std::rethrow_exception(p);
  198. } catch (error_already_set &e) { e.restore(); return;
  199. } catch (const builtin_exception &e) { e.set_error(); return;
  200. } catch (const std::bad_alloc &e) { PyErr_SetString(PyExc_MemoryError, e.what()); return;
  201. } catch (const std::domain_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return;
  202. } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError, e.what()); return;
  203. } catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return;
  204. } catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return;
  205. } catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return;
  206. } catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return;
  207. } catch (...) {
  208. PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!");
  209. return;
  210. }
  211. }
  212. );
  213. internals_ptr->static_property_type = make_static_property_type();
  214. internals_ptr->default_metaclass = make_default_metaclass();
  215. internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass);
  216. }
  217. return **internals_pp;
  218. }
  219. /// Works like `internals.registered_types_cpp`, but for module-local registered types:
  220. inline type_map<type_info *> &registered_local_types_cpp() {
  221. static type_map<type_info *> locals{};
  222. return locals;
  223. }
  224. /// Constructs a std::string with the given arguments, stores it in `internals`, and returns its
  225. /// `c_str()`. Such strings objects have a long storage duration -- the internal strings are only
  226. /// cleared when the program exits or after interpreter shutdown (when embedding), and so are
  227. /// suitable for c-style strings needed by Python internals (such as PyTypeObject's tp_name).
  228. template <typename... Args>
  229. const char *c_str(Args &&...args) {
  230. auto &strings = get_internals().static_strings;
  231. strings.emplace_front(std::forward<Args>(args)...);
  232. return strings.front().c_str();
  233. }
  234. NAMESPACE_END(detail)
  235. /// Returns a named pointer that is shared among all extension modules (using the same
  236. /// pybind11 version) running in the current interpreter. Names starting with underscores
  237. /// are reserved for internal usage. Returns `nullptr` if no matching entry was found.
  238. inline PYBIND11_NOINLINE void *get_shared_data(const std::string &name) {
  239. auto &internals = detail::get_internals();
  240. auto it = internals.shared_data.find(name);
  241. return it != internals.shared_data.end() ? it->second : nullptr;
  242. }
  243. /// Set the shared data that can be later recovered by `get_shared_data()`.
  244. inline PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) {
  245. detail::get_internals().shared_data[name] = data;
  246. return data;
  247. }
  248. /// Returns a typed reference to a shared data entry (by using `get_shared_data()`) if
  249. /// such entry exists. Otherwise, a new object of default-constructible type `T` is
  250. /// added to the shared data under the given name and a reference to it is returned.
  251. template<typename T>
  252. T &get_or_create_shared_data(const std::string &name) {
  253. auto &internals = detail::get_internals();
  254. auto it = internals.shared_data.find(name);
  255. T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr);
  256. if (!ptr) {
  257. ptr = new T();
  258. internals.shared_data[name] = ptr;
  259. }
  260. return *ptr;
  261. }
  262. NAMESPACE_END(PYBIND11_NAMESPACE)