internals.h revision 12391
17008Snate@binkert.org/* 27008Snate@binkert.org pybind11/detail/internals.h: Internal data structure and related functions 37008Snate@binkert.org 47008Snate@binkert.org Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch> 57008Snate@binkert.org 67008Snate@binkert.org All rights reserved. Use of this source code is governed by a 77008Snate@binkert.org BSD-style license that can be found in the LICENSE file. 87008Snate@binkert.org*/ 97008Snate@binkert.org 107008Snate@binkert.org#pragma once 117008Snate@binkert.org 127008Snate@binkert.org#include "../pytypes.h" 137008Snate@binkert.org 147008Snate@binkert.orgNAMESPACE_BEGIN(PYBIND11_NAMESPACE) 157008Snate@binkert.orgNAMESPACE_BEGIN(detail) 167008Snate@binkert.org// Forward declarations 177008Snate@binkert.orginline PyTypeObject *make_static_property_type(); 187008Snate@binkert.orginline PyTypeObject *make_default_metaclass(); 197008Snate@binkert.orginline PyObject *make_object_base_type(PyTypeObject *metaclass); 207008Snate@binkert.org 217008Snate@binkert.org// Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly 227008Snate@binkert.org// other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module 237008Snate@binkert.org// even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under 247008Snate@binkert.org// libstdc++, this doesn't happen: equality and the type_index hash are based on the type name, 257008Snate@binkert.org// which works. If not under a known-good stl, provide our own name-based hash and equality 267008Snate@binkert.org// functions that use the type name. 277008Snate@binkert.org#if defined(__GLIBCXX__) 286285Snate@binkert.orginline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; } 297039Snate@binkert.orgusing type_hash = std::hash<std::type_index>; 307039Snate@binkert.orgusing type_equal_to = std::equal_to<std::type_index>; 316285Snate@binkert.org#else 326285Snate@binkert.orginline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { 3310472Sandreas.hansson@arm.com return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0; 347039Snate@binkert.org} 359104Shestness@cs.utexas.edu 3610518Snilay@cs.wisc.edustruct type_hash { 376285Snate@binkert.org size_t operator()(const std::type_index &t) const { 3810518Snilay@cs.wisc.edu size_t hash = 5381; 3910518Snilay@cs.wisc.edu const char *ptr = t.name(); 4010518Snilay@cs.wisc.edu while (auto c = static_cast<unsigned char>(*ptr++)) 4110518Snilay@cs.wisc.edu hash = (hash * 33) ^ c; 426876Ssteve.reinhardt@amd.com return hash; 436876Ssteve.reinhardt@amd.com } 4410518Snilay@cs.wisc.edu}; 4510518Snilay@cs.wisc.edu 467039Snate@binkert.orgstruct type_equal_to { 477039Snate@binkert.org bool operator()(const std::type_index &lhs, const std::type_index &rhs) const { 487039Snate@binkert.org return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0; 497039Snate@binkert.org } 507039Snate@binkert.org}; 517039Snate@binkert.org#endif 527039Snate@binkert.org 539208Snilay@cs.wisc.edutemplate <typename value_type> 547039Snate@binkert.orgusing type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>; 556285Snate@binkert.org 566285Snate@binkert.orgstruct overload_hash { 5710518Snilay@cs.wisc.edu inline size_t operator()(const std::pair<const PyObject *, const char *>& v) const { 587039Snate@binkert.org size_t value = std::hash<const void *>()(v.first); 597039Snate@binkert.org value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2); 606876Ssteve.reinhardt@amd.com return value; 617039Snate@binkert.org } 627039Snate@binkert.org}; 6310518Snilay@cs.wisc.edu 6410518Snilay@cs.wisc.edu/// Internal data structure used to track registered instances and types. 6510518Snilay@cs.wisc.edu/// Whenever binary incompatible changes are made to this structure, 6610518Snilay@cs.wisc.edu/// `PYBIND11_INTERNALS_VERSION` must be incremented. 6710518Snilay@cs.wisc.edustruct internals { 6810518Snilay@cs.wisc.edu type_map<type_info *> registered_types_cpp; // std::type_index -> pybind11's type information 6910518Snilay@cs.wisc.edu std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py; // PyTypeObject* -> base type_info(s) 7010518Snilay@cs.wisc.edu std::unordered_multimap<const void *, instance*> registered_instances; // void * -> instance* 7110518Snilay@cs.wisc.edu std::unordered_set<std::pair<const PyObject *, const char *>, overload_hash> inactive_overload_cache; 7210518Snilay@cs.wisc.edu type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions; 7310518Snilay@cs.wisc.edu std::unordered_map<const PyObject *, std::vector<PyObject *>> patients; 7410518Snilay@cs.wisc.edu std::forward_list<void (*) (std::exception_ptr)> registered_exception_translators; 7510518Snilay@cs.wisc.edu std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across extensions 7610518Snilay@cs.wisc.edu std::vector<PyObject *> loader_patient_stack; // Used by `loader_life_support` 7710518Snilay@cs.wisc.edu std::forward_list<std::string> static_strings; // Stores the std::strings backing detail::c_str() 7810518Snilay@cs.wisc.edu PyTypeObject *static_property_type; 7910518Snilay@cs.wisc.edu PyTypeObject *default_metaclass; 8010518Snilay@cs.wisc.edu PyObject *instance_base; 8110518Snilay@cs.wisc.edu#if defined(WITH_THREAD) 8210518Snilay@cs.wisc.edu decltype(PyThread_create_key()) tstate = 0; // Usually an int but a long on Cygwin64 with Python 3.x 8310518Snilay@cs.wisc.edu PyInterpreterState *istate = nullptr; 8410518Snilay@cs.wisc.edu#endif 8510518Snilay@cs.wisc.edu}; 8610518Snilay@cs.wisc.edu 8710518Snilay@cs.wisc.edu/// Additional type information which does not fit into the PyTypeObject. 8810518Snilay@cs.wisc.edu/// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`. 8910518Snilay@cs.wisc.edustruct type_info { 9010518Snilay@cs.wisc.edu PyTypeObject *type; 9110518Snilay@cs.wisc.edu const std::type_info *cpptype; 9210518Snilay@cs.wisc.edu size_t type_size, holder_size_in_ptrs; 9310518Snilay@cs.wisc.edu void *(*operator_new)(size_t); 9410518Snilay@cs.wisc.edu void (*init_instance)(instance *, const void *); 9510518Snilay@cs.wisc.edu void (*dealloc)(value_and_holder &v_h); 967039Snate@binkert.org std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions; 978615Snilay@cs.wisc.edu std::vector<std::pair<const std::type_info *, void *(*)(void *)>> implicit_casts; 987039Snate@binkert.org std::vector<bool (*)(PyObject *, void *&)> *direct_conversions; 998688Snilay@cs.wisc.edu buffer_info *(*get_buffer)(PyObject *, void *) = nullptr; 1008688Snilay@cs.wisc.edu void *get_buffer_data = nullptr; 1018688Snilay@cs.wisc.edu void *(*module_local_load)(PyObject *, const type_info *) = nullptr; 1026285Snate@binkert.org /* A simple type never occurs as a (direct or indirect) parent 10310518Snilay@cs.wisc.edu * of a class that makes use of multiple inheritance */ 10410518Snilay@cs.wisc.edu bool simple_type : 1; 10510518Snilay@cs.wisc.edu /* True if there is no multiple inheritance in this type's inheritance tree */ 10610518Snilay@cs.wisc.edu bool simple_ancestors : 1; 10710518Snilay@cs.wisc.edu /* for base vs derived holder_type checks */ 10810518Snilay@cs.wisc.edu bool default_holder : 1; 1097039Snate@binkert.org /* true if this is a type registered with py::module_local */ 1107039Snate@binkert.org bool module_local : 1; 1117039Snate@binkert.org}; 1126285Snate@binkert.org 1139104Shestness@cs.utexas.edu/// Tracks the `internals` and `type_info` ABI version independent of the main library version 1149104Shestness@cs.utexas.edu#define PYBIND11_INTERNALS_VERSION 1 1157039Snate@binkert.org 1167039Snate@binkert.org#if defined(WITH_THREAD) 11710518Snilay@cs.wisc.edu# define PYBIND11_INTERNALS_KIND "" 11810518Snilay@cs.wisc.edu#else 11910518Snilay@cs.wisc.edu# define PYBIND11_INTERNALS_KIND "_without_thread" 12010518Snilay@cs.wisc.edu#endif 12110518Snilay@cs.wisc.edu 12210518Snilay@cs.wisc.edu#define PYBIND11_INTERNALS_ID "__pybind11_internals_v" \ 12310518Snilay@cs.wisc.edu PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND "__" 12410518Snilay@cs.wisc.edu 12510518Snilay@cs.wisc.edu#define PYBIND11_MODULE_LOCAL_ID "__pybind11_module_local_v" \ 12610518Snilay@cs.wisc.edu PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND "__" 12710518Snilay@cs.wisc.edu 12810518Snilay@cs.wisc.edu/// Each module locally stores a pointer to the `internals` data. The data 12910518Snilay@cs.wisc.edu/// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`. 1306285Snate@binkert.orginline internals *&get_internals_ptr() { 1317039Snate@binkert.org static internals *internals_ptr = nullptr; 13210518Snilay@cs.wisc.edu return internals_ptr; 13310518Snilay@cs.wisc.edu} 13410518Snilay@cs.wisc.edu 13510518Snilay@cs.wisc.edu/// Return a reference to the current `internals` data 13610518Snilay@cs.wisc.eduPYBIND11_NOINLINE inline internals &get_internals() { 13710518Snilay@cs.wisc.edu auto *&internals_ptr = get_internals_ptr(); 13810518Snilay@cs.wisc.edu if (internals_ptr) 13910518Snilay@cs.wisc.edu return *internals_ptr; 14010518Snilay@cs.wisc.edu 14110518Snilay@cs.wisc.edu constexpr auto *id = PYBIND11_INTERNALS_ID; 14210518Snilay@cs.wisc.edu auto builtins = handle(PyEval_GetBuiltins()); 14310518Snilay@cs.wisc.edu if (builtins.contains(id) && isinstance<capsule>(builtins[id])) { 14410518Snilay@cs.wisc.edu internals_ptr = *static_cast<internals **>(capsule(builtins[id])); 1457039Snate@binkert.org 1467039Snate@binkert.org // We loaded builtins through python's builtins, which means that our `error_already_set` 1477039Snate@binkert.org // and `builtin_exception` may be different local classes than the ones set up in the 1486285Snate@binkert.org // initial exception translator, below, so add another for our local exception classes. 1496285Snate@binkert.org // 1507039Snate@binkert.org // libstdc++ doesn't require this (types there are identified only by name) 151#if !defined(__GLIBCXX__) 152 internals_ptr->registered_exception_translators.push_front( 153 [](std::exception_ptr p) -> void { 154 try { 155 if (p) std::rethrow_exception(p); 156 } catch (error_already_set &e) { e.restore(); return; 157 } catch (const builtin_exception &e) { e.set_error(); return; 158 } 159 } 160 ); 161#endif 162 } else { 163 internals_ptr = new internals(); 164#if defined(WITH_THREAD) 165 PyEval_InitThreads(); 166 PyThreadState *tstate = PyThreadState_Get(); 167 internals_ptr->tstate = PyThread_create_key(); 168 PyThread_set_key_value(internals_ptr->tstate, tstate); 169 internals_ptr->istate = tstate->interp; 170#endif 171 builtins[id] = capsule(&internals_ptr); 172 internals_ptr->registered_exception_translators.push_front( 173 [](std::exception_ptr p) -> void { 174 try { 175 if (p) std::rethrow_exception(p); 176 } catch (error_already_set &e) { e.restore(); return; 177 } catch (const builtin_exception &e) { e.set_error(); return; 178 } catch (const std::bad_alloc &e) { PyErr_SetString(PyExc_MemoryError, e.what()); return; 179 } catch (const std::domain_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; 180 } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; 181 } catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; 182 } catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return; 183 } catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; 184 } catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return; 185 } catch (...) { 186 PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!"); 187 return; 188 } 189 } 190 ); 191 internals_ptr->static_property_type = make_static_property_type(); 192 internals_ptr->default_metaclass = make_default_metaclass(); 193 internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass); 194 } 195 return *internals_ptr; 196} 197 198/// Works like `internals.registered_types_cpp`, but for module-local registered types: 199inline type_map<type_info *> ®istered_local_types_cpp() { 200 static type_map<type_info *> locals{}; 201 return locals; 202} 203 204/// Constructs a std::string with the given arguments, stores it in `internals`, and returns its 205/// `c_str()`. Such strings objects have a long storage duration -- the internal strings are only 206/// cleared when the program exits or after interpreter shutdown (when embedding), and so are 207/// suitable for c-style strings needed by Python internals (such as PyTypeObject's tp_name). 208template <typename... Args> 209const char *c_str(Args &&...args) { 210 auto &strings = get_internals().static_strings; 211 strings.emplace_front(std::forward<Args>(args)...); 212 return strings.front().c_str(); 213} 214 215NAMESPACE_END(detail) 216 217/// Returns a named pointer that is shared among all extension modules (using the same 218/// pybind11 version) running in the current interpreter. Names starting with underscores 219/// are reserved for internal usage. Returns `nullptr` if no matching entry was found. 220inline PYBIND11_NOINLINE void *get_shared_data(const std::string &name) { 221 auto &internals = detail::get_internals(); 222 auto it = internals.shared_data.find(name); 223 return it != internals.shared_data.end() ? it->second : nullptr; 224} 225 226/// Set the shared data that can be later recovered by `get_shared_data()`. 227inline PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) { 228 detail::get_internals().shared_data[name] = data; 229 return data; 230} 231 232/// Returns a typed reference to a shared data entry (by using `get_shared_data()`) if 233/// such entry exists. Otherwise, a new object of default-constructible type `T` is 234/// added to the shared data under the given name and a reference to it is returned. 235template<typename T> 236T &get_or_create_shared_data(const std::string &name) { 237 auto &internals = detail::get_internals(); 238 auto it = internals.shared_data.find(name); 239 T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr); 240 if (!ptr) { 241 ptr = new T(); 242 internals.shared_data[name] = ptr; 243 } 244 return *ptr; 245} 246 247NAMESPACE_END(PYBIND11_NAMESPACE) 248