Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 1 | /* |
Dean Moldovan | f580649 | 2017-08-14 00:35:53 +0200 | [diff] [blame] | 2 | pybind11/detail/class.h: Python C API implementation details for py::class_ |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 3 | |
| 4 | Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch> |
| 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 | |
Dean Moldovan | f580649 | 2017-08-14 00:35:53 +0200 | [diff] [blame] | 12 | #include "../attr.h" |
Steven Johnson | 4ddf7c4 | 2019-06-10 12:54:56 -0700 | [diff] [blame] | 13 | #include "../options.h" |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 14 | |
Jason Rhinelander | a859dd6 | 2017-08-10 12:03:29 -0400 | [diff] [blame] | 15 | NAMESPACE_BEGIN(PYBIND11_NAMESPACE) |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 16 | NAMESPACE_BEGIN(detail) |
| 17 | |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 18 | #if PY_VERSION_HEX >= 0x03030000 |
| 19 | # define PYBIND11_BUILTIN_QUALNAME |
| 20 | # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) |
| 21 | #else |
| 22 | // In pre-3.3 Python, we still set __qualname__ so that we can produce reliable function type |
| 23 | // signatures; in 3.3+ this macro expands to nothing: |
| 24 | # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) setattr((PyObject *) obj, "__qualname__", nameobj) |
| 25 | #endif |
| 26 | |
bennorth | cb3d406 | 2017-07-20 14:21:31 +0100 | [diff] [blame] | 27 | inline PyTypeObject *type_incref(PyTypeObject *type) { |
| 28 | Py_INCREF(type); |
| 29 | return type; |
| 30 | } |
| 31 | |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 32 | #if !defined(PYPY_VERSION) |
| 33 | |
| 34 | /// `pybind11_static_property.__get__()`: Always pass the class instead of the instance. |
| 35 | extern "C" inline PyObject *pybind11_static_get(PyObject *self, PyObject * /*ob*/, PyObject *cls) { |
| 36 | return PyProperty_Type.tp_descr_get(self, cls, cls); |
| 37 | } |
| 38 | |
| 39 | /// `pybind11_static_property.__set__()`: Just like the above `__get__()`. |
| 40 | extern "C" inline int pybind11_static_set(PyObject *self, PyObject *obj, PyObject *value) { |
| 41 | PyObject *cls = PyType_Check(obj) ? obj : (PyObject *) Py_TYPE(obj); |
| 42 | return PyProperty_Type.tp_descr_set(self, cls, value); |
| 43 | } |
| 44 | |
| 45 | /** A `static_property` is the same as a `property` but the `__get__()` and `__set__()` |
| 46 | methods are modified to always use the object type instead of a concrete instance. |
| 47 | Return value: New reference. */ |
| 48 | inline PyTypeObject *make_static_property_type() { |
| 49 | constexpr auto *name = "pybind11_static_property"; |
| 50 | auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name)); |
| 51 | |
| 52 | /* Danger zone: from now (and until PyType_Ready), make sure to |
| 53 | issue no Python C API calls which could potentially invoke the |
| 54 | garbage collector (the GC will call type_traverse(), which will in |
| 55 | turn find the newly constructed type in an invalid state) */ |
| 56 | auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0); |
| 57 | if (!heap_type) |
| 58 | pybind11_fail("make_static_property_type(): error allocating type!"); |
| 59 | |
| 60 | heap_type->ht_name = name_obj.inc_ref().ptr(); |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 61 | #ifdef PYBIND11_BUILTIN_QUALNAME |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 62 | heap_type->ht_qualname = name_obj.inc_ref().ptr(); |
| 63 | #endif |
| 64 | |
| 65 | auto type = &heap_type->ht_type; |
| 66 | type->tp_name = name; |
bennorth | cb3d406 | 2017-07-20 14:21:31 +0100 | [diff] [blame] | 67 | type->tp_base = type_incref(&PyProperty_Type); |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 68 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; |
| 69 | type->tp_descr_get = pybind11_static_get; |
| 70 | type->tp_descr_set = pybind11_static_set; |
| 71 | |
| 72 | if (PyType_Ready(type) < 0) |
| 73 | pybind11_fail("make_static_property_type(): failure in PyType_Ready()!"); |
| 74 | |
Dean Moldovan | 1769ea4 | 2017-03-15 15:38:14 +0100 | [diff] [blame] | 75 | setattr((PyObject *) type, "__module__", str("pybind11_builtins")); |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 76 | PYBIND11_SET_OLDPY_QUALNAME(type, name_obj); |
Dean Moldovan | 1769ea4 | 2017-03-15 15:38:14 +0100 | [diff] [blame] | 77 | |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 78 | return type; |
| 79 | } |
| 80 | |
| 81 | #else // PYPY |
| 82 | |
| 83 | /** PyPy has some issues with the above C API, so we evaluate Python code instead. |
| 84 | This function will only be called once so performance isn't really a concern. |
| 85 | Return value: New reference. */ |
| 86 | inline PyTypeObject *make_static_property_type() { |
| 87 | auto d = dict(); |
| 88 | PyObject *result = PyRun_String(R"(\ |
| 89 | class pybind11_static_property(property): |
| 90 | def __get__(self, obj, cls): |
| 91 | return property.__get__(self, cls, cls) |
| 92 | |
| 93 | def __set__(self, obj, value): |
| 94 | cls = obj if isinstance(obj, type) else type(obj) |
| 95 | property.__set__(self, cls, value) |
| 96 | )", Py_file_input, d.ptr(), d.ptr() |
| 97 | ); |
| 98 | if (result == nullptr) |
| 99 | throw error_already_set(); |
| 100 | Py_DECREF(result); |
| 101 | return (PyTypeObject *) d["pybind11_static_property"].cast<object>().release().ptr(); |
| 102 | } |
| 103 | |
| 104 | #endif // PYPY |
| 105 | |
| 106 | /** Types with static properties need to handle `Type.static_prop = x` in a specific way. |
| 107 | By default, Python replaces the `static_property` itself, but for wrapped C++ types |
| 108 | we need to call `static_property.__set__()` in order to propagate the new value to |
| 109 | the underlying C++ data structure. */ |
| 110 | extern "C" inline int pybind11_meta_setattro(PyObject* obj, PyObject* name, PyObject* value) { |
| 111 | // Use `_PyType_Lookup()` instead of `PyObject_GetAttr()` in order to get the raw |
| 112 | // descriptor (`property`) instead of calling `tp_descr_get` (`property.__get__()`). |
| 113 | PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name); |
| 114 | |
Dean Moldovan | e0e2ea3 | 2017-04-06 23:45:12 +0200 | [diff] [blame] | 115 | // The following assignment combinations are possible: |
| 116 | // 1. `Type.static_prop = value` --> descr_set: `Type.static_prop.__set__(value)` |
| 117 | // 2. `Type.static_prop = other_static_prop` --> setattro: replace existing `static_prop` |
| 118 | // 3. `Type.regular_attribute = value` --> setattro: regular attribute assignment |
| 119 | const auto static_prop = (PyObject *) get_internals().static_property_type; |
| 120 | const auto call_descr_set = descr && PyObject_IsInstance(descr, static_prop) |
| 121 | && !PyObject_IsInstance(value, static_prop); |
| 122 | if (call_descr_set) { |
| 123 | // Call `static_property.__set__()` instead of replacing the `static_property`. |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 124 | #if !defined(PYPY_VERSION) |
| 125 | return Py_TYPE(descr)->tp_descr_set(descr, obj, value); |
| 126 | #else |
| 127 | if (PyObject *result = PyObject_CallMethod(descr, "__set__", "OO", obj, value)) { |
| 128 | Py_DECREF(result); |
| 129 | return 0; |
| 130 | } else { |
| 131 | return -1; |
| 132 | } |
| 133 | #endif |
| 134 | } else { |
Dean Moldovan | e0e2ea3 | 2017-04-06 23:45:12 +0200 | [diff] [blame] | 135 | // Replace existing attribute. |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 136 | return PyType_Type.tp_setattro(obj, name, value); |
| 137 | } |
| 138 | } |
| 139 | |
Jason Rhinelander | 0a90b2d | 2017-04-16 20:30:52 -0400 | [diff] [blame] | 140 | #if PY_MAJOR_VERSION >= 3 |
Jason Rhinelander | 37b2383 | 2017-05-22 12:06:16 -0400 | [diff] [blame] | 141 | /** |
| 142 | * Python 3's PyInstanceMethod_Type hides itself via its tp_descr_get, which prevents aliasing |
Jason Rhinelander | 0a90b2d | 2017-04-16 20:30:52 -0400 | [diff] [blame] | 143 | * methods via cls.attr("m2") = cls.attr("m1"): instead the tp_descr_get returns a plain function, |
| 144 | * when called on a class, or a PyMethod, when called on an instance. Override that behaviour here |
| 145 | * to do a special case bypass for PyInstanceMethod_Types. |
| 146 | */ |
| 147 | extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name) { |
| 148 | PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name); |
| 149 | if (descr && PyInstanceMethod_Check(descr)) { |
| 150 | Py_INCREF(descr); |
| 151 | return descr; |
| 152 | } |
| 153 | else { |
| 154 | return PyType_Type.tp_getattro(obj, name); |
| 155 | } |
| 156 | } |
| 157 | #endif |
| 158 | |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 159 | /** This metaclass is assigned by default to all pybind11 types and is required in order |
| 160 | for static properties to function correctly. Users may override this using `py::metaclass`. |
| 161 | Return value: New reference. */ |
| 162 | inline PyTypeObject* make_default_metaclass() { |
| 163 | constexpr auto *name = "pybind11_type"; |
| 164 | auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name)); |
| 165 | |
| 166 | /* Danger zone: from now (and until PyType_Ready), make sure to |
| 167 | issue no Python C API calls which could potentially invoke the |
| 168 | garbage collector (the GC will call type_traverse(), which will in |
| 169 | turn find the newly constructed type in an invalid state) */ |
| 170 | auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0); |
| 171 | if (!heap_type) |
| 172 | pybind11_fail("make_default_metaclass(): error allocating metaclass!"); |
| 173 | |
| 174 | heap_type->ht_name = name_obj.inc_ref().ptr(); |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 175 | #ifdef PYBIND11_BUILTIN_QUALNAME |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 176 | heap_type->ht_qualname = name_obj.inc_ref().ptr(); |
| 177 | #endif |
| 178 | |
| 179 | auto type = &heap_type->ht_type; |
| 180 | type->tp_name = name; |
bennorth | cb3d406 | 2017-07-20 14:21:31 +0100 | [diff] [blame] | 181 | type->tp_base = type_incref(&PyType_Type); |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 182 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 183 | |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 184 | type->tp_setattro = pybind11_meta_setattro; |
Jason Rhinelander | 0a90b2d | 2017-04-16 20:30:52 -0400 | [diff] [blame] | 185 | #if PY_MAJOR_VERSION >= 3 |
| 186 | type->tp_getattro = pybind11_meta_getattro; |
| 187 | #endif |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 188 | |
| 189 | if (PyType_Ready(type) < 0) |
| 190 | pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!"); |
| 191 | |
Dean Moldovan | 1769ea4 | 2017-03-15 15:38:14 +0100 | [diff] [blame] | 192 | setattr((PyObject *) type, "__module__", str("pybind11_builtins")); |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 193 | PYBIND11_SET_OLDPY_QUALNAME(type, name_obj); |
Dean Moldovan | 1769ea4 | 2017-03-15 15:38:14 +0100 | [diff] [blame] | 194 | |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 195 | return type; |
| 196 | } |
| 197 | |
Jason Rhinelander | 1f8a100 | 2017-04-21 19:01:30 -0400 | [diff] [blame] | 198 | /// For multiple inheritance types we need to recursively register/deregister base pointers for any |
| 199 | /// base classes with pointers that are difference from the instance value pointer so that we can |
| 200 | /// correctly recognize an offset base class pointer. This calls a function with any offset base ptrs. |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 201 | inline void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self, |
| 202 | bool (*f)(void * /*parentptr*/, instance * /*self*/)) { |
Jason Rhinelander | 1f8a100 | 2017-04-21 19:01:30 -0400 | [diff] [blame] | 203 | for (handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) { |
| 204 | if (auto parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) { |
| 205 | for (auto &c : parent_tinfo->implicit_casts) { |
| 206 | if (c.first == tinfo->cpptype) { |
| 207 | auto *parentptr = c.second(valueptr); |
| 208 | if (parentptr != valueptr) |
| 209 | f(parentptr, self); |
| 210 | traverse_offset_bases(parentptr, parent_tinfo, self, f); |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
Jason Rhinelander | 9290099 | 2017-04-20 15:09:20 -0400 | [diff] [blame] | 216 | } |
| 217 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 218 | inline bool register_instance_impl(void *ptr, instance *self) { |
Jason Rhinelander | 1f8a100 | 2017-04-21 19:01:30 -0400 | [diff] [blame] | 219 | get_internals().registered_instances.emplace(ptr, self); |
| 220 | return true; // unused, but gives the same signature as the deregister func |
| 221 | } |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 222 | inline bool deregister_instance_impl(void *ptr, instance *self) { |
Jason Rhinelander | 9290099 | 2017-04-20 15:09:20 -0400 | [diff] [blame] | 223 | auto ®istered_instances = get_internals().registered_instances; |
Jason Rhinelander | 1f8a100 | 2017-04-21 19:01:30 -0400 | [diff] [blame] | 224 | auto range = registered_instances.equal_range(ptr); |
Jason Rhinelander | 9290099 | 2017-04-20 15:09:20 -0400 | [diff] [blame] | 225 | for (auto it = range.first; it != range.second; ++it) { |
Jason Rhinelander | 1f8a100 | 2017-04-21 19:01:30 -0400 | [diff] [blame] | 226 | if (Py_TYPE(self) == Py_TYPE(it->second)) { |
Jason Rhinelander | 9290099 | 2017-04-20 15:09:20 -0400 | [diff] [blame] | 227 | registered_instances.erase(it); |
| 228 | return true; |
| 229 | } |
| 230 | } |
| 231 | return false; |
| 232 | } |
| 233 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 234 | inline void register_instance(instance *self, void *valptr, const type_info *tinfo) { |
| 235 | register_instance_impl(valptr, self); |
Jason Rhinelander | 1f8a100 | 2017-04-21 19:01:30 -0400 | [diff] [blame] | 236 | if (!tinfo->simple_ancestors) |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 237 | traverse_offset_bases(valptr, tinfo, self, register_instance_impl); |
Jason Rhinelander | 1f8a100 | 2017-04-21 19:01:30 -0400 | [diff] [blame] | 238 | } |
| 239 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 240 | inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo) { |
| 241 | bool ret = deregister_instance_impl(valptr, self); |
Jason Rhinelander | 1f8a100 | 2017-04-21 19:01:30 -0400 | [diff] [blame] | 242 | if (!tinfo->simple_ancestors) |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 243 | traverse_offset_bases(valptr, tinfo, self, deregister_instance_impl); |
Jason Rhinelander | 1f8a100 | 2017-04-21 19:01:30 -0400 | [diff] [blame] | 244 | return ret; |
| 245 | } |
| 246 | |
Jason Rhinelander | fd81a03 | 2017-07-31 23:32:34 -0400 | [diff] [blame] | 247 | /// Instance creation function for all pybind11 types. It allocates the internal instance layout for |
| 248 | /// holding C++ objects and holders. Allocation is done lazily (the first time the instance is cast |
| 249 | /// to a reference or pointer), and initialization is done by an `__init__` function. |
| 250 | inline PyObject *make_new_instance(PyTypeObject *type) { |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 251 | #if defined(PYPY_VERSION) |
| 252 | // PyPy gets tp_basicsize wrong (issue 2482) under multiple inheritance when the first inherited |
| 253 | // object is a a plain Python type (i.e. not derived from an extension type). Fix it. |
| 254 | ssize_t instance_size = static_cast<ssize_t>(sizeof(instance)); |
| 255 | if (type->tp_basicsize < instance_size) { |
| 256 | type->tp_basicsize = instance_size; |
Jason Rhinelander | 9290099 | 2017-04-20 15:09:20 -0400 | [diff] [blame] | 257 | } |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 258 | #endif |
| 259 | PyObject *self = type->tp_alloc(type, 0); |
| 260 | auto inst = reinterpret_cast<instance *>(self); |
| 261 | // Allocate the value/holder internals: |
| 262 | inst->allocate_layout(); |
| 263 | |
| 264 | inst->owned = true; |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 265 | |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 266 | return self; |
| 267 | } |
| 268 | |
Jason Rhinelander | 9290099 | 2017-04-20 15:09:20 -0400 | [diff] [blame] | 269 | /// Instance creation function for all pybind11 types. It only allocates space for the |
| 270 | /// C++ object, but doesn't call the constructor -- an `__init__` function must do that. |
| 271 | extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *) { |
| 272 | return make_new_instance(type); |
| 273 | } |
| 274 | |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 275 | /// An `__init__` function constructs the C++ object. Users should provide at least one |
| 276 | /// of these using `py::init` or directly with `.def(__init__, ...)`. Otherwise, the |
| 277 | /// following default function will be used which simply throws an exception. |
| 278 | extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject *) { |
| 279 | PyTypeObject *type = Py_TYPE(self); |
| 280 | std::string msg; |
| 281 | #if defined(PYPY_VERSION) |
| 282 | msg += handle((PyObject *) type).attr("__module__").cast<std::string>() + "."; |
| 283 | #endif |
| 284 | msg += type->tp_name; |
| 285 | msg += ": No constructor defined!"; |
| 286 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 287 | return -1; |
| 288 | } |
| 289 | |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 290 | inline void add_patient(PyObject *nurse, PyObject *patient) { |
| 291 | auto &internals = get_internals(); |
| 292 | auto instance = reinterpret_cast<detail::instance *>(nurse); |
| 293 | instance->has_patients = true; |
| 294 | Py_INCREF(patient); |
| 295 | internals.patients[nurse].push_back(patient); |
| 296 | } |
| 297 | |
| 298 | inline void clear_patients(PyObject *self) { |
| 299 | auto instance = reinterpret_cast<detail::instance *>(self); |
| 300 | auto &internals = get_internals(); |
| 301 | auto pos = internals.patients.find(self); |
| 302 | assert(pos != internals.patients.end()); |
| 303 | // Clearing the patients can cause more Python code to run, which |
| 304 | // can invalidate the iterator. Extract the vector of patients |
| 305 | // from the unordered_map first. |
| 306 | auto patients = std::move(pos->second); |
| 307 | internals.patients.erase(pos); |
| 308 | instance->has_patients = false; |
| 309 | for (PyObject *&patient : patients) |
| 310 | Py_CLEAR(patient); |
| 311 | } |
| 312 | |
Jason Rhinelander | 9290099 | 2017-04-20 15:09:20 -0400 | [diff] [blame] | 313 | /// Clears all internal data from the instance and removes it from registered instances in |
| 314 | /// preparation for deallocation. |
| 315 | inline void clear_instance(PyObject *self) { |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 316 | auto instance = reinterpret_cast<detail::instance *>(self); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 317 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 318 | // Deallocate any values/holders, if present: |
| 319 | for (auto &v_h : values_and_holders(instance)) { |
| 320 | if (v_h) { |
Jason Rhinelander | cca20a7 | 2017-07-29 03:56:01 -0400 | [diff] [blame] | 321 | |
| 322 | // We have to deregister before we call dealloc because, for virtual MI types, we still |
| 323 | // need to be able to get the parent pointers. |
| 324 | if (v_h.instance_registered() && !deregister_instance(instance, v_h.value_ptr(), v_h.type)) |
| 325 | pybind11_fail("pybind11_object_dealloc(): Tried to deallocate unregistered instance!"); |
Jason Rhinelander | 353615f | 2017-07-25 00:53:23 -0400 | [diff] [blame] | 326 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 327 | if (instance->owned || v_h.holder_constructed()) |
| 328 | v_h.type->dealloc(v_h); |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 329 | } |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 330 | } |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 331 | // Deallocate the value/holder layout internals: |
| 332 | instance->deallocate_layout(); |
| 333 | |
| 334 | if (instance->weakrefs) |
| 335 | PyObject_ClearWeakRefs(self); |
| 336 | |
| 337 | PyObject **dict_ptr = _PyObject_GetDictPtr(self); |
| 338 | if (dict_ptr) |
| 339 | Py_CLEAR(*dict_ptr); |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 340 | |
| 341 | if (instance->has_patients) |
| 342 | clear_patients(self); |
Jason Rhinelander | 9290099 | 2017-04-20 15:09:20 -0400 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /// Instance destructor function for all pybind11 types. It calls `type_info.dealloc` |
| 346 | /// to destroy the C++ object itself, while the rest is Python bookkeeping. |
| 347 | extern "C" inline void pybind11_object_dealloc(PyObject *self) { |
| 348 | clear_instance(self); |
Wenzel Jakob | c14c276 | 2017-08-25 16:02:18 +0200 | [diff] [blame] | 349 | |
| 350 | auto type = Py_TYPE(self); |
| 351 | type->tp_free(self); |
| 352 | |
| 353 | // `type->tp_dealloc != pybind11_object_dealloc` means that we're being called |
| 354 | // as part of a derived type's dealloc, in which case we're not allowed to decref |
| 355 | // the type here. For cross-module compatibility, we shouldn't compare directly |
| 356 | // with `pybind11_object_dealloc`, but with the common one stashed in internals. |
| 357 | auto pybind11_object_type = (PyTypeObject *) get_internals().instance_base; |
| 358 | if (type->tp_dealloc == pybind11_object_type->tp_dealloc) |
| 359 | Py_DECREF(type); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 362 | /** Create the type which can be used as a common base for all classes. This is |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 363 | needed in order to satisfy Python's requirements for multiple inheritance. |
| 364 | Return value: New reference. */ |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 365 | inline PyObject *make_object_base_type(PyTypeObject *metaclass) { |
| 366 | constexpr auto *name = "pybind11_object"; |
| 367 | auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name)); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 368 | |
| 369 | /* Danger zone: from now (and until PyType_Ready), make sure to |
| 370 | issue no Python C API calls which could potentially invoke the |
| 371 | garbage collector (the GC will call type_traverse(), which will in |
| 372 | turn find the newly constructed type in an invalid state) */ |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 373 | auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 374 | if (!heap_type) |
| 375 | pybind11_fail("make_object_base_type(): error allocating type!"); |
| 376 | |
| 377 | heap_type->ht_name = name_obj.inc_ref().ptr(); |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 378 | #ifdef PYBIND11_BUILTIN_QUALNAME |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 379 | heap_type->ht_qualname = name_obj.inc_ref().ptr(); |
| 380 | #endif |
| 381 | |
| 382 | auto type = &heap_type->ht_type; |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 383 | type->tp_name = name; |
bennorth | cb3d406 | 2017-07-20 14:21:31 +0100 | [diff] [blame] | 384 | type->tp_base = type_incref(&PyBaseObject_Type); |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 385 | type->tp_basicsize = static_cast<ssize_t>(sizeof(instance)); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 386 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; |
| 387 | |
| 388 | type->tp_new = pybind11_object_new; |
| 389 | type->tp_init = pybind11_object_init; |
| 390 | type->tp_dealloc = pybind11_object_dealloc; |
| 391 | |
| 392 | /* Support weak references (needed for the keep_alive feature) */ |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 393 | type->tp_weaklistoffset = offsetof(instance, weakrefs); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 394 | |
| 395 | if (PyType_Ready(type) < 0) |
| 396 | pybind11_fail("PyType_Ready failed in make_object_base_type():" + error_string()); |
| 397 | |
Dean Moldovan | 1769ea4 | 2017-03-15 15:38:14 +0100 | [diff] [blame] | 398 | setattr((PyObject *) type, "__module__", str("pybind11_builtins")); |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 399 | PYBIND11_SET_OLDPY_QUALNAME(type, name_obj); |
Dean Moldovan | 1769ea4 | 2017-03-15 15:38:14 +0100 | [diff] [blame] | 400 | |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 401 | assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC)); |
| 402 | return (PyObject *) heap_type; |
| 403 | } |
| 404 | |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 405 | /// dynamic_attr: Support for `d = instance.__dict__`. |
| 406 | extern "C" inline PyObject *pybind11_get_dict(PyObject *self, void *) { |
| 407 | PyObject *&dict = *_PyObject_GetDictPtr(self); |
| 408 | if (!dict) |
| 409 | dict = PyDict_New(); |
| 410 | Py_XINCREF(dict); |
| 411 | return dict; |
| 412 | } |
| 413 | |
| 414 | /// dynamic_attr: Support for `instance.__dict__ = dict()`. |
| 415 | extern "C" inline int pybind11_set_dict(PyObject *self, PyObject *new_dict, void *) { |
| 416 | if (!PyDict_Check(new_dict)) { |
| 417 | PyErr_Format(PyExc_TypeError, "__dict__ must be set to a dictionary, not a '%.200s'", |
| 418 | Py_TYPE(new_dict)->tp_name); |
| 419 | return -1; |
| 420 | } |
| 421 | PyObject *&dict = *_PyObject_GetDictPtr(self); |
| 422 | Py_INCREF(new_dict); |
| 423 | Py_CLEAR(dict); |
| 424 | dict = new_dict; |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | /// dynamic_attr: Allow the garbage collector to traverse the internal instance `__dict__`. |
| 429 | extern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *arg) { |
| 430 | PyObject *&dict = *_PyObject_GetDictPtr(self); |
| 431 | Py_VISIT(dict); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | /// dynamic_attr: Allow the GC to clear the dictionary. |
| 436 | extern "C" inline int pybind11_clear(PyObject *self) { |
| 437 | PyObject *&dict = *_PyObject_GetDictPtr(self); |
| 438 | Py_CLEAR(dict); |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | /// Give instances of this type a `__dict__` and opt into garbage collection. |
| 443 | inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) { |
| 444 | auto type = &heap_type->ht_type; |
| 445 | #if defined(PYPY_VERSION) |
| 446 | pybind11_fail(std::string(type->tp_name) + ": dynamic attributes are " |
| 447 | "currently not supported in " |
| 448 | "conjunction with PyPy!"); |
| 449 | #endif |
| 450 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 451 | type->tp_dictoffset = type->tp_basicsize; // place dict at the end |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 452 | type->tp_basicsize += (ssize_t)sizeof(PyObject *); // and allocate enough space for it |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 453 | type->tp_traverse = pybind11_traverse; |
| 454 | type->tp_clear = pybind11_clear; |
| 455 | |
| 456 | static PyGetSetDef getset[] = { |
| 457 | {const_cast<char*>("__dict__"), pybind11_get_dict, pybind11_set_dict, nullptr, nullptr}, |
| 458 | {nullptr, nullptr, nullptr, nullptr, nullptr} |
| 459 | }; |
| 460 | type->tp_getset = getset; |
| 461 | } |
| 462 | |
| 463 | /// buffer_protocol: Fill in the view as specified by flags. |
| 464 | extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int flags) { |
Dean Moldovan | 427e4af | 2017-05-28 16:35:02 +0200 | [diff] [blame] | 465 | // Look for a `get_buffer` implementation in this type's info or any bases (following MRO). |
| 466 | type_info *tinfo = nullptr; |
| 467 | for (auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) { |
| 468 | tinfo = get_type_info((PyTypeObject *) type.ptr()); |
| 469 | if (tinfo && tinfo->get_buffer) |
| 470 | break; |
| 471 | } |
Axel Huebl | 1c627c9 | 2019-06-10 22:18:11 +0200 | [diff] [blame] | 472 | if (view == nullptr || !tinfo || !tinfo->get_buffer) { |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 473 | if (view) |
| 474 | view->obj = nullptr; |
Dean Moldovan | 427e4af | 2017-05-28 16:35:02 +0200 | [diff] [blame] | 475 | PyErr_SetString(PyExc_BufferError, "pybind11_getbuffer(): Internal error"); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 476 | return -1; |
| 477 | } |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 478 | std::memset(view, 0, sizeof(Py_buffer)); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 479 | buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data); |
| 480 | view->obj = obj; |
| 481 | view->ndim = 1; |
| 482 | view->internal = info; |
| 483 | view->buf = info->ptr; |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 484 | view->itemsize = info->itemsize; |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 485 | view->len = view->itemsize; |
| 486 | for (auto s : info->shape) |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 487 | view->len *= s; |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 488 | if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) |
| 489 | view->format = const_cast<char *>(info->format.c_str()); |
| 490 | if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) { |
| 491 | view->ndim = (int) info->ndim; |
Jason Rhinelander | b68959e | 2017-04-06 18:16:35 -0400 | [diff] [blame] | 492 | view->strides = &info->strides[0]; |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 493 | view->shape = &info->shape[0]; |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 494 | } |
| 495 | Py_INCREF(view->obj); |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | /// buffer_protocol: Release the resources of the buffer. |
| 500 | extern "C" inline void pybind11_releasebuffer(PyObject *, Py_buffer *view) { |
| 501 | delete (buffer_info *) view->internal; |
| 502 | } |
| 503 | |
| 504 | /// Give this type a buffer interface. |
| 505 | inline void enable_buffer_protocol(PyHeapTypeObject *heap_type) { |
| 506 | heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer; |
| 507 | #if PY_MAJOR_VERSION < 3 |
| 508 | heap_type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER; |
| 509 | #endif |
| 510 | |
| 511 | heap_type->as_buffer.bf_getbuffer = pybind11_getbuffer; |
| 512 | heap_type->as_buffer.bf_releasebuffer = pybind11_releasebuffer; |
| 513 | } |
| 514 | |
| 515 | /** Create a brand new Python type according to the `type_record` specification. |
| 516 | Return value: New reference. */ |
| 517 | inline PyObject* make_new_python_type(const type_record &rec) { |
| 518 | auto name = reinterpret_steal<object>(PYBIND11_FROM_STRING(rec.name)); |
| 519 | |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 520 | auto qualname = name; |
| 521 | if (rec.scope && !PyModule_Check(rec.scope.ptr()) && hasattr(rec.scope, "__qualname__")) { |
| 522 | #if PY_MAJOR_VERSION >= 3 |
| 523 | qualname = reinterpret_steal<object>( |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 524 | PyUnicode_FromFormat("%U.%U", rec.scope.attr("__qualname__").ptr(), name.ptr())); |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 525 | #else |
| 526 | qualname = str(rec.scope.attr("__qualname__").cast<std::string>() + "." + rec.name); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 527 | #endif |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 528 | } |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 529 | |
| 530 | object module; |
| 531 | if (rec.scope) { |
| 532 | if (hasattr(rec.scope, "__module__")) |
| 533 | module = rec.scope.attr("__module__"); |
| 534 | else if (hasattr(rec.scope, "__name__")) |
| 535 | module = rec.scope.attr("__name__"); |
| 536 | } |
| 537 | |
Jason Rhinelander | 2640c95 | 2017-08-04 10:42:39 -0400 | [diff] [blame] | 538 | auto full_name = c_str( |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 539 | #if !defined(PYPY_VERSION) |
Jason Rhinelander | 2640c95 | 2017-08-04 10:42:39 -0400 | [diff] [blame] | 540 | module ? str(module).cast<std::string>() + "." + rec.name : |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 541 | #endif |
Jason Rhinelander | 2640c95 | 2017-08-04 10:42:39 -0400 | [diff] [blame] | 542 | rec.name); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 543 | |
| 544 | char *tp_doc = nullptr; |
| 545 | if (rec.doc && options::show_user_defined_docstrings()) { |
| 546 | /* Allocate memory for docstring (using PyObject_MALLOC, since |
| 547 | Python will free this later on) */ |
| 548 | size_t size = strlen(rec.doc) + 1; |
| 549 | tp_doc = (char *) PyObject_MALLOC(size); |
| 550 | memcpy((void *) tp_doc, rec.doc, size); |
| 551 | } |
| 552 | |
| 553 | auto &internals = get_internals(); |
| 554 | auto bases = tuple(rec.bases); |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 555 | auto base = (bases.size() == 0) ? internals.instance_base |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 556 | : bases[0].ptr(); |
| 557 | |
| 558 | /* Danger zone: from now (and until PyType_Ready), make sure to |
| 559 | issue no Python C API calls which could potentially invoke the |
| 560 | garbage collector (the GC will call type_traverse(), which will in |
| 561 | turn find the newly constructed type in an invalid state) */ |
Dean Moldovan | dd01665 | 2017-02-16 23:02:56 +0100 | [diff] [blame] | 562 | auto metaclass = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr() |
| 563 | : internals.default_metaclass; |
| 564 | |
| 565 | auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 566 | if (!heap_type) |
| 567 | pybind11_fail(std::string(rec.name) + ": Unable to create type object!"); |
| 568 | |
| 569 | heap_type->ht_name = name.release().ptr(); |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 570 | #ifdef PYBIND11_BUILTIN_QUALNAME |
| 571 | heap_type->ht_qualname = qualname.inc_ref().ptr(); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 572 | #endif |
| 573 | |
| 574 | auto type = &heap_type->ht_type; |
Jason Rhinelander | 2640c95 | 2017-08-04 10:42:39 -0400 | [diff] [blame] | 575 | type->tp_name = full_name; |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 576 | type->tp_doc = tp_doc; |
bennorth | cb3d406 | 2017-07-20 14:21:31 +0100 | [diff] [blame] | 577 | type->tp_base = type_incref((PyTypeObject *)base); |
Jason Rhinelander | e45c211 | 2017-02-22 21:36:09 -0500 | [diff] [blame] | 578 | type->tp_basicsize = static_cast<ssize_t>(sizeof(instance)); |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 579 | if (bases.size() > 0) |
| 580 | type->tp_bases = bases.release().ptr(); |
| 581 | |
| 582 | /* Don't inherit base __init__ */ |
| 583 | type->tp_init = pybind11_object_init; |
| 584 | |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 585 | /* Supported protocols */ |
| 586 | type->tp_as_number = &heap_type->as_number; |
| 587 | type->tp_as_sequence = &heap_type->as_sequence; |
| 588 | type->tp_as_mapping = &heap_type->as_mapping; |
Jeremy Maitin-Shepard | a3f4a0e | 2019-07-18 00:02:35 -0700 | [diff] [blame^] | 589 | #if PY_VERSION_HEX >= 0x03050000 |
| 590 | type->tp_as_async = &heap_type->as_async; |
| 591 | #endif |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 592 | |
| 593 | /* Flags */ |
| 594 | type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; |
| 595 | #if PY_MAJOR_VERSION < 3 |
| 596 | type->tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 597 | #endif |
| 598 | |
| 599 | if (rec.dynamic_attr) |
| 600 | enable_dynamic_attributes(heap_type); |
| 601 | |
| 602 | if (rec.buffer_protocol) |
| 603 | enable_buffer_protocol(heap_type); |
| 604 | |
| 605 | if (PyType_Ready(type) < 0) |
| 606 | pybind11_fail(std::string(rec.name) + ": PyType_Ready failed (" + error_string() + ")!"); |
| 607 | |
| 608 | assert(rec.dynamic_attr ? PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC) |
| 609 | : !PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC)); |
| 610 | |
| 611 | /* Register type with the parent scope */ |
| 612 | if (rec.scope) |
| 613 | setattr(rec.scope, rec.name, (PyObject *) type); |
Wenzel Jakob | c14c276 | 2017-08-25 16:02:18 +0200 | [diff] [blame] | 614 | else |
| 615 | Py_INCREF(type); // Keep it alive forever (reference leak) |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 616 | |
| 617 | if (module) // Needed by pydoc |
| 618 | setattr((PyObject *) type, "__module__", module); |
| 619 | |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 620 | PYBIND11_SET_OLDPY_QUALNAME(type, qualname); |
| 621 | |
Dean Moldovan | 08cbe8d | 2017-02-15 21:10:25 +0100 | [diff] [blame] | 622 | return (PyObject *) type; |
| 623 | } |
| 624 | |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 625 | NAMESPACE_END(detail) |
Jason Rhinelander | a859dd6 | 2017-08-10 12:03:29 -0400 | [diff] [blame] | 626 | NAMESPACE_END(PYBIND11_NAMESPACE) |