12 #include "../pytypes.h" 23 #if PY_VERSION_HEX >= 0x03070000 24 # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr 25 # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key)) 26 # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value)) 27 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr) 28 # define PYBIND11_TLS_FREE(key) PyThread_tss_free(key) 31 # define PYBIND11_TLS_KEY_INIT(var) decltype(PyThread_create_key()) var = 0 32 # define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key)) 33 # if PY_MAJOR_VERSION < 3 34 # define PYBIND11_TLS_DELETE_VALUE(key) \ 35 PyThread_delete_key_value(key) 36 # define PYBIND11_TLS_REPLACE_VALUE(key, value) \ 38 PyThread_delete_key_value((key)); \ 39 PyThread_set_key_value((key), (value)); \ 42 # define PYBIND11_TLS_DELETE_VALUE(key) \ 43 PyThread_set_key_value((key), nullptr) 44 # define PYBIND11_TLS_REPLACE_VALUE(key, value) \ 45 PyThread_set_key_value((key), (value)) 47 # define PYBIND11_TLS_FREE(key) (void)key 56 #if defined(__GLIBCXX__) 57 inline bool same_type(
const std::type_info &lhs,
const std::type_info &rhs) {
return lhs == rhs; }
58 using type_hash = std::hash<std::type_index>;
61 inline bool same_type(
const std::type_info &lhs,
const std::type_info &rhs) {
62 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
68 const char *
ptr = t.name();
69 while (
auto c = static_cast<unsigned char>(*ptr++))
70 hash = (hash * 33) ^
c;
76 bool operator()(
const std::type_index &lhs,
const std::type_index &rhs)
const {
77 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
82 template <
typename value_type>
83 using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
86 inline size_t operator()(
const std::pair<const PyObject *, const char *>&
v)
const {
87 size_t value = std::hash<const void *>()(v.first);
88 value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2);
102 std::unordered_map<const PyObject *, std::vector<PyObject *>>
patients;
110 #if defined(WITH_THREAD) 112 PyInterpreterState *istate =
nullptr;
136 std::vector<std::pair<const std::type_info *, void *(*)(void *)>>
implicit_casts;
139 void *get_buffer_data =
nullptr;
140 void *(*module_local_load)(PyObject *,
const type_info *) =
nullptr;
143 bool simple_type : 1;
145 bool simple_ancestors : 1;
147 bool default_holder : 1;
153 #define PYBIND11_INTERNALS_VERSION 4 156 #if defined(_MSC_VER) && defined(_DEBUG) 157 # define PYBIND11_BUILD_TYPE "_debug" 159 # define PYBIND11_BUILD_TYPE "" 163 #if defined(_MSC_VER) 164 # define PYBIND11_COMPILER_TYPE "_msvc" 165 #elif defined(__INTEL_COMPILER) 166 # define PYBIND11_COMPILER_TYPE "_icc" 167 #elif defined(__clang__) 168 # define PYBIND11_COMPILER_TYPE "_clang" 170 # define PYBIND11_COMPILER_TYPE "_pgi" 171 #elif defined(__MINGW32__) 172 # define PYBIND11_COMPILER_TYPE "_mingw" 173 #elif defined(__CYGWIN__) 174 # define PYBIND11_COMPILER_TYPE "_gcc_cygwin" 175 #elif defined(__GNUC__) 176 # define PYBIND11_COMPILER_TYPE "_gcc" 178 # define PYBIND11_COMPILER_TYPE "_unknown" 181 #if defined(_LIBCPP_VERSION) 182 # define PYBIND11_STDLIB "_libcpp" 183 #elif defined(__GLIBCXX__) || defined(__GLIBCPP__) 184 # define PYBIND11_STDLIB "_libstdcpp" 186 # define PYBIND11_STDLIB "" 190 #if defined(__GXX_ABI_VERSION) 191 # define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION) 193 # define PYBIND11_BUILD_ABI "" 196 #if defined(WITH_THREAD) 197 # define PYBIND11_INTERNALS_KIND "" 199 # define PYBIND11_INTERNALS_KIND "_without_thread" 202 #define PYBIND11_INTERNALS_ID "__pybind11_internals_v" \ 203 PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__" 205 #define PYBIND11_MODULE_LOCAL_ID "__pybind11_module_local_v" \ 206 PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__" 211 static internals **internals_pp =
nullptr;
217 if (p) std::rethrow_exception(p);
220 }
catch (
const std::bad_alloc &e) { PyErr_SetString(PyExc_MemoryError, e.what());
return;
221 }
catch (
const std::domain_error &e) { PyErr_SetString(PyExc_ValueError, e.what());
return;
222 }
catch (
const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError, e.what());
return;
223 }
catch (
const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what());
return;
224 }
catch (
const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what());
return;
225 }
catch (
const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what());
return;
226 }
catch (
const std::overflow_error &e) { PyErr_SetString(PyExc_OverflowError, e.what());
return;
227 }
catch (
const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what());
return;
229 PyErr_SetString(PyExc_RuntimeError,
"Caught an unknown exception!");
234 #if !defined(__GLIBCXX__) 237 if (p) std::rethrow_exception(p);
247 if (internals_pp && *internals_pp)
248 return **internals_pp;
252 struct gil_scoped_acquire_local {
253 gil_scoped_acquire_local() : state (PyGILState_Ensure()) {}
254 ~gil_scoped_acquire_local() { PyGILState_Release(state); }
255 const PyGILState_STATE state;
259 auto builtins =
handle(PyEval_GetBuiltins());
260 if (builtins.contains(
id) && isinstance<capsule>(builtins[
id])) {
268 #if !defined(__GLIBCXX__) 272 if (!internals_pp) internals_pp =
new internals*();
273 auto *&internals_ptr = *internals_pp;
275 #if defined(WITH_THREAD) 277 #if PY_VERSION_HEX < 0x03090000 278 PyEval_InitThreads();
280 PyThreadState *tstate = PyThreadState_Get();
281 #if PY_VERSION_HEX >= 0x03070000 282 internals_ptr->tstate = PyThread_tss_alloc();
283 if (!internals_ptr->tstate || PyThread_tss_create(internals_ptr->tstate))
284 pybind11_fail(
"get_internals: could not successfully initialize the TSS key!");
285 PyThread_tss_set(internals_ptr->tstate, tstate);
287 internals_ptr->tstate = PyThread_create_key();
288 if (internals_ptr->tstate == -1)
289 pybind11_fail(
"get_internals: could not successfully initialize the TLS key!");
290 PyThread_set_key_value(internals_ptr->tstate, tstate);
292 internals_ptr->istate = tstate->interp;
300 return **internals_pp;
313 template <
typename... Args>
316 strings.emplace_front(std::forward<Args>(
args)...);
317 return strings.front().c_str();
size_t operator()(const std::type_index &t) const
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().
std::forward_list< void(*)(std::exception_ptr)> registered_exception_translators
std::vector< PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions
std::vector< PyObject * > loader_patient_stack
virtual void set_error() const =0
Set the error using the Python C API.
T & get_or_create_shared_data(const std::string &name)
Q id(Eigen::AngleAxisd(0, Q_z_axis))
type_map< type_info * > & registered_local_types_cpp()
Works like internals.registered_types_cpp, but for module-local registered types: ...
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)
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') ...
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
Annotation that marks a class as local to the module:
std::unordered_set< std::pair< const PyObject *, const char * >, override_hash > inactive_override_cache
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
Eigen::Triplet< double > T
void translate_exception(std::exception_ptr p)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
std::vector< bool(*)(PyObject *, void *&)> * direct_conversions
std::vector< std::pair< const std::type_info *, void *(*)(void *)> > implicit_casts
size_t operator()(const std::pair< const PyObject *, const char * > &v) const
PyTypeObject * default_metaclass
#define PYBIND11_TLS_KEY_INIT(var)
#define PYBIND11_INTERNALS_ID
std::forward_list< std::string > static_strings
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.
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()
#define PYBIND11_NAMESPACE_END(name)
bool operator()(const std::type_index &lhs, const std::type_index &rhs) const
#define PYBIND11_NAMESPACE_BEGIN(name)