internals.h revision 12391
112391Sjason@lowepower.com/*
212391Sjason@lowepower.com    pybind11/detail/internals.h: Internal data structure and related functions
312391Sjason@lowepower.com
412391Sjason@lowepower.com    Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
512391Sjason@lowepower.com
612391Sjason@lowepower.com    All rights reserved. Use of this source code is governed by a
712391Sjason@lowepower.com    BSD-style license that can be found in the LICENSE file.
812391Sjason@lowepower.com*/
912391Sjason@lowepower.com
1012391Sjason@lowepower.com#pragma once
1112391Sjason@lowepower.com
1212391Sjason@lowepower.com#include "../pytypes.h"
1312391Sjason@lowepower.com
1412391Sjason@lowepower.comNAMESPACE_BEGIN(PYBIND11_NAMESPACE)
1512391Sjason@lowepower.comNAMESPACE_BEGIN(detail)
1612391Sjason@lowepower.com// Forward declarations
1712391Sjason@lowepower.cominline PyTypeObject *make_static_property_type();
1812391Sjason@lowepower.cominline PyTypeObject *make_default_metaclass();
1912391Sjason@lowepower.cominline PyObject *make_object_base_type(PyTypeObject *metaclass);
2012391Sjason@lowepower.com
2112391Sjason@lowepower.com// Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
2212391Sjason@lowepower.com// other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module
2312391Sjason@lowepower.com// even when `A` is the same, non-hidden-visibility type (e.g. from a common include).  Under
2412391Sjason@lowepower.com// libstdc++, this doesn't happen: equality and the type_index hash are based on the type name,
2512391Sjason@lowepower.com// which works.  If not under a known-good stl, provide our own name-based hash and equality
2612391Sjason@lowepower.com// functions that use the type name.
2712391Sjason@lowepower.com#if defined(__GLIBCXX__)
2812391Sjason@lowepower.cominline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
2912391Sjason@lowepower.comusing type_hash = std::hash<std::type_index>;
3012391Sjason@lowepower.comusing type_equal_to = std::equal_to<std::type_index>;
3112391Sjason@lowepower.com#else
3212391Sjason@lowepower.cominline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
3312391Sjason@lowepower.com    return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
3412391Sjason@lowepower.com}
3512391Sjason@lowepower.com
3612391Sjason@lowepower.comstruct type_hash {
3712391Sjason@lowepower.com    size_t operator()(const std::type_index &t) const {
3812391Sjason@lowepower.com        size_t hash = 5381;
3912391Sjason@lowepower.com        const char *ptr = t.name();
4012391Sjason@lowepower.com        while (auto c = static_cast<unsigned char>(*ptr++))
4112391Sjason@lowepower.com            hash = (hash * 33) ^ c;
4212391Sjason@lowepower.com        return hash;
4312391Sjason@lowepower.com    }
4412391Sjason@lowepower.com};
4512391Sjason@lowepower.com
4612391Sjason@lowepower.comstruct type_equal_to {
4712391Sjason@lowepower.com    bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
4812391Sjason@lowepower.com        return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
4912391Sjason@lowepower.com    }
5012391Sjason@lowepower.com};
5112391Sjason@lowepower.com#endif
5212391Sjason@lowepower.com
5312391Sjason@lowepower.comtemplate <typename value_type>
5412391Sjason@lowepower.comusing type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
5512391Sjason@lowepower.com
5612391Sjason@lowepower.comstruct overload_hash {
5712391Sjason@lowepower.com    inline size_t operator()(const std::pair<const PyObject *, const char *>& v) const {
5812391Sjason@lowepower.com        size_t value = std::hash<const void *>()(v.first);
5912391Sjason@lowepower.com        value ^= std::hash<const void *>()(v.second)  + 0x9e3779b9 + (value<<6) + (value>>2);
6012391Sjason@lowepower.com        return value;
6112391Sjason@lowepower.com    }
6212391Sjason@lowepower.com};
6312391Sjason@lowepower.com
6412391Sjason@lowepower.com/// Internal data structure used to track registered instances and types.
6512391Sjason@lowepower.com/// Whenever binary incompatible changes are made to this structure,
6612391Sjason@lowepower.com/// `PYBIND11_INTERNALS_VERSION` must be incremented.
6712391Sjason@lowepower.comstruct internals {
6812391Sjason@lowepower.com    type_map<type_info *> registered_types_cpp; // std::type_index -> pybind11's type information
6912391Sjason@lowepower.com    std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py; // PyTypeObject* -> base type_info(s)
7012391Sjason@lowepower.com    std::unordered_multimap<const void *, instance*> registered_instances; // void * -> instance*
7112391Sjason@lowepower.com    std::unordered_set<std::pair<const PyObject *, const char *>, overload_hash> inactive_overload_cache;
7212391Sjason@lowepower.com    type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
7312391Sjason@lowepower.com    std::unordered_map<const PyObject *, std::vector<PyObject *>> patients;
7412391Sjason@lowepower.com    std::forward_list<void (*) (std::exception_ptr)> registered_exception_translators;
7512391Sjason@lowepower.com    std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across extensions
7612391Sjason@lowepower.com    std::vector<PyObject *> loader_patient_stack; // Used by `loader_life_support`
7712391Sjason@lowepower.com    std::forward_list<std::string> static_strings; // Stores the std::strings backing detail::c_str()
7812391Sjason@lowepower.com    PyTypeObject *static_property_type;
7912391Sjason@lowepower.com    PyTypeObject *default_metaclass;
8012391Sjason@lowepower.com    PyObject *instance_base;
8112391Sjason@lowepower.com#if defined(WITH_THREAD)
8212391Sjason@lowepower.com    decltype(PyThread_create_key()) tstate = 0; // Usually an int but a long on Cygwin64 with Python 3.x
8312391Sjason@lowepower.com    PyInterpreterState *istate = nullptr;
8412391Sjason@lowepower.com#endif
8512391Sjason@lowepower.com};
8612391Sjason@lowepower.com
8712391Sjason@lowepower.com/// Additional type information which does not fit into the PyTypeObject.
8812391Sjason@lowepower.com/// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`.
8912391Sjason@lowepower.comstruct type_info {
9012391Sjason@lowepower.com    PyTypeObject *type;
9112391Sjason@lowepower.com    const std::type_info *cpptype;
9212391Sjason@lowepower.com    size_t type_size, holder_size_in_ptrs;
9312391Sjason@lowepower.com    void *(*operator_new)(size_t);
9412391Sjason@lowepower.com    void (*init_instance)(instance *, const void *);
9512391Sjason@lowepower.com    void (*dealloc)(value_and_holder &v_h);
9612391Sjason@lowepower.com    std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions;
9712391Sjason@lowepower.com    std::vector<std::pair<const std::type_info *, void *(*)(void *)>> implicit_casts;
9812391Sjason@lowepower.com    std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
9912391Sjason@lowepower.com    buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
10012391Sjason@lowepower.com    void *get_buffer_data = nullptr;
10112391Sjason@lowepower.com    void *(*module_local_load)(PyObject *, const type_info *) = nullptr;
10212391Sjason@lowepower.com    /* A simple type never occurs as a (direct or indirect) parent
10312391Sjason@lowepower.com     * of a class that makes use of multiple inheritance */
10412391Sjason@lowepower.com    bool simple_type : 1;
10512391Sjason@lowepower.com    /* True if there is no multiple inheritance in this type's inheritance tree */
10612391Sjason@lowepower.com    bool simple_ancestors : 1;
10712391Sjason@lowepower.com    /* for base vs derived holder_type checks */
10812391Sjason@lowepower.com    bool default_holder : 1;
10912391Sjason@lowepower.com    /* true if this is a type registered with py::module_local */
11012391Sjason@lowepower.com    bool module_local : 1;
11112391Sjason@lowepower.com};
11212391Sjason@lowepower.com
11312391Sjason@lowepower.com/// Tracks the `internals` and `type_info` ABI version independent of the main library version
11412391Sjason@lowepower.com#define PYBIND11_INTERNALS_VERSION 1
11512391Sjason@lowepower.com
11612391Sjason@lowepower.com#if defined(WITH_THREAD)
11712391Sjason@lowepower.com#  define PYBIND11_INTERNALS_KIND ""
11812391Sjason@lowepower.com#else
11912391Sjason@lowepower.com#  define PYBIND11_INTERNALS_KIND "_without_thread"
12012391Sjason@lowepower.com#endif
12112391Sjason@lowepower.com
12212391Sjason@lowepower.com#define PYBIND11_INTERNALS_ID "__pybind11_internals_v" \
12312391Sjason@lowepower.com    PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND "__"
12412391Sjason@lowepower.com
12512391Sjason@lowepower.com#define PYBIND11_MODULE_LOCAL_ID "__pybind11_module_local_v" \
12612391Sjason@lowepower.com    PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND "__"
12712391Sjason@lowepower.com
12812391Sjason@lowepower.com/// Each module locally stores a pointer to the `internals` data. The data
12912391Sjason@lowepower.com/// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`.
13012391Sjason@lowepower.cominline internals *&get_internals_ptr() {
13112391Sjason@lowepower.com    static internals *internals_ptr = nullptr;
13212391Sjason@lowepower.com    return internals_ptr;
13312391Sjason@lowepower.com}
13412391Sjason@lowepower.com
13512391Sjason@lowepower.com/// Return a reference to the current `internals` data
13612391Sjason@lowepower.comPYBIND11_NOINLINE inline internals &get_internals() {
13712391Sjason@lowepower.com    auto *&internals_ptr = get_internals_ptr();
13812391Sjason@lowepower.com    if (internals_ptr)
13912391Sjason@lowepower.com        return *internals_ptr;
14012391Sjason@lowepower.com
14112391Sjason@lowepower.com    constexpr auto *id = PYBIND11_INTERNALS_ID;
14212391Sjason@lowepower.com    auto builtins = handle(PyEval_GetBuiltins());
14312391Sjason@lowepower.com    if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
14412391Sjason@lowepower.com        internals_ptr = *static_cast<internals **>(capsule(builtins[id]));
14512391Sjason@lowepower.com
14612391Sjason@lowepower.com        // We loaded builtins through python's builtins, which means that our `error_already_set`
14712391Sjason@lowepower.com        // and `builtin_exception` may be different local classes than the ones set up in the
14812391Sjason@lowepower.com        // initial exception translator, below, so add another for our local exception classes.
14912391Sjason@lowepower.com        //
15012391Sjason@lowepower.com        // libstdc++ doesn't require this (types there are identified only by name)
15112391Sjason@lowepower.com#if !defined(__GLIBCXX__)
15212391Sjason@lowepower.com        internals_ptr->registered_exception_translators.push_front(
15312391Sjason@lowepower.com            [](std::exception_ptr p) -> void {
15412391Sjason@lowepower.com                try {
15512391Sjason@lowepower.com                    if (p) std::rethrow_exception(p);
15612391Sjason@lowepower.com                } catch (error_already_set &e)       { e.restore();   return;
15712391Sjason@lowepower.com                } catch (const builtin_exception &e) { e.set_error(); return;
15812391Sjason@lowepower.com                }
15912391Sjason@lowepower.com            }
16012391Sjason@lowepower.com        );
16112391Sjason@lowepower.com#endif
16212391Sjason@lowepower.com    } else {
16312391Sjason@lowepower.com        internals_ptr = new internals();
16412391Sjason@lowepower.com#if defined(WITH_THREAD)
16512391Sjason@lowepower.com        PyEval_InitThreads();
16612391Sjason@lowepower.com        PyThreadState *tstate = PyThreadState_Get();
16712391Sjason@lowepower.com        internals_ptr->tstate = PyThread_create_key();
16812391Sjason@lowepower.com        PyThread_set_key_value(internals_ptr->tstate, tstate);
16912391Sjason@lowepower.com        internals_ptr->istate = tstate->interp;
17012391Sjason@lowepower.com#endif
17112391Sjason@lowepower.com        builtins[id] = capsule(&internals_ptr);
17212391Sjason@lowepower.com        internals_ptr->registered_exception_translators.push_front(
17312391Sjason@lowepower.com            [](std::exception_ptr p) -> void {
17412391Sjason@lowepower.com                try {
17512391Sjason@lowepower.com                    if (p) std::rethrow_exception(p);
17612391Sjason@lowepower.com                } catch (error_already_set &e)           { e.restore();                                    return;
17712391Sjason@lowepower.com                } catch (const builtin_exception &e)     { e.set_error();                                  return;
17812391Sjason@lowepower.com                } catch (const std::bad_alloc &e)        { PyErr_SetString(PyExc_MemoryError,   e.what()); return;
17912391Sjason@lowepower.com                } catch (const std::domain_error &e)     { PyErr_SetString(PyExc_ValueError,    e.what()); return;
18012391Sjason@lowepower.com                } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError,    e.what()); return;
18112391Sjason@lowepower.com                } catch (const std::length_error &e)     { PyErr_SetString(PyExc_ValueError,    e.what()); return;
18212391Sjason@lowepower.com                } catch (const std::out_of_range &e)     { PyErr_SetString(PyExc_IndexError,    e.what()); return;
18312391Sjason@lowepower.com                } catch (const std::range_error &e)      { PyErr_SetString(PyExc_ValueError,    e.what()); return;
18412391Sjason@lowepower.com                } catch (const std::exception &e)        { PyErr_SetString(PyExc_RuntimeError,  e.what()); return;
18512391Sjason@lowepower.com                } catch (...) {
18612391Sjason@lowepower.com                    PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!");
18712391Sjason@lowepower.com                    return;
18812391Sjason@lowepower.com                }
18912391Sjason@lowepower.com            }
19012391Sjason@lowepower.com        );
19112391Sjason@lowepower.com        internals_ptr->static_property_type = make_static_property_type();
19212391Sjason@lowepower.com        internals_ptr->default_metaclass = make_default_metaclass();
19312391Sjason@lowepower.com        internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass);
19412391Sjason@lowepower.com    }
19512391Sjason@lowepower.com    return *internals_ptr;
19612391Sjason@lowepower.com}
19712391Sjason@lowepower.com
19812391Sjason@lowepower.com/// Works like `internals.registered_types_cpp`, but for module-local registered types:
19912391Sjason@lowepower.cominline type_map<type_info *> &registered_local_types_cpp() {
20012391Sjason@lowepower.com    static type_map<type_info *> locals{};
20112391Sjason@lowepower.com    return locals;
20212391Sjason@lowepower.com}
20312391Sjason@lowepower.com
20412391Sjason@lowepower.com/// Constructs a std::string with the given arguments, stores it in `internals`, and returns its
20512391Sjason@lowepower.com/// `c_str()`.  Such strings objects have a long storage duration -- the internal strings are only
20612391Sjason@lowepower.com/// cleared when the program exits or after interpreter shutdown (when embedding), and so are
20712391Sjason@lowepower.com/// suitable for c-style strings needed by Python internals (such as PyTypeObject's tp_name).
20812391Sjason@lowepower.comtemplate <typename... Args>
20912391Sjason@lowepower.comconst char *c_str(Args &&...args) {
21012391Sjason@lowepower.com    auto &strings = get_internals().static_strings;
21112391Sjason@lowepower.com    strings.emplace_front(std::forward<Args>(args)...);
21212391Sjason@lowepower.com    return strings.front().c_str();
21312391Sjason@lowepower.com}
21412391Sjason@lowepower.com
21512391Sjason@lowepower.comNAMESPACE_END(detail)
21612391Sjason@lowepower.com
21712391Sjason@lowepower.com/// Returns a named pointer that is shared among all extension modules (using the same
21812391Sjason@lowepower.com/// pybind11 version) running in the current interpreter. Names starting with underscores
21912391Sjason@lowepower.com/// are reserved for internal usage. Returns `nullptr` if no matching entry was found.
22012391Sjason@lowepower.cominline PYBIND11_NOINLINE void *get_shared_data(const std::string &name) {
22112391Sjason@lowepower.com    auto &internals = detail::get_internals();
22212391Sjason@lowepower.com    auto it = internals.shared_data.find(name);
22312391Sjason@lowepower.com    return it != internals.shared_data.end() ? it->second : nullptr;
22412391Sjason@lowepower.com}
22512391Sjason@lowepower.com
22612391Sjason@lowepower.com/// Set the shared data that can be later recovered by `get_shared_data()`.
22712391Sjason@lowepower.cominline PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) {
22812391Sjason@lowepower.com    detail::get_internals().shared_data[name] = data;
22912391Sjason@lowepower.com    return data;
23012391Sjason@lowepower.com}
23112391Sjason@lowepower.com
23212391Sjason@lowepower.com/// Returns a typed reference to a shared data entry (by using `get_shared_data()`) if
23312391Sjason@lowepower.com/// such entry exists. Otherwise, a new object of default-constructible type `T` is
23412391Sjason@lowepower.com/// added to the shared data under the given name and a reference to it is returned.
23512391Sjason@lowepower.comtemplate<typename T>
23612391Sjason@lowepower.comT &get_or_create_shared_data(const std::string &name) {
23712391Sjason@lowepower.com    auto &internals = detail::get_internals();
23812391Sjason@lowepower.com    auto it = internals.shared_data.find(name);
23912391Sjason@lowepower.com    T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr);
24012391Sjason@lowepower.com    if (!ptr) {
24112391Sjason@lowepower.com        ptr = new T();
24212391Sjason@lowepower.com        internals.shared_data[name] = ptr;
24312391Sjason@lowepower.com    }
24412391Sjason@lowepower.com    return *ptr;
24512391Sjason@lowepower.com}
24612391Sjason@lowepower.com
24712391Sjason@lowepower.comNAMESPACE_END(PYBIND11_NAMESPACE)
248