12#include "../pytypes.h"
30#ifndef PYBIND11_INTERNALS_VERSION
31# define PYBIND11_INTERNALS_VERSION 4
47#if PY_VERSION_HEX >= 0x03070000
50# if PYBIND11_INTERNALS_VERSION > 4
51# define PYBIND11_TLS_KEY_REF Py_tss_t &
55# define PYBIND11_TLS_KEY_INIT(var) \
56 _Pragma("GCC diagnostic push") \
57 _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") \
59 = Py_tss_NEEDS_INIT; \
60 _Pragma("GCC diagnostic pop")
62# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT;
64# define PYBIND11_TLS_KEY_CREATE(var) (PyThread_tss_create(&(var)) == 0)
65# define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get(&(key))
66# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value))
67# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr)
68# define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key))
70# define PYBIND11_TLS_KEY_REF Py_tss_t *
71# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr;
72# define PYBIND11_TLS_KEY_CREATE(var) \
73 (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0))
74# define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
75# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value))
76# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
77# define PYBIND11_TLS_FREE(key) PyThread_tss_free(key)
81# define PYBIND11_TLS_KEY_REF decltype(PyThread_create_key())
82# define PYBIND11_TLS_KEY_INIT(var) PYBIND11_TLS_KEY_REF var = 0;
83# define PYBIND11_TLS_KEY_CREATE(var) (((var) = PyThread_create_key()) != -1)
84# define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key))
85# if PY_MAJOR_VERSION < 3 || defined(PYPY_VERSION)
90 PyThread_delete_key_value(key);
91 PyThread_set_key_value(key, value);
93# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_delete_key_value(key)
94# define PYBIND11_TLS_REPLACE_VALUE(key, value) \
95 ::pybind11::detail::tls_replace_value((key), (value))
97# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_set_key_value((key), nullptr)
98# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_set_key_value((key), (value))
100# define PYBIND11_TLS_FREE(key) (void) key
109#if defined(__GLIBCXX__)
110inline bool same_type(
const std::type_info &lhs,
const std::type_info &rhs) {
return lhs == rhs; }
111using type_hash = std::hash<std::type_index>;
114inline bool same_type(
const std::type_info &lhs,
const std::type_info &rhs) {
115 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
121 const char *ptr = t.name();
122 while (
auto c =
static_cast<unsigned char>(*ptr++)) {
130 bool operator()(
const std::type_index &lhs,
const std::type_index &rhs)
const {
131 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
136template <
typename value_type>
137using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
140 inline size_t operator()(
const std::pair<const PyObject *, const char *> &v)
const {
141 size_t value = std::hash<const void *>()(v.first);
142 value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value << 6) + (value >> 2);
156 std::unordered_set<std::pair<const PyObject *, const char *>,
override_hash>
159 std::unordered_map<const PyObject *, std::vector<PyObject *>>
patients;
163#if PYBIND11_INTERNALS_VERSION == 4
164 std::vector<PyObject *> unused_loader_patient_stack_remove_at_v5;
171#if defined(WITH_THREAD)
173# if PYBIND11_INTERNALS_VERSION > 4
176 PyInterpreterState *istate =
nullptr;
178# if PYBIND11_INTERNALS_VERSION > 4
204 std::vector<std::pair<
const std::type_info *,
void *(*) (
void *)>>
implicit_casts;
208 void *(*module_local_load)(PyObject *,
const type_info *) =
nullptr;
223#if defined(_MSC_VER) && defined(_DEBUG)
224# define PYBIND11_BUILD_TYPE "_debug"
226# define PYBIND11_BUILD_TYPE ""
232#ifndef PYBIND11_COMPILER_TYPE
233# if defined(_MSC_VER)
234# define PYBIND11_COMPILER_TYPE "_msvc"
235# elif defined(__INTEL_COMPILER)
236# define PYBIND11_COMPILER_TYPE "_icc"
237# elif defined(__clang__)
238# define PYBIND11_COMPILER_TYPE "_clang"
240# define PYBIND11_COMPILER_TYPE "_pgi"
241# elif defined(__MINGW32__)
242# define PYBIND11_COMPILER_TYPE "_mingw"
243# elif defined(__CYGWIN__)
244# define PYBIND11_COMPILER_TYPE "_gcc_cygwin"
245# elif defined(__GNUC__)
246# define PYBIND11_COMPILER_TYPE "_gcc"
248# define PYBIND11_COMPILER_TYPE "_unknown"
253#ifndef PYBIND11_STDLIB
254# if defined(_LIBCPP_VERSION)
255# define PYBIND11_STDLIB "_libcpp"
256# elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
257# define PYBIND11_STDLIB "_libstdcpp"
259# define PYBIND11_STDLIB ""
264#ifndef PYBIND11_BUILD_ABI
265# if defined(__GXX_ABI_VERSION)
266# define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION)
268# define PYBIND11_BUILD_ABI ""
272#ifndef PYBIND11_INTERNALS_KIND
273# if defined(WITH_THREAD)
274# define PYBIND11_INTERNALS_KIND ""
276# define PYBIND11_INTERNALS_KIND "_without_thread"
280#define PYBIND11_INTERNALS_ID \
281 "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
282 PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
283 PYBIND11_BUILD_TYPE "__"
285#define PYBIND11_MODULE_LOCAL_ID \
286 "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
287 PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
288 PYBIND11_BUILD_TYPE "__"
293 static internals **internals_pp =
nullptr;
297#if PY_VERSION_HEX >= 0x03030000
304 std::exception_ptr nested = exc.nested_ptr();
305 if (nested !=
nullptr && nested != p) {
315 if (
const auto *nep =
dynamic_cast<const std::nested_exception *
>(std::addressof(exc))) {
329inline bool raise_err(PyObject *exc_type,
const char *msg) {
330#if PY_VERSION_HEX >= 0x03030000
331 if (PyErr_Occurred()) {
336 PyErr_SetString(exc_type, msg);
345 std::rethrow_exception(p);
352 if (
const auto *nep =
dynamic_cast<const std::nested_exception *
>(std::addressof(e))) {
357 }
catch (
const std::bad_alloc &e) {
361 }
catch (
const std::domain_error &e) {
365 }
catch (
const std::invalid_argument &e) {
369 }
catch (
const std::length_error &e) {
373 }
catch (
const std::out_of_range &e) {
377 }
catch (
const std::range_error &e) {
381 }
catch (
const std::overflow_error &e) {
385 }
catch (
const std::exception &e) {
389 }
catch (
const std::nested_exception &e) {
391 raise_err(PyExc_RuntimeError,
"Caught an unknown nested exception!");
394 raise_err(PyExc_RuntimeError,
"Caught an unknown exception!");
399#if !defined(__GLIBCXX__)
403 std::rethrow_exception(p);
418 if (internals_pp && *internals_pp) {
419 return **internals_pp;
424 struct gil_scoped_acquire_local {
425 gil_scoped_acquire_local() : state(PyGILState_Ensure()) {}
426 ~gil_scoped_acquire_local() { PyGILState_Release(state); }
427 const PyGILState_STATE state;
431 auto builtins =
handle(PyEval_GetBuiltins());
432 if (builtins.contains(
id) && isinstance<capsule>(builtins[
id])) {
442#if !defined(__GLIBCXX__)
449 auto *&internals_ptr = *internals_pp;
451#if defined(WITH_THREAD)
453# if PY_VERSION_HEX < 0x03090000
454 PyEval_InitThreads();
456 PyThreadState *tstate = PyThreadState_Get();
458 pybind11_fail(
"get_internals: could not successfully initialize the tstate TSS key!");
462# if PYBIND11_INTERNALS_VERSION > 4
464 pybind11_fail(
"get_internals: could not successfully initialize the "
465 "loader_life_support TSS key!");
468 internals_ptr->istate = tstate->interp;
470 builtins[id] =
capsule(internals_pp);
476 return **internals_pp;
488#if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
499 struct shared_loader_life_support_data {
501 shared_loader_life_support_data() {
503 pybind11_fail(
"local_internals: could not successfully initialize the "
504 "loader_life_support TLS key!");
515 ptr =
new shared_loader_life_support_data;
517 loader_life_support_tls_key
518 =
static_cast<shared_loader_life_support_data *
>(ptr)->loader_life_support_tls_key;
533template <
typename... Args>
536 strings.emplace_front(std::forward<Args>(
args)...);
537 return strings.front().c_str();
546 auto &
internals = detail::get_internals();
553 detail::get_internals().shared_data[
name] =
data;
562 auto &
internals = detail::get_internals();
C++ bindings of builtin Python exceptions.
Fetch and hold an error which was already set in Python.
void restore()
Give the currently-held error back to Python, if any.
const char * what() const noexcept override
The what() result is built lazily on demand.
\rst Holds a reference to a Python object (no reference counting)
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Thrown when pybind11::cast or.
#define PYBIND11_NOINLINE
#define PYBIND11_STR_TYPE
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)
#define PYBIND11_TLS_KEY_INIT(var)
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.
std::unordered_map< std::type_index, value_type, type_hash, type_equal_to > type_map
internals **& get_internals_pp()
Each module locally stores a pointer to the internals data.
bool handle_nested_exception(const T &, std::exception_ptr &)
PyTypeObject * make_default_metaclass()
This metaclass is assigned by default to all pybind11 types and is required in order for static prope...
PyObject * make_object_base_type(PyTypeObject *metaclass)
Create the type which can be used as a common base for all classes.
void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value)
bool raise_err(PyObject *exc_type, const char *msg)
#define PYBIND11_TLS_KEY_REF
void(*)(std::exception_ptr) ExceptionTranslator
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
PyTypeObject * make_static_property_type()
A static_property is the same as a property but the __get__() and __set__() methods are modified to a...
void translate_local_exception(std::exception_ptr p)
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().
void translate_exception(std::exception_ptr p)
#define PYBIND11_TLS_REPLACE_VALUE(key, value)
#define PYBIND11_INTERNALS_ID
#define PYBIND11_TLS_KEY_CREATE(var)
const char * c_str(Args &&...args)
Constructs a std::string with the given arguments, stores it in internals, and returns its c_str().
local_internals & get_local_internals()
Works like get_internals, but for things which are locally registered.
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) ...
bool same_type(const std::type_info &lhs, const std::type_info &rhs)
arr data(const arr &a, Ix... index)
#define PYBIND11_TLS_KEY_INIT(var)
#define PYBIND11_TLS_FREE(key)
std::unordered_map< std::type_index, value_type, type_hash, type_equal_to > type_map
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
void raise_from(PyObject *type, const char *message)
Replaces the current Python error indicator with the chosen error, performing a 'raise from' to indic...
Information record describing a Python buffer object.
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
Internal data structure used to track registered instances and types.
type_map< type_info * > registered_types_cpp
std::unordered_map< std::string, void * > shared_data
std::unordered_multimap< const void *, instance * > registered_instances
std::unordered_map< const PyObject *, std::vector< PyObject * > > patients
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
PyTypeObject * static_property_type
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
PyTypeObject * default_metaclass
std::forward_list< ExceptionTranslator > registered_exception_translators
std::forward_list< std::string > static_strings
std::unordered_set< std::pair< const PyObject *, const char * >, override_hash > inactive_override_cache
type_map< type_info * > registered_types_cpp
std::forward_list< ExceptionTranslator > registered_exception_translators
Annotation for function names.
size_t operator()(const std::pair< const PyObject *, const char * > &v) const
bool operator()(const std::type_index &lhs, const std::type_index &rhs) const
size_t operator()(const std::type_index &t) const
Additional type information which does not fit into the PyTypeObject.
std::vector< std::pair< const std::type_info *, void *(*)(void *)> > implicit_casts
void(* dealloc)(value_and_holder &v_h)
size_t holder_size_in_ptrs
const std::type_info * cpptype
void(* init_instance)(instance *, const void *)
std::vector< bool(*)(PyObject *, void *&)> * direct_conversions
std::vector< PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions