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) 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);
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 PyObject *
self = PyType_Type.tp_call(type, args, kwargs);
164 if (
self ==
nullptr) {
169 auto instance =
reinterpret_cast<detail::instance *
>(
self);
173 if (!vh.holder_constructed()) {
174 PyErr_Format(PyExc_TypeError,
"%.200s.__init__() must be called when overriding __init__",
175 vh.type->type->tp_name);
188 constexpr
auto *name =
"pybind11_type";
195 auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
197 pybind11_fail(
"make_default_metaclass(): error allocating metaclass!");
199 heap_type->ht_name = name_obj.inc_ref().ptr();
200 #ifdef PYBIND11_BUILTIN_QUALNAME 201 heap_type->ht_qualname = name_obj.inc_ref().ptr();
204 auto type = &heap_type->ht_type;
207 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
212 #if PY_MAJOR_VERSION >= 3 213 type->tp_getattro = pybind11_meta_getattro;
216 if (PyType_Ready(
type) < 0)
217 pybind11_fail(
"make_default_metaclass(): failure in PyType_Ready()!");
219 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
230 for (
handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
231 if (
auto parent_tinfo =
get_type_info((PyTypeObject *)
h.ptr())) {
232 for (
auto &
c : parent_tinfo->implicit_casts) {
233 if (
c.first == tinfo->cpptype) {
234 auto *parentptr =
c.second(valueptr);
235 if (parentptr != valueptr)
251 auto range = registered_instances.equal_range(ptr);
252 for (
auto it = range.first; it != range.second; ++it) {
253 if (Py_TYPE(
self) == Py_TYPE(it->second)) {
254 registered_instances.erase(it);
278 #if defined(PYPY_VERSION) 282 if (type->tp_basicsize < instance_size) {
283 type->tp_basicsize = instance_size;
286 PyObject *
self = type->tp_alloc(type, 0);
289 inst->allocate_layout();
306 PyTypeObject *
type = Py_TYPE(
self);
308 #if defined(PYPY_VERSION) 309 msg +=
handle((PyObject *) type).attr(
"__module__").
cast<std::string>() +
".";
311 msg += type->tp_name;
312 msg +=
": No constructor defined!";
313 PyErr_SetString(PyExc_TypeError, msg.c_str());
319 auto instance =
reinterpret_cast<detail::instance *
>(nurse);
326 auto instance =
reinterpret_cast<detail::instance *
>(
self);
333 auto patients = std::move(
pos->second);
336 for (PyObject *&patient : patients)
343 auto instance =
reinterpret_cast<detail::instance *
>(
self);
352 pybind11_fail(
"pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
355 v_h.type->dealloc(v_h);
362 PyObject_ClearWeakRefs(
self);
364 PyObject **dict_ptr = _PyObject_GetDictPtr(
self);
377 auto type = Py_TYPE(
self);
380 #if PY_VERSION_HEX < 0x03080000 386 if (
type->tp_dealloc == pybind11_object_type->tp_dealloc)
399 constexpr
auto *name =
"pybind11_object";
406 auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
408 pybind11_fail(
"make_object_base_type(): error allocating type!");
410 heap_type->ht_name = name_obj.inc_ref().ptr();
411 #ifdef PYBIND11_BUILTIN_QUALNAME 412 heap_type->ht_qualname = name_obj.inc_ref().ptr();
415 auto type = &heap_type->ht_type;
419 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
428 if (PyType_Ready(
type) < 0)
431 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
434 assert(!PyType_HasFeature(
type, Py_TPFLAGS_HAVE_GC));
435 return (PyObject *) heap_type;
440 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
449 if (!PyDict_Check(new_dict)) {
450 PyErr_Format(PyExc_TypeError,
"__dict__ must be set to a dictionary, not a '%.200s'",
451 Py_TYPE(new_dict)->tp_name);
454 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
463 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
470 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
477 auto type = &heap_type->ht_type;
478 #if defined(PYPY_VERSION) && (PYPY_VERSION_NUM < 0x06000000) 480 "currently not supported in " 481 "conjunction with PyPy!");
483 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
484 type->tp_dictoffset =
type->tp_basicsize;
489 static PyGetSetDef getset[] = {
491 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}
493 type->tp_getset = getset;
500 for (
auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
505 if (view ==
nullptr || !tinfo || !tinfo->
get_buffer) {
508 PyErr_SetString(PyExc_BufferError,
"pybind11_getbuffer(): Internal error");
511 std::memset(view, 0,
sizeof(Py_buffer));
515 view->internal =
info;
516 view->buf = info->
ptr;
518 view->len = view->itemsize;
519 for (
auto s : info->
shape)
522 if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->
readonly) {
525 PyErr_SetString(PyExc_BufferError,
"Writable buffer requested for readonly storage");
528 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
529 view->format =
const_cast<char *
>(info->
format.c_str());
530 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
532 view->strides = &info->
strides[0];
533 view->shape = &info->
shape[0];
535 Py_INCREF(view->obj);
546 heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer;
547 #if PY_MAJOR_VERSION < 3 548 heap_type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
560 auto qualname =
name;
562 #if PY_MAJOR_VERSION >= 3 563 qualname = reinterpret_steal<object>(
564 PyUnicode_FromFormat(
"%U.%U", rec.
scope.attr(
"__qualname__").
ptr(), name.ptr()));
566 qualname =
str(rec.
scope.attr(
"__qualname__").
cast<std::string>() +
"." + rec.
name);
573 module = rec.
scope.attr(
"__module__");
575 module = rec.
scope.attr(
"__name__");
578 auto full_name =
c_str(
579 #
if !defined(PYPY_VERSION)
580 module ?
str(module).cast<std::string>() +
"." + rec.
name :
584 char *tp_doc =
nullptr;
588 size_t size = strlen(rec.
doc) + 1;
589 tp_doc = (
char *) PyObject_MALLOC(size);
590 memcpy((
void *) tp_doc, rec.
doc, size);
609 heap_type->ht_name = name.release().ptr();
610 #ifdef PYBIND11_BUILTIN_QUALNAME 611 heap_type->ht_qualname = qualname.inc_ref().ptr();
614 auto type = &heap_type->ht_type;
615 type->tp_name = full_name;
616 type->tp_doc = tp_doc;
626 type->tp_as_number = &heap_type->as_number;
627 type->tp_as_sequence = &heap_type->as_sequence;
628 type->tp_as_mapping = &heap_type->as_mapping;
629 #if PY_VERSION_HEX >= 0x03050000 630 type->tp_as_async = &heap_type->as_async;
634 type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE;
635 #if PY_MAJOR_VERSION < 3 636 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
639 type->tp_flags |= Py_TPFLAGS_BASETYPE;
647 if (PyType_Ready(
type) < 0)
651 : !PyType_HasFeature(
type, Py_TPFLAGS_HAVE_GC));
664 return (PyObject *)
type;
bool has_patients
If true, get_internals().patients has an entry for this object.
int pybind11_meta_setattro(PyObject *obj, PyObject *name, PyObject *value)
PyObject * weakrefs
Weak references.
int pybind11_traverse(PyObject *self, visitproc visit, void *arg)
dynamic_attr: Allow the garbage collector to traverse the internal instance __dict__.
#define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
void deallocate_layout()
Destroys/deallocates all of the above.
bool is_final
Is the class inheritable from python classes?
bool hasattr(handle obj, handle name)
std::vector< ssize_t > strides
PyObject * pybind11_static_get(PyObject *self, PyObject *, PyObject *cls)
pybind11_static_property.__get__(): Always pass the class instead of the instance.
PyObject * ptr() const
Return the underlying PyObject * pointer.
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
void pybind11_object_dealloc(PyObject *self)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set view
bool buffer_protocol
Does the class implement the buffer protocol?
const char * doc
Optional docstring.
void clear_instance(PyObject *self)
std::unordered_multimap< const void *, instance * > registered_instances
bool deregister_instance(instance *self, void *valptr, const type_info *tinfo)
#define PYBIND11_NAMESPACE
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof') ...
const char * c_str(Args &&...args)
int pybind11_set_dict(PyObject *self, PyObject *new_dict, void *)
dynamic_attr: Support for instance.__dict__ = dict().
void pybind11_releasebuffer(PyObject *, Py_buffer *view)
buffer_protocol: Release the resources of the buffer.
bool deregister_instance_impl(void *ptr, instance *self)
PyExc_RuntimeError[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
PyTypeObject * type_incref(PyTypeObject *type)
PyObject * make_new_python_type(const type_record &rec)
PyObject * pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs)
metaclass __call__ function that is used to create all pybind11 objects.
void add_patient(PyObject *nurse, PyObject *patient)
handle metaclass
Custom metaclass (optional)
PyTypeObject * static_property_type
void clear_patients(PyObject *self)
handle scope
Handle to the parent scope.
int pybind11_clear(PyObject *self)
dynamic_attr: Allow the GC to clear the dictionary.
int pybind11_static_set(PyObject *self, PyObject *obj, PyObject *value)
pybind11_static_property.__set__(): Just like the above __get__().
const char * name
Name of the class.
list bases
List of base classes of the newly created type.
#define PYBIND11_FROM_STRING
void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self, bool(*f)(void *, instance *))
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
PyObject * pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *)
PyObject * pybind11_get_dict(PyObject *self, void *)
dynamic_attr: Support for d = instance.__dict__.
std::vector< ssize_t > shape
PyObject * make_new_instance(PyTypeObject *type)
PyObject * make_object_base_type(PyTypeObject *metaclass)
PyTypeObject * default_metaclass
bool owned
If true, the pointer is owned which means we're free to manage it with a holder.
Special data structure which (temporarily) holds metadata about a bound class.
PYBIND11_NOINLINE detail::type_info * get_type_info(PyTypeObject *type)
bool register_instance_impl(void *ptr, instance *self)
int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int flags)
buffer_protocol: Fill in the view as specified by flags.
int pybind11_object_init(PyObject *self, PyObject *, PyObject *)
std::unordered_map< const PyObject *, std::vector< PyObject * > > patients
static bool show_user_defined_docstrings()
void enable_buffer_protocol(PyHeapTypeObject *heap_type)
Give this type a buffer interface.
Annotation for function names.
Annotation indicating that a class derives from another given type.
PYBIND11_NOINLINE std::string error_string()
Information record describing a Python buffer object.
PyTypeObject * make_static_property_type()
buffer_info *(* get_buffer)(PyObject *, void *)
void register_instance(instance *self, void *valptr, const type_info *tinfo)
void setattr(handle obj, handle name, handle value)
PyTypeObject * make_default_metaclass()
#define PYBIND11_NAMESPACE_END(name)
bool dynamic_attr
Does the class manage a dict?
#define PYBIND11_NAMESPACE_BEGIN(name)
void enable_dynamic_attributes(PyHeapTypeObject *heap_type)
Give instances of this type a __dict__ and opt into garbage collection.