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) 30 # define PYBIND11_TLS_KEY_INIT(var) decltype(PyThread_create_key()) var = 0 31 # define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key)) 32 # if PY_MAJOR_VERSION < 3 33 # define PYBIND11_TLS_DELETE_VALUE(key) \ 34 PyThread_delete_key_value(key) 35 # define PYBIND11_TLS_REPLACE_VALUE(key, value) \ 37 PyThread_delete_key_value((key)); \ 38 PyThread_set_key_value((key), (value)); \ 41 # define PYBIND11_TLS_DELETE_VALUE(key) \ 42 PyThread_set_key_value((key), nullptr) 43 # define PYBIND11_TLS_REPLACE_VALUE(key, value) \ 44 PyThread_set_key_value((key), (value)) 54 #if defined(__GLIBCXX__) 55 inline bool same_type(
const std::type_info &lhs,
const std::type_info &rhs) {
return lhs == rhs; }
56 using type_hash = std::hash<std::type_index>;
57 using type_equal_to = std::equal_to<std::type_index>;
59 inline bool same_type(
const std::type_info &lhs,
const std::type_info &rhs) {
60 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
66 const char *ptr = t.name();
67 while (
auto c = static_cast<unsigned char>(*ptr++))
68 hash = (hash * 33) ^ c;
74 bool operator()(
const std::type_index &lhs,
const std::type_index &rhs)
const {
75 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
80 template <
typename value_type>
81 using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
84 inline size_t operator()(
const std::pair<const PyObject *, const char *>& v)
const {
85 size_t value = std::hash<const void *>()(v.first);
86 value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2);
100 std::unordered_map<const PyObject *, std::vector<PyObject *>>
patients;
108 #if defined(WITH_THREAD) 110 PyInterpreterState *istate =
nullptr;
124 std::vector<std::pair<const std::type_info *, void *(*)(void *)>>
implicit_casts;
127 void *get_buffer_data =
nullptr;
128 void *(*module_local_load)(PyObject *,
const type_info *) =
nullptr;
141 #define PYBIND11_INTERNALS_VERSION 3 144 #if defined(_MSC_VER) && defined(_DEBUG) 145 # define PYBIND11_BUILD_TYPE "_debug" 147 # define PYBIND11_BUILD_TYPE "" 151 #if defined(__GXX_ABI_VERSION) 152 # define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION) 154 # define PYBIND11_BUILD_ABI "" 157 #if defined(WITH_THREAD) 158 # define PYBIND11_INTERNALS_KIND "" 160 # define PYBIND11_INTERNALS_KIND "_without_thread" 163 #define PYBIND11_INTERNALS_ID "__pybind11_internals_v" \ 164 PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__" 166 #define PYBIND11_MODULE_LOCAL_ID "__pybind11_module_local_v" \ 167 PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__" 172 static internals **internals_pp =
nullptr;
178 if (p) std::rethrow_exception(p);
181 }
catch (
const std::bad_alloc &e) { PyErr_SetString(PyExc_MemoryError, e.what());
return;
182 }
catch (
const std::domain_error &e) { PyErr_SetString(PyExc_ValueError, e.what());
return;
183 }
catch (
const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError, e.what());
return;
184 }
catch (
const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what());
return;
185 }
catch (
const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what());
return;
186 }
catch (
const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what());
return;
187 }
catch (
const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what());
return;
189 PyErr_SetString(PyExc_RuntimeError,
"Caught an unknown exception!");
194 #if !defined(__GLIBCXX__) 197 if (p) std::rethrow_exception(p);
207 if (internals_pp && *internals_pp)
208 return **internals_pp;
212 struct gil_scoped_acquire_local {
213 gil_scoped_acquire_local() : state (PyGILState_Ensure()) {}
214 ~gil_scoped_acquire_local() { PyGILState_Release(state); }
215 const PyGILState_STATE state;
219 auto builtins =
handle(PyEval_GetBuiltins());
220 if (builtins.contains(
id) && isinstance<capsule>(builtins[id])) {
228 #if !defined(__GLIBCXX__) 232 if (!internals_pp) internals_pp =
new internals*();
233 auto *&internals_ptr = *internals_pp;
235 #if defined(WITH_THREAD) 236 PyEval_InitThreads();
237 PyThreadState *tstate = PyThreadState_Get();
238 #if PY_VERSION_HEX >= 0x03070000 239 internals_ptr->tstate = PyThread_tss_alloc();
240 if (!internals_ptr->tstate || PyThread_tss_create(internals_ptr->tstate))
241 pybind11_fail(
"get_internals: could not successfully initialize the TSS key!");
242 PyThread_tss_set(internals_ptr->tstate, tstate);
244 internals_ptr->tstate = PyThread_create_key();
245 if (internals_ptr->tstate == -1)
246 pybind11_fail(
"get_internals: could not successfully initialize the TLS key!");
247 PyThread_set_key_value(internals_ptr->tstate, tstate);
249 internals_ptr->istate = tstate->interp;
251 builtins[id] =
capsule(internals_pp);
257 return **internals_pp;
270 template <
typename... Args>
272 auto &strings = get_internals().static_strings;
273 strings.emplace_front(std::forward<Args>(
args)...);
274 return strings.front().c_str();
283 auto &
internals = detail::get_internals();
290 detail::get_internals().shared_data[
name] =
data;
299 auto &internals = detail::get_internals();
300 auto it = internals.shared_data.find(name);
301 T *ptr = (T *) (it != internals.shared_data.end() ? it->second :
nullptr);
304 internals.shared_data[name] = ptr;
std::unordered_set< std::pair< const PyObject *, const char * >, overload_hash > inactive_overload_cache
std::unordered_map< std::type_index, value_type, type_hash, type_equal_to > type_map
std::vector< PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions
T & get_or_create_shared_data(const std::string &name)
PyTypeObject * static_property_type
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
#define PYBIND11_NAMESPACE
size_t operator()(const std::type_index &t) const
void translate_local_exception(std::exception_ptr p)
std::vector< PyObject * > loader_patient_stack
internals **& get_internals_pp()
virtual void set_error() const =0
Set the error using the Python C API.
type_map< type_info * > & registered_local_types_cpp()
Works like internals.registered_types_cpp, but for module-local registered types: ...
C++ bindings of builtin Python exceptions.
arr data(const arr &a, Ix... index)
Information record describing a Python buffer object.
bool operator()(const std::type_index &lhs, const std::type_index &rhs) const
#define PYBIND11_NOINLINE
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)
std::unordered_map< std::string, void * > shared_data
op_< op_hash, op_u, self_t, undefined_t > hash(const self_t &)
std::vector< bool(*)(PyObject *, void *&)> * direct_conversions
#define NAMESPACE_END(name)
PyTypeObject * default_metaclass
PyObject * make_object_base_type(PyTypeObject *metaclass)
const std::type_info * cpptype
PyTypeObject * make_default_metaclass()
void translate_exception(std::exception_ptr p)
std::forward_list< void(*)(std::exception_ptr)> registered_exception_translators
PyTypeObject * make_static_property_type()
#define PYBIND11_TLS_KEY_INIT(var)
std::unordered_multimap< const void *, instance * > registered_instances
#define PYBIND11_INTERNALS_ID
Annotation for function names.
type_map< type_info * > registered_types_cpp
const char * c_str(Args &&...args)
std::forward_list< std::string > static_strings
#define NAMESPACE_BEGIN(name)
std::vector< std::pair< const std::type_info *, void *(*)(void *)> > implicit_casts
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
std::unordered_map< const PyObject *, std::vector< PyObject * > > patients
size_t operator()(const std::pair< const PyObject *, const char *> &v) const