Go to the documentation of this file.
14 #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
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 *>;
151 #ifdef Py_GIL_DISABLED
157 pymutex() : mutex({}) {}
158 void lock() { PyMutex_Lock(&mutex); }
159 void unlock() { PyMutex_Unlock(&mutex); }
163 struct instance_map_shard {
167 char padding[64 - (
sizeof(
instance_map) +
sizeof(pymutex)) % 64];
170 static_assert(
sizeof(instance_map_shard) % 64 == 0,
171 "instance_map_shard size is not a multiple of 64 bytes");
178 #ifdef Py_GIL_DISABLED
185 #ifdef Py_GIL_DISABLED
186 std::unique_ptr<instance_map_shard[]> instance_shards;
187 size_t instance_shards_mask;
191 std::unordered_set<std::pair<const PyObject *, const char *>,
override_hash>
194 std::unordered_map<const PyObject *, std::vector<PyObject *>>
patients;
198 #if PYBIND11_INTERNALS_VERSION == 4
208 #if PYBIND11_INTERNALS_VERSION > 4
210 #endif // PYBIND11_INTERNALS_VERSION > 4
214 #if PYBIND11_INTERNALS_VERSION > 4
224 #if PYBIND11_INTERNALS_VERSION > 4
226 #endif // PYBIND11_INTERNALS_VERSION > 4
249 std::vector<std::pair<
const std::type_info *,
void *(*) (
void *)>>
implicit_casts;
253 void *(*module_local_load)(PyObject *,
const type_info *) =
nullptr;
268 #if defined(_MSC_VER) && defined(_DEBUG)
269 # define PYBIND11_BUILD_TYPE "_debug"
271 # define PYBIND11_BUILD_TYPE ""
277 #ifndef PYBIND11_COMPILER_TYPE
278 # if defined(_MSC_VER)
279 # define PYBIND11_COMPILER_TYPE "_msvc"
280 # elif defined(__INTEL_COMPILER)
281 # define PYBIND11_COMPILER_TYPE "_icc"
282 # elif defined(__clang__)
283 # define PYBIND11_COMPILER_TYPE "_clang"
284 # elif defined(__PGI)
285 # define PYBIND11_COMPILER_TYPE "_pgi"
286 # elif defined(__MINGW32__)
287 # define PYBIND11_COMPILER_TYPE "_mingw"
288 # elif defined(__CYGWIN__)
289 # define PYBIND11_COMPILER_TYPE "_gcc_cygwin"
290 # elif defined(__GNUC__)
291 # define PYBIND11_COMPILER_TYPE "_gcc"
293 # define PYBIND11_COMPILER_TYPE "_unknown"
298 #ifndef PYBIND11_STDLIB
299 # if defined(_LIBCPP_VERSION)
300 # define PYBIND11_STDLIB "_libcpp"
301 # elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
302 # define PYBIND11_STDLIB "_libstdcpp"
304 # define PYBIND11_STDLIB ""
310 #ifndef PYBIND11_BUILD_ABI
311 # if defined(__GXX_ABI_VERSION)
312 # define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION)
313 # elif defined(_MSC_VER)
314 # define PYBIND11_BUILD_ABI "_mscver" PYBIND11_TOSTRING(_MSC_VER)
316 # define PYBIND11_BUILD_ABI ""
320 #ifndef PYBIND11_INTERNALS_KIND
321 # define PYBIND11_INTERNALS_KIND ""
324 #define PYBIND11_PLATFORM_ABI_ID \
325 PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
328 #define PYBIND11_INTERNALS_ID \
329 "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
330 PYBIND11_PLATFORM_ABI_ID "__"
332 #define PYBIND11_MODULE_LOCAL_ID \
333 "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
334 PYBIND11_PLATFORM_ABI_ID "__"
339 static internals **internals_pp =
nullptr;
349 std::exception_ptr nested = exc.nested_ptr();
350 if (nested !=
nullptr && nested !=
p) {
360 if (
const auto *nep =
dynamic_cast<const std::nested_exception *
>(std::addressof(exc))) {
367 if (PyErr_Occurred()) {
380 std::rethrow_exception(
p);
387 if (
const auto *nep =
dynamic_cast<const std::nested_exception *
>(std::addressof(
e))) {
392 }
catch (
const std::bad_alloc &
e) {
396 }
catch (
const std::domain_error &
e) {
400 }
catch (
const std::invalid_argument &
e) {
404 }
catch (
const std::length_error &
e) {
408 }
catch (
const std::out_of_range &
e) {
412 }
catch (
const std::range_error &
e) {
416 }
catch (
const std::overflow_error &
e) {
420 }
catch (
const std::exception &
e) {
424 }
catch (
const std::nested_exception &
e) {
426 raise_err(PyExc_RuntimeError,
"Caught an unknown nested exception!");
429 raise_err(PyExc_RuntimeError,
"Caught an unknown exception!");
434 #if !defined(__GLIBCXX__)
438 std::rethrow_exception(
p);
452 #if PYBIND11_INTERNALS_VERSION <= 4 || PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION)
453 state_dict = reinterpret_borrow<object>(PyEval_GetBuiltins());
455 # if PY_VERSION_HEX < 0x03090000
456 PyInterpreterState *istate = _PyInterpreterState_Get();
458 PyInterpreterState *istate = PyInterpreterState_Get();
461 state_dict = reinterpret_borrow<object>(PyInterpreterState_GetDict(istate));
465 raise_from(PyExc_SystemError,
"pybind11::detail::get_python_state_dict() FAILED");
472 return reinterpret_steal<object>(
477 void *raw_ptr = PyCapsule_GetPointer(obj.
ptr(),
nullptr);
478 if (raw_ptr ==
nullptr) {
479 raise_from(PyExc_SystemError,
"pybind11::detail::get_internals_pp_from_capsule() FAILED");
482 return static_cast<internals **
>(raw_ptr);
502 if (internals_pp && *internals_pp) {
503 return **internals_pp;
506 #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
511 struct gil_scoped_acquire_local {
512 gil_scoped_acquire_local() :
state(PyGILState_Ensure()) {}
513 gil_scoped_acquire_local(
const gil_scoped_acquire_local &) =
delete;
514 gil_scoped_acquire_local &operator=(
const gil_scoped_acquire_local &) =
delete;
515 ~gil_scoped_acquire_local() { PyGILState_Release(
state); }
516 const PyGILState_STATE
state;
525 if (internals_pp && *internals_pp) {
533 #if !defined(__GLIBCXX__)
540 auto *&internals_ptr = *internals_pp;
543 PyThreadState *tstate = PyThreadState_Get();
546 pybind11_fail(
"get_internals: could not successfully initialize the tstate TSS key!");
550 #if PYBIND11_INTERNALS_VERSION > 4
553 pybind11_fail(
"get_internals: could not successfully initialize the "
554 "loader_life_support TSS key!");
557 internals_ptr->istate = tstate->interp;
563 #ifdef Py_GIL_DISABLED
567 if (num_shards == 0) {
570 internals_ptr->instance_shards.reset(
new instance_map_shard[num_shards]);
571 internals_ptr->instance_shards_mask = num_shards - 1;
572 #endif // Py_GIL_DISABLED
574 return **internals_pp;
586 #if PYBIND11_INTERNALS_VERSION == 4
602 pybind11_fail(
"local_internals: could not successfully initialize the "
603 "loader_life_support TLS key!");
616 loader_life_support_tls_key
619 #endif // PYBIND11_INTERNALS_VERSION == 4
633 #ifdef Py_GIL_DISABLED
634 # define PYBIND11_LOCK_INTERNALS(internals) std::unique_lock<pymutex> lock((internals).mutex)
636 # define PYBIND11_LOCK_INTERNALS(internals)
639 template <
typename F>
650 z = (
z ^ (
z >> 30)) * 0xbf58476d1ce4e5b9;
651 z = (
z ^ (
z >> 27)) * 0x94d049bb133111eb;
652 return z ^ (
z >> 31);
655 template <
typename F>
657 const F &cb) -> decltype(cb(std::declval<instance_map &>())) {
660 #ifdef Py_GIL_DISABLED
668 auto idx =
static_cast<size_t>(
hash &
internals.instance_shards_mask);
670 auto &shard =
internals.instance_shards[idx];
671 std::unique_lock<pymutex> lock(shard.mutex);
672 return cb(shard.registered_instances);
683 #ifdef Py_GIL_DISABLED
685 for (
size_t i = 0;
i <=
internals.instance_shards_mask; ++
i) {
687 std::unique_lock<pymutex> lock(shard.mutex);
688 count += shard.registered_instances.size();
700 template <
typename... Args>
707 strings.emplace_front(std::forward<Args>(
args)...);
708 return strings.front().c_str();
712 #if PYBIND11_INTERNALS_VERSION > 4
753 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
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.
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 Wed Mar 19 2025 03:01:50