13 #include "../options.h"
18 #if PY_VERSION_HEX >= 0x03030000 && !defined(PYPY_VERSION)
19 # define PYBIND11_BUILTIN_QUALNAME
20 # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
24 # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) setattr((PyObject *) obj, "__qualname__", nameobj)
28 #if !defined(PYPY_VERSION)
31 auto module_name =
handle((PyObject *)
type).attr(
"__module__").
cast<std::string>();
44 #if !defined(PYPY_VERSION)
48 return PyProperty_Type.tp_descr_get(
self, cls, cls);
53 PyObject *cls = PyType_Check(obj) ? obj : (PyObject *) Py_TYPE(obj);
54 return PyProperty_Type.tp_descr_set(
self, cls,
value);
61 constexpr
auto *
name =
"pybind11_static_property";
68 auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
70 pybind11_fail(
"make_static_property_type(): error allocating type!");
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();
77 auto type = &heap_type->ht_type;
80 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
84 if (PyType_Ready(
type) < 0)
85 pybind11_fail(
"make_static_property_type(): failure in PyType_Ready()!");
87 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
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)
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()
110 if (result ==
nullptr)
113 return (PyTypeObject *) d[
"pybind11_static_property"].cast<
object>().release().ptr();
125 PyObject *
descr = _PyType_Lookup((PyTypeObject *) obj,
name);
132 const auto call_descr_set =
descr &&
value && PyObject_IsInstance(
descr, static_prop)
133 && !PyObject_IsInstance(
value, static_prop);
134 if (call_descr_set) {
136 #if !defined(PYPY_VERSION)
139 if (PyObject *result = PyObject_CallMethod(
descr,
"__set__",
"OO", obj,
value)) {
148 return PyType_Type.tp_setattro(obj,
name,
value);
152 #if PY_MAJOR_VERSION >= 3
159 extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *
name) {
160 PyObject *
descr = _PyType_Lookup((PyTypeObject *) obj,
name);
166 return PyType_Type.tp_getattro(obj,
name);
176 if (
self ==
nullptr) {
181 auto instance =
reinterpret_cast<detail::instance *
>(
self);
185 if (!vh.holder_constructed()) {
186 PyErr_Format(PyExc_TypeError,
"%.200s.__init__() must be called when overriding __init__",
198 auto *
type = (PyTypeObject *) obj;
206 found_type->second.size() == 1 &&
207 found_type->second[0]->type ==
type) {
209 auto *tinfo = found_type->second[0];
210 auto tindex = std::type_index(*tinfo->cpptype);
213 if (tinfo->module_local)
221 for (
auto it = cache.begin(),
last = cache.end(); it !=
last; ) {
222 if (it->first == (PyObject *) tinfo->type)
223 it = cache.erase(it);
231 PyType_Type.tp_dealloc(obj);
238 constexpr
auto *
name =
"pybind11_type";
245 auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
247 pybind11_fail(
"make_default_metaclass(): error allocating metaclass!");
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();
254 auto type = &heap_type->ht_type;
257 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
262 #if PY_MAJOR_VERSION >= 3
263 type->tp_getattro = pybind11_meta_getattro;
268 if (PyType_Ready(
type) < 0)
269 pybind11_fail(
"make_default_metaclass(): failure in PyType_Ready()!");
271 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
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)
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);
330 #if defined(PYPY_VERSION)
334 if (
type->tp_basicsize < instance_size) {
335 type->tp_basicsize = instance_size;
338 PyObject *
self =
type->tp_alloc(
type, 0);
339 auto inst =
reinterpret_cast<instance *
>(
self);
356 PyTypeObject *
type = Py_TYPE(
self);
358 PyErr_SetString(PyExc_TypeError,
msg.c_str());
364 auto instance =
reinterpret_cast<detail::instance *
>(nurse);
371 auto instance =
reinterpret_cast<detail::instance *
>(
self);
381 for (PyObject *&patient : patients)
388 auto instance =
reinterpret_cast<detail::instance *
>(
self);
397 pybind11_fail(
"pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
400 v_h.type->dealloc(v_h);
407 PyObject_ClearWeakRefs(
self);
409 PyObject **dict_ptr = _PyObject_GetDictPtr(
self);
422 auto type = Py_TYPE(
self);
425 #if PY_VERSION_HEX < 0x03080000
431 if (
type->tp_dealloc == pybind11_object_type->tp_dealloc)
444 constexpr
auto *
name =
"pybind11_object";
453 pybind11_fail(
"make_object_base_type(): error allocating type!");
455 heap_type->ht_name = name_obj.inc_ref().ptr();
456 #ifdef PYBIND11_BUILTIN_QUALNAME
457 heap_type->ht_qualname = name_obj.inc_ref().ptr();
460 auto type = &heap_type->ht_type;
464 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
473 if (PyType_Ready(
type) < 0)
476 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
479 assert(!PyType_HasFeature(
type, Py_TPFLAGS_HAVE_GC));
480 return (PyObject *) heap_type;
485 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
494 if (!PyDict_Check(new_dict)) {
495 PyErr_Format(PyExc_TypeError,
"__dict__ must be set to a dictionary, not a '%.200s'",
499 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
508 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
515 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
522 auto type = &heap_type->ht_type;
523 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
524 type->tp_dictoffset =
type->tp_basicsize;
529 static PyGetSetDef getset[] = {
531 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}
533 type->tp_getset = getset;
540 for (
auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
545 if (view ==
nullptr || !tinfo || !tinfo->
get_buffer) {
548 PyErr_SetString(PyExc_BufferError,
"pybind11_getbuffer(): Internal error");
551 std::memset(view, 0,
sizeof(Py_buffer));
553 if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->
readonly) {
556 PyErr_SetString(PyExc_BufferError,
"Writable buffer requested for readonly storage");
561 view->internal = info;
562 view->buf = info->
ptr;
564 view->len = view->itemsize;
565 for (
auto s : info->
shape)
568 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
569 view->format =
const_cast<char *
>(info->
format.c_str());
570 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
571 view->ndim = (int) info->
ndim;
572 view->strides = &info->
strides[0];
573 view->shape = &info->
shape[0];
575 Py_INCREF(view->obj);
586 heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer;
587 #if PY_MAJOR_VERSION < 3
588 heap_type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
600 auto qualname =
name;
602 #if PY_MAJOR_VERSION >= 3
603 qualname = reinterpret_steal<object>(
604 PyUnicode_FromFormat(
"%U.%U", rec.
scope.attr(
"__qualname__").
ptr(),
name.ptr()));
606 qualname =
str(rec.
scope.attr(
"__qualname__").
cast<std::string>() +
"." + rec.
name);
618 auto full_name =
c_str(
619 #
if !defined(PYPY_VERSION)
624 char *tp_doc =
nullptr;
628 size_t size = strlen(rec.
doc) + 1;
629 tp_doc = (
char *) PyObject_MALLOC(
size);
630 memcpy((
void *) tp_doc, rec.
doc,
size);
649 heap_type->ht_name =
name.release().ptr();
650 #ifdef PYBIND11_BUILTIN_QUALNAME
651 heap_type->ht_qualname = qualname.inc_ref().ptr();
654 auto type = &heap_type->ht_type;
655 type->tp_name = full_name;
656 type->tp_doc = tp_doc;
666 type->tp_as_number = &heap_type->as_number;
667 type->tp_as_sequence = &heap_type->as_sequence;
668 type->tp_as_mapping = &heap_type->as_mapping;
669 #if PY_VERSION_HEX >= 0x03050000
670 type->tp_as_async = &heap_type->as_async;
674 type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE;
675 #if PY_MAJOR_VERSION < 3
676 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
679 type->tp_flags |= Py_TPFLAGS_BASETYPE;
687 if (PyType_Ready(
type) < 0)
691 : !PyType_HasFeature(
type, Py_TPFLAGS_HAVE_GC));
704 return (PyObject *)
type;