Go to the documentation of this file.
14 #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
18 #include "../pytypes.h"
38 #ifndef PYBIND11_INTERNALS_VERSION
39 # if PY_VERSION_HEX >= 0x030C0000 || defined(_MSC_VER)
42 # define PYBIND11_INTERNALS_VERSION 5
44 # define PYBIND11_INTERNALS_VERSION 4
49 static_assert(PY_VERSION_HEX < 0x030C0000 || PYBIND11_INTERNALS_VERSION >= 5,
50 "pybind11 ABI version 5 is the minimum for Python 3.12+");
69 #if PYBIND11_INTERNALS_VERSION > 4
70 # define PYBIND11_TLS_KEY_REF Py_tss_t &
71 # if defined(__clang__)
72 # define PYBIND11_TLS_KEY_INIT(var) \
73 _Pragma("clang diagnostic push") \
74 _Pragma("clang diagnostic ignored \"-Wmissing-field-initializers\"") \
76 = Py_tss_NEEDS_INIT; \
77 _Pragma("clang diagnostic pop")
78 # elif defined(__GNUC__) && !defined(__INTEL_COMPILER)
79 # define PYBIND11_TLS_KEY_INIT(var) \
80 _Pragma("GCC diagnostic push") \
81 _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") \
83 = Py_tss_NEEDS_INIT; \
84 _Pragma("GCC diagnostic pop")
86 # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT;
88 # define PYBIND11_TLS_KEY_CREATE(var) (PyThread_tss_create(&(var)) == 0)
89 # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get(&(key))
90 # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value))
91 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr)
92 # define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key))
94 # define PYBIND11_TLS_KEY_REF Py_tss_t *
95 # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr;
96 # define PYBIND11_TLS_KEY_CREATE(var) \
97 (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0))
98 # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
99 # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value))
100 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
101 # define PYBIND11_TLS_FREE(key) PyThread_tss_free(key)
110 #if (PYBIND11_INTERNALS_VERSION <= 4 && defined(__GLIBCXX__)) \
111 || (PYBIND11_INTERNALS_VERSION >= 5 && !defined(_LIBCPP_VERSION))
112 inline bool same_type(
const std::type_info &lhs,
const std::type_info &rhs) {
return lhs == rhs; }
113 using type_hash = std::hash<std::type_index>;
116 inline bool same_type(
const std::type_info &lhs,
const std::type_info &rhs) {
117 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
123 const char *ptr =
t.name();
124 while (
auto c =
static_cast<unsigned char>(*ptr++)) {
132 bool operator()(
const std::type_index &lhs,
const std::type_index &rhs)
const {
133 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
138 template <
typename value_type>
139 using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
142 inline size_t operator()(
const std::pair<const PyObject *, const char *> &
v)
const {
143 size_t value = std::hash<const void *>()(
v.first);
144 value ^= std::hash<const void *>()(
v.second) + 0x9e3779b9 + (
value << 6) + (
value >> 2);
149 using instance_map = std::unordered_multimap<const void *, instance *>;
163 #ifdef Py_GIL_DISABLED
170 #ifdef Py_GIL_DISABLED
171 std::unique_ptr<instance_map_shard[]> instance_shards;
172 size_t instance_shards_mask;
176 std::unordered_set<std::pair<const PyObject *, const char *>,
override_hash>
179 std::unordered_map<const PyObject *, std::vector<PyObject *>>
patients;
183 #if PYBIND11_INTERNALS_VERSION == 4
193 #if PYBIND11_INTERNALS_VERSION > 4
195 #endif // PYBIND11_INTERNALS_VERSION > 4
199 #if PYBIND11_INTERNALS_VERSION > 4
209 #if PYBIND11_INTERNALS_VERSION > 4
211 #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 ""
295 #ifndef PYBIND11_BUILD_ABI
296 # if defined(__GXX_ABI_VERSION)
297 # define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION)
298 # elif defined(_MSC_VER)
299 # define PYBIND11_BUILD_ABI "_mscver" PYBIND11_TOSTRING(_MSC_VER)
301 # define PYBIND11_BUILD_ABI ""
305 #ifndef PYBIND11_INTERNALS_KIND
306 # define PYBIND11_INTERNALS_KIND ""
309 #define PYBIND11_INTERNALS_ID \
310 "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
311 PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB \
312 PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__"
314 #define PYBIND11_MODULE_LOCAL_ID \
315 "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
316 PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB \
317 PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__"
322 static internals **internals_pp =
nullptr;
332 std::exception_ptr nested = exc.nested_ptr();
333 if (nested !=
nullptr && nested !=
p) {
343 if (
const auto *nep =
dynamic_cast<const std::nested_exception *
>(std::addressof(exc))) {
350 if (PyErr_Occurred()) {
363 std::rethrow_exception(
p);
370 if (
const auto *nep =
dynamic_cast<const std::nested_exception *
>(std::addressof(
e))) {
375 }
catch (
const std::bad_alloc &
e) {
379 }
catch (
const std::domain_error &
e) {
383 }
catch (
const std::invalid_argument &
e) {
387 }
catch (
const std::length_error &
e) {
391 }
catch (
const std::out_of_range &
e) {
395 }
catch (
const std::range_error &
e) {
399 }
catch (
const std::overflow_error &
e) {
403 }
catch (
const std::exception &
e) {
407 }
catch (
const std::nested_exception &
e) {
409 raise_err(PyExc_RuntimeError,
"Caught an unknown nested exception!");
412 raise_err(PyExc_RuntimeError,
"Caught an unknown exception!");
417 #if !defined(__GLIBCXX__)
421 std::rethrow_exception(
p);
435 #if PYBIND11_INTERNALS_VERSION <= 4 || PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION)
436 state_dict = reinterpret_borrow<object>(PyEval_GetBuiltins());
438 # if PY_VERSION_HEX < 0x03090000
439 PyInterpreterState *istate = _PyInterpreterState_Get();
441 PyInterpreterState *istate = PyInterpreterState_Get();
444 state_dict = reinterpret_borrow<object>(PyInterpreterState_GetDict(istate));
448 raise_from(PyExc_SystemError,
"pybind11::detail::get_python_state_dict() FAILED");
455 return reinterpret_steal<object>(
460 void *raw_ptr = PyCapsule_GetPointer(obj.
ptr(),
nullptr);
461 if (raw_ptr ==
nullptr) {
462 raise_from(PyExc_SystemError,
"pybind11::detail::get_internals_pp_from_capsule() FAILED");
465 return static_cast<internals **
>(raw_ptr);
485 if (internals_pp && *internals_pp) {
486 return **internals_pp;
489 #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
494 struct gil_scoped_acquire_local {
495 gil_scoped_acquire_local() :
state(PyGILState_Ensure()) {}
496 gil_scoped_acquire_local(
const gil_scoped_acquire_local &) =
delete;
497 gil_scoped_acquire_local &operator=(
const gil_scoped_acquire_local &) =
delete;
498 ~gil_scoped_acquire_local() { PyGILState_Release(
state); }
499 const PyGILState_STATE
state;
508 if (internals_pp && *internals_pp) {
516 #if !defined(__GLIBCXX__)
523 auto *&internals_ptr = *internals_pp;
526 PyThreadState *tstate = PyThreadState_Get();
529 pybind11_fail(
"get_internals: could not successfully initialize the tstate TSS key!");
533 #if PYBIND11_INTERNALS_VERSION > 4
536 pybind11_fail(
"get_internals: could not successfully initialize the "
537 "loader_life_support TSS key!");
540 internals_ptr->istate = tstate->interp;
546 #ifdef Py_GIL_DISABLED
550 if (num_shards == 0) {
554 internals_ptr->instance_shards_mask = num_shards - 1;
555 #endif // Py_GIL_DISABLED
557 return **internals_pp;
569 #if PYBIND11_INTERNALS_VERSION == 4
585 pybind11_fail(
"local_internals: could not successfully initialize the "
586 "loader_life_support TLS key!");
599 loader_life_support_tls_key
602 #endif // PYBIND11_INTERNALS_VERSION == 4
616 #ifdef Py_GIL_DISABLED
617 # define PYBIND11_LOCK_INTERNALS(internals) std::unique_lock<std::mutex> lock((internals).mutex)
619 # define PYBIND11_LOCK_INTERNALS(internals)
622 template <
typename F>
633 z = (
z ^ (
z >> 30)) * 0xbf58476d1ce4e5b9;
634 z = (
z ^ (
z >> 27)) * 0x94d049bb133111eb;
635 return z ^ (
z >> 31);
638 template <
typename F>
640 const F &cb) -> decltype(cb(std::declval<instance_map &>())) {
643 #ifdef Py_GIL_DISABLED
651 auto idx =
static_cast<size_t>(
hash &
internals.instance_shards_mask);
653 auto &shard =
internals.instance_shards[idx];
654 std::unique_lock<std::mutex> lock(shard.mutex);
655 return cb(shard.registered_instances);
666 #ifdef Py_GIL_DISABLED
668 for (
size_t i = 0;
i <=
internals.instance_shards_mask; ++
i) {
670 std::unique_lock<std::mutex> lock(shard.mutex);
671 count += shard.registered_instances.size();
683 template <
typename... Args>
690 strings.emplace_front(std::forward<Args>(
args)...);
691 return strings.front().c_str();
695 #if PYBIND11_INTERNALS_VERSION > 4
736 template <
typename T>
bool handle_nested_exception(const T &exc, const std::exception_ptr &p)
void(*)(std::exception_ptr) ExceptionTranslator
char padding[64 -(sizeof(std::mutex)+sizeof(instance_map)) % 64]
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
auto with_instance_map(const void *ptr, const F &cb) -> decltype(cb(std::declval< instance_map & >()))
Array< double, 1, 3 > e(1./3., 0.5, 2.)
uint64_t round_up_to_next_pow2(uint64_t x)
type_map< type_info * > registered_types_cpp
std::vector< std::pair< const std::type_info *, void *(*)(void *)> > implicit_casts
#define PYBIND11_LOCK_INTERNALS(internals)
std::vector< PyObject * > unused_loader_patient_stack_remove_at_v5
PyTypeObject * make_static_property_type()
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
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)
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
size_t num_registered_instances()
instance_map 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()
std::unordered_multimap< const void *, instance * > instance_map
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
PyObject * dict_getitemstringref(PyObject *v, const char *key)
#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.
PyInterpreterState * istate
void set_error(const handle &type, const char *message)
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
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
auto with_internals(const F &cb) -> decltype(cb(get_internals()))
object get_internals_obj_from_state_dict(handle state_dict)
Information record describing a Python buffer object.
internals & operator=(const internals &other)=delete
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
std::uint64_t mix64(std::uint64_t z)
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
unsigned __int64 uint64_t
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.
instance_map registered_instances
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
_W64 unsigned int uintptr_t
size_t holder_size_in_ptrs
gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:01:17