|
| 1 | +/* |
| 2 | + pybind11/class_support.h: Python C API implementation details for py::class_ |
| 3 | +
|
| 4 | + Copyright (c) 2017 Wenzel Jakob <[email protected]> |
| 5 | +
|
| 6 | + All rights reserved. Use of this source code is governed by a |
| 7 | + BSD-style license that can be found in the LICENSE file. |
| 8 | +*/ |
| 9 | + |
| 10 | +#pragma once |
| 11 | + |
| 12 | +#include "attr.h" |
| 13 | + |
| 14 | +NAMESPACE_BEGIN(pybind11) |
| 15 | +NAMESPACE_BEGIN(detail) |
| 16 | + |
| 17 | +#if !defined(PYPY_VERSION) |
| 18 | + |
| 19 | +/// `pybind11_static_property.__get__()`: Always pass the class instead of the instance. |
| 20 | +extern "C" inline PyObject *pybind11_static_get(PyObject *self, PyObject * /*ob*/, PyObject *cls) { |
| 21 | + return PyProperty_Type.tp_descr_get(self, cls, cls); |
| 22 | +} |
| 23 | + |
| 24 | +/// `pybind11_static_property.__set__()`: Just like the above `__get__()`. |
| 25 | +extern "C" inline int pybind11_static_set(PyObject *self, PyObject *obj, PyObject *value) { |
| 26 | + PyObject *cls = PyType_Check(obj) ? obj : (PyObject *) Py_TYPE(obj); |
| 27 | + return PyProperty_Type.tp_descr_set(self, cls, value); |
| 28 | +} |
| 29 | + |
| 30 | +/** A `static_property` is the same as a `property` but the `__get__()` and `__set__()` |
| 31 | + methods are modified to always use the object type instead of a concrete instance. |
| 32 | + Return value: New reference. */ |
| 33 | +inline PyTypeObject *make_static_property_type() { |
| 34 | + constexpr auto *name = "pybind11_static_property"; |
| 35 | + auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name)); |
| 36 | + |
| 37 | + /* Danger zone: from now (and until PyType_Ready), make sure to |
| 38 | + issue no Python C API calls which could potentially invoke the |
| 39 | + garbage collector (the GC will call type_traverse(), which will in |
| 40 | + turn find the newly constructed type in an invalid state) */ |
| 41 | + auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0); |
| 42 | + if (!heap_type) |
| 43 | + pybind11_fail("make_static_property_type(): error allocating type!"); |
| 44 | + |
| 45 | + heap_type->ht_name = name_obj.inc_ref().ptr(); |
| 46 | +#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 |
| 47 | + heap_type->ht_qualname = name_obj.inc_ref().ptr(); |
| 48 | +#endif |
| 49 | + |
| 50 | + auto type = &heap_type->ht_type; |
| 51 | + type->tp_name = name; |
| 52 | + type->tp_base = &PyProperty_Type; |
| 53 | + type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; |
| 54 | + type->tp_descr_get = pybind11_static_get; |
| 55 | + type->tp_descr_set = pybind11_static_set; |
| 56 | + |
| 57 | + if (PyType_Ready(type) < 0) |
| 58 | + pybind11_fail("make_static_property_type(): failure in PyType_Ready()!"); |
| 59 | + |
| 60 | + return type; |
| 61 | +} |
| 62 | + |
| 63 | +#else // PYPY |
| 64 | + |
| 65 | +/** PyPy has some issues with the above C API, so we evaluate Python code instead. |
| 66 | + This function will only be called once so performance isn't really a concern. |
| 67 | + Return value: New reference. */ |
| 68 | +inline PyTypeObject *make_static_property_type() { |
| 69 | + auto d = dict(); |
| 70 | + PyObject *result = PyRun_String(R"(\ |
| 71 | + class pybind11_static_property(property): |
| 72 | + def __get__(self, obj, cls): |
| 73 | + return property.__get__(self, cls, cls) |
| 74 | +
|
| 75 | + def __set__(self, obj, value): |
| 76 | + cls = obj if isinstance(obj, type) else type(obj) |
| 77 | + property.__set__(self, cls, value) |
| 78 | + )", Py_file_input, d.ptr(), d.ptr() |
| 79 | + ); |
| 80 | + if (result == nullptr) |
| 81 | + throw error_already_set(); |
| 82 | + Py_DECREF(result); |
| 83 | + return (PyTypeObject *) d["pybind11_static_property"].cast<object>().release().ptr(); |
| 84 | +} |
| 85 | + |
| 86 | +#endif // PYPY |
| 87 | + |
| 88 | +/** Types with static properties need to handle `Type.static_prop = x` in a specific way. |
| 89 | + By default, Python replaces the `static_property` itself, but for wrapped C++ types |
| 90 | + we need to call `static_property.__set__()` in order to propagate the new value to |
| 91 | + the underlying C++ data structure. */ |
| 92 | +extern "C" inline int pybind11_meta_setattro(PyObject* obj, PyObject* name, PyObject* value) { |
| 93 | + // Use `_PyType_Lookup()` instead of `PyObject_GetAttr()` in order to get the raw |
| 94 | + // descriptor (`property`) instead of calling `tp_descr_get` (`property.__get__()`). |
| 95 | + PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name); |
| 96 | + |
| 97 | + // Call `static_property.__set__()` instead of replacing the `static_property`. |
| 98 | + if (descr && PyObject_IsInstance(descr, (PyObject *) get_internals().static_property_type)) { |
| 99 | +#if !defined(PYPY_VERSION) |
| 100 | + return Py_TYPE(descr)->tp_descr_set(descr, obj, value); |
| 101 | +#else |
| 102 | + if (PyObject *result = PyObject_CallMethod(descr, "__set__", "OO", obj, value)) { |
| 103 | + Py_DECREF(result); |
| 104 | + return 0; |
| 105 | + } else { |
| 106 | + return -1; |
| 107 | + } |
| 108 | +#endif |
| 109 | + } else { |
| 110 | + return PyType_Type.tp_setattro(obj, name, value); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/** This metaclass is assigned by default to all pybind11 types and is required in order |
| 115 | + for static properties to function correctly. Users may override this using `py::metaclass`. |
| 116 | + Return value: New reference. */ |
| 117 | +inline PyTypeObject* make_default_metaclass() { |
| 118 | + constexpr auto *name = "pybind11_type"; |
| 119 | + auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name)); |
| 120 | + |
| 121 | + /* Danger zone: from now (and until PyType_Ready), make sure to |
| 122 | + issue no Python C API calls which could potentially invoke the |
| 123 | + garbage collector (the GC will call type_traverse(), which will in |
| 124 | + turn find the newly constructed type in an invalid state) */ |
| 125 | + auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0); |
| 126 | + if (!heap_type) |
| 127 | + pybind11_fail("make_default_metaclass(): error allocating metaclass!"); |
| 128 | + |
| 129 | + heap_type->ht_name = name_obj.inc_ref().ptr(); |
| 130 | +#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 |
| 131 | + heap_type->ht_qualname = name_obj.inc_ref().ptr(); |
| 132 | +#endif |
| 133 | + |
| 134 | + auto type = &heap_type->ht_type; |
| 135 | + type->tp_name = name; |
| 136 | + type->tp_base = &PyType_Type; |
| 137 | + type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; |
| 138 | + type->tp_setattro = pybind11_meta_setattro; |
| 139 | + |
| 140 | + if (PyType_Ready(type) < 0) |
| 141 | + pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!"); |
| 142 | + |
| 143 | + return type; |
| 144 | +} |
| 145 | + |
| 146 | +NAMESPACE_END(detail) |
| 147 | +NAMESPACE_END(pybind11) |
0 commit comments