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