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