Go to the documentation of this file.
20 #if !defined(PYPY_VERSION)
21 # define PYBIND11_BUILTIN_QUALNAME
22 # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
26 # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) \
27 setattr((PyObject *) obj, "__qualname__", nameobj)
31 #if !defined(PYPY_VERSION)
47 #if !defined(PYPY_VERSION)
51 return PyProperty_Type.tp_descr_get(
self, cls, cls);
56 PyObject *cls = PyType_Check(obj) ? obj : (PyObject *) Py_TYPE(obj);
57 return PyProperty_Type.tp_descr_set(
self, cls,
value);
67 constexpr
auto *
name =
"pybind11_static_property";
74 auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
76 pybind11_fail(
"make_static_property_type(): error allocating type!");
79 heap_type->ht_name = name_obj.inc_ref().ptr();
80 # ifdef PYBIND11_BUILTIN_QUALNAME
81 heap_type->ht_qualname = name_obj.inc_ref().ptr();
84 auto *
type = &heap_type->ht_type;
87 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
91 # if PY_VERSION_HEX >= 0x030C0000
97 if (PyType_Ready(
type) < 0) {
98 pybind11_fail(
"make_static_property_type(): failure in PyType_Ready()!");
101 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
114 PyObject *
result = PyRun_String(R
"(\
115 class pybind11_static_property(property):
116 def __get__(self, obj, cls):
117 return property.__get__(self, cls, cls)
119 def __set__(self, obj, value):
120 cls = obj if isinstance(obj, type) else type(obj)
121 property.__set__(self, cls, value)
129 return (PyTypeObject *)
d[
"pybind11_static_property"].cast<
object>().
release().ptr();
141 PyObject *
descr = _PyType_Lookup((PyTypeObject *) obj,
name);
148 const auto call_descr_set = (
descr !=
nullptr) && (
value !=
nullptr)
149 && (PyObject_IsInstance(
descr, static_prop) != 0)
150 && (PyObject_IsInstance(
value, static_prop) == 0);
151 if (call_descr_set) {
153 #if !defined(PYPY_VERSION)
156 if (PyObject *
result = PyObject_CallMethod(
descr,
"__set__",
"OO", obj,
value)) {
165 return PyType_Type.tp_setattro(obj,
name,
value);
176 PyObject *
descr = _PyType_Lookup((PyTypeObject *) obj,
name);
181 return PyType_Type.tp_getattro(obj,
name);
189 if (
self ==
nullptr) {
195 for (
const auto &vh : vhs) {
196 if (!vh.holder_constructed() && !vhs.is_redundant_value_and_holder(vh)) {
197 PyErr_Format(PyExc_TypeError,
198 "%.200s.__init__() must be called when overriding __init__",
211 auto *
type = (PyTypeObject *) obj;
218 && found_type->second[0]->type ==
type) {
220 auto *tinfo = found_type->second[0];
221 auto tindex = std::type_index(*tinfo->cpptype);
224 if (tinfo->module_local) {
233 for (
auto it = cache.begin(),
last = cache.end(); it !=
last;) {
234 if (it->first == (PyObject *) tinfo->type) {
235 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 = instances.equal_range(ptr);
322 for (
auto it =
range.first; it !=
range.second; ++it) {
323 if (self == it->second) {
351 #if defined(PYPY_VERSION)
355 if (
type->tp_basicsize < instance_size) {
356 type->tp_basicsize = instance_size;
359 PyObject *
self =
type->tp_alloc(
type, 0);
362 inst->allocate_layout();
377 PyTypeObject *
type = Py_TYPE(
self);
384 auto *
instance =
reinterpret_cast<detail::instance *
>(nurse);
392 auto *
instance =
reinterpret_cast<detail::instance *
>(
self);
393 std::vector<PyObject *> patients;
400 "FATAL: Internal consistency check failed: Invalid clear_patients() call.");
406 patients = std::move(
pos->second);
411 for (PyObject *&patient : patients) {
419 auto *
instance =
reinterpret_cast<detail::instance *
>(
self);
427 if (v_h.instance_registered()
430 "pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
434 v_h.type->dealloc(v_h);
442 PyObject_ClearWeakRefs(
self);
445 PyObject **dict_ptr = _PyObject_GetDictPtr(
self);
458 auto *
type = Py_TYPE(
self);
463 if (PyType_HasFeature(
type, Py_TPFLAGS_HAVE_GC) != 0) {
464 PyObject_GC_UnTrack(
self);
471 #if PY_VERSION_HEX < 0x03080000
477 if (
type->tp_dealloc == pybind11_object_type->tp_dealloc)
492 constexpr
auto *
name =
"pybind11_object";
501 pybind11_fail(
"make_object_base_type(): error allocating type!");
504 heap_type->ht_name = name_obj.inc_ref().ptr();
505 #ifdef PYBIND11_BUILTIN_QUALNAME
506 heap_type->ht_qualname = name_obj.inc_ref().ptr();
509 auto *
type = &heap_type->ht_type;
513 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
522 if (PyType_Ready(
type) < 0) {
526 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
529 assert(!PyType_HasFeature(
type, Py_TPFLAGS_HAVE_GC));
530 return (PyObject *) heap_type;
535 #if PY_VERSION_HEX >= 0x030D0000
536 PyObject_VisitManagedDict(
self, visit,
arg);
538 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
542 #if PY_VERSION_HEX >= 0x03090000
543 Py_VISIT(Py_TYPE(
self));
550 #if PY_VERSION_HEX >= 0x030D0000
551 PyObject_ClearManagedDict(
self);
553 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
561 auto *
type = &heap_type->ht_type;
562 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
563 #if PY_VERSION_HEX < 0x030B0000
564 type->tp_dictoffset =
type->tp_basicsize;
565 type->tp_basicsize += (
ssize_t)
sizeof(PyObject *);
567 type->tp_flags |= Py_TPFLAGS_MANAGED_DICT;
572 static PyGetSetDef getset[]
573 = {{
"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict,
nullptr,
nullptr},
574 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}};
575 type->tp_getset = getset;
582 for (
auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
592 set_error(PyExc_BufferError,
"pybind11_getbuffer(): Internal error");
595 std::memset(
view, 0,
sizeof(Py_buffer));
601 raise_from(PyExc_BufferError,
"Error getting buffer");
604 if (
info ==
nullptr) {
605 pybind11_fail(
"FATAL UNEXPECTED SITUATION: tinfo->get_buffer() returned nullptr.");
608 if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE &&
info->readonly) {
611 set_error(PyExc_BufferError,
"Writable buffer requested for readonly storage");
620 for (
auto s :
info->shape) {
623 view->readonly =
static_cast<int>(
info->readonly);
624 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
625 view->format =
const_cast<char *
>(
info->format.c_str());
627 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
629 view->strides =
info->strides.data();
632 Py_INCREF(
view->obj);
643 heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer;
654 auto qualname =
name;
656 qualname = reinterpret_steal<object>(
657 PyUnicode_FromFormat(
"%U.%U", rec.
scope.attr(
"__qualname__").
ptr(),
name.ptr()));
669 const auto *full_name =
c_str(
670 #
if !defined(PYPY_VERSION)
675 char *tp_doc =
nullptr;
678 size_t size = std::strlen(rec.
doc) + 1;
679 #if PY_VERSION_HEX >= 0x030D0000
680 tp_doc = (
char *) PyMem_MALLOC(
size);
682 tp_doc = (
char *) PyObject_MALLOC(
size);
684 std::memcpy((
void *) tp_doc, rec.
doc,
size);
703 heap_type->ht_name =
name.release().ptr();
704 #ifdef PYBIND11_BUILTIN_QUALNAME
705 heap_type->ht_qualname = qualname.inc_ref().ptr();
708 auto *
type = &heap_type->ht_type;
709 type->tp_name = full_name;
710 type->tp_doc = tp_doc;
713 if (!bases.empty()) {
721 type->tp_as_number = &heap_type->as_number;
722 type->tp_as_sequence = &heap_type->as_sequence;
723 type->tp_as_mapping = &heap_type->as_mapping;
724 type->tp_as_async = &heap_type->as_async;
727 type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE;
729 type->tp_flags |= Py_TPFLAGS_BASETYPE;
744 if (PyType_Ready(
type) < 0) {
763 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.
auto with_instance_map(const void *ptr, const F &cb) -> decltype(cb(std::declval< instance_map & >()))
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)
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)
void try_translate_exceptions()
void raise_from(PyObject *type, const char *message)
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_multimap< const void *, instance * > instance_map
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)
void set_error(const handle &type, const char *message)
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.
auto with_internals(const F &cb) -> decltype(cb(get_internals()))
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 Wed Mar 19 2025 03:01:23