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 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__) 110 inline bool same_type(
const std::type_info &lhs,
const std::type_info &rhs) {
return lhs == rhs; }
111 using type_hash = std::hash<std::type_index>;
114 inline 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++)) {
123 hash = (hash * 33) ^
c;
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;
136 template <
typename value_type>
137 using 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 171 #if defined(WITH_THREAD) 173 # if PYBIND11_INTERNALS_VERSION > 4 175 # endif // PYBIND11_INTERNALS_VERSION > 4 176 PyInterpreterState *istate =
nullptr;
178 # if PYBIND11_INTERNALS_VERSION > 4 180 # endif // PYBIND11_INTERNALS_VERSION > 4 204 std::vector<std::pair<const std::type_info *, void *(*) (void *)>>
implicit_casts;
207 void *get_buffer_data =
nullptr;
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" 239 # elif defined(__PGI) 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;
303 std::exception_ptr nested = exc.nested_ptr();
304 if (nested !=
nullptr && nested != p) {
314 if (
const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
321 if (PyErr_Occurred()) {
325 PyErr_SetString(exc_type, msg);
334 std::rethrow_exception(p);
341 if (
const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
346 }
catch (
const std::bad_alloc &e) {
350 }
catch (
const std::domain_error &e) {
354 }
catch (
const std::invalid_argument &e) {
358 }
catch (
const std::length_error &e) {
362 }
catch (
const std::out_of_range &e) {
366 }
catch (
const std::range_error &e) {
370 }
catch (
const std::overflow_error &e) {
372 raise_err(PyExc_OverflowError, e.what());
374 }
catch (
const std::exception &e) {
378 }
catch (
const std::nested_exception &e) {
380 raise_err(PyExc_RuntimeError,
"Caught an unknown nested exception!");
383 raise_err(PyExc_RuntimeError,
"Caught an unknown exception!");
388 #if !defined(__GLIBCXX__) 392 std::rethrow_exception(p);
407 if (internals_pp && *internals_pp) {
408 return **internals_pp;
413 struct gil_scoped_acquire_local {
414 gil_scoped_acquire_local() :
state(PyGILState_Ensure()) {}
415 ~gil_scoped_acquire_local() { PyGILState_Release(
state); }
416 const PyGILState_STATE
state;
421 auto builtins =
handle(PyEval_GetBuiltins());
422 if (builtins.contains(
id) && isinstance<capsule>(builtins[
id])) {
432 #if !defined(__GLIBCXX__) 439 auto *&internals_ptr = *internals_pp;
441 #if defined(WITH_THREAD) 443 # if PY_VERSION_HEX < 0x03090000 444 PyEval_InitThreads();
446 PyThreadState *tstate = PyThreadState_Get();
448 pybind11_fail(
"get_internals: could not successfully initialize the tstate TSS key!");
452 # if PYBIND11_INTERNALS_VERSION > 4 454 pybind11_fail(
"get_internals: could not successfully initialize the " 455 "loader_life_support TSS key!");
458 internals_ptr->istate = tstate->interp;
466 return **internals_pp;
478 #if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4 489 struct shared_loader_life_support_data {
491 shared_loader_life_support_data() {
493 pybind11_fail(
"local_internals: could not successfully initialize the " 494 "loader_life_support TLS key!");
505 ptr =
new shared_loader_life_support_data;
507 loader_life_support_tls_key
508 =
static_cast<shared_loader_life_support_data *
>(ptr)->loader_life_support_tls_key;
510 #endif // defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4 523 template <
typename... Args>
526 strings.emplace_front(std::forward<Args>(
args)...);
527 return strings.front().c_str();
550 template <
typename T>
const gtsam::Symbol key('X', 0)
void(*)(std::exception_ptr) ExceptionTranslator
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().
virtual void set_error() const =0
Set the error using the Python C API.
T & get_or_create_shared_data(const std::string &name)
std::forward_list< ExceptionTranslator > registered_exception_translators
PyTypeObject * make_default_metaclass()
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
PyTypeObject * make_static_property_type()
type_map< type_info * > registered_types_cpp
PyObject * make_object_base_type(PyTypeObject *metaclass)
bool handle_nested_exception(const T &exc, const std::exception_ptr &p)
void raise_from(PyObject *type, const char *message)
std::forward_list< ExceptionTranslator > registered_exception_translators
std::vector< std::pair< const std::type_info *, void *(*)(void *)> > implicit_casts
bool operator()(const std::type_index &lhs, const std::type_index &rhs) const
std::unordered_multimap< const void *, instance * > registered_instances
#define PYBIND11_TLS_FREE(key)
#define PYBIND11_NAMESPACE
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof') ...
static const Similarity3 id
std::vector< PyObject * > unused_loader_patient_stack_remove_at_v5
const char * c_str(Args &&...args)
bool same_type(const std::type_info &lhs, const std::type_info &rhs)
PyExc_RuntimeError [[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
PYBIND11_NOINLINE void * get_shared_data(const std::string &name)
PyTypeObject * static_property_type
const std::type_info * cpptype
#define PYBIND11_TLS_KEY_REF
size_t operator()(const std::pair< const PyObject *, const char *> &v) const
std::unordered_set< std::pair< const PyObject *, const char * >, override_hash > inactive_override_cache
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
Array< int, Dynamic, 1 > v
local_internals & get_local_internals()
Works like get_internals, but for things which are locally registered.
type_map< type_info * > registered_types_cpp
Eigen::Triplet< double > T
Array< double, 1, 3 > e(1./3., 0.5, 2.)
#define PYBIND11_STR_TYPE
std::vector< bool(*)(PyObject *, void *&)> * direct_conversions
PyTypeObject * default_metaclass
#define PYBIND11_TLS_KEY_INIT(var)
#define PYBIND11_INTERNALS_ID
void translate_exception(std::exception_ptr)
std::forward_list< std::string > static_strings
#define PYBIND11_TLS_REPLACE_VALUE(key, value)
RAII wrapper that temporarily clears any Python error state.
std::unordered_map< const PyObject *, std::vector< PyObject * > > patients
std::unordered_map< std::type_index, value_type, type_hash, type_equal_to > type_map
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)
bool raise_err(PyObject *exc_type, const char *msg)
void translate_local_exception(std::exception_ptr p)
Annotation for function names.
Information record describing a Python buffer object.
std::unordered_map< std::string, void * > shared_data
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
#define PYBIND11_NOINLINE
internals **& get_internals_pp()
size_t operator()(const std::type_index &t) const
std::vector< PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)
#define PYBIND11_TLS_KEY_CREATE(var)