13 #include "../options.h" 18 #if !defined(PYPY_VERSION) 19 # define PYBIND11_BUILTIN_QUALNAME 20 # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) 24 # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) \ 25 setattr((PyObject *) obj, "__qualname__", nameobj) 29 #if !defined(PYPY_VERSION) 36 return std::move(
module_name) +
"." + type->tp_name;
45 #if !defined(PYPY_VERSION) 49 return PyProperty_Type.tp_descr_get(
self, cls, cls);
54 PyObject *cls = PyType_Check(obj) ? obj : (PyObject *) Py_TYPE(obj);
55 return PyProperty_Type.tp_descr_set(
self, cls, value);
62 constexpr
auto *
name =
"pybind11_static_property";
69 auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
71 pybind11_fail(
"make_static_property_type(): error allocating type!");
74 heap_type->ht_name = name_obj.inc_ref().ptr();
75 # ifdef PYBIND11_BUILTIN_QUALNAME 76 heap_type->ht_qualname = name_obj.inc_ref().ptr();
79 auto *
type = &heap_type->ht_type;
82 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
86 if (PyType_Ready(
type) < 0) {
87 pybind11_fail(
"make_static_property_type(): failure in PyType_Ready()!");
90 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
103 PyObject *
result = PyRun_String(R
"(\ 104 class pybind11_static_property(property): 105 def __get__(self, obj, cls): 106 return property.__get__(self, cls, cls) 108 def __set__(self, obj, value): 109 cls = obj if isinstance(obj, type) else type(obj) 110 property.__set__(self, cls, value) 115 if (result ==
nullptr)
118 return (PyTypeObject *)
d[
"pybind11_static_property"].cast<
object>().
release().ptr();
130 PyObject *
descr = _PyType_Lookup((PyTypeObject *) obj, name);
137 const auto call_descr_set = (descr !=
nullptr) && (value !=
nullptr)
138 && (PyObject_IsInstance(descr, static_prop) != 0)
139 && (PyObject_IsInstance(value, static_prop) == 0);
140 if (call_descr_set) {
142 #if !defined(PYPY_VERSION) 143 return Py_TYPE(descr)->tp_descr_set(descr, obj, value);
145 if (PyObject *
result = PyObject_CallMethod(descr,
"__set__",
"OO", obj, value)) {
154 return PyType_Type.tp_setattro(obj, name, value);
165 PyObject *
descr = _PyType_Lookup((PyTypeObject *) obj, name);
166 if (descr && PyInstanceMethod_Check(descr)) {
170 return PyType_Type.tp_getattro(obj, name);
177 PyObject *
self = PyType_Type.tp_call(type, args, kwargs);
178 if (
self ==
nullptr) {
183 auto *
instance =
reinterpret_cast<detail::instance *
>(
self);
187 if (!vh.holder_constructed()) {
188 PyErr_Format(PyExc_TypeError,
189 "%.200s.__init__() must be called when overriding __init__",
201 auto *
type = (PyTypeObject *) obj;
209 && found_type->second[0]->type ==
type) {
211 auto *tinfo = found_type->second[0];
212 auto tindex = std::type_index(*tinfo->cpptype);
215 if (tinfo->module_local) {
224 for (
auto it = cache.begin(),
last = cache.end(); it !=
last;) {
225 if (it->first == (PyObject *) tinfo->type) {
226 it = cache.erase(it);
235 PyType_Type.tp_dealloc(obj);
242 constexpr
auto *
name =
"pybind11_type";
249 auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
251 pybind11_fail(
"make_default_metaclass(): error allocating metaclass!");
254 heap_type->ht_name = name_obj.inc_ref().ptr();
255 #ifdef PYBIND11_BUILTIN_QUALNAME 256 heap_type->ht_qualname = name_obj.inc_ref().ptr();
259 auto *
type = &heap_type->ht_type;
262 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
271 if (PyType_Ready(
type) < 0) {
272 pybind11_fail(
"make_default_metaclass(): failure in PyType_Ready()!");
275 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
286 const detail::type_info *tinfo,
289 for (
handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
290 if (
auto *parent_tinfo =
get_type_info((PyTypeObject *)
h.ptr())) {
291 for (
auto &
c : parent_tinfo->implicit_casts) {
292 if (
c.first == tinfo->cpptype) {
293 auto *parentptr =
c.second(valueptr);
294 if (parentptr != valueptr) {
311 auto range = registered_instances.equal_range(ptr);
312 for (
auto it =
range.first; it !=
range.second; ++it) {
313 if (
self == it->second) {
314 registered_instances.erase(it);
340 #if defined(PYPY_VERSION) 344 if (type->tp_basicsize < instance_size) {
345 type->tp_basicsize = instance_size;
348 PyObject *
self = type->tp_alloc(type, 0);
351 inst->allocate_layout();
366 PyTypeObject *
type = Py_TYPE(
self);
368 PyErr_SetString(PyExc_TypeError, msg.c_str());
374 auto *
instance =
reinterpret_cast<detail::instance *
>(nurse);
381 auto *
instance =
reinterpret_cast<detail::instance *
>(
self);
388 auto patients = std::move(
pos->second);
391 for (PyObject *&patient : patients) {
399 auto *
instance =
reinterpret_cast<detail::instance *
>(
self);
407 if (v_h.instance_registered()
410 "pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
414 v_h.type->dealloc(v_h);
422 PyObject_ClearWeakRefs(
self);
425 PyObject **dict_ptr = _PyObject_GetDictPtr(
self);
440 auto *
type = Py_TYPE(
self);
443 #if PY_VERSION_HEX < 0x03080000 449 if (
type->tp_dealloc == pybind11_object_type->tp_dealloc)
464 constexpr
auto *
name =
"pybind11_object";
471 auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
473 pybind11_fail(
"make_object_base_type(): error allocating type!");
476 heap_type->ht_name = name_obj.inc_ref().ptr();
477 #ifdef PYBIND11_BUILTIN_QUALNAME 478 heap_type->ht_qualname = name_obj.inc_ref().ptr();
481 auto *
type = &heap_type->ht_type;
485 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
494 if (PyType_Ready(
type) < 0) {
498 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
501 assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
502 return (PyObject *) heap_type;
507 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
517 if (!PyDict_Check(new_dict)) {
518 PyErr_Format(PyExc_TypeError,
519 "__dict__ must be set to a dictionary, not a '%.200s'",
523 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
532 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
535 #if PY_VERSION_HEX >= 0x03090000 536 Py_VISIT(Py_TYPE(
self));
543 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
550 auto *
type = &heap_type->ht_type;
551 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
552 #if PY_VERSION_HEX < 0x030B0000 553 type->tp_dictoffset =
type->tp_basicsize;
554 type->tp_basicsize += (
ssize_t)
sizeof(PyObject *);
556 type->tp_flags |= Py_TPFLAGS_MANAGED_DICT;
561 static PyGetSetDef getset[] = {
563 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}};
564 type->tp_getset = getset;
571 for (
auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
577 if (view ==
nullptr || !tinfo || !tinfo->
get_buffer) {
581 PyErr_SetString(PyExc_BufferError,
"pybind11_getbuffer(): Internal error");
584 std::memset(view, 0,
sizeof(Py_buffer));
586 if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->
readonly) {
589 PyErr_SetString(PyExc_BufferError,
"Writable buffer requested for readonly storage");
594 view->internal =
info;
595 view->buf = info->
ptr;
597 view->len = view->itemsize;
598 for (
auto s : info->
shape) {
601 view->readonly =
static_cast<int>(info->
readonly);
602 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
603 view->format =
const_cast<char *
>(info->
format.c_str());
605 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
607 view->strides = info->
strides.data();
608 view->shape = info->
shape.data();
610 Py_INCREF(view->obj);
621 heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer;
632 auto qualname =
name;
634 qualname = reinterpret_steal<object>(
635 PyUnicode_FromFormat(
"%U.%U", rec.
scope.attr(
"__qualname__").
ptr(),
name.ptr()));
641 module_ = rec.
scope.attr(
"__module__");
643 module_ = rec.
scope.attr(
"__name__");
647 const auto *full_name =
c_str(
648 #
if !defined(PYPY_VERSION)
649 module_ ?
str(module_).cast<std::string>() +
"." + rec.
name :
653 char *tp_doc =
nullptr;
657 size_t size = std::strlen(rec.
doc) + 1;
658 tp_doc = (
char *) PyObject_MALLOC(size);
659 std::memcpy((
void *) tp_doc, rec.
doc, size);
678 heap_type->ht_name =
name.release().ptr();
679 #ifdef PYBIND11_BUILTIN_QUALNAME 680 heap_type->ht_qualname = qualname.inc_ref().ptr();
683 auto *
type = &heap_type->ht_type;
684 type->tp_name = full_name;
685 type->tp_doc = tp_doc;
688 if (!bases.empty()) {
696 type->tp_as_number = &heap_type->as_number;
697 type->tp_as_sequence = &heap_type->as_sequence;
698 type->tp_as_mapping = &heap_type->as_mapping;
699 type->tp_as_async = &heap_type->as_async;
702 type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE;
704 type->tp_flags |= Py_TPFLAGS_BASETYPE;
719 if (PyType_Ready(
type) < 0) {
720 pybind11_fail(std::string(rec.
name) +
": PyType_Ready failed: " + error_string());
738 return (PyObject *)
type;
PYBIND11_NOINLINE detail::type_info * get_type_info(PyTypeObject *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)
#define PYBIND11_BUILTINS_MODULE
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.
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
void pybind11_object_dealloc(PyObject *self)
type_map< type_info * > registered_types_cpp
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?
Wrapper for Python extension modules.
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') ...
static const symbolic::SymbolExpr< internal::symbolic_last_tag > last
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.
custom_type_setup::callback custom_type_setup_callback
Custom type setup.
PyTypeObject * type_incref(PyTypeObject *type)
void pybind11_meta_dealloc(PyObject *obj)
Cleanup the type-info for a pybind11-registered 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.
PyObject * pybind11_meta_getattro(PyObject *obj, PyObject *name)
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.
std::unordered_set< std::pair< const PyObject *, const char * >, override_hash > inactive_override_cache
std::string get_fully_qualified_tp_name(PyTypeObject *type)
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
local_internals & get_local_internals()
Works like get_internals, but for things which are locally registered.
type_map< type_info * > registered_types_cpp
#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.
std::string error_string()
Special data structure which (temporarily) holds metadata about a bound class.
bool register_instance_impl(void *ptr, instance *self)
Double_ range(const Point2_ &p, const Point2_ &q)
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.
Information record describing a Python buffer object.
PyObject * ptr() const
Return the underlying PyObject * pointer.
PyTypeObject * make_static_property_type()
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
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.