112391Sjason@lowepower.com/*
212391Sjason@lowepower.com    pybind11/detail/class.h: Python C API implementation details for py::class_
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 "../attr.h"
1314299Sbbruce@ucdavis.edu#include "../options.h"
1412391Sjason@lowepower.com
1512391Sjason@lowepower.comNAMESPACE_BEGIN(PYBIND11_NAMESPACE)
1612391Sjason@lowepower.comNAMESPACE_BEGIN(detail)
1712391Sjason@lowepower.com
1814299Sbbruce@ucdavis.edu#if PY_VERSION_HEX >= 0x03030000
1914299Sbbruce@ucdavis.edu#  define PYBIND11_BUILTIN_QUALNAME
2014299Sbbruce@ucdavis.edu#  define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
2114299Sbbruce@ucdavis.edu#else
2214299Sbbruce@ucdavis.edu// In pre-3.3 Python, we still set __qualname__ so that we can produce reliable function type
2314299Sbbruce@ucdavis.edu// signatures; in 3.3+ this macro expands to nothing:
2414299Sbbruce@ucdavis.edu#  define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) setattr((PyObject *) obj, "__qualname__", nameobj)
2514299Sbbruce@ucdavis.edu#endif
2614299Sbbruce@ucdavis.edu
2712391Sjason@lowepower.cominline PyTypeObject *type_incref(PyTypeObject *type) {
2812391Sjason@lowepower.com    Py_INCREF(type);
2912391Sjason@lowepower.com    return type;
3012391Sjason@lowepower.com}
3112391Sjason@lowepower.com
3212391Sjason@lowepower.com#if !defined(PYPY_VERSION)
3312391Sjason@lowepower.com
3412391Sjason@lowepower.com/// `pybind11_static_property.__get__()`: Always pass the class instead of the instance.
3512391Sjason@lowepower.comextern "C" inline PyObject *pybind11_static_get(PyObject *self, PyObject * /*ob*/, PyObject *cls) {
3612391Sjason@lowepower.com    return PyProperty_Type.tp_descr_get(self, cls, cls);
3712391Sjason@lowepower.com}
3812391Sjason@lowepower.com
3912391Sjason@lowepower.com/// `pybind11_static_property.__set__()`: Just like the above `__get__()`.
4012391Sjason@lowepower.comextern "C" inline int pybind11_static_set(PyObject *self, PyObject *obj, PyObject *value) {
4112391Sjason@lowepower.com    PyObject *cls = PyType_Check(obj) ? obj : (PyObject *) Py_TYPE(obj);
4212391Sjason@lowepower.com    return PyProperty_Type.tp_descr_set(self, cls, value);
4312391Sjason@lowepower.com}
4412391Sjason@lowepower.com
4512391Sjason@lowepower.com/** A `static_property` is the same as a `property` but the `__get__()` and `__set__()`
4612391Sjason@lowepower.com    methods are modified to always use the object type instead of a concrete instance.
4712391Sjason@lowepower.com    Return value: New reference. */
4812391Sjason@lowepower.cominline PyTypeObject *make_static_property_type() {
4912391Sjason@lowepower.com    constexpr auto *name = "pybind11_static_property";
5012391Sjason@lowepower.com    auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
5112391Sjason@lowepower.com
5212391Sjason@lowepower.com    /* Danger zone: from now (and until PyType_Ready), make sure to
5312391Sjason@lowepower.com       issue no Python C API calls which could potentially invoke the
5412391Sjason@lowepower.com       garbage collector (the GC will call type_traverse(), which will in
5512391Sjason@lowepower.com       turn find the newly constructed type in an invalid state) */
5612391Sjason@lowepower.com    auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
5712391Sjason@lowepower.com    if (!heap_type)
5812391Sjason@lowepower.com        pybind11_fail("make_static_property_type(): error allocating type!");
5912391Sjason@lowepower.com
6012391Sjason@lowepower.com    heap_type->ht_name = name_obj.inc_ref().ptr();
6114299Sbbruce@ucdavis.edu#ifdef PYBIND11_BUILTIN_QUALNAME
6212391Sjason@lowepower.com    heap_type->ht_qualname = name_obj.inc_ref().ptr();
6312391Sjason@lowepower.com#endif
6412391Sjason@lowepower.com
6512391Sjason@lowepower.com    auto type = &heap_type->ht_type;
6612391Sjason@lowepower.com    type->tp_name = name;
6712391Sjason@lowepower.com    type->tp_base = type_incref(&PyProperty_Type);
6812391Sjason@lowepower.com    type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
6912391Sjason@lowepower.com    type->tp_descr_get = pybind11_static_get;
7012391Sjason@lowepower.com    type->tp_descr_set = pybind11_static_set;
7112391Sjason@lowepower.com
7212391Sjason@lowepower.com    if (PyType_Ready(type) < 0)
7312391Sjason@lowepower.com        pybind11_fail("make_static_property_type(): failure in PyType_Ready()!");
7412391Sjason@lowepower.com
7512391Sjason@lowepower.com    setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
7614299Sbbruce@ucdavis.edu    PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
7712391Sjason@lowepower.com
7812391Sjason@lowepower.com    return type;
7912391Sjason@lowepower.com}
8012391Sjason@lowepower.com
8112391Sjason@lowepower.com#else // PYPY
8212391Sjason@lowepower.com
8312391Sjason@lowepower.com/** PyPy has some issues with the above C API, so we evaluate Python code instead.
8412391Sjason@lowepower.com    This function will only be called once so performance isn't really a concern.
8512391Sjason@lowepower.com    Return value: New reference. */
8612391Sjason@lowepower.cominline PyTypeObject *make_static_property_type() {
8712391Sjason@lowepower.com    auto d = dict();
8812391Sjason@lowepower.com    PyObject *result = PyRun_String(R"(\
8912391Sjason@lowepower.com        class pybind11_static_property(property):
9012391Sjason@lowepower.com            def __get__(self, obj, cls):
9112391Sjason@lowepower.com                return property.__get__(self, cls, cls)
9212391Sjason@lowepower.com
9312391Sjason@lowepower.com            def __set__(self, obj, value):
9412391Sjason@lowepower.com                cls = obj if isinstance(obj, type) else type(obj)
9512391Sjason@lowepower.com                property.__set__(self, cls, value)
9612391Sjason@lowepower.com        )", Py_file_input, d.ptr(), d.ptr()
9712391Sjason@lowepower.com    );
9812391Sjason@lowepower.com    if (result == nullptr)
9912391Sjason@lowepower.com        throw error_already_set();
10012391Sjason@lowepower.com    Py_DECREF(result);
10112391Sjason@lowepower.com    return (PyTypeObject *) d["pybind11_static_property"].cast<object>().release().ptr();
10212391Sjason@lowepower.com}
10312391Sjason@lowepower.com
10412391Sjason@lowepower.com#endif // PYPY
10512391Sjason@lowepower.com
10612391Sjason@lowepower.com/** Types with static properties need to handle `Type.static_prop = x` in a specific way.
10712391Sjason@lowepower.com    By default, Python replaces the `static_property` itself, but for wrapped C++ types
10812391Sjason@lowepower.com    we need to call `static_property.__set__()` in order to propagate the new value to
10912391Sjason@lowepower.com    the underlying C++ data structure. */
11012391Sjason@lowepower.comextern "C" inline int pybind11_meta_setattro(PyObject* obj, PyObject* name, PyObject* value) {
11112391Sjason@lowepower.com    // Use `_PyType_Lookup()` instead of `PyObject_GetAttr()` in order to get the raw
11212391Sjason@lowepower.com    // descriptor (`property`) instead of calling `tp_descr_get` (`property.__get__()`).
11312391Sjason@lowepower.com    PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name);
11412391Sjason@lowepower.com
11512391Sjason@lowepower.com    // The following assignment combinations are possible:
11612391Sjason@lowepower.com    //   1. `Type.static_prop = value`             --> descr_set: `Type.static_prop.__set__(value)`
11712391Sjason@lowepower.com    //   2. `Type.static_prop = other_static_prop` --> setattro:  replace existing `static_prop`
11812391Sjason@lowepower.com    //   3. `Type.regular_attribute = value`       --> setattro:  regular attribute assignment
11912391Sjason@lowepower.com    const auto static_prop = (PyObject *) get_internals().static_property_type;
12012391Sjason@lowepower.com    const auto call_descr_set = descr && PyObject_IsInstance(descr, static_prop)
12112391Sjason@lowepower.com                                && !PyObject_IsInstance(value, static_prop);
12212391Sjason@lowepower.com    if (call_descr_set) {
12312391Sjason@lowepower.com        // Call `static_property.__set__()` instead of replacing the `static_property`.
12412391Sjason@lowepower.com#if !defined(PYPY_VERSION)
12512391Sjason@lowepower.com        return Py_TYPE(descr)->tp_descr_set(descr, obj, value);
12612391Sjason@lowepower.com#else
12712391Sjason@lowepower.com        if (PyObject *result = PyObject_CallMethod(descr, "__set__", "OO", obj, value)) {
12812391Sjason@lowepower.com            Py_DECREF(result);
12912391Sjason@lowepower.com            return 0;
13012391Sjason@lowepower.com        } else {
13112391Sjason@lowepower.com            return -1;
13212391Sjason@lowepower.com        }
13312391Sjason@lowepower.com#endif
13412391Sjason@lowepower.com    } else {
13512391Sjason@lowepower.com        // Replace existing attribute.
13612391Sjason@lowepower.com        return PyType_Type.tp_setattro(obj, name, value);
13712391Sjason@lowepower.com    }
13812391Sjason@lowepower.com}
13912391Sjason@lowepower.com
14012391Sjason@lowepower.com#if PY_MAJOR_VERSION >= 3
14112391Sjason@lowepower.com/**
14212391Sjason@lowepower.com * Python 3's PyInstanceMethod_Type hides itself via its tp_descr_get, which prevents aliasing
14312391Sjason@lowepower.com * methods via cls.attr("m2") = cls.attr("m1"): instead the tp_descr_get returns a plain function,
14412391Sjason@lowepower.com * when called on a class, or a PyMethod, when called on an instance.  Override that behaviour here
14512391Sjason@lowepower.com * to do a special case bypass for PyInstanceMethod_Types.
14612391Sjason@lowepower.com */
14712391Sjason@lowepower.comextern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name) {
14812391Sjason@lowepower.com    PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name);
14912391Sjason@lowepower.com    if (descr && PyInstanceMethod_Check(descr)) {
15012391Sjason@lowepower.com        Py_INCREF(descr);
15112391Sjason@lowepower.com        return descr;
15212391Sjason@lowepower.com    }
15312391Sjason@lowepower.com    else {
15412391Sjason@lowepower.com        return PyType_Type.tp_getattro(obj, name);
15512391Sjason@lowepower.com    }
15612391Sjason@lowepower.com}
15712391Sjason@lowepower.com#endif
15812391Sjason@lowepower.com
15912391Sjason@lowepower.com/** This metaclass is assigned by default to all pybind11 types and is required in order
16012391Sjason@lowepower.com    for static properties to function correctly. Users may override this using `py::metaclass`.
16112391Sjason@lowepower.com    Return value: New reference. */
16212391Sjason@lowepower.cominline PyTypeObject* make_default_metaclass() {
16312391Sjason@lowepower.com    constexpr auto *name = "pybind11_type";
16412391Sjason@lowepower.com    auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
16512391Sjason@lowepower.com
16612391Sjason@lowepower.com    /* Danger zone: from now (and until PyType_Ready), make sure to
16712391Sjason@lowepower.com       issue no Python C API calls which could potentially invoke the
16812391Sjason@lowepower.com       garbage collector (the GC will call type_traverse(), which will in
16912391Sjason@lowepower.com       turn find the newly constructed type in an invalid state) */
17012391Sjason@lowepower.com    auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
17112391Sjason@lowepower.com    if (!heap_type)
17212391Sjason@lowepower.com        pybind11_fail("make_default_metaclass(): error allocating metaclass!");
17312391Sjason@lowepower.com
17412391Sjason@lowepower.com    heap_type->ht_name = name_obj.inc_ref().ptr();
17514299Sbbruce@ucdavis.edu#ifdef PYBIND11_BUILTIN_QUALNAME
17612391Sjason@lowepower.com    heap_type->ht_qualname = name_obj.inc_ref().ptr();
17712391Sjason@lowepower.com#endif
17812391Sjason@lowepower.com
17912391Sjason@lowepower.com    auto type = &heap_type->ht_type;
18012391Sjason@lowepower.com    type->tp_name = name;
18112391Sjason@lowepower.com    type->tp_base = type_incref(&PyType_Type);
18212391Sjason@lowepower.com    type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
18312391Sjason@lowepower.com
18412391Sjason@lowepower.com    type->tp_setattro = pybind11_meta_setattro;
18512391Sjason@lowepower.com#if PY_MAJOR_VERSION >= 3
18612391Sjason@lowepower.com    type->tp_getattro = pybind11_meta_getattro;
18712391Sjason@lowepower.com#endif
18812391Sjason@lowepower.com
18912391Sjason@lowepower.com    if (PyType_Ready(type) < 0)
19012391Sjason@lowepower.com        pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!");
19112391Sjason@lowepower.com
19212391Sjason@lowepower.com    setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
19314299Sbbruce@ucdavis.edu    PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
19412391Sjason@lowepower.com
19512391Sjason@lowepower.com    return type;
19612391Sjason@lowepower.com}
19712391Sjason@lowepower.com
19812391Sjason@lowepower.com/// For multiple inheritance types we need to recursively register/deregister base pointers for any
19912391Sjason@lowepower.com/// base classes with pointers that are difference from the instance value pointer so that we can
20012391Sjason@lowepower.com/// correctly recognize an offset base class pointer. This calls a function with any offset base ptrs.
20112391Sjason@lowepower.cominline void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self,
20212391Sjason@lowepower.com        bool (*f)(void * /*parentptr*/, instance * /*self*/)) {
20312391Sjason@lowepower.com    for (handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
20412391Sjason@lowepower.com        if (auto parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) {
20512391Sjason@lowepower.com            for (auto &c : parent_tinfo->implicit_casts) {
20612391Sjason@lowepower.com                if (c.first == tinfo->cpptype) {
20712391Sjason@lowepower.com                    auto *parentptr = c.second(valueptr);
20812391Sjason@lowepower.com                    if (parentptr != valueptr)
20912391Sjason@lowepower.com                        f(parentptr, self);
21012391Sjason@lowepower.com                    traverse_offset_bases(parentptr, parent_tinfo, self, f);
21112391Sjason@lowepower.com                    break;
21212391Sjason@lowepower.com                }
21312391Sjason@lowepower.com            }
21412391Sjason@lowepower.com        }
21512391Sjason@lowepower.com    }
21612391Sjason@lowepower.com}
21712391Sjason@lowepower.com
21812391Sjason@lowepower.cominline bool register_instance_impl(void *ptr, instance *self) {
21912391Sjason@lowepower.com    get_internals().registered_instances.emplace(ptr, self);
22012391Sjason@lowepower.com    return true; // unused, but gives the same signature as the deregister func
22112391Sjason@lowepower.com}
22212391Sjason@lowepower.cominline bool deregister_instance_impl(void *ptr, instance *self) {
22312391Sjason@lowepower.com    auto &registered_instances = get_internals().registered_instances;
22412391Sjason@lowepower.com    auto range = registered_instances.equal_range(ptr);
22512391Sjason@lowepower.com    for (auto it = range.first; it != range.second; ++it) {
22612391Sjason@lowepower.com        if (Py_TYPE(self) == Py_TYPE(it->second)) {
22712391Sjason@lowepower.com            registered_instances.erase(it);
22812391Sjason@lowepower.com            return true;
22912391Sjason@lowepower.com        }
23012391Sjason@lowepower.com    }
23112391Sjason@lowepower.com    return false;
23212391Sjason@lowepower.com}
23312391Sjason@lowepower.com
23412391Sjason@lowepower.cominline void register_instance(instance *self, void *valptr, const type_info *tinfo) {
23512391Sjason@lowepower.com    register_instance_impl(valptr, self);
23612391Sjason@lowepower.com    if (!tinfo->simple_ancestors)
23712391Sjason@lowepower.com        traverse_offset_bases(valptr, tinfo, self, register_instance_impl);
23812391Sjason@lowepower.com}
23912391Sjason@lowepower.com
24012391Sjason@lowepower.cominline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo) {
24112391Sjason@lowepower.com    bool ret = deregister_instance_impl(valptr, self);
24212391Sjason@lowepower.com    if (!tinfo->simple_ancestors)
24312391Sjason@lowepower.com        traverse_offset_bases(valptr, tinfo, self, deregister_instance_impl);
24412391Sjason@lowepower.com    return ret;
24512391Sjason@lowepower.com}
24612391Sjason@lowepower.com
24712391Sjason@lowepower.com/// Instance creation function for all pybind11 types. It allocates the internal instance layout for
24812391Sjason@lowepower.com/// holding C++ objects and holders.  Allocation is done lazily (the first time the instance is cast
24912391Sjason@lowepower.com/// to a reference or pointer), and initialization is done by an `__init__` function.
25012391Sjason@lowepower.cominline PyObject *make_new_instance(PyTypeObject *type) {
25112391Sjason@lowepower.com#if defined(PYPY_VERSION)
25212391Sjason@lowepower.com    // PyPy gets tp_basicsize wrong (issue 2482) under multiple inheritance when the first inherited
25312391Sjason@lowepower.com    // object is a a plain Python type (i.e. not derived from an extension type).  Fix it.
25412391Sjason@lowepower.com    ssize_t instance_size = static_cast<ssize_t>(sizeof(instance));
25512391Sjason@lowepower.com    if (type->tp_basicsize < instance_size) {
25612391Sjason@lowepower.com        type->tp_basicsize = instance_size;
25712391Sjason@lowepower.com    }
25812391Sjason@lowepower.com#endif
25912391Sjason@lowepower.com    PyObject *self = type->tp_alloc(type, 0);
26012391Sjason@lowepower.com    auto inst = reinterpret_cast<instance *>(self);
26112391Sjason@lowepower.com    // Allocate the value/holder internals:
26212391Sjason@lowepower.com    inst->allocate_layout();
26312391Sjason@lowepower.com
26412391Sjason@lowepower.com    inst->owned = true;
26512391Sjason@lowepower.com
26612391Sjason@lowepower.com    return self;
26712391Sjason@lowepower.com}
26812391Sjason@lowepower.com
26912391Sjason@lowepower.com/// Instance creation function for all pybind11 types. It only allocates space for the
27012391Sjason@lowepower.com/// C++ object, but doesn't call the constructor -- an `__init__` function must do that.
27112391Sjason@lowepower.comextern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *) {
27212391Sjason@lowepower.com    return make_new_instance(type);
27312391Sjason@lowepower.com}
27412391Sjason@lowepower.com
27512391Sjason@lowepower.com/// An `__init__` function constructs the C++ object. Users should provide at least one
27612391Sjason@lowepower.com/// of these using `py::init` or directly with `.def(__init__, ...)`. Otherwise, the
27712391Sjason@lowepower.com/// following default function will be used which simply throws an exception.
27812391Sjason@lowepower.comextern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject *) {
27912391Sjason@lowepower.com    PyTypeObject *type = Py_TYPE(self);
28012391Sjason@lowepower.com    std::string msg;
28112391Sjason@lowepower.com#if defined(PYPY_VERSION)
28212391Sjason@lowepower.com    msg += handle((PyObject *) type).attr("__module__").cast<std::string>() + ".";
28312391Sjason@lowepower.com#endif
28412391Sjason@lowepower.com    msg += type->tp_name;
28512391Sjason@lowepower.com    msg += ": No constructor defined!";
28612391Sjason@lowepower.com    PyErr_SetString(PyExc_TypeError, msg.c_str());
28712391Sjason@lowepower.com    return -1;
28812391Sjason@lowepower.com}
28912391Sjason@lowepower.com
29012391Sjason@lowepower.cominline void add_patient(PyObject *nurse, PyObject *patient) {
29112391Sjason@lowepower.com    auto &internals = get_internals();
29212391Sjason@lowepower.com    auto instance = reinterpret_cast<detail::instance *>(nurse);
29312391Sjason@lowepower.com    instance->has_patients = true;
29412391Sjason@lowepower.com    Py_INCREF(patient);
29512391Sjason@lowepower.com    internals.patients[nurse].push_back(patient);
29612391Sjason@lowepower.com}
29712391Sjason@lowepower.com
29812391Sjason@lowepower.cominline void clear_patients(PyObject *self) {
29912391Sjason@lowepower.com    auto instance = reinterpret_cast<detail::instance *>(self);
30012391Sjason@lowepower.com    auto &internals = get_internals();
30112391Sjason@lowepower.com    auto pos = internals.patients.find(self);
30212391Sjason@lowepower.com    assert(pos != internals.patients.end());
30312391Sjason@lowepower.com    // Clearing the patients can cause more Python code to run, which
30412391Sjason@lowepower.com    // can invalidate the iterator. Extract the vector of patients
30512391Sjason@lowepower.com    // from the unordered_map first.
30612391Sjason@lowepower.com    auto patients = std::move(pos->second);
30712391Sjason@lowepower.com    internals.patients.erase(pos);
30812391Sjason@lowepower.com    instance->has_patients = false;
30912391Sjason@lowepower.com    for (PyObject *&patient : patients)
31012391Sjason@lowepower.com        Py_CLEAR(patient);
31112391Sjason@lowepower.com}
31212391Sjason@lowepower.com
31312391Sjason@lowepower.com/// Clears all internal data from the instance and removes it from registered instances in
31412391Sjason@lowepower.com/// preparation for deallocation.
31512391Sjason@lowepower.cominline void clear_instance(PyObject *self) {
31612391Sjason@lowepower.com    auto instance = reinterpret_cast<detail::instance *>(self);
31712391Sjason@lowepower.com
31812391Sjason@lowepower.com    // Deallocate any values/holders, if present:
31912391Sjason@lowepower.com    for (auto &v_h : values_and_holders(instance)) {
32012391Sjason@lowepower.com        if (v_h) {
32112391Sjason@lowepower.com
32212391Sjason@lowepower.com            // We have to deregister before we call dealloc because, for virtual MI types, we still
32312391Sjason@lowepower.com            // need to be able to get the parent pointers.
32412391Sjason@lowepower.com            if (v_h.instance_registered() && !deregister_instance(instance, v_h.value_ptr(), v_h.type))
32512391Sjason@lowepower.com                pybind11_fail("pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
32612391Sjason@lowepower.com
32712391Sjason@lowepower.com            if (instance->owned || v_h.holder_constructed())
32812391Sjason@lowepower.com                v_h.type->dealloc(v_h);
32912391Sjason@lowepower.com        }
33012391Sjason@lowepower.com    }
33112391Sjason@lowepower.com    // Deallocate the value/holder layout internals:
33212391Sjason@lowepower.com    instance->deallocate_layout();
33312391Sjason@lowepower.com
33412391Sjason@lowepower.com    if (instance->weakrefs)
33512391Sjason@lowepower.com        PyObject_ClearWeakRefs(self);
33612391Sjason@lowepower.com
33712391Sjason@lowepower.com    PyObject **dict_ptr = _PyObject_GetDictPtr(self);
33812391Sjason@lowepower.com    if (dict_ptr)
33912391Sjason@lowepower.com        Py_CLEAR(*dict_ptr);
34012391Sjason@lowepower.com
34112391Sjason@lowepower.com    if (instance->has_patients)
34212391Sjason@lowepower.com        clear_patients(self);
34312391Sjason@lowepower.com}
34412391Sjason@lowepower.com
34512391Sjason@lowepower.com/// Instance destructor function for all pybind11 types. It calls `type_info.dealloc`
34612391Sjason@lowepower.com/// to destroy the C++ object itself, while the rest is Python bookkeeping.
34712391Sjason@lowepower.comextern "C" inline void pybind11_object_dealloc(PyObject *self) {
34812391Sjason@lowepower.com    clear_instance(self);
34912391Sjason@lowepower.com
35012391Sjason@lowepower.com    auto type = Py_TYPE(self);
35112391Sjason@lowepower.com    type->tp_free(self);
35212391Sjason@lowepower.com
35312391Sjason@lowepower.com    // `type->tp_dealloc != pybind11_object_dealloc` means that we're being called
35412391Sjason@lowepower.com    // as part of a derived type's dealloc, in which case we're not allowed to decref
35512391Sjason@lowepower.com    // the type here. For cross-module compatibility, we shouldn't compare directly
35612391Sjason@lowepower.com    // with `pybind11_object_dealloc`, but with the common one stashed in internals.
35712391Sjason@lowepower.com    auto pybind11_object_type = (PyTypeObject *) get_internals().instance_base;
35812391Sjason@lowepower.com    if (type->tp_dealloc == pybind11_object_type->tp_dealloc)
35912391Sjason@lowepower.com        Py_DECREF(type);
36012391Sjason@lowepower.com}
36112391Sjason@lowepower.com
36212391Sjason@lowepower.com/** Create the type which can be used as a common base for all classes.  This is
36312391Sjason@lowepower.com    needed in order to satisfy Python's requirements for multiple inheritance.
36412391Sjason@lowepower.com    Return value: New reference. */
36512391Sjason@lowepower.cominline PyObject *make_object_base_type(PyTypeObject *metaclass) {
36612391Sjason@lowepower.com    constexpr auto *name = "pybind11_object";
36712391Sjason@lowepower.com    auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
36812391Sjason@lowepower.com
36912391Sjason@lowepower.com    /* Danger zone: from now (and until PyType_Ready), make sure to
37012391Sjason@lowepower.com       issue no Python C API calls which could potentially invoke the
37112391Sjason@lowepower.com       garbage collector (the GC will call type_traverse(), which will in
37212391Sjason@lowepower.com       turn find the newly constructed type in an invalid state) */
37312391Sjason@lowepower.com    auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
37412391Sjason@lowepower.com    if (!heap_type)
37512391Sjason@lowepower.com        pybind11_fail("make_object_base_type(): error allocating type!");
37612391Sjason@lowepower.com
37712391Sjason@lowepower.com    heap_type->ht_name = name_obj.inc_ref().ptr();
37814299Sbbruce@ucdavis.edu#ifdef PYBIND11_BUILTIN_QUALNAME
37912391Sjason@lowepower.com    heap_type->ht_qualname = name_obj.inc_ref().ptr();
38012391Sjason@lowepower.com#endif
38112391Sjason@lowepower.com
38212391Sjason@lowepower.com    auto type = &heap_type->ht_type;
38312391Sjason@lowepower.com    type->tp_name = name;
38412391Sjason@lowepower.com    type->tp_base = type_incref(&PyBaseObject_Type);
38512391Sjason@lowepower.com    type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
38612391Sjason@lowepower.com    type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
38712391Sjason@lowepower.com
38812391Sjason@lowepower.com    type->tp_new = pybind11_object_new;
38912391Sjason@lowepower.com    type->tp_init = pybind11_object_init;
39012391Sjason@lowepower.com    type->tp_dealloc = pybind11_object_dealloc;
39112391Sjason@lowepower.com
39212391Sjason@lowepower.com    /* Support weak references (needed for the keep_alive feature) */
39312391Sjason@lowepower.com    type->tp_weaklistoffset = offsetof(instance, weakrefs);
39412391Sjason@lowepower.com
39512391Sjason@lowepower.com    if (PyType_Ready(type) < 0)
39612391Sjason@lowepower.com        pybind11_fail("PyType_Ready failed in make_object_base_type():" + error_string());
39712391Sjason@lowepower.com
39812391Sjason@lowepower.com    setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
39914299Sbbruce@ucdavis.edu    PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
40012391Sjason@lowepower.com
40112391Sjason@lowepower.com    assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
40212391Sjason@lowepower.com    return (PyObject *) heap_type;
40312391Sjason@lowepower.com}
40412391Sjason@lowepower.com
40512391Sjason@lowepower.com/// dynamic_attr: Support for `d = instance.__dict__`.
40612391Sjason@lowepower.comextern "C" inline PyObject *pybind11_get_dict(PyObject *self, void *) {
40712391Sjason@lowepower.com    PyObject *&dict = *_PyObject_GetDictPtr(self);
40812391Sjason@lowepower.com    if (!dict)
40912391Sjason@lowepower.com        dict = PyDict_New();
41012391Sjason@lowepower.com    Py_XINCREF(dict);
41112391Sjason@lowepower.com    return dict;
41212391Sjason@lowepower.com}
41312391Sjason@lowepower.com
41412391Sjason@lowepower.com/// dynamic_attr: Support for `instance.__dict__ = dict()`.
41512391Sjason@lowepower.comextern "C" inline int pybind11_set_dict(PyObject *self, PyObject *new_dict, void *) {
41612391Sjason@lowepower.com    if (!PyDict_Check(new_dict)) {
41712391Sjason@lowepower.com        PyErr_Format(PyExc_TypeError, "__dict__ must be set to a dictionary, not a '%.200s'",
41812391Sjason@lowepower.com                     Py_TYPE(new_dict)->tp_name);
41912391Sjason@lowepower.com        return -1;
42012391Sjason@lowepower.com    }
42112391Sjason@lowepower.com    PyObject *&dict = *_PyObject_GetDictPtr(self);
42212391Sjason@lowepower.com    Py_INCREF(new_dict);
42312391Sjason@lowepower.com    Py_CLEAR(dict);
42412391Sjason@lowepower.com    dict = new_dict;
42512391Sjason@lowepower.com    return 0;
42612391Sjason@lowepower.com}
42712391Sjason@lowepower.com
42812391Sjason@lowepower.com/// dynamic_attr: Allow the garbage collector to traverse the internal instance `__dict__`.
42912391Sjason@lowepower.comextern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *arg) {
43012391Sjason@lowepower.com    PyObject *&dict = *_PyObject_GetDictPtr(self);
43112391Sjason@lowepower.com    Py_VISIT(dict);
43212391Sjason@lowepower.com    return 0;
43312391Sjason@lowepower.com}
43412391Sjason@lowepower.com
43512391Sjason@lowepower.com/// dynamic_attr: Allow the GC to clear the dictionary.
43612391Sjason@lowepower.comextern "C" inline int pybind11_clear(PyObject *self) {
43712391Sjason@lowepower.com    PyObject *&dict = *_PyObject_GetDictPtr(self);
43812391Sjason@lowepower.com    Py_CLEAR(dict);
43912391Sjason@lowepower.com    return 0;
44012391Sjason@lowepower.com}
44112391Sjason@lowepower.com
44212391Sjason@lowepower.com/// Give instances of this type a `__dict__` and opt into garbage collection.
44312391Sjason@lowepower.cominline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
44412391Sjason@lowepower.com    auto type = &heap_type->ht_type;
44512391Sjason@lowepower.com#if defined(PYPY_VERSION)
44612391Sjason@lowepower.com    pybind11_fail(std::string(type->tp_name) + ": dynamic attributes are "
44712391Sjason@lowepower.com                                               "currently not supported in "
44812391Sjason@lowepower.com                                               "conjunction with PyPy!");
44912391Sjason@lowepower.com#endif
45012391Sjason@lowepower.com    type->tp_flags |= Py_TPFLAGS_HAVE_GC;
45112391Sjason@lowepower.com    type->tp_dictoffset = type->tp_basicsize; // place dict at the end
45212391Sjason@lowepower.com    type->tp_basicsize += (ssize_t)sizeof(PyObject *); // and allocate enough space for it
45312391Sjason@lowepower.com    type->tp_traverse = pybind11_traverse;
45412391Sjason@lowepower.com    type->tp_clear = pybind11_clear;
45512391Sjason@lowepower.com
45612391Sjason@lowepower.com    static PyGetSetDef getset[] = {
45712391Sjason@lowepower.com        {const_cast<char*>("__dict__"), pybind11_get_dict, pybind11_set_dict, nullptr, nullptr},
45812391Sjason@lowepower.com        {nullptr, nullptr, nullptr, nullptr, nullptr}
45912391Sjason@lowepower.com    };
46012391Sjason@lowepower.com    type->tp_getset = getset;
46112391Sjason@lowepower.com}
46212391Sjason@lowepower.com
46312391Sjason@lowepower.com/// buffer_protocol: Fill in the view as specified by flags.
46412391Sjason@lowepower.comextern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int flags) {
46512391Sjason@lowepower.com    // Look for a `get_buffer` implementation in this type's info or any bases (following MRO).
46612391Sjason@lowepower.com    type_info *tinfo = nullptr;
46712391Sjason@lowepower.com    for (auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
46812391Sjason@lowepower.com        tinfo = get_type_info((PyTypeObject *) type.ptr());
46912391Sjason@lowepower.com        if (tinfo && tinfo->get_buffer)
47012391Sjason@lowepower.com            break;
47112391Sjason@lowepower.com    }
47214299Sbbruce@ucdavis.edu    if (view == nullptr || !tinfo || !tinfo->get_buffer) {
47312391Sjason@lowepower.com        if (view)
47412391Sjason@lowepower.com            view->obj = nullptr;
47512391Sjason@lowepower.com        PyErr_SetString(PyExc_BufferError, "pybind11_getbuffer(): Internal error");
47612391Sjason@lowepower.com        return -1;
47712391Sjason@lowepower.com    }
47812391Sjason@lowepower.com    std::memset(view, 0, sizeof(Py_buffer));
47912391Sjason@lowepower.com    buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data);
48012391Sjason@lowepower.com    view->obj = obj;
48112391Sjason@lowepower.com    view->ndim = 1;
48212391Sjason@lowepower.com    view->internal = info;
48312391Sjason@lowepower.com    view->buf = info->ptr;
48412391Sjason@lowepower.com    view->itemsize = info->itemsize;
48512391Sjason@lowepower.com    view->len = view->itemsize;
48612391Sjason@lowepower.com    for (auto s : info->shape)
48712391Sjason@lowepower.com        view->len *= s;
48812391Sjason@lowepower.com    if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
48912391Sjason@lowepower.com        view->format = const_cast<char *>(info->format.c_str());
49012391Sjason@lowepower.com    if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
49112391Sjason@lowepower.com        view->ndim = (int) info->ndim;
49212391Sjason@lowepower.com        view->strides = &info->strides[0];
49312391Sjason@lowepower.com        view->shape = &info->shape[0];
49412391Sjason@lowepower.com    }
49512391Sjason@lowepower.com    Py_INCREF(view->obj);
49612391Sjason@lowepower.com    return 0;
49712391Sjason@lowepower.com}
49812391Sjason@lowepower.com
49912391Sjason@lowepower.com/// buffer_protocol: Release the resources of the buffer.
50012391Sjason@lowepower.comextern "C" inline void pybind11_releasebuffer(PyObject *, Py_buffer *view) {
50112391Sjason@lowepower.com    delete (buffer_info *) view->internal;
50212391Sjason@lowepower.com}
50312391Sjason@lowepower.com
50412391Sjason@lowepower.com/// Give this type a buffer interface.
50512391Sjason@lowepower.cominline void enable_buffer_protocol(PyHeapTypeObject *heap_type) {
50612391Sjason@lowepower.com    heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer;
50712391Sjason@lowepower.com#if PY_MAJOR_VERSION < 3
50812391Sjason@lowepower.com    heap_type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
50912391Sjason@lowepower.com#endif
51012391Sjason@lowepower.com
51112391Sjason@lowepower.com    heap_type->as_buffer.bf_getbuffer = pybind11_getbuffer;
51212391Sjason@lowepower.com    heap_type->as_buffer.bf_releasebuffer = pybind11_releasebuffer;
51312391Sjason@lowepower.com}
51412391Sjason@lowepower.com
51512391Sjason@lowepower.com/** Create a brand new Python type according to the `type_record` specification.
51612391Sjason@lowepower.com    Return value: New reference. */
51712391Sjason@lowepower.cominline PyObject* make_new_python_type(const type_record &rec) {
51812391Sjason@lowepower.com    auto name = reinterpret_steal<object>(PYBIND11_FROM_STRING(rec.name));
51912391Sjason@lowepower.com
52014299Sbbruce@ucdavis.edu    auto qualname = name;
52114299Sbbruce@ucdavis.edu    if (rec.scope && !PyModule_Check(rec.scope.ptr()) && hasattr(rec.scope, "__qualname__")) {
52214299Sbbruce@ucdavis.edu#if PY_MAJOR_VERSION >= 3
52314299Sbbruce@ucdavis.edu        qualname = reinterpret_steal<object>(
52412391Sjason@lowepower.com            PyUnicode_FromFormat("%U.%U", rec.scope.attr("__qualname__").ptr(), name.ptr()));
52514299Sbbruce@ucdavis.edu#else
52614299Sbbruce@ucdavis.edu        qualname = str(rec.scope.attr("__qualname__").cast<std::string>() + "." + rec.name);
52714299Sbbruce@ucdavis.edu#endif
52812391Sjason@lowepower.com    }
52912391Sjason@lowepower.com
53012391Sjason@lowepower.com    object module;
53112391Sjason@lowepower.com    if (rec.scope) {
53212391Sjason@lowepower.com        if (hasattr(rec.scope, "__module__"))
53312391Sjason@lowepower.com            module = rec.scope.attr("__module__");
53412391Sjason@lowepower.com        else if (hasattr(rec.scope, "__name__"))
53512391Sjason@lowepower.com            module = rec.scope.attr("__name__");
53612391Sjason@lowepower.com    }
53712391Sjason@lowepower.com
53812391Sjason@lowepower.com    auto full_name = c_str(
53912391Sjason@lowepower.com#if !defined(PYPY_VERSION)
54012391Sjason@lowepower.com        module ? str(module).cast<std::string>() + "." + rec.name :
54112391Sjason@lowepower.com#endif
54212391Sjason@lowepower.com        rec.name);
54312391Sjason@lowepower.com
54412391Sjason@lowepower.com    char *tp_doc = nullptr;
54512391Sjason@lowepower.com    if (rec.doc && options::show_user_defined_docstrings()) {
54612391Sjason@lowepower.com        /* Allocate memory for docstring (using PyObject_MALLOC, since
54712391Sjason@lowepower.com           Python will free this later on) */
54812391Sjason@lowepower.com        size_t size = strlen(rec.doc) + 1;
54912391Sjason@lowepower.com        tp_doc = (char *) PyObject_MALLOC(size);
55012391Sjason@lowepower.com        memcpy((void *) tp_doc, rec.doc, size);
55112391Sjason@lowepower.com    }
55212391Sjason@lowepower.com
55312391Sjason@lowepower.com    auto &internals = get_internals();
55412391Sjason@lowepower.com    auto bases = tuple(rec.bases);
55512391Sjason@lowepower.com    auto base = (bases.size() == 0) ? internals.instance_base
55612391Sjason@lowepower.com                                    : bases[0].ptr();
55712391Sjason@lowepower.com
55812391Sjason@lowepower.com    /* Danger zone: from now (and until PyType_Ready), make sure to
55912391Sjason@lowepower.com       issue no Python C API calls which could potentially invoke the
56012391Sjason@lowepower.com       garbage collector (the GC will call type_traverse(), which will in
56112391Sjason@lowepower.com       turn find the newly constructed type in an invalid state) */
56212391Sjason@lowepower.com    auto metaclass = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr()
56312391Sjason@lowepower.com                                         : internals.default_metaclass;
56412391Sjason@lowepower.com
56512391Sjason@lowepower.com    auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
56612391Sjason@lowepower.com    if (!heap_type)
56712391Sjason@lowepower.com        pybind11_fail(std::string(rec.name) + ": Unable to create type object!");
56812391Sjason@lowepower.com
56912391Sjason@lowepower.com    heap_type->ht_name = name.release().ptr();
57014299Sbbruce@ucdavis.edu#ifdef PYBIND11_BUILTIN_QUALNAME
57114299Sbbruce@ucdavis.edu    heap_type->ht_qualname = qualname.inc_ref().ptr();
57212391Sjason@lowepower.com#endif
57312391Sjason@lowepower.com
57412391Sjason@lowepower.com    auto type = &heap_type->ht_type;
57512391Sjason@lowepower.com    type->tp_name = full_name;
57612391Sjason@lowepower.com    type->tp_doc = tp_doc;
57712391Sjason@lowepower.com    type->tp_base = type_incref((PyTypeObject *)base);
57812391Sjason@lowepower.com    type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
57912391Sjason@lowepower.com    if (bases.size() > 0)
58012391Sjason@lowepower.com        type->tp_bases = bases.release().ptr();
58112391Sjason@lowepower.com
58212391Sjason@lowepower.com    /* Don't inherit base __init__ */
58312391Sjason@lowepower.com    type->tp_init = pybind11_object_init;
58412391Sjason@lowepower.com
58512391Sjason@lowepower.com    /* Supported protocols */
58612391Sjason@lowepower.com    type->tp_as_number = &heap_type->as_number;
58712391Sjason@lowepower.com    type->tp_as_sequence = &heap_type->as_sequence;
58812391Sjason@lowepower.com    type->tp_as_mapping = &heap_type->as_mapping;
58914299Sbbruce@ucdavis.edu#if PY_VERSION_HEX >= 0x03050000
59014299Sbbruce@ucdavis.edu    type->tp_as_async = &heap_type->as_async;
59114299Sbbruce@ucdavis.edu#endif
59212391Sjason@lowepower.com
59312391Sjason@lowepower.com    /* Flags */
59412391Sjason@lowepower.com    type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
59512391Sjason@lowepower.com#if PY_MAJOR_VERSION < 3
59612391Sjason@lowepower.com    type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
59712391Sjason@lowepower.com#endif
59812391Sjason@lowepower.com
59912391Sjason@lowepower.com    if (rec.dynamic_attr)
60012391Sjason@lowepower.com        enable_dynamic_attributes(heap_type);
60112391Sjason@lowepower.com
60212391Sjason@lowepower.com    if (rec.buffer_protocol)
60312391Sjason@lowepower.com        enable_buffer_protocol(heap_type);
60412391Sjason@lowepower.com
60512391Sjason@lowepower.com    if (PyType_Ready(type) < 0)
60612391Sjason@lowepower.com        pybind11_fail(std::string(rec.name) + ": PyType_Ready failed (" + error_string() + ")!");
60712391Sjason@lowepower.com
60812391Sjason@lowepower.com    assert(rec.dynamic_attr ? PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC)
60912391Sjason@lowepower.com                            : !PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
61012391Sjason@lowepower.com
61112391Sjason@lowepower.com    /* Register type with the parent scope */
61212391Sjason@lowepower.com    if (rec.scope)
61312391Sjason@lowepower.com        setattr(rec.scope, rec.name, (PyObject *) type);
61412391Sjason@lowepower.com    else
61512391Sjason@lowepower.com        Py_INCREF(type); // Keep it alive forever (reference leak)
61612391Sjason@lowepower.com
61712391Sjason@lowepower.com    if (module) // Needed by pydoc
61812391Sjason@lowepower.com        setattr((PyObject *) type, "__module__", module);
61912391Sjason@lowepower.com
62014299Sbbruce@ucdavis.edu    PYBIND11_SET_OLDPY_QUALNAME(type, qualname);
62114299Sbbruce@ucdavis.edu
62212391Sjason@lowepower.com    return (PyObject *) type;
62312391Sjason@lowepower.com}
62412391Sjason@lowepower.com
62512391Sjason@lowepower.comNAMESPACE_END(detail)
62612391Sjason@lowepower.comNAMESPACE_END(PYBIND11_NAMESPACE)
627