Go to the documentation of this file.
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)
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);
65 constexpr
auto *
name =
"pybind11_static_property";
72 auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
74 pybind11_fail(
"make_static_property_type(): error allocating type!");
77 heap_type->ht_name = name_obj.inc_ref().ptr();
78 # ifdef PYBIND11_BUILTIN_QUALNAME
79 heap_type->ht_qualname = name_obj.inc_ref().ptr();
82 auto *
type = &heap_type->ht_type;
85 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
89 if (PyType_Ready(
type) < 0) {
90 pybind11_fail(
"make_static_property_type(): failure in PyType_Ready()!");
93 # if PY_VERSION_HEX >= 0x030C0000
100 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
113 PyObject *
result = PyRun_String(R
"(\
114 class pybind11_static_property(property):
115 def __get__(self, obj, cls):
116 return property.__get__(self, cls, cls)
118 def __set__(self, obj, value):
119 cls = obj if isinstance(obj, type) else type(obj)
120 property.__set__(self, cls, value)
128 return (PyTypeObject *)
d[
"pybind11_static_property"].cast<
object>().
release().ptr();
140 PyObject *
descr = _PyType_Lookup((PyTypeObject *) obj,
name);
147 const auto call_descr_set = (
descr !=
nullptr) && (
value !=
nullptr)
148 && (PyObject_IsInstance(
descr, static_prop) != 0)
149 && (PyObject_IsInstance(
value, static_prop) == 0);
150 if (call_descr_set) {
152 #if !defined(PYPY_VERSION)
155 if (PyObject *
result = PyObject_CallMethod(
descr,
"__set__",
"OO", obj,
value)) {
164 return PyType_Type.tp_setattro(obj,
name,
value);
175 PyObject *
descr = _PyType_Lookup((PyTypeObject *) obj,
name);
180 return PyType_Type.tp_getattro(obj,
name);
188 if (
self ==
nullptr) {
193 auto *
instance =
reinterpret_cast<detail::instance *
>(
self);
197 if (!vh.holder_constructed()) {
198 PyErr_Format(PyExc_TypeError,
199 "%.200s.__init__() must be called when overriding __init__",
211 auto *
type = (PyTypeObject *) obj;
219 && found_type->second[0]->type ==
type) {
221 auto *tinfo = found_type->second[0];
222 auto tindex = std::type_index(*tinfo->cpptype);
225 if (tinfo->module_local) {
234 for (
auto it = cache.begin(),
last = cache.end(); it !=
last;) {
235 if (it->first == (PyObject *) tinfo->type) {
236 it = cache.erase(it);
245 PyType_Type.tp_dealloc(obj);
252 constexpr
auto *
name =
"pybind11_type";
259 auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
261 pybind11_fail(
"make_default_metaclass(): error allocating metaclass!");
264 heap_type->ht_name = name_obj.inc_ref().ptr();
265 #ifdef PYBIND11_BUILTIN_QUALNAME
266 heap_type->ht_qualname = name_obj.inc_ref().ptr();
269 auto *
type = &heap_type->ht_type;
272 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
281 if (PyType_Ready(
type) < 0) {
282 pybind11_fail(
"make_default_metaclass(): failure in PyType_Ready()!");
285 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
296 const detail::type_info *tinfo,
299 for (
handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
300 if (
auto *parent_tinfo =
get_type_info((PyTypeObject *)
h.ptr())) {
301 for (
auto &
c : parent_tinfo->implicit_casts) {
302 if (
c.first == tinfo->cpptype) {
303 auto *parentptr =
c.second(valueptr);
304 if (parentptr != valueptr) {
321 auto range = registered_instances.equal_range(ptr);
322 for (
auto it =
range.first; it !=
range.second; ++it) {
323 if (
self == it->second) {
324 registered_instances.erase(it);
350 #if defined(PYPY_VERSION)
354 if (
type->tp_basicsize < instance_size) {
355 type->tp_basicsize = instance_size;
358 PyObject *
self =
type->tp_alloc(
type, 0);
361 inst->allocate_layout();
376 PyTypeObject *
type = Py_TYPE(
self);
378 PyErr_SetString(PyExc_TypeError,
msg.c_str());
384 auto *
instance =
reinterpret_cast<detail::instance *
>(nurse);
391 auto *
instance =
reinterpret_cast<detail::instance *
>(
self);
398 auto patients = std::move(
pos->second);
401 for (PyObject *&patient : patients) {
409 auto *
instance =
reinterpret_cast<detail::instance *
>(
self);
417 if (v_h.instance_registered()
420 "pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
424 v_h.type->dealloc(v_h);
432 PyObject_ClearWeakRefs(
self);
435 PyObject **dict_ptr = _PyObject_GetDictPtr(
self);
448 auto *
type = Py_TYPE(
self);
453 if (PyType_HasFeature(
type, Py_TPFLAGS_HAVE_GC) != 0) {
454 PyObject_GC_UnTrack(
self);
461 #if PY_VERSION_HEX < 0x03080000
467 if (
type->tp_dealloc == pybind11_object_type->tp_dealloc)
482 constexpr
auto *
name =
"pybind11_object";
491 pybind11_fail(
"make_object_base_type(): error allocating type!");
494 heap_type->ht_name = name_obj.inc_ref().ptr();
495 #ifdef PYBIND11_BUILTIN_QUALNAME
496 heap_type->ht_qualname = name_obj.inc_ref().ptr();
499 auto *
type = &heap_type->ht_type;
503 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
512 if (PyType_Ready(
type) < 0) {
516 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
519 assert(!PyType_HasFeature(
type, Py_TPFLAGS_HAVE_GC));
520 return (PyObject *) heap_type;
525 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
528 #if PY_VERSION_HEX >= 0x03090000
529 Py_VISIT(Py_TYPE(
self));
536 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
543 auto *
type = &heap_type->ht_type;
544 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
545 #if PY_VERSION_HEX < 0x030B0000
546 type->tp_dictoffset =
type->tp_basicsize;
547 type->tp_basicsize += (
ssize_t)
sizeof(PyObject *);
549 type->tp_flags |= Py_TPFLAGS_MANAGED_DICT;
554 static PyGetSetDef getset[] = {{
555 #if PY_VERSION_HEX < 0x03070000
556 const_cast<char *
>(
"__dict__"),
560 PyObject_GenericGetDict,
561 PyObject_GenericSetDict,
564 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}};
565 type->tp_getset = getset;
572 for (
auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
582 PyErr_SetString(PyExc_BufferError,
"pybind11_getbuffer(): Internal error");
585 std::memset(
view, 0,
sizeof(Py_buffer));
587 if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE &&
info->readonly) {
590 PyErr_SetString(PyExc_BufferError,
"Writable buffer requested for readonly storage");
599 for (
auto s :
info->shape) {
602 view->readonly =
static_cast<int>(
info->readonly);
603 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
604 view->format =
const_cast<char *
>(
info->format.c_str());
606 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
608 view->strides =
info->strides.data();
611 Py_INCREF(
view->obj);
622 heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer;
633 auto qualname =
name;
635 qualname = reinterpret_steal<object>(
636 PyUnicode_FromFormat(
"%U.%U", rec.
scope.attr(
"__qualname__").
ptr(),
name.ptr()));
648 const auto *full_name =
c_str(
649 #
if !defined(PYPY_VERSION)
654 char *tp_doc =
nullptr;
658 size_t size = std::strlen(rec.
doc) + 1;
659 tp_doc = (
char *) PyObject_MALLOC(
size);
660 std::memcpy((
void *) tp_doc, rec.
doc,
size);
679 heap_type->ht_name =
name.release().ptr();
680 #ifdef PYBIND11_BUILTIN_QUALNAME
681 heap_type->ht_qualname = qualname.inc_ref().ptr();
684 auto *
type = &heap_type->ht_type;
685 type->tp_name = full_name;
686 type->tp_doc = tp_doc;
689 if (!bases.empty()) {
697 type->tp_as_number = &heap_type->as_number;
698 type->tp_as_sequence = &heap_type->as_sequence;
699 type->tp_as_mapping = &heap_type->as_mapping;
700 type->tp_as_async = &heap_type->as_async;
703 type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE;
705 type->tp_flags |= Py_TPFLAGS_BASETYPE;
720 if (PyType_Ready(
type) < 0) {
739 return (PyObject *)
type;
const char * doc
Optional docstring.
bool deregister_instance(instance *self, void *valptr, const type_info *tinfo)
Annotation for function names.
int pybind11_object_init(PyObject *self, PyObject *, PyObject *)
void setattr(handle obj, handle name, handle value)
int pybind11_static_set(PyObject *self, PyObject *obj, PyObject *value)
pybind11_static_property.__set__(): Just like the above __get__().
Annotation indicating that a class derives from another given type.
void clear_patients(PyObject *self)
void add_patient(PyObject *nurse, PyObject *patient)
void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self, bool(*f)(void *, instance *))
static const double d[K][N]
bool dynamic_attr
Does the class manage a dict?
type_map< type_info * > registered_types_cpp
#define PYBIND11_NAMESPACE_END(name)
bool hasattr(handle obj, handle name)
void enable_dynamic_attributes(PyHeapTypeObject *heap_type)
Give instances of this type a __dict__ and opt into garbage collection.
PyTypeObject * static_property_type
const char * name
Name of the class.
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
#define PYBIND11_NAMESPACE_BEGIN(name)
PyTypeObject * make_static_property_type()
static const symbolic::SymbolExpr< internal::symbolic_last_tag > last
#define PYBIND11_FROM_STRING
PyObject * make_new_instance(PyTypeObject *type)
bool deregister_instance_impl(void *ptr, instance *self)
std::unordered_multimap< const void *, instance * > registered_instances
PyObject * make_object_base_type(PyTypeObject *metaclass)
#define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
Double_ range(const Point2_ &p, const Point2_ &q)
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
handle metaclass
Custom metaclass (optional)
std::string get_fully_qualified_tp_name(PyTypeObject *type)
void pybind11_meta_dealloc(PyObject *obj)
Cleanup the type-info for a pybind11-registered type.
std::unordered_set< std::pair< const PyObject *, const char * >, override_hash > inactive_override_cache
PyObject * pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *)
list bases
List of base classes of the newly created type.
type_map< type_info * > registered_types_cpp
Special data structure which (temporarily) holds metadata about a bound class.
void deallocate_layout()
Destroys/deallocates all of the above.
int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int flags)
buffer_protocol: Fill in the view as specified by flags.
PyObject * pybind11_meta_getattro(PyObject *obj, PyObject *name)
std::string error_string()
PYBIND11_NOINLINE detail::type_info * get_type_info(PyTypeObject *type)
bool has_patients
If true, get_internals().patients has an entry for this object.
bool owned
If true, the pointer is owned which means we're free to manage it with a holder.
int pybind11_clear(PyObject *self)
dynamic_attr: Allow the GC to clear the dictionary.
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
PyTypeObject * make_default_metaclass()
void pybind11_object_dealloc(PyObject *self)
bool register_instance_impl(void *ptr, instance *self)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
void pybind11_releasebuffer(PyObject *, Py_buffer *view)
buffer_protocol: Release the resources of the buffer.
bool is_final
Is the class inheritable from python classes?
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
int pybind11_meta_setattro(PyObject *obj, PyObject *name, PyObject *value)
void enable_buffer_protocol(PyHeapTypeObject *heap_type)
Give this type a buffer interface.
PyObject * ptr() const
Return the underlying PyObject * pointer.
std::unordered_map< const PyObject *, std::vector< PyObject * > > patients
Wrapper for Python extension modules.
Information record describing a Python buffer object.
const char * c_str(Args &&...args)
local_internals & get_local_internals()
Works like get_internals, but for things which are locally registered.
PyObject * weakrefs
Weak references.
static bool show_user_defined_docstrings()
PyObject * make_new_python_type(const type_record &rec)
void clear_instance(PyObject *self)
handle scope
Handle to the parent scope.
PyTypeObject * default_metaclass
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
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.
int pybind11_traverse(PyObject *self, visitproc visit, void *arg)
dynamic_attr: Allow the garbage collector to traverse the internal instance __dict__.
#define PYBIND11_BUILTINS_MODULE
PyObject * pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs)
metaclass __call__ function that is used to create all pybind11 objects.
PyTypeObject * type_incref(PyTypeObject *type)
buffer_info *(* get_buffer)(PyObject *, void *)
void register_instance(instance *self, void *valptr, const type_info *tinfo)
custom_type_setup::callback custom_type_setup_callback
Custom type setup.
gtsam
Author(s):
autogenerated on Thu Jun 13 2024 03:01:53