Go to the documentation of this file.
14 #if defined(WITH_THREAD) && defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
18 #include "../pytypes.h"
36 #ifndef PYBIND11_INTERNALS_VERSION
37 # if PY_VERSION_HEX >= 0x030C0000
39 # define PYBIND11_INTERNALS_VERSION 5
41 # define PYBIND11_INTERNALS_VERSION 4
46 static_assert(PY_VERSION_HEX < 0x030C0000 || PYBIND11_INTERNALS_VERSION >= 5,
47 "pybind11 ABI version 5 is the minimum for Python 3.12+");
64 #if PY_VERSION_HEX >= 0x03070000
67 # if PYBIND11_INTERNALS_VERSION > 4
68 # define PYBIND11_TLS_KEY_REF Py_tss_t &
69 # if defined(__GNUC__) && !defined(__INTEL_COMPILER)
72 # define PYBIND11_TLS_KEY_INIT(var) \
73 _Pragma("GCC diagnostic push") \
74 _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") \
76 = Py_tss_NEEDS_INIT; \
77 _Pragma("GCC diagnostic pop")
79 # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT;
81 # define PYBIND11_TLS_KEY_CREATE(var) (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_delete(&(key))
87 # define PYBIND11_TLS_KEY_REF Py_tss_t *
88 # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr;
89 # define PYBIND11_TLS_KEY_CREATE(var) \
90 (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0))
91 # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
92 # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value))
93 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
94 # define PYBIND11_TLS_FREE(key) PyThread_tss_free(key)
98 # define PYBIND11_TLS_KEY_REF decltype(PyThread_create_key())
99 # define PYBIND11_TLS_KEY_INIT(var) PYBIND11_TLS_KEY_REF var = 0;
100 # define PYBIND11_TLS_KEY_CREATE(var) (((var) = PyThread_create_key()) != -1)
101 # define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key))
102 # if defined(PYPY_VERSION)
107 PyThread_delete_key_value(
key);
110 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_delete_key_value(key)
111 # define PYBIND11_TLS_REPLACE_VALUE(key, value) \
112 ::pybind11::detail::tls_replace_value((key), (value))
114 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_set_key_value((key), nullptr)
115 # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_set_key_value((key), (value))
117 # define PYBIND11_TLS_FREE(key) (void) key
126 #if (PYBIND11_INTERNALS_VERSION <= 4 && defined(__GLIBCXX__)) \
127 || (PYBIND11_INTERNALS_VERSION >= 5 && !defined(_LIBCPP_VERSION))
128 inline bool same_type(
const std::type_info &lhs,
const std::type_info &rhs) {
return lhs == rhs; }
129 using type_hash = std::hash<std::type_index>;
132 inline bool same_type(
const std::type_info &lhs,
const std::type_info &rhs) {
133 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
139 const char *ptr =
t.name();
140 while (
auto c =
static_cast<unsigned char>(*ptr++)) {
148 bool operator()(
const std::type_index &lhs,
const std::type_index &rhs)
const {
149 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
154 template <
typename value_type>
155 using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
158 inline size_t operator()(
const std::pair<const PyObject *, const char *> &
v)
const {
159 size_t value = std::hash<const void *>()(
v.first);
160 value ^= std::hash<const void *>()(
v.second) + 0x9e3779b9 + (
value << 6) + (
value >> 2);
174 std::unordered_set<std::pair<const PyObject *, const char *>,
override_hash>
177 std::unordered_map<const PyObject *, std::vector<PyObject *>>
patients;
181 #if PYBIND11_INTERNALS_VERSION == 4
189 #if defined(WITH_THREAD)
192 # if PYBIND11_INTERNALS_VERSION > 4
194 # endif // PYBIND11_INTERNALS_VERSION > 4
196 PyInterpreterState *istate =
nullptr;
198 # if PYBIND11_INTERNALS_VERSION > 4
208 # if PYBIND11_INTERNALS_VERSION > 4
210 # endif // PYBIND11_INTERNALS_VERSION > 4
234 std::vector<std::pair<
const std::type_info *,
void *(*) (
void *)>>
implicit_casts;
238 void *(*module_local_load)(PyObject *,
const type_info *) =
nullptr;
253 #if defined(_MSC_VER) && defined(_DEBUG)
254 # define PYBIND11_BUILD_TYPE "_debug"
256 # define PYBIND11_BUILD_TYPE ""
262 #ifndef PYBIND11_COMPILER_TYPE
263 # if defined(_MSC_VER)
264 # define PYBIND11_COMPILER_TYPE "_msvc"
265 # elif defined(__INTEL_COMPILER)
266 # define PYBIND11_COMPILER_TYPE "_icc"
267 # elif defined(__clang__)
268 # define PYBIND11_COMPILER_TYPE "_clang"
269 # elif defined(__PGI)
270 # define PYBIND11_COMPILER_TYPE "_pgi"
271 # elif defined(__MINGW32__)
272 # define PYBIND11_COMPILER_TYPE "_mingw"
273 # elif defined(__CYGWIN__)
274 # define PYBIND11_COMPILER_TYPE "_gcc_cygwin"
275 # elif defined(__GNUC__)
276 # define PYBIND11_COMPILER_TYPE "_gcc"
278 # define PYBIND11_COMPILER_TYPE "_unknown"
283 #ifndef PYBIND11_STDLIB
284 # if defined(_LIBCPP_VERSION)
285 # define PYBIND11_STDLIB "_libcpp"
286 # elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
287 # define PYBIND11_STDLIB "_libstdcpp"
289 # define PYBIND11_STDLIB ""
294 #ifndef PYBIND11_BUILD_ABI
295 # if defined(__GXX_ABI_VERSION)
296 # define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION)
298 # define PYBIND11_BUILD_ABI ""
302 #ifndef PYBIND11_INTERNALS_KIND
303 # if defined(WITH_THREAD)
304 # define PYBIND11_INTERNALS_KIND ""
306 # define PYBIND11_INTERNALS_KIND "_without_thread"
310 #define PYBIND11_INTERNALS_ID \
311 "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
312 PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
313 PYBIND11_BUILD_TYPE "__"
315 #define PYBIND11_MODULE_LOCAL_ID \
316 "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
317 PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
318 PYBIND11_BUILD_TYPE "__"
323 static internals **internals_pp =
nullptr;
333 std::exception_ptr nested = exc.nested_ptr();
334 if (nested !=
nullptr && nested !=
p) {
344 if (
const auto *nep =
dynamic_cast<const std::nested_exception *
>(std::addressof(exc))) {
351 if (PyErr_Occurred()) {
355 PyErr_SetString(exc_type,
msg);
364 std::rethrow_exception(
p);
371 if (
const auto *nep =
dynamic_cast<const std::nested_exception *
>(std::addressof(
e))) {
376 }
catch (
const std::bad_alloc &
e) {
380 }
catch (
const std::domain_error &
e) {
384 }
catch (
const std::invalid_argument &
e) {
388 }
catch (
const std::length_error &
e) {
392 }
catch (
const std::out_of_range &
e) {
396 }
catch (
const std::range_error &
e) {
400 }
catch (
const std::overflow_error &
e) {
404 }
catch (
const std::exception &
e) {
408 }
catch (
const std::nested_exception &
e) {
410 raise_err(PyExc_RuntimeError,
"Caught an unknown nested exception!");
413 raise_err(PyExc_RuntimeError,
"Caught an unknown exception!");
418 #if !defined(__GLIBCXX__)
422 std::rethrow_exception(
p);
436 #if PYBIND11_INTERNALS_VERSION <= 4 || PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION)
437 state_dict = reinterpret_borrow<object>(PyEval_GetBuiltins());
439 # if PY_VERSION_HEX < 0x03090000
440 PyInterpreterState *istate = _PyInterpreterState_Get();
442 PyInterpreterState *istate = PyInterpreterState_Get();
445 state_dict = reinterpret_borrow<object>(PyInterpreterState_GetDict(istate));
449 raise_from(PyExc_SystemError,
"pybind11::detail::get_python_state_dict() FAILED");
459 void *raw_ptr = PyCapsule_GetPointer(obj.
ptr(),
nullptr);
460 if (raw_ptr ==
nullptr) {
461 raise_from(PyExc_SystemError,
"pybind11::detail::get_internals_pp_from_capsule() FAILED");
463 return static_cast<internals **
>(raw_ptr);
469 if (internals_pp && *internals_pp) {
470 return **internals_pp;
473 #if defined(WITH_THREAD)
474 # if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
479 struct gil_scoped_acquire_local {
480 gil_scoped_acquire_local() :
state(PyGILState_Ensure()) {}
481 gil_scoped_acquire_local(
const gil_scoped_acquire_local &) =
delete;
482 gil_scoped_acquire_local &operator=(
const gil_scoped_acquire_local &) =
delete;
483 ~gil_scoped_acquire_local() { PyGILState_Release(
state); }
484 const PyGILState_STATE
state;
494 if (internals_pp && *internals_pp) {
502 #if !defined(__GLIBCXX__)
509 auto *&internals_ptr = *internals_pp;
511 #if defined(WITH_THREAD)
513 PyThreadState *tstate = PyThreadState_Get();
516 pybind11_fail(
"get_internals: could not successfully initialize the tstate TSS key!");
520 # if PYBIND11_INTERNALS_VERSION > 4
523 pybind11_fail(
"get_internals: could not successfully initialize the "
524 "loader_life_support TSS key!");
527 internals_ptr->istate = tstate->interp;
535 return **internals_pp;
547 #if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
558 struct shared_loader_life_support_data {
560 shared_loader_life_support_data() {
563 pybind11_fail(
"local_internals: could not successfully initialize the "
564 "loader_life_support TLS key!");
575 ptr =
new shared_loader_life_support_data;
577 loader_life_support_tls_key
578 =
static_cast<shared_loader_life_support_data *
>(ptr)->loader_life_support_tls_key;
580 #endif // defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
598 template <
typename... Args>
601 strings.emplace_front(std::forward<Args>(
args)...);
602 return strings.front().c_str();
606 #if PYBIND11_INTERNALS_VERSION > 4
644 template <
typename T>
bool handle_nested_exception(const T &exc, const std::exception_ptr &p)
void(*)(std::exception_ptr) ExceptionTranslator
RAII wrapper that temporarily clears any Python error state.
void translate_exception(std::exception_ptr)
std::unordered_map< std::string, void * > shared_data
Annotation for function names.
size_t operator()(const std::type_index &t) const
std::vector< PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions
Array< double, 1, 3 > e(1./3., 0.5, 2.)
type_map< type_info * > registered_types_cpp
std::vector< std::pair< const std::type_info *, void *(*)(void *)> > implicit_casts
std::vector< PyObject * > unused_loader_patient_stack_remove_at_v5
PyTypeObject * make_static_property_type()
std::forward_list< ExceptionTranslator > registered_exception_translators
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NOINLINE
Eigen::Triplet< double > T
PyTypeObject * static_property_type
PyObject * make_object_base_type(PyTypeObject *metaclass)
PyObject * dict_getitemstring(PyObject *v, const char *key)
internals ** get_internals_pp_from_capsule(handle obj)
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
#define PYBIND11_NAMESPACE_BEGIN(name)
void(* dealloc)(value_and_holder &v_h)
std::forward_list< ExceptionTranslator > registered_exception_translators
const std::type_info * cpptype
std::unordered_multimap< const void *, instance * > registered_instances
T & get_or_create_shared_data(const std::string &name)
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
bool same_type(const std::type_info &lhs, const std::type_info &rhs)
#define PYBIND11_TLS_REPLACE_VALUE(key, value)
void raise_from(PyObject *type, const char *message)
const char * get_function_record_capsule_name()
#define PYBIND11_TLS_KEY_REF
bool raise_err(PyObject *exc_type, const char *msg)
std::unordered_set< std::pair< const PyObject *, const char * >, override_hash > inactive_override_cache
#define PYBIND11_INTERNALS_ID
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().
type_map< type_info * > registered_types_cpp
constexpr const char * internals_function_record_capsule_name
#define PYBIND11_TLS_KEY_CREATE(var)
A small structure to hold a non zero as a triplet (i,j,value).
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
void translate_local_exception(std::exception_ptr p)
std::vector< bool(*)(PyObject *, void *&)> * direct_conversions
bool is_function_record_capsule(const capsule &cap)
std::forward_list< std::string > static_strings
const gtsam::Symbol key('X', 0)
object get_python_state_dict()
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
PyObject * ptr() const
Return the underlying PyObject * pointer.
std::unordered_map< const PyObject *, std::vector< PyObject * > > patients
object get_internals_obj_from_state_dict(handle state_dict)
Information record describing a Python buffer object.
const char * c_str(Args &&...args)
local_internals & get_local_internals()
Works like get_internals, but for things which are locally registered.
Array< int, Dynamic, 1 > v
void(* init_instance)(instance *, const void *)
#define PYBIND11_TLS_KEY_INIT(var)
size_t operator()(const std::pair< const PyObject *, const char * > &v) const
PyTypeObject * default_metaclass
bool operator()(const std::type_index &lhs, const std::type_index &rhs) const
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
internals **& get_internals_pp()
#define PYBIND11_TLS_FREE(key)
PYBIND11_NOINLINE void * get_shared_data(const std::string &name)
C++ bindings of builtin Python exceptions.
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
PyTypeObject * make_default_metaclass()
std::unordered_map< std::type_index, value_type, type_hash, type_equal_to > type_map
size_t holder_size_in_ptrs
gtsam
Author(s):
autogenerated on Thu Jun 13 2024 03:02:38