14#if defined(WITH_THREAD) && defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
18#include "../pytypes.h"
36#ifndef PYBIND11_INTERNALS_VERSION
37# define PYBIND11_INTERNALS_VERSION 4
55#if PY_VERSION_HEX >= 0x03070000
58# if PYBIND11_INTERNALS_VERSION > 4
59# define PYBIND11_TLS_KEY_REF Py_tss_t &
60# if defined(__GNUC__) && !defined(__INTEL_COMPILER)
63# define PYBIND11_TLS_KEY_INIT(var) \
64 _Pragma("GCC diagnostic push") \
65 _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") \
67 = Py_tss_NEEDS_INIT; \
68 _Pragma("GCC diagnostic pop")
70# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT;
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))
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)
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)
98 PyThread_delete_key_value(key);
99 PyThread_set_key_value(key, value);
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))
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))
108# define PYBIND11_TLS_FREE(key) (void) key
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>;
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;
129 const char *ptr = t.name();
130 while (
auto c =
static_cast<unsigned char>(*ptr++)) {
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;
144template <
typename value_type>
145using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
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);
164 std::unordered_set<std::pair<const PyObject *, const char *>,
override_hash>
167 std::unordered_map<const PyObject *, std::vector<PyObject *>>
patients;
169 std::unordered_map<std::string, void *>
shared_data;
171#if PYBIND11_INTERNALS_VERSION == 4
172 std::vector<PyObject *> unused_loader_patient_stack_remove_at_v5;
179#if defined(WITH_THREAD)
182# if PYBIND11_INTERNALS_VERSION > 4
186 PyInterpreterState *istate =
nullptr;
188# if PYBIND11_INTERNALS_VERSION > 4
198# if PYBIND11_INTERNALS_VERSION > 4
220 void *(*operator_new)(
size_t);
224 std::vector<std::pair<
const std::type_info *,
void *(*) (
void *)>>
implicit_casts;
226 buffer_info *(*get_buffer)(PyObject *,
void *) =
nullptr;
228 void *(*module_local_load)(PyObject *,
const type_info *) =
nullptr;
243#if defined(_MSC_VER) && defined(_DEBUG)
244# define PYBIND11_BUILD_TYPE "_debug"
246# define PYBIND11_BUILD_TYPE ""
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"
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"
268# define PYBIND11_COMPILER_TYPE "_unknown"
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"
279# define PYBIND11_STDLIB ""
284#ifndef PYBIND11_BUILD_ABI
285# if defined(__GXX_ABI_VERSION)
286# define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION)
288# define PYBIND11_BUILD_ABI ""
292#ifndef PYBIND11_INTERNALS_KIND
293# if defined(WITH_THREAD)
294# define PYBIND11_INTERNALS_KIND ""
296# define PYBIND11_INTERNALS_KIND "_without_thread"
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 "__"
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 "__"
313 static internals **internals_pp =
nullptr;
323 std::exception_ptr nested = exc.nested_ptr();
324 if (nested !=
nullptr && nested != p) {
334 if (
const auto *nep =
dynamic_cast<const std::nested_exception *
>(std::addressof(exc))) {
340inline bool raise_err(PyObject *exc_type,
const char *msg) {
341 if (PyErr_Occurred()) {
345 PyErr_SetString(exc_type, msg);
354 std::rethrow_exception(p);
361 if (
const auto *nep =
dynamic_cast<const std::nested_exception *
>(std::addressof(e))) {
366 }
catch (
const std::bad_alloc &e) {
370 }
catch (
const std::domain_error &e) {
374 }
catch (
const std::invalid_argument &e) {
378 }
catch (
const std::length_error &e) {
382 }
catch (
const std::out_of_range &e) {
386 }
catch (
const std::range_error &e) {
390 }
catch (
const std::overflow_error &e) {
394 }
catch (
const std::exception &e) {
398 }
catch (
const std::nested_exception &e) {
400 raise_err(PyExc_RuntimeError,
"Caught an unknown nested exception!");
403 raise_err(PyExc_RuntimeError,
"Caught an unknown exception!");
408#if !defined(__GLIBCXX__)
412 std::rethrow_exception(p);
427 if (internals_pp && *internals_pp) {
428 return **internals_pp;
431#if defined(WITH_THREAD)
432# if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
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;
449 auto builtins =
handle(PyEval_GetBuiltins());
450 if (builtins.contains(
id) && isinstance<capsule>(builtins[
id])) {
460#if !defined(__GLIBCXX__)
467 auto *&internals_ptr = *internals_pp;
469#if defined(WITH_THREAD)
471 PyThreadState *tstate = PyThreadState_Get();
473 pybind11_fail(
"get_internals: could not successfully initialize the tstate TSS key!");
477# if PYBIND11_INTERNALS_VERSION > 4
479 pybind11_fail(
"get_internals: could not successfully initialize the "
480 "loader_life_support TSS key!");
483 internals_ptr->istate = tstate->interp;
485 builtins[id] =
capsule(internals_pp);
491 return **internals_pp;
503#if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
514 struct shared_loader_life_support_data {
516 shared_loader_life_support_data() {
518 pybind11_fail(
"local_internals: could not successfully initialize the "
519 "loader_life_support TLS key!");
530 ptr =
new shared_loader_life_support_data;
532 loader_life_support_tls_key
533 =
static_cast<shared_loader_life_support_data *
>(ptr)->loader_life_support_tls_key;
553template <
typename... Args>
556 strings.emplace_front(std::forward<Args>(
args)...);
557 return strings.front().c_str();
561#if PYBIND11_INTERNALS_VERSION > 4
585 auto &
internals = detail::get_internals();
592 detail::get_internals().shared_data[
name] =
data;
601 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)
const char * get_function_record_capsule_name()
constexpr const char * internals_function_record_capsule_name
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
bool is_function_record_capsule(const capsule &cap)
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.
RAII wrapper that temporarily clears any Python error state.
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