13 #include "../options.h" 18 #if PY_VERSION_HEX >= 0x03030000 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) 32 #if !defined(PYPY_VERSION) 36 return PyProperty_Type.tp_descr_get(
self, cls, cls);
41 PyObject *cls = PyType_Check(obj) ? obj : (PyObject *) Py_TYPE(obj);
42 return PyProperty_Type.tp_descr_set(
self, cls, value);
49 constexpr
auto *
name =
"pybind11_static_property";
56 auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
58 pybind11_fail(
"make_static_property_type(): error allocating type!");
60 heap_type->ht_name = name_obj.inc_ref().ptr();
61 #ifdef PYBIND11_BUILTIN_QUALNAME 62 heap_type->ht_qualname = name_obj.inc_ref().ptr();
65 auto type = &heap_type->ht_type;
68 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
72 if (PyType_Ready(
type) < 0)
73 pybind11_fail(
"make_static_property_type(): failure in PyType_Ready()!");
75 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
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) 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() 98 if (result ==
nullptr)
101 return (PyTypeObject *)
d[
"pybind11_static_property"].cast<
object>().
release().ptr();
113 PyObject *
descr = _PyType_Lookup((PyTypeObject *) obj, name);
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) {
124 #if !defined(PYPY_VERSION) 125 return Py_TYPE(descr)->tp_descr_set(descr, obj, value);
127 if (PyObject *result = PyObject_CallMethod(descr,
"__set__",
"OO", obj, value)) {
136 return PyType_Type.tp_setattro(obj, name, value);
140 #if PY_MAJOR_VERSION >= 3 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)) {
154 return PyType_Type.tp_getattro(obj, name);
163 constexpr
auto *name =
"pybind11_type";
170 auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
172 pybind11_fail(
"make_default_metaclass(): error allocating metaclass!");
174 heap_type->ht_name = name_obj.inc_ref().ptr();
175 #ifdef PYBIND11_BUILTIN_QUALNAME 176 heap_type->ht_qualname = name_obj.inc_ref().ptr();
179 auto type = &heap_type->ht_type;
180 type->tp_name = name;
182 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
185 #if PY_MAJOR_VERSION >= 3 186 type->tp_getattro = pybind11_meta_getattro;
189 if (PyType_Ready(
type) < 0)
190 pybind11_fail(
"make_default_metaclass(): failure in PyType_Ready()!");
192 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
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)
219 get_internals().registered_instances.emplace(ptr,
self);
223 auto ®istered_instances = get_internals().registered_instances;
224 auto range = registered_instances.equal_range(ptr);
225 for (
auto it =
range.first; it !=
range.second; ++it) {
226 if (Py_TYPE(
self) == Py_TYPE(it->second)) {
227 registered_instances.erase(it);
251 #if defined(PYPY_VERSION) 255 if (type->tp_basicsize < instance_size) {
256 type->tp_basicsize = instance_size;
259 PyObject *
self = type->tp_alloc(type, 0);
260 auto inst =
reinterpret_cast<instance *
>(
self);
279 PyTypeObject *
type = Py_TYPE(
self);
281 #if defined(PYPY_VERSION) 282 msg +=
handle((PyObject *) type).attr(
"__module__").cast<std::string>() +
".";
284 msg += type->tp_name;
285 msg +=
": No constructor defined!";
286 PyErr_SetString(PyExc_TypeError, msg.c_str());
309 for (PyObject *&patient : patients)
325 pybind11_fail(
"pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
328 v_h.type->dealloc(v_h);
335 PyObject_ClearWeakRefs(
self);
337 PyObject **dict_ptr = _PyObject_GetDictPtr(
self);
350 auto type = Py_TYPE(
self);
357 auto pybind11_object_type = (PyTypeObject *) get_internals().instance_base;
358 if (
type->tp_dealloc == pybind11_object_type->tp_dealloc)
366 constexpr
auto *name =
"pybind11_object";
373 auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
375 pybind11_fail(
"make_object_base_type(): error allocating type!");
377 heap_type->ht_name = name_obj.inc_ref().ptr();
378 #ifdef PYBIND11_BUILTIN_QUALNAME 379 heap_type->ht_qualname = name_obj.inc_ref().ptr();
382 auto type = &heap_type->ht_type;
383 type->tp_name = name;
386 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
395 if (PyType_Ready(
type) < 0)
396 pybind11_fail(
"PyType_Ready failed in make_object_base_type():" +
error_string());
398 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
401 assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
402 return (PyObject *) heap_type;
407 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
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);
421 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
430 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
437 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
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!");
450 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
451 type->tp_dictoffset =
type->tp_basicsize;
456 static PyGetSetDef getset[] = {
458 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}
460 type->tp_getset = getset;
467 for (
auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
468 tinfo = get_type_info((PyTypeObject *)
type.ptr());
472 if (view ==
nullptr || !tinfo || !tinfo->
get_buffer) {
475 PyErr_SetString(PyExc_BufferError,
"pybind11_getbuffer(): Internal error");
478 std::memset(view, 0,
sizeof(Py_buffer));
482 view->internal = info;
483 view->buf = info->
ptr;
485 view->len = view->itemsize;
486 for (
auto s : info->
shape)
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;
492 view->strides = &info->
strides[0];
493 view->shape = &info->
shape[0];
495 Py_INCREF(view->obj);
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;
520 auto qualname = name;
522 #if PY_MAJOR_VERSION >= 3 523 qualname = reinterpret_steal<object>(
524 PyUnicode_FromFormat(
"%U.%U", rec.
scope.
attr(
"__qualname__").ptr(), name.ptr()));
526 qualname =
str(rec.
scope.
attr(
"__qualname__").cast<std::string>() +
"." + rec.
name);
538 auto full_name =
c_str(
539 #
if !defined(PYPY_VERSION)
540 module ?
str(module).cast<std::string>() +
"." + rec.
name :
544 char *tp_doc =
nullptr;
545 if (rec.
doc && options::show_user_defined_docstrings()) {
548 size_t size = strlen(rec.
doc) + 1;
549 tp_doc = (
char *) PyObject_MALLOC(size);
550 memcpy((
void *) tp_doc, rec.
doc, size);
567 pybind11_fail(std::string(rec.
name) +
": Unable to create type object!");
569 heap_type->ht_name = name.release().ptr();
570 #ifdef PYBIND11_BUILTIN_QUALNAME 571 heap_type->ht_qualname = qualname.inc_ref().ptr();
574 auto type = &heap_type->ht_type;
575 type->tp_name = full_name;
576 type->tp_doc = tp_doc;
579 if (
bases.size() > 0)
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;
589 #if PY_VERSION_HEX >= 0x03050000 590 type->tp_as_async = &heap_type->as_async;
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;
605 if (PyType_Ready(
type) < 0)
606 pybind11_fail(std::string(rec.
name) +
": PyType_Ready failed (" +
error_string() +
")!");
609 : !PyType_HasFeature(
type, Py_TPFLAGS_HAVE_GC));
618 setattr((PyObject *) type,
"__module__", module);
622 return (PyObject *)
type;
#define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
bool has_patients
If true, get_internals().patients has an entry for this object.
int pybind11_static_set(PyObject *self, PyObject *obj, PyObject *value)
pybind11_static_property.__set__(): Just like the above __get__().
#define PYBIND11_FROM_STRING
#define PYBIND11_NAMESPACE
PyObject * ptr() const
Return the underlying PyObject * pointer.
void enable_dynamic_attributes(PyHeapTypeObject *heap_type)
Give instances of this type a __dict__ and opt into garbage collection.
void pybind11_releasebuffer(PyObject *, Py_buffer *view)
buffer_protocol: Release the resources of the buffer.
void register_instance(instance *self, void *valptr, const type_info *tinfo)
std::string error_string()
const char * name
Name of the class.
void add_patient(PyObject *nurse, PyObject *patient)
void allocate_layout()
Initializes all of the above type/values/holders data (but not the instance values themselves) ...
int pybind11_object_init(PyObject *self, PyObject *, PyObject *)
bool deregister_instance_impl(void *ptr, instance *self)
int pybind11_meta_setattro(PyObject *obj, PyObject *name, PyObject *value)
bool deregister_instance(instance *self, void *valptr, const type_info *tinfo)
std::vector< ssize_t > strides
Information record describing a Python buffer object.
void clear_instance(PyObject *self)
Wrapper for Python extension modules.
std::vector< ssize_t > shape
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof') ...
buffer_info *(* get_buffer)(PyObject *, void *)
bool register_instance_impl(void *ptr, instance *self)
void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self, bool(*f)(void *, instance *))
PyObject * make_new_instance(PyTypeObject *type)
obj_attr_accessor attr(handle key) const
std::vector< type_info * > & bases
PyObject * pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *)
size_t function_call handle ret
PyTypeObject * type_incref(PyTypeObject *type)
handle metaclass
Custom metaclass (optional)
handle scope
Handle to the parent scope.
#define NAMESPACE_END(name)
void clear_patients(PyObject *self)
PyTypeObject * default_metaclass
PyObject * make_object_base_type(PyTypeObject *metaclass)
const std::type_info * cpptype
int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int flags)
buffer_protocol: Fill in the view as specified by flags.
PyTypeObject * make_default_metaclass()
bool owned
If true, the pointer is owned which means we're free to manage it with a holder.
bool buffer_protocol
Does the class implement the buffer protocol?
PyObject * pybind11_static_get(PyObject *self, PyObject *, PyObject *cls)
pybind11_static_property.__get__(): Always pass the class instead of the instance.
void deallocate_layout()
Destroys/deallocates all of the above.
PyTypeObject * make_static_property_type()
bool dynamic_attr
Does the class manage a dict?
PyObject * weakrefs
Weak references.
int pybind11_clear(PyObject *self)
dynamic_attr: Allow the GC to clear the dictionary.
Annotation indicating that a class derives from another given type.
const detail::type_info * type
Annotation for function names.
const char * c_str(Args &&...args)
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
void pybind11_object_dealloc(PyObject *self)
PyObject * make_new_python_type(const type_record &rec)
#define NAMESPACE_BEGIN(name)
const char * doc
Optional docstring.
void enable_buffer_protocol(PyHeapTypeObject *heap_type)
Give this type a buffer interface.
Special data structure which (temporarily) holds metadata about a bound class.
list bases
List of base classes of the newly created type.
void setattr(handle obj, handle name, handle value)
const std::type_info & tinfo
int pybind11_set_dict(PyObject *self, PyObject *new_dict, void *)
dynamic_attr: Support for instance.__dict__ = dict().
int pybind11_traverse(PyObject *self, visitproc visit, void *arg)
dynamic_attr: Allow the garbage collector to traverse the internal instance __dict__.
bool hasattr(handle obj, handle name)
std::unordered_map< const PyObject *, std::vector< PyObject * > > patients
PyObject * pybind11_get_dict(PyObject *self, void *)
dynamic_attr: Support for d = instance.__dict__.