μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
internals.h
Go to the documentation of this file.
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 "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
36#ifndef PYBIND11_INTERNALS_VERSION
37# define PYBIND11_INTERNALS_VERSION 4
38#endif
39
41
42using ExceptionTranslator = void (*)(std::exception_ptr);
43
45
46constexpr const char *internals_function_record_capsule_name = "pybind11_function_record_capsule";
47
48// Forward declarations
49inline PyTypeObject *make_static_property_type();
50inline PyTypeObject *make_default_metaclass();
51inline PyObject *make_object_base_type(PyTypeObject *metaclass);
52
53// The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new
54// Thread Specific Storage (TSS) API.
55#if PY_VERSION_HEX >= 0x03070000
56// Avoid unnecessary allocation of `Py_tss_t`, since we cannot use
57// `Py_LIMITED_API` anyway.
58# if PYBIND11_INTERNALS_VERSION > 4
59# define PYBIND11_TLS_KEY_REF Py_tss_t &
60# if defined(__GNUC__) && !defined(__INTEL_COMPILER)
61// Clang on macOS warns due to `Py_tss_NEEDS_INIT` not specifying an initializer
62// for every field.
63# define PYBIND11_TLS_KEY_INIT(var) \
64 _Pragma("GCC diagnostic push") \
65 _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") \
66 Py_tss_t var \
67 = Py_tss_NEEDS_INIT; \
68 _Pragma("GCC diagnostic pop")
69# else
70# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT;
71# endif
72# define PYBIND11_TLS_KEY_CREATE(var) (PyThread_tss_create(&(var)) == 0)
73# define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get(&(key))
74# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value))
75# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr)
76# define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key))
77# else
78# define PYBIND11_TLS_KEY_REF Py_tss_t *
79# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr;
80# define PYBIND11_TLS_KEY_CREATE(var) \
81 (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0))
82# define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
83# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value))
84# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
85# define PYBIND11_TLS_FREE(key) PyThread_tss_free(key)
86# endif
87#else
88// Usually an int but a long on Cygwin64 with Python 3.x
89# define PYBIND11_TLS_KEY_REF decltype(PyThread_create_key())
90# define PYBIND11_TLS_KEY_INIT(var) PYBIND11_TLS_KEY_REF var = 0;
91# define PYBIND11_TLS_KEY_CREATE(var) (((var) = PyThread_create_key()) != -1)
92# define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key))
93# if defined(PYPY_VERSION)
94// On CPython < 3.4 and on PyPy, `PyThread_set_key_value` strangely does not set
95// the value if it has already been set. Instead, it must first be deleted and
96// then set again.
97inline void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value) {
98 PyThread_delete_key_value(key);
99 PyThread_set_key_value(key, value);
100}
101# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_delete_key_value(key)
102# define PYBIND11_TLS_REPLACE_VALUE(key, value) \
103 ::pybind11::detail::tls_replace_value((key), (value))
104# else
105# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_set_key_value((key), nullptr)
106# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_set_key_value((key), (value))
107# endif
108# define PYBIND11_TLS_FREE(key) (void) key
109#endif
110
111// Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
112// other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module
113// even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under
114// libstdc++, this doesn't happen: equality and the type_index hash are based on the type name,
115// which works. If not under a known-good stl, provide our own name-based hash and equality
116// functions that use the type name.
117#if defined(__GLIBCXX__)
118inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
119using type_hash = std::hash<std::type_index>;
120using type_equal_to = std::equal_to<std::type_index>;
121#else
122inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
123 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
124}
125
126struct type_hash {
127 size_t operator()(const std::type_index &t) const {
128 size_t hash = 5381;
129 const char *ptr = t.name();
130 while (auto c = static_cast<unsigned char>(*ptr++)) {
131 hash = (hash * 33) ^ c;
132 }
133 return hash;
134 }
135};
136
137struct type_equal_to {
138 bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
139 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
140 }
141};
142#endif
143
144template <typename value_type>
145using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
146
147struct override_hash {
148 inline size_t operator()(const std::pair<const PyObject *, const char *> &v) const {
149 size_t value = std::hash<const void *>()(v.first);
150 value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value << 6) + (value >> 2);
151 return value;
152 }
153};
154
158struct internals {
159 // std::type_index -> pybind11's type information
161 // PyTypeObject* -> base type_info(s)
162 std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py;
163 std::unordered_multimap<const void *, instance *> registered_instances; // void * -> instance*
164 std::unordered_set<std::pair<const PyObject *, const char *>, override_hash>
166 type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
167 std::unordered_map<const PyObject *, std::vector<PyObject *>> patients;
168 std::forward_list<ExceptionTranslator> registered_exception_translators;
169 std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across
170 // extensions
171#if PYBIND11_INTERNALS_VERSION == 4
172 std::vector<PyObject *> unused_loader_patient_stack_remove_at_v5;
173#endif
174 std::forward_list<std::string> static_strings; // Stores the std::strings backing
175 // detail::c_str()
176 PyTypeObject *static_property_type;
177 PyTypeObject *default_metaclass;
178 PyObject *instance_base;
179#if defined(WITH_THREAD)
180 // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
182# if PYBIND11_INTERNALS_VERSION > 4
183 PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
184# endif // PYBIND11_INTERNALS_VERSION > 4
185 // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
186 PyInterpreterState *istate = nullptr;
187
188# if PYBIND11_INTERNALS_VERSION > 4
189 // Note that we have to use a std::string to allocate memory to ensure a unique address
190 // We want unique addresses since we use pointer equality to compare function records
191 std::string function_record_capsule_name = internals_function_record_capsule_name;
192# endif
193
194 internals() = default;
195 internals(const internals &other) = delete;
196 internals &operator=(const internals &other) = delete;
197 ~internals() {
198# if PYBIND11_INTERNALS_VERSION > 4
199 PYBIND11_TLS_FREE(loader_life_support_tls_key);
200# endif // PYBIND11_INTERNALS_VERSION > 4
201
202 // This destructor is called *after* Py_Finalize() in finalize_interpreter().
203 // That *SHOULD BE* fine. The following details what happens when PyThread_tss_free is
204 // called. PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does
205 // nothing. PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree.
206 // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX).
207 // Neither of those have anything to do with CPython internals. PyMem_RawFree *requires*
208 // that the `tstate` be allocated with the CPython allocator.
209 PYBIND11_TLS_FREE(tstate);
210 }
211#endif
212};
213
216struct type_info {
217 PyTypeObject *type;
218 const std::type_info *cpptype;
220 void *(*operator_new)(size_t);
221 void (*init_instance)(instance *, const void *);
222 void (*dealloc)(value_and_holder &v_h);
223 std::vector<PyObject *(*) (PyObject *, PyTypeObject *)> implicit_conversions;
224 std::vector<std::pair<const std::type_info *, void *(*) (void *)>> implicit_casts;
225 std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
226 buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
227 void *get_buffer_data = nullptr;
228 void *(*module_local_load)(PyObject *, const type_info *) = nullptr;
229 /* A simple type never occurs as a (direct or indirect) parent
230 * of a class that makes use of multiple inheritance.
231 * A type can be simple even if it has non-simple ancestors as long as it has no descendants.
232 */
233 bool simple_type : 1;
234 /* True if there is no multiple inheritance in this type's inheritance tree */
235 bool simple_ancestors : 1;
236 /* for base vs derived holder_type checks */
237 bool default_holder : 1;
238 /* true if this is a type registered with py::module_local */
239 bool module_local : 1;
240};
241
243#if defined(_MSC_VER) && defined(_DEBUG)
244# define PYBIND11_BUILD_TYPE "_debug"
245#else
246# define PYBIND11_BUILD_TYPE ""
247#endif
248
252#ifndef PYBIND11_COMPILER_TYPE
253# if defined(_MSC_VER)
254# define PYBIND11_COMPILER_TYPE "_msvc"
255# elif defined(__INTEL_COMPILER)
256# define PYBIND11_COMPILER_TYPE "_icc"
257# elif defined(__clang__)
258# define PYBIND11_COMPILER_TYPE "_clang"
259# elif defined(__PGI)
260# define PYBIND11_COMPILER_TYPE "_pgi"
261# elif defined(__MINGW32__)
262# define PYBIND11_COMPILER_TYPE "_mingw"
263# elif defined(__CYGWIN__)
264# define PYBIND11_COMPILER_TYPE "_gcc_cygwin"
265# elif defined(__GNUC__)
266# define PYBIND11_COMPILER_TYPE "_gcc"
267# else
268# define PYBIND11_COMPILER_TYPE "_unknown"
269# endif
270#endif
271
273#ifndef PYBIND11_STDLIB
274# if defined(_LIBCPP_VERSION)
275# define PYBIND11_STDLIB "_libcpp"
276# elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
277# define PYBIND11_STDLIB "_libstdcpp"
278# else
279# define PYBIND11_STDLIB ""
280# endif
281#endif
282
284#ifndef PYBIND11_BUILD_ABI
285# if defined(__GXX_ABI_VERSION)
286# define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION)
287# else
288# define PYBIND11_BUILD_ABI ""
289# endif
290#endif
291
292#ifndef PYBIND11_INTERNALS_KIND
293# if defined(WITH_THREAD)
294# define PYBIND11_INTERNALS_KIND ""
295# else
296# define PYBIND11_INTERNALS_KIND "_without_thread"
297# endif
298#endif
299
300#define PYBIND11_INTERNALS_ID \
301 "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
302 PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
303 PYBIND11_BUILD_TYPE "__"
304
305#define PYBIND11_MODULE_LOCAL_ID \
306 "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
307 PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
308 PYBIND11_BUILD_TYPE "__"
309
313 static internals **internals_pp = nullptr;
314 return internals_pp;
315}
316
317// forward decl
318inline void translate_exception(std::exception_ptr);
319
320template <class T,
322bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
323 std::exception_ptr nested = exc.nested_ptr();
324 if (nested != nullptr && nested != p) {
325 translate_exception(nested);
326 return true;
327 }
328 return false;
329}
330
331template <class T,
333bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
334 if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
335 return handle_nested_exception(*nep, p);
336 }
337 return false;
338}
339
340inline bool raise_err(PyObject *exc_type, const char *msg) {
341 if (PyErr_Occurred()) {
342 raise_from(exc_type, msg);
343 return true;
344 }
345 PyErr_SetString(exc_type, msg);
346 return false;
347}
348
349inline void translate_exception(std::exception_ptr p) {
350 if (!p) {
351 return;
352 }
353 try {
354 std::rethrow_exception(p);
355 } catch (error_already_set &e) {
357 e.restore();
358 return;
359 } catch (const builtin_exception &e) {
360 // Could not use template since it's an abstract class.
361 if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
363 }
364 e.set_error();
365 return;
366 } catch (const std::bad_alloc &e) {
368 raise_err(PyExc_MemoryError, e.what());
369 return;
370 } catch (const std::domain_error &e) {
372 raise_err(PyExc_ValueError, e.what());
373 return;
374 } catch (const std::invalid_argument &e) {
376 raise_err(PyExc_ValueError, e.what());
377 return;
378 } catch (const std::length_error &e) {
380 raise_err(PyExc_ValueError, e.what());
381 return;
382 } catch (const std::out_of_range &e) {
384 raise_err(PyExc_IndexError, e.what());
385 return;
386 } catch (const std::range_error &e) {
388 raise_err(PyExc_ValueError, e.what());
389 return;
390 } catch (const std::overflow_error &e) {
392 raise_err(PyExc_OverflowError, e.what());
393 return;
394 } catch (const std::exception &e) {
396 raise_err(PyExc_RuntimeError, e.what());
397 return;
398 } catch (const std::nested_exception &e) {
400 raise_err(PyExc_RuntimeError, "Caught an unknown nested exception!");
401 return;
402 } catch (...) {
403 raise_err(PyExc_RuntimeError, "Caught an unknown exception!");
404 return;
405 }
406}
407
408#if !defined(__GLIBCXX__)
409inline void translate_local_exception(std::exception_ptr p) {
410 try {
411 if (p) {
412 std::rethrow_exception(p);
413 }
414 } catch (error_already_set &e) {
415 e.restore();
416 return;
417 } catch (const builtin_exception &e) {
418 e.set_error();
419 return;
420 }
421}
422#endif
423
426 auto **&internals_pp = get_internals_pp();
427 if (internals_pp && *internals_pp) {
428 return **internals_pp;
429 }
430
431#if defined(WITH_THREAD)
432# if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
434# else
435 // Ensure that the GIL is held since we will need to make Python calls.
436 // Cannot use py::gil_scoped_acquire here since that constructor calls get_internals.
437 struct gil_scoped_acquire_local {
438 gil_scoped_acquire_local() : state(PyGILState_Ensure()) {}
439 gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete;
440 gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete;
441 ~gil_scoped_acquire_local() { PyGILState_Release(state); }
442 const PyGILState_STATE state;
443 } gil;
444# endif
445#endif
446 error_scope err_scope;
447
449 auto builtins = handle(PyEval_GetBuiltins());
450 if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
451 internals_pp = static_cast<internals **>(capsule(builtins[id]));
452
453 // We loaded builtins through python's builtins, which means that our `error_already_set`
454 // and `builtin_exception` may be different local classes than the ones set up in the
455 // initial exception translator, below, so add another for our local exception classes.
456 //
457 // libstdc++ doesn't require this (types there are identified only by name)
458 // libc++ with CPython doesn't require this (types are explicitly exported)
459 // libc++ with PyPy still need it, awaiting further investigation
460#if !defined(__GLIBCXX__)
462#endif
463 } else {
464 if (!internals_pp) {
465 internals_pp = new internals *();
466 }
467 auto *&internals_ptr = *internals_pp;
468 internals_ptr = new internals();
469#if defined(WITH_THREAD)
470
471 PyThreadState *tstate = PyThreadState_Get();
472 if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->tstate)) {
473 pybind11_fail("get_internals: could not successfully initialize the tstate TSS key!");
474 }
475 PYBIND11_TLS_REPLACE_VALUE(internals_ptr->tstate, tstate);
476
477# if PYBIND11_INTERNALS_VERSION > 4
478 if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->loader_life_support_tls_key)) {
479 pybind11_fail("get_internals: could not successfully initialize the "
480 "loader_life_support TSS key!");
481 }
482# endif
483 internals_ptr->istate = tstate->interp;
484#endif
485 builtins[id] = capsule(internals_pp);
486 internals_ptr->registered_exception_translators.push_front(&translate_exception);
487 internals_ptr->static_property_type = make_static_property_type();
488 internals_ptr->default_metaclass = make_default_metaclass();
489 internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass);
490 }
491 return **internals_pp;
492}
493
494// the internals struct (above) is shared between all the modules. local_internals are only
495// for a single module. Any changes made to internals may require an update to
496// PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design,
497// restricted to a single module. Whether a module has local internals or not should not
498// impact any other modules, because the only things accessing the local internals is the
499// module that contains them.
500struct local_internals {
502 std::forward_list<ExceptionTranslator> registered_exception_translators;
503#if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
504
505 // For ABI compatibility, we can't store the loader_life_support TLS key in
506 // the `internals` struct directly. Instead, we store it in `shared_data` and
507 // cache a copy in `local_internals`. If we allocated a separate TLS key for
508 // each instance of `local_internals`, we could end up allocating hundreds of
509 // TLS keys if hundreds of different pybind11 modules are loaded (which is a
510 // plausible number).
511 PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
512
513 // Holds the shared TLS key for the loader_life_support stack.
514 struct shared_loader_life_support_data {
515 PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
516 shared_loader_life_support_data() {
517 if (!PYBIND11_TLS_KEY_CREATE(loader_life_support_tls_key)) {
518 pybind11_fail("local_internals: could not successfully initialize the "
519 "loader_life_support TLS key!");
520 }
521 }
522 // We can't help but leak the TLS key, because Python never unloads extension modules.
523 };
524
526 auto &internals = get_internals();
527 // Get or create the `loader_life_support_stack_key`.
528 auto &ptr = internals.shared_data["_life_support"];
529 if (!ptr) {
530 ptr = new shared_loader_life_support_data;
531 }
532 loader_life_support_tls_key
533 = static_cast<shared_loader_life_support_data *>(ptr)->loader_life_support_tls_key;
534 }
535#endif // defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
536};
537
540 // Current static can be created in the interpreter finalization routine. If the later will be
541 // destroyed in another static variable destructor, creation of this static there will cause
542 // static deinitialization fiasco. In order to avoid it we avoid destruction of the
543 // local_internals static. One can read more about the problem and current solution here:
544 // https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
545 static auto *locals = new local_internals();
546 return *locals;
547}
548
553template <typename... Args>
554const char *c_str(Args &&...args) {
555 auto &strings = get_internals().static_strings;
556 strings.emplace_front(std::forward<Args>(args)...);
557 return strings.front().c_str();
558}
559
561#if PYBIND11_INTERNALS_VERSION > 4
562 return get_internals().function_record_capsule_name.c_str();
563#else
564 return nullptr;
565#endif
566}
567
568// Determine whether or not the following capsule contains a pybind11 function record.
569// Note that we use `internals` to make sure that only ABI compatible records are touched.
570//
571// This check is currently used in two places:
572// - An important optimization in functional.h to avoid overhead in C++ -> Python -> C++
573// - The sibling feature of cpp_function to allow overloads
574inline bool is_function_record_capsule(const capsule &cap) {
575 // Pointer equality as we rely on internals() to ensure unique pointers
576 return cap.name() == get_function_record_capsule_name();
577}
578
580
581
585 auto &internals = detail::get_internals();
586 auto it = internals.shared_data.find(name);
587 return it != internals.shared_data.end() ? it->second : nullptr;
588}
589
591PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) {
592 detail::get_internals().shared_data[name] = data;
593 return data;
594}
595
599template <typename T>
600T &get_or_create_shared_data(const std::string &name) {
601 auto &internals = detail::get_internals();
602 auto it = internals.shared_data.find(name);
603 T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr);
604 if (!ptr) {
605 ptr = new T();
607 }
608 return *ptr;
609}
610
Definition: pytypes.h:1776
C++ bindings of builtin Python exceptions.
Definition: common.h:961
Fetch and hold an error which was already set in Python.
Definition: pytypes.h:379
void restore()
Give the currently-held error back to Python, if any.
Definition: pytypes.h:395
const char * what() const noexcept override
The what() result is built lazily on demand.
Definition: pybind11.h:2657
\rst Holds a reference to a Python object (no reference counting)
Definition: pytypes.h:194
ssize_t hash(handle obj)
Definition: pytypes.h:581
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
Definition: common.h:625
PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Thrown when pybind11::cast or.
Definition: common.h:992
#define PYBIND11_NOINLINE
Definition: common.h:129
#define PYBIND11_STR_TYPE
Definition: common.h:320
std::size_t size_t
Definition: common.h:461
#define PYBIND11_NAMESPACE_END(name)
Definition: common.h:21
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: common.h:20
#define PYBIND11_TLS_KEY_INIT(var)
Definition: internals.h:82
T & get_or_create_shared_data(const std::string &name)
Returns a typed reference to a shared data entry (by using get_shared_data()) if such entry exists.
Definition: internals.h:561
std::unordered_map< std::type_index, value_type, type_hash, type_equal_to > type_map
Definition: internals.h:137
internals **& get_internals_pp()
Each module locally stores a pointer to the internals data.
Definition: internals.h:292
bool handle_nested_exception(const T &, std::exception_ptr &)
Definition: internals.h:324
PyTypeObject * make_default_metaclass()
This metaclass is assigned by default to all pybind11 types and is required in order for static prope...
Definition: class.h:243
PyObject * make_object_base_type(PyTypeObject *metaclass)
Create the type which can be used as a common base for all classes.
Definition: class.h:465
void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value)
Definition: internals.h:89
bool raise_err(PyObject *exc_type, const char *msg)
Definition: internals.h:329
#define PYBIND11_TLS_KEY_REF
Definition: internals.h:81
void(*)(std::exception_ptr) ExceptionTranslator
Definition: internals.h:36
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
Definition: internals.h:416
PyTypeObject * make_static_property_type()
A static_property is the same as a property but the __get__() and __set__() methods are modified to a...
Definition: class.h:61
void translate_local_exception(std::exception_ptr p)
Definition: internals.h:400
PYBIND11_NOINLINE void * set_shared_data(const std::string &name, void *data)
Set the shared data that can be later recovered by get_shared_data().
Definition: internals.h:552
void translate_exception(std::exception_ptr p)
Definition: internals.h:340
#define PYBIND11_TLS_REPLACE_VALUE(key, value)
Definition: internals.h:94
#define PYBIND11_INTERNALS_ID
Definition: internals.h:280
#define PYBIND11_TLS_KEY_CREATE(var)
Definition: internals.h:83
const char * c_str(Args &&...args)
Constructs a std::string with the given arguments, stores it in internals, and returns its c_str().
Definition: internals.h:534
local_internals & get_local_internals()
Works like get_internals, but for things which are locally registered.
Definition: internals.h:524
PYBIND11_NOINLINE void * get_shared_data(const std::string &name)
Returns a named pointer that is shared among all extension modules (using the same pybind11 version) ...
Definition: internals.h:545
bool same_type(const std::type_info &lhs, const std::type_info &rhs)
Definition: internals.h:114
arr data(const arr &a, Ix... index)
#define PYBIND11_TLS_KEY_INIT(var)
Definition: internals.h:90
#define PYBIND11_TLS_FREE(key)
Definition: internals.h:108
const char * get_function_record_capsule_name()
Definition: internals.h:560
constexpr const char * internals_function_record_capsule_name
Definition: internals.h:46
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
Definition: internals.h:425
bool is_function_record_capsule(const capsule &cap)
Definition: internals.h:574
void raise_from(PyObject *type, const char *message)
Replaces the current Python error indicator with the chosen error, performing a 'raise from' to indic...
Definition: pytypes.h:721
Information record describing a Python buffer object.
Definition: buffer_info.h:43
RAII wrapper that temporarily clears any Python error state.
Definition: common.h:1044
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
Definition: common.h:554
Internal data structure used to track registered instances and types.
Definition: internals.h:150
type_map< type_info * > registered_types_cpp
Definition: internals.h:152
std::unordered_map< std::string, void * > shared_data
Definition: internals.h:161
std::unordered_multimap< const void *, instance * > registered_instances
Definition: internals.h:155
std::unordered_map< const PyObject *, std::vector< PyObject * > > patients
Definition: internals.h:159
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
Definition: internals.h:154
PyTypeObject * static_property_type
Definition: internals.h:168
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
Definition: internals.h:158
PyTypeObject * default_metaclass
Definition: internals.h:169
PyObject * instance_base
Definition: internals.h:170
std::forward_list< ExceptionTranslator > registered_exception_translators
Definition: internals.h:160
std::forward_list< std::string > static_strings
Definition: internals.h:166
std::unordered_set< std::pair< const PyObject *, const char * >, override_hash > inactive_override_cache
Definition: internals.h:157
type_map< type_info * > registered_types_cpp
Definition: internals.h:486
std::forward_list< ExceptionTranslator > registered_exception_translators
Definition: internals.h:487
Annotation which requests that a special metaclass is created for a type.
Definition: attr.h:81
Annotation for function names.
Definition: attr.h:47
size_t operator()(const std::pair< const PyObject *, const char * > &v) const
Definition: internals.h:148
bool operator()(const std::type_index &lhs, const std::type_index &rhs) const
Definition: internals.h:138
size_t operator()(const std::type_index &t) const
Definition: internals.h:127
Additional type information which does not fit into the PyTypeObject.
Definition: internals.h:196
std::vector< std::pair< const std::type_info *, void *(*)(void *)> > implicit_casts
Definition: internals.h:204
bool simple_type
Definition: internals.h:213
void(* dealloc)(value_and_holder &v_h)
Definition: internals.h:202
size_t holder_size_in_ptrs
Definition: internals.h:199
size_t type_size
Definition: internals.h:199
const std::type_info * cpptype
Definition: internals.h:198
void(* init_instance)(instance *, const void *)
Definition: internals.h:201
std::vector< bool(*)(PyObject *, void *&)> * direct_conversions
Definition: internals.h:205
bool module_local
Definition: internals.h:219
bool simple_ancestors
Definition: internals.h:215
size_t type_align
Definition: internals.h:199
std::vector< PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions
Definition: internals.h:203
void * get_buffer_data
Definition: internals.h:207
PyTypeObject * type
Definition: internals.h:197
bool default_holder
Definition: internals.h:217