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