internals.h revision 14299:2fbea9df56d2
112841Sgabeblack@google.com/* 212841Sgabeblack@google.com pybind11/detail/internals.h: Internal data structure and related functions 312841Sgabeblack@google.com 412841Sgabeblack@google.com Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch> 512841Sgabeblack@google.com 612841Sgabeblack@google.com All rights reserved. Use of this source code is governed by a 712841Sgabeblack@google.com BSD-style license that can be found in the LICENSE file. 812841Sgabeblack@google.com*/ 912841Sgabeblack@google.com 1012841Sgabeblack@google.com#pragma once 1112841Sgabeblack@google.com 1212841Sgabeblack@google.com#include "../pytypes.h" 1312841Sgabeblack@google.com 1412841Sgabeblack@google.comNAMESPACE_BEGIN(PYBIND11_NAMESPACE) 1512841Sgabeblack@google.comNAMESPACE_BEGIN(detail) 1612841Sgabeblack@google.com// Forward declarations 1712841Sgabeblack@google.cominline PyTypeObject *make_static_property_type(); 1812841Sgabeblack@google.cominline PyTypeObject *make_default_metaclass(); 1912841Sgabeblack@google.cominline PyObject *make_object_base_type(PyTypeObject *metaclass); 2012841Sgabeblack@google.com 2112841Sgabeblack@google.com// The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new 2212841Sgabeblack@google.com// Thread Specific Storage (TSS) API. 2312841Sgabeblack@google.com#if PY_VERSION_HEX >= 0x03070000 2412841Sgabeblack@google.com# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr 2512841Sgabeblack@google.com# define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key)) 2612841Sgabeblack@google.com# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value)) 2712841Sgabeblack@google.com# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr) 2812841Sgabeblack@google.com#else 2912841Sgabeblack@google.com // Usually an int but a long on Cygwin64 with Python 3.x 3012841Sgabeblack@google.com# define PYBIND11_TLS_KEY_INIT(var) decltype(PyThread_create_key()) var = 0 3112841Sgabeblack@google.com# define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key)) 3212841Sgabeblack@google.com# if PY_MAJOR_VERSION < 3 3312841Sgabeblack@google.com# define PYBIND11_TLS_DELETE_VALUE(key) \ 3412841Sgabeblack@google.com PyThread_delete_key_value(key) 3512841Sgabeblack@google.com# define PYBIND11_TLS_REPLACE_VALUE(key, value) \ 3612841Sgabeblack@google.com do { \ 3712878Sgabeblack@google.com PyThread_delete_key_value((key)); \ 3812841Sgabeblack@google.com PyThread_set_key_value((key), (value)); \ 3912878Sgabeblack@google.com } while (false) 4012878Sgabeblack@google.com# else 4112841Sgabeblack@google.com# define PYBIND11_TLS_DELETE_VALUE(key) \ 4212908Sgabeblack@google.com PyThread_set_key_value((key), nullptr) 4312841Sgabeblack@google.com# define PYBIND11_TLS_REPLACE_VALUE(key, value) \ 4412908Sgabeblack@google.com PyThread_set_key_value((key), (value)) 4512841Sgabeblack@google.com# endif 4612841Sgabeblack@google.com#endif 4712841Sgabeblack@google.com 4812841Sgabeblack@google.com// Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly 4912841Sgabeblack@google.com// other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module 5012841Sgabeblack@google.com// even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under 5112841Sgabeblack@google.com// libstdc++, this doesn't happen: equality and the type_index hash are based on the type name, 5212841Sgabeblack@google.com// which works. If not under a known-good stl, provide our own name-based hash and equality 5312841Sgabeblack@google.com// functions that use the type name. 5412841Sgabeblack@google.com#if defined(__GLIBCXX__) 5512841Sgabeblack@google.cominline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; } 5612841Sgabeblack@google.comusing type_hash = std::hash<std::type_index>; 5712841Sgabeblack@google.comusing type_equal_to = std::equal_to<std::type_index>; 5812841Sgabeblack@google.com#else 5912841Sgabeblack@google.cominline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { 6012841Sgabeblack@google.com return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0; 6112841Sgabeblack@google.com} 6212841Sgabeblack@google.com 6312841Sgabeblack@google.comstruct type_hash { 6412841Sgabeblack@google.com size_t operator()(const std::type_index &t) const { 6512841Sgabeblack@google.com size_t hash = 5381; 6612841Sgabeblack@google.com const char *ptr = t.name(); 6712841Sgabeblack@google.com while (auto c = static_cast<unsigned char>(*ptr++)) 6812841Sgabeblack@google.com hash = (hash * 33) ^ c; 6912841Sgabeblack@google.com return hash; 7012841Sgabeblack@google.com } 7112841Sgabeblack@google.com}; 7212841Sgabeblack@google.com 7312841Sgabeblack@google.comstruct type_equal_to { 7412841Sgabeblack@google.com bool operator()(const std::type_index &lhs, const std::type_index &rhs) const { 7512841Sgabeblack@google.com return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0; 7612841Sgabeblack@google.com } 7712841Sgabeblack@google.com}; 7812841Sgabeblack@google.com#endif 7912841Sgabeblack@google.com 8012841Sgabeblack@google.comtemplate <typename value_type> 8112841Sgabeblack@google.comusing type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>; 8212841Sgabeblack@google.com 8312841Sgabeblack@google.comstruct overload_hash { 8412841Sgabeblack@google.com inline size_t operator()(const std::pair<const PyObject *, const char *>& v) const { 8512841Sgabeblack@google.com size_t value = std::hash<const void *>()(v.first); 8612841Sgabeblack@google.com value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2); 8712841Sgabeblack@google.com return value; 8812841Sgabeblack@google.com } 8912841Sgabeblack@google.com}; 9012841Sgabeblack@google.com 9112841Sgabeblack@google.com/// Internal data structure used to track registered instances and types. 9212841Sgabeblack@google.com/// Whenever binary incompatible changes are made to this structure, 9312841Sgabeblack@google.com/// `PYBIND11_INTERNALS_VERSION` must be incremented. 9412865Sgabeblack@google.comstruct internals { 9512841Sgabeblack@google.com type_map<type_info *> registered_types_cpp; // std::type_index -> pybind11's type information 9612841Sgabeblack@google.com std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py; // PyTypeObject* -> base type_info(s) 9712841Sgabeblack@google.com std::unordered_multimap<const void *, instance*> registered_instances; // void * -> instance* 9812841Sgabeblack@google.com std::unordered_set<std::pair<const PyObject *, const char *>, overload_hash> inactive_overload_cache; 9912841Sgabeblack@google.com type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions; 10013324Sgabeblack@google.com std::unordered_map<const PyObject *, std::vector<PyObject *>> patients; 10113324Sgabeblack@google.com std::forward_list<void (*) (std::exception_ptr)> registered_exception_translators; 10213324Sgabeblack@google.com std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across extensions 10313324Sgabeblack@google.com std::vector<PyObject *> loader_patient_stack; // Used by `loader_life_support` 10413324Sgabeblack@google.com std::forward_list<std::string> static_strings; // Stores the std::strings backing detail::c_str() 10513324Sgabeblack@google.com PyTypeObject *static_property_type; 10613324Sgabeblack@google.com PyTypeObject *default_metaclass; 10713324Sgabeblack@google.com PyObject *instance_base; 10813324Sgabeblack@google.com#if defined(WITH_THREAD) 10913324Sgabeblack@google.com PYBIND11_TLS_KEY_INIT(tstate); 11013324Sgabeblack@google.com PyInterpreterState *istate = nullptr; 11113324Sgabeblack@google.com#endif 11213324Sgabeblack@google.com}; 11313324Sgabeblack@google.com 11413324Sgabeblack@google.com/// Additional type information which does not fit into the PyTypeObject. 11513324Sgabeblack@google.com/// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`. 11613324Sgabeblack@google.comstruct type_info { 11713324Sgabeblack@google.com PyTypeObject *type; 11813324Sgabeblack@google.com const std::type_info *cpptype; 11913324Sgabeblack@google.com size_t type_size, type_align, holder_size_in_ptrs; 12013324Sgabeblack@google.com void *(*operator_new)(size_t); 12113324Sgabeblack@google.com void (*init_instance)(instance *, const void *); 12213324Sgabeblack@google.com void (*dealloc)(value_and_holder &v_h); 12313324Sgabeblack@google.com std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions; 12413324Sgabeblack@google.com std::vector<std::pair<const std::type_info *, void *(*)(void *)>> implicit_casts; 12513324Sgabeblack@google.com std::vector<bool (*)(PyObject *, void *&)> *direct_conversions; 12613324Sgabeblack@google.com buffer_info *(*get_buffer)(PyObject *, void *) = nullptr; 12713324Sgabeblack@google.com void *get_buffer_data = nullptr; 12813324Sgabeblack@google.com void *(*module_local_load)(PyObject *, const type_info *) = nullptr; 12913324Sgabeblack@google.com /* A simple type never occurs as a (direct or indirect) parent 13013324Sgabeblack@google.com * of a class that makes use of multiple inheritance */ 13112841Sgabeblack@google.com bool simple_type : 1; 132 /* True if there is no multiple inheritance in this type's inheritance tree */ 133 bool simple_ancestors : 1; 134 /* for base vs derived holder_type checks */ 135 bool default_holder : 1; 136 /* true if this is a type registered with py::module_local */ 137 bool module_local : 1; 138}; 139 140/// Tracks the `internals` and `type_info` ABI version independent of the main library version 141#define PYBIND11_INTERNALS_VERSION 3 142 143/// On MSVC, debug and release builds are not ABI-compatible! 144#if defined(_MSC_VER) && defined(_DEBUG) 145# define PYBIND11_BUILD_TYPE "_debug" 146#else 147# define PYBIND11_BUILD_TYPE "" 148#endif 149 150/// Let's assume that different compilers are ABI-incompatible. 151#if defined(_MSC_VER) 152# define PYBIND11_COMPILER_TYPE "_msvc" 153#elif defined(__INTEL_COMPILER) 154# define PYBIND11_COMPILER_TYPE "_icc" 155#elif defined(__clang__) 156# define PYBIND11_COMPILER_TYPE "_clang" 157#elif defined(__PGI) 158# define PYBIND11_COMPILER_TYPE "_pgi" 159#elif defined(__MINGW32__) 160# define PYBIND11_COMPILER_TYPE "_mingw" 161#elif defined(__CYGWIN__) 162# define PYBIND11_COMPILER_TYPE "_gcc_cygwin" 163#elif defined(__GNUC__) 164# define PYBIND11_COMPILER_TYPE "_gcc" 165#else 166# define PYBIND11_COMPILER_TYPE "_unknown" 167#endif 168 169#if defined(_LIBCPP_VERSION) 170# define PYBIND11_STDLIB "_libcpp" 171#elif defined(__GLIBCXX__) || defined(__GLIBCPP__) 172# define PYBIND11_STDLIB "_libstdcpp" 173#else 174# define PYBIND11_STDLIB "" 175#endif 176 177/// On Linux/OSX, changes in __GXX_ABI_VERSION__ indicate ABI incompatibility. 178#if defined(__GXX_ABI_VERSION) 179# define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION) 180#else 181# define PYBIND11_BUILD_ABI "" 182#endif 183 184#if defined(WITH_THREAD) 185# define PYBIND11_INTERNALS_KIND "" 186#else 187# define PYBIND11_INTERNALS_KIND "_without_thread" 188#endif 189 190#define PYBIND11_INTERNALS_ID "__pybind11_internals_v" \ 191 PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__" 192 193#define PYBIND11_MODULE_LOCAL_ID "__pybind11_module_local_v" \ 194 PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__" 195 196/// Each module locally stores a pointer to the `internals` data. The data 197/// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`. 198inline internals **&get_internals_pp() { 199 static internals **internals_pp = nullptr; 200 return internals_pp; 201} 202 203inline void translate_exception(std::exception_ptr p) { 204 try { 205 if (p) std::rethrow_exception(p); 206 } catch (error_already_set &e) { e.restore(); return; 207 } catch (const builtin_exception &e) { e.set_error(); return; 208 } catch (const std::bad_alloc &e) { PyErr_SetString(PyExc_MemoryError, e.what()); return; 209 } catch (const std::domain_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; 210 } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; 211 } catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; 212 } catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return; 213 } catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; 214 } catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return; 215 } catch (...) { 216 PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!"); 217 return; 218 } 219} 220 221#if !defined(__GLIBCXX__) 222inline void translate_local_exception(std::exception_ptr p) { 223 try { 224 if (p) std::rethrow_exception(p); 225 } catch (error_already_set &e) { e.restore(); return; 226 } catch (const builtin_exception &e) { e.set_error(); return; 227 } 228} 229#endif 230 231/// Return a reference to the current `internals` data 232PYBIND11_NOINLINE inline internals &get_internals() { 233 auto **&internals_pp = get_internals_pp(); 234 if (internals_pp && *internals_pp) 235 return **internals_pp; 236 237 // Ensure that the GIL is held since we will need to make Python calls. 238 // Cannot use py::gil_scoped_acquire here since that constructor calls get_internals. 239 struct gil_scoped_acquire_local { 240 gil_scoped_acquire_local() : state (PyGILState_Ensure()) {} 241 ~gil_scoped_acquire_local() { PyGILState_Release(state); } 242 const PyGILState_STATE state; 243 } gil; 244 245 constexpr auto *id = PYBIND11_INTERNALS_ID; 246 auto builtins = handle(PyEval_GetBuiltins()); 247 if (builtins.contains(id) && isinstance<capsule>(builtins[id])) { 248 internals_pp = static_cast<internals **>(capsule(builtins[id])); 249 250 // We loaded builtins through python's builtins, which means that our `error_already_set` 251 // and `builtin_exception` may be different local classes than the ones set up in the 252 // initial exception translator, below, so add another for our local exception classes. 253 // 254 // libstdc++ doesn't require this (types there are identified only by name) 255#if !defined(__GLIBCXX__) 256 (*internals_pp)->registered_exception_translators.push_front(&translate_local_exception); 257#endif 258 } else { 259 if (!internals_pp) internals_pp = new internals*(); 260 auto *&internals_ptr = *internals_pp; 261 internals_ptr = new internals(); 262#if defined(WITH_THREAD) 263 PyEval_InitThreads(); 264 PyThreadState *tstate = PyThreadState_Get(); 265 #if PY_VERSION_HEX >= 0x03070000 266 internals_ptr->tstate = PyThread_tss_alloc(); 267 if (!internals_ptr->tstate || PyThread_tss_create(internals_ptr->tstate)) 268 pybind11_fail("get_internals: could not successfully initialize the TSS key!"); 269 PyThread_tss_set(internals_ptr->tstate, tstate); 270 #else 271 internals_ptr->tstate = PyThread_create_key(); 272 if (internals_ptr->tstate == -1) 273 pybind11_fail("get_internals: could not successfully initialize the TLS key!"); 274 PyThread_set_key_value(internals_ptr->tstate, tstate); 275 #endif 276 internals_ptr->istate = tstate->interp; 277#endif 278 builtins[id] = capsule(internals_pp); 279 internals_ptr->registered_exception_translators.push_front(&translate_exception); 280 internals_ptr->static_property_type = make_static_property_type(); 281 internals_ptr->default_metaclass = make_default_metaclass(); 282 internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass); 283 } 284 return **internals_pp; 285} 286 287/// Works like `internals.registered_types_cpp`, but for module-local registered types: 288inline type_map<type_info *> ®istered_local_types_cpp() { 289 static type_map<type_info *> locals{}; 290 return locals; 291} 292 293/// Constructs a std::string with the given arguments, stores it in `internals`, and returns its 294/// `c_str()`. Such strings objects have a long storage duration -- the internal strings are only 295/// cleared when the program exits or after interpreter shutdown (when embedding), and so are 296/// suitable for c-style strings needed by Python internals (such as PyTypeObject's tp_name). 297template <typename... Args> 298const char *c_str(Args &&...args) { 299 auto &strings = get_internals().static_strings; 300 strings.emplace_front(std::forward<Args>(args)...); 301 return strings.front().c_str(); 302} 303 304NAMESPACE_END(detail) 305 306/// Returns a named pointer that is shared among all extension modules (using the same 307/// pybind11 version) running in the current interpreter. Names starting with underscores 308/// are reserved for internal usage. Returns `nullptr` if no matching entry was found. 309inline PYBIND11_NOINLINE void *get_shared_data(const std::string &name) { 310 auto &internals = detail::get_internals(); 311 auto it = internals.shared_data.find(name); 312 return it != internals.shared_data.end() ? it->second : nullptr; 313} 314 315/// Set the shared data that can be later recovered by `get_shared_data()`. 316inline PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) { 317 detail::get_internals().shared_data[name] = data; 318 return data; 319} 320 321/// Returns a typed reference to a shared data entry (by using `get_shared_data()`) if 322/// such entry exists. Otherwise, a new object of default-constructible type `T` is 323/// added to the shared data under the given name and a reference to it is returned. 324template<typename T> 325T &get_or_create_shared_data(const std::string &name) { 326 auto &internals = detail::get_internals(); 327 auto it = internals.shared_data.find(name); 328 T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr); 329 if (!ptr) { 330 ptr = new T(); 331 internals.shared_data[name] = ptr; 332 } 333 return *ptr; 334} 335 336NAMESPACE_END(PYBIND11_NAMESPACE) 337