Warning, file /jana2/src/python/externals/pybind11-2.10.3/include/pybind11/detail/internals.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #pragma once
0011
0012 #include "common.h"
0013
0014 #if defined(WITH_THREAD) && defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
0015 # include "../gil.h"
0016 #endif
0017
0018 #include "../pytypes.h"
0019
0020 #include <exception>
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 #ifndef PYBIND11_INTERNALS_VERSION
0037 # define PYBIND11_INTERNALS_VERSION 4
0038 #endif
0039
0040 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0041
0042 using ExceptionTranslator = void (*)(std::exception_ptr);
0043
0044 PYBIND11_NAMESPACE_BEGIN(detail)
0045
0046 constexpr const char *internals_function_record_capsule_name = "pybind11_function_record_capsule";
0047
0048
0049 inline PyTypeObject *make_static_property_type();
0050 inline PyTypeObject *make_default_metaclass();
0051 inline PyObject *make_object_base_type(PyTypeObject *metaclass);
0052
0053
0054
0055 #if PY_VERSION_HEX >= 0x03070000
0056
0057
0058 # if PYBIND11_INTERNALS_VERSION > 4
0059 # define PYBIND11_TLS_KEY_REF Py_tss_t &
0060 # if defined(__GNUC__) && !defined(__INTEL_COMPILER)
0061
0062
0063 # define PYBIND11_TLS_KEY_INIT(var) \
0064 _Pragma("GCC diagnostic push") \
0065 _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") \
0066 Py_tss_t var \
0067 = Py_tss_NEEDS_INIT; \
0068 _Pragma("GCC diagnostic pop")
0069 # else
0070 # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT;
0071 # endif
0072 # define PYBIND11_TLS_KEY_CREATE(var) (PyThread_tss_create(&(var)) == 0)
0073 # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get(&(key))
0074 # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value))
0075 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr)
0076 # define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key))
0077 # else
0078 # define PYBIND11_TLS_KEY_REF Py_tss_t *
0079 # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr;
0080 # define PYBIND11_TLS_KEY_CREATE(var) \
0081 (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0))
0082 # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
0083 # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value))
0084 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
0085 # define PYBIND11_TLS_FREE(key) PyThread_tss_free(key)
0086 # endif
0087 #else
0088
0089 # define PYBIND11_TLS_KEY_REF decltype(PyThread_create_key())
0090 # define PYBIND11_TLS_KEY_INIT(var) PYBIND11_TLS_KEY_REF var = 0;
0091 # define PYBIND11_TLS_KEY_CREATE(var) (((var) = PyThread_create_key()) != -1)
0092 # define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key))
0093 # if defined(PYPY_VERSION)
0094
0095
0096
0097 inline void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value) {
0098 PyThread_delete_key_value(key);
0099 PyThread_set_key_value(key, value);
0100 }
0101 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_delete_key_value(key)
0102 # define PYBIND11_TLS_REPLACE_VALUE(key, value) \
0103 ::pybind11::detail::tls_replace_value((key), (value))
0104 # else
0105 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_set_key_value((key), nullptr)
0106 # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_set_key_value((key), (value))
0107 # endif
0108 # define PYBIND11_TLS_FREE(key) (void) key
0109 #endif
0110
0111
0112
0113
0114
0115
0116
0117 #if defined(__GLIBCXX__)
0118 inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
0119 using type_hash = std::hash<std::type_index>;
0120 using type_equal_to = std::equal_to<std::type_index>;
0121 #else
0122 inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
0123 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
0124 }
0125
0126 struct type_hash {
0127 size_t operator()(const std::type_index &t) const {
0128 size_t hash = 5381;
0129 const char *ptr = t.name();
0130 while (auto c = static_cast<unsigned char>(*ptr++)) {
0131 hash = (hash * 33) ^ c;
0132 }
0133 return hash;
0134 }
0135 };
0136
0137 struct type_equal_to {
0138 bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
0139 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
0140 }
0141 };
0142 #endif
0143
0144 template <typename value_type>
0145 using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
0146
0147 struct override_hash {
0148 inline size_t operator()(const std::pair<const PyObject *, const char *> &v) const {
0149 size_t value = std::hash<const void *>()(v.first);
0150 value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value << 6) + (value >> 2);
0151 return value;
0152 }
0153 };
0154
0155
0156
0157
0158 struct internals {
0159
0160 type_map<type_info *> registered_types_cpp;
0161
0162 std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py;
0163 std::unordered_multimap<const void *, instance *> registered_instances;
0164 std::unordered_set<std::pair<const PyObject *, const char *>, override_hash>
0165 inactive_override_cache;
0166 type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
0167 std::unordered_map<const PyObject *, std::vector<PyObject *>> patients;
0168 std::forward_list<ExceptionTranslator> registered_exception_translators;
0169 std::unordered_map<std::string, void *> shared_data;
0170
0171 #if PYBIND11_INTERNALS_VERSION == 4
0172 std::vector<PyObject *> unused_loader_patient_stack_remove_at_v5;
0173 #endif
0174 std::forward_list<std::string> static_strings;
0175
0176 PyTypeObject *static_property_type;
0177 PyTypeObject *default_metaclass;
0178 PyObject *instance_base;
0179 #if defined(WITH_THREAD)
0180
0181 PYBIND11_TLS_KEY_INIT(tstate)
0182 # if PYBIND11_INTERNALS_VERSION > 4
0183 PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
0184 # endif
0185
0186 PyInterpreterState *istate = nullptr;
0187
0188 # if PYBIND11_INTERNALS_VERSION > 4
0189
0190
0191 std::string function_record_capsule_name = internals_function_record_capsule_name;
0192 # endif
0193
0194 internals() = default;
0195 internals(const internals &other) = delete;
0196 internals &operator=(const internals &other) = delete;
0197 ~internals() {
0198 # if PYBIND11_INTERNALS_VERSION > 4
0199 PYBIND11_TLS_FREE(loader_life_support_tls_key);
0200 # endif
0201
0202
0203
0204
0205
0206
0207
0208
0209 PYBIND11_TLS_FREE(tstate);
0210 }
0211 #endif
0212 };
0213
0214
0215
0216 struct type_info {
0217 PyTypeObject *type;
0218 const std::type_info *cpptype;
0219 size_t type_size, type_align, holder_size_in_ptrs;
0220 void *(*operator_new)(size_t);
0221 void (*init_instance)(instance *, const void *);
0222 void (*dealloc)(value_and_holder &v_h);
0223 std::vector<PyObject *(*) (PyObject *, PyTypeObject *)> implicit_conversions;
0224 std::vector<std::pair<const std::type_info *, void *(*) (void *)>> implicit_casts;
0225 std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
0226 buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
0227 void *get_buffer_data = nullptr;
0228 void *(*module_local_load)(PyObject *, const type_info *) = nullptr;
0229
0230
0231
0232
0233 bool simple_type : 1;
0234
0235 bool simple_ancestors : 1;
0236
0237 bool default_holder : 1;
0238
0239 bool module_local : 1;
0240 };
0241
0242
0243 #if defined(_MSC_VER) && defined(_DEBUG)
0244 # define PYBIND11_BUILD_TYPE "_debug"
0245 #else
0246 # define PYBIND11_BUILD_TYPE ""
0247 #endif
0248
0249
0250
0251
0252 #ifndef PYBIND11_COMPILER_TYPE
0253 # if defined(_MSC_VER)
0254 # define PYBIND11_COMPILER_TYPE "_msvc"
0255 # elif defined(__INTEL_COMPILER)
0256 # define PYBIND11_COMPILER_TYPE "_icc"
0257 # elif defined(__clang__)
0258 # define PYBIND11_COMPILER_TYPE "_clang"
0259 # elif defined(__PGI)
0260 # define PYBIND11_COMPILER_TYPE "_pgi"
0261 # elif defined(__MINGW32__)
0262 # define PYBIND11_COMPILER_TYPE "_mingw"
0263 # elif defined(__CYGWIN__)
0264 # define PYBIND11_COMPILER_TYPE "_gcc_cygwin"
0265 # elif defined(__GNUC__)
0266 # define PYBIND11_COMPILER_TYPE "_gcc"
0267 # else
0268 # define PYBIND11_COMPILER_TYPE "_unknown"
0269 # endif
0270 #endif
0271
0272
0273 #ifndef PYBIND11_STDLIB
0274 # if defined(_LIBCPP_VERSION)
0275 # define PYBIND11_STDLIB "_libcpp"
0276 # elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
0277 # define PYBIND11_STDLIB "_libstdcpp"
0278 # else
0279 # define PYBIND11_STDLIB ""
0280 # endif
0281 #endif
0282
0283
0284 #ifndef PYBIND11_BUILD_ABI
0285 # if defined(__GXX_ABI_VERSION)
0286 # define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION)
0287 # else
0288 # define PYBIND11_BUILD_ABI ""
0289 # endif
0290 #endif
0291
0292 #ifndef PYBIND11_INTERNALS_KIND
0293 # if defined(WITH_THREAD)
0294 # define PYBIND11_INTERNALS_KIND ""
0295 # else
0296 # define PYBIND11_INTERNALS_KIND "_without_thread"
0297 # endif
0298 #endif
0299
0300 #define PYBIND11_INTERNALS_ID \
0301 "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
0302 PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
0303 PYBIND11_BUILD_TYPE "__"
0304
0305 #define PYBIND11_MODULE_LOCAL_ID \
0306 "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
0307 PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
0308 PYBIND11_BUILD_TYPE "__"
0309
0310
0311
0312 inline internals **&get_internals_pp() {
0313 static internals **internals_pp = nullptr;
0314 return internals_pp;
0315 }
0316
0317
0318 inline void translate_exception(std::exception_ptr);
0319
0320 template <class T,
0321 enable_if_t<std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
0322 bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
0323 std::exception_ptr nested = exc.nested_ptr();
0324 if (nested != nullptr && nested != p) {
0325 translate_exception(nested);
0326 return true;
0327 }
0328 return false;
0329 }
0330
0331 template <class T,
0332 enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
0333 bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
0334 if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
0335 return handle_nested_exception(*nep, p);
0336 }
0337 return false;
0338 }
0339
0340 inline bool raise_err(PyObject *exc_type, const char *msg) {
0341 if (PyErr_Occurred()) {
0342 raise_from(exc_type, msg);
0343 return true;
0344 }
0345 PyErr_SetString(exc_type, msg);
0346 return false;
0347 }
0348
0349 inline void translate_exception(std::exception_ptr p) {
0350 if (!p) {
0351 return;
0352 }
0353 try {
0354 std::rethrow_exception(p);
0355 } catch (error_already_set &e) {
0356 handle_nested_exception(e, p);
0357 e.restore();
0358 return;
0359 } catch (const builtin_exception &e) {
0360
0361 if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
0362 handle_nested_exception(*nep, p);
0363 }
0364 e.set_error();
0365 return;
0366 } catch (const std::bad_alloc &e) {
0367 handle_nested_exception(e, p);
0368 raise_err(PyExc_MemoryError, e.what());
0369 return;
0370 } catch (const std::domain_error &e) {
0371 handle_nested_exception(e, p);
0372 raise_err(PyExc_ValueError, e.what());
0373 return;
0374 } catch (const std::invalid_argument &e) {
0375 handle_nested_exception(e, p);
0376 raise_err(PyExc_ValueError, e.what());
0377 return;
0378 } catch (const std::length_error &e) {
0379 handle_nested_exception(e, p);
0380 raise_err(PyExc_ValueError, e.what());
0381 return;
0382 } catch (const std::out_of_range &e) {
0383 handle_nested_exception(e, p);
0384 raise_err(PyExc_IndexError, e.what());
0385 return;
0386 } catch (const std::range_error &e) {
0387 handle_nested_exception(e, p);
0388 raise_err(PyExc_ValueError, e.what());
0389 return;
0390 } catch (const std::overflow_error &e) {
0391 handle_nested_exception(e, p);
0392 raise_err(PyExc_OverflowError, e.what());
0393 return;
0394 } catch (const std::exception &e) {
0395 handle_nested_exception(e, p);
0396 raise_err(PyExc_RuntimeError, e.what());
0397 return;
0398 } catch (const std::nested_exception &e) {
0399 handle_nested_exception(e, p);
0400 raise_err(PyExc_RuntimeError, "Caught an unknown nested exception!");
0401 return;
0402 } catch (...) {
0403 raise_err(PyExc_RuntimeError, "Caught an unknown exception!");
0404 return;
0405 }
0406 }
0407
0408 #if !defined(__GLIBCXX__)
0409 inline void translate_local_exception(std::exception_ptr p) {
0410 try {
0411 if (p) {
0412 std::rethrow_exception(p);
0413 }
0414 } catch (error_already_set &e) {
0415 e.restore();
0416 return;
0417 } catch (const builtin_exception &e) {
0418 e.set_error();
0419 return;
0420 }
0421 }
0422 #endif
0423
0424
0425 PYBIND11_NOINLINE internals &get_internals() {
0426 auto **&internals_pp = get_internals_pp();
0427 if (internals_pp && *internals_pp) {
0428 return **internals_pp;
0429 }
0430
0431 #if defined(WITH_THREAD)
0432 # if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
0433 gil_scoped_acquire gil;
0434 # else
0435
0436
0437 struct gil_scoped_acquire_local {
0438 gil_scoped_acquire_local() : state(PyGILState_Ensure()) {}
0439 gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete;
0440 gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete;
0441 ~gil_scoped_acquire_local() { PyGILState_Release(state); }
0442 const PyGILState_STATE state;
0443 } gil;
0444 # endif
0445 #endif
0446 error_scope err_scope;
0447
0448 PYBIND11_STR_TYPE id(PYBIND11_INTERNALS_ID);
0449 auto builtins = handle(PyEval_GetBuiltins());
0450 if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
0451 internals_pp = static_cast<internals **>(capsule(builtins[id]));
0452
0453
0454
0455
0456
0457
0458
0459
0460 #if !defined(__GLIBCXX__)
0461 (*internals_pp)->registered_exception_translators.push_front(&translate_local_exception);
0462 #endif
0463 } else {
0464 if (!internals_pp) {
0465 internals_pp = new internals *();
0466 }
0467 auto *&internals_ptr = *internals_pp;
0468 internals_ptr = new internals();
0469 #if defined(WITH_THREAD)
0470
0471 PyThreadState *tstate = PyThreadState_Get();
0472 if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->tstate)) {
0473 pybind11_fail("get_internals: could not successfully initialize the tstate TSS key!");
0474 }
0475 PYBIND11_TLS_REPLACE_VALUE(internals_ptr->tstate, tstate);
0476
0477 # if PYBIND11_INTERNALS_VERSION > 4
0478 if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->loader_life_support_tls_key)) {
0479 pybind11_fail("get_internals: could not successfully initialize the "
0480 "loader_life_support TSS key!");
0481 }
0482 # endif
0483 internals_ptr->istate = tstate->interp;
0484 #endif
0485 builtins[id] = capsule(internals_pp);
0486 internals_ptr->registered_exception_translators.push_front(&translate_exception);
0487 internals_ptr->static_property_type = make_static_property_type();
0488 internals_ptr->default_metaclass = make_default_metaclass();
0489 internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass);
0490 }
0491 return **internals_pp;
0492 }
0493
0494
0495
0496
0497
0498
0499
0500 struct local_internals {
0501 type_map<type_info *> registered_types_cpp;
0502 std::forward_list<ExceptionTranslator> registered_exception_translators;
0503 #if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
0504
0505
0506
0507
0508
0509
0510
0511 PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
0512
0513
0514 struct shared_loader_life_support_data {
0515 PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
0516 shared_loader_life_support_data() {
0517 if (!PYBIND11_TLS_KEY_CREATE(loader_life_support_tls_key)) {
0518 pybind11_fail("local_internals: could not successfully initialize the "
0519 "loader_life_support TLS key!");
0520 }
0521 }
0522
0523 };
0524
0525 local_internals() {
0526 auto &internals = get_internals();
0527
0528 auto &ptr = internals.shared_data["_life_support"];
0529 if (!ptr) {
0530 ptr = new shared_loader_life_support_data;
0531 }
0532 loader_life_support_tls_key
0533 = static_cast<shared_loader_life_support_data *>(ptr)->loader_life_support_tls_key;
0534 }
0535 #endif
0536 };
0537
0538
0539 inline local_internals &get_local_internals() {
0540
0541
0542
0543
0544
0545 static auto *locals = new local_internals();
0546 return *locals;
0547 }
0548
0549
0550
0551
0552
0553 template <typename... Args>
0554 const char *c_str(Args &&...args) {
0555 auto &strings = get_internals().static_strings;
0556 strings.emplace_front(std::forward<Args>(args)...);
0557 return strings.front().c_str();
0558 }
0559
0560 inline const char *get_function_record_capsule_name() {
0561 #if PYBIND11_INTERNALS_VERSION > 4
0562 return get_internals().function_record_capsule_name.c_str();
0563 #else
0564 return nullptr;
0565 #endif
0566 }
0567
0568
0569
0570
0571
0572
0573
0574 inline bool is_function_record_capsule(const capsule &cap) {
0575
0576 return cap.name() == get_function_record_capsule_name();
0577 }
0578
0579 PYBIND11_NAMESPACE_END(detail)
0580
0581
0582
0583
0584 PYBIND11_NOINLINE void *get_shared_data(const std::string &name) {
0585 auto &internals = detail::get_internals();
0586 auto it = internals.shared_data.find(name);
0587 return it != internals.shared_data.end() ? it->second : nullptr;
0588 }
0589
0590
0591 PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) {
0592 detail::get_internals().shared_data[name] = data;
0593 return data;
0594 }
0595
0596
0597
0598
0599 template <typename T>
0600 T &get_or_create_shared_data(const std::string &name) {
0601 auto &internals = detail::get_internals();
0602 auto it = internals.shared_data.find(name);
0603 T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr);
0604 if (!ptr) {
0605 ptr = new T();
0606 internals.shared_data[name] = ptr;
0607 }
0608 return *ptr;
0609 }
0610
0611 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)