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