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 PY_VERSION_HEX >= 0x030C0000
95 if (PyType_Ready(
type) < 0) {
96 pybind11_fail(
"make_static_property_type(): failure in PyType_Ready()!");
99 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
112 PyObject *
result = PyRun_String(R
"(\
113 class pybind11_static_property(property):
114 def __get__(self, obj, cls):
115 return property.__get__(self, cls, cls)
117 def __set__(self, obj, value):
118 cls = obj if isinstance(obj, type) else type(obj)
119 property.__set__(self, cls, value)
127 return (PyTypeObject *)
d[
"pybind11_static_property"].cast<
object>().
release().ptr();
139 PyObject *
descr = _PyType_Lookup((PyTypeObject *) obj,
name);
146 const auto call_descr_set = (
descr !=
nullptr) && (
value !=
nullptr)
147 && (PyObject_IsInstance(
descr, static_prop) != 0)
148 && (PyObject_IsInstance(
value, static_prop) == 0);
149 if (call_descr_set) {
151 #if !defined(PYPY_VERSION)
154 if (PyObject *
result = PyObject_CallMethod(
descr,
"__set__",
"OO", obj,
value)) {
163 return PyType_Type.tp_setattro(obj,
name,
value);
174 PyObject *
descr = _PyType_Lookup((PyTypeObject *) obj,
name);
179 return PyType_Type.tp_getattro(obj,
name);
187 if (
self ==
nullptr) {
193 for (
const auto &vh : vhs) {
194 if (!vh.holder_constructed() && !vhs.is_redundant_value_and_holder(vh)) {
195 PyErr_Format(PyExc_TypeError,
196 "%.200s.__init__() must be called when overriding __init__",
209 auto *
type = (PyTypeObject *) obj;
216 && found_type->second[0]->type ==
type) {
218 auto *tinfo = found_type->second[0];
219 auto tindex = std::type_index(*tinfo->cpptype);
222 if (tinfo->module_local) {
231 for (
auto it = cache.begin(),
last = cache.end(); it !=
last;) {
232 if (it->first == (PyObject *) tinfo->type) {
233 it = cache.erase(it);
243 PyType_Type.tp_dealloc(obj);
250 constexpr
auto *
name =
"pybind11_type";
257 auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
259 pybind11_fail(
"make_default_metaclass(): error allocating metaclass!");
262 heap_type->ht_name = name_obj.inc_ref().ptr();
263 #ifdef PYBIND11_BUILTIN_QUALNAME
264 heap_type->ht_qualname = name_obj.inc_ref().ptr();
267 auto *
type = &heap_type->ht_type;
270 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
279 if (PyType_Ready(
type) < 0) {
280 pybind11_fail(
"make_default_metaclass(): failure in PyType_Ready()!");
283 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
294 const detail::type_info *tinfo,
297 for (
handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
298 if (
auto *parent_tinfo =
get_type_info((PyTypeObject *)
h.ptr())) {
299 for (
auto &
c : parent_tinfo->implicit_casts) {
300 if (
c.first == tinfo->cpptype) {
301 auto *parentptr =
c.second(valueptr);
302 if (parentptr != valueptr) {
319 auto range = instances.equal_range(ptr);
320 for (
auto it =
range.first; it !=
range.second; ++it) {
321 if (self == it->second) {
349 #if defined(PYPY_VERSION)
353 if (
type->tp_basicsize < instance_size) {
354 type->tp_basicsize = instance_size;
357 PyObject *
self =
type->tp_alloc(
type, 0);
360 inst->allocate_layout();
375 PyTypeObject *
type = Py_TYPE(
self);
382 auto *
instance =
reinterpret_cast<detail::instance *
>(nurse);
390 auto *
instance =
reinterpret_cast<detail::instance *
>(
self);
391 std::vector<PyObject *> patients;
398 "FATAL: Internal consistency check failed: Invalid clear_patients() call.");
404 patients = std::move(
pos->second);
409 for (PyObject *&patient : patients) {
417 auto *
instance =
reinterpret_cast<detail::instance *
>(
self);
425 if (v_h.instance_registered()
428 "pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
432 v_h.type->dealloc(v_h);
440 PyObject_ClearWeakRefs(
self);
443 PyObject **dict_ptr = _PyObject_GetDictPtr(
self);
456 auto *
type = Py_TYPE(
self);
461 if (PyType_HasFeature(
type, Py_TPFLAGS_HAVE_GC) != 0) {
462 PyObject_GC_UnTrack(
self);
469 #if PY_VERSION_HEX < 0x03080000
475 if (
type->tp_dealloc == pybind11_object_type->tp_dealloc)
490 constexpr
auto *
name =
"pybind11_object";
499 pybind11_fail(
"make_object_base_type(): error allocating type!");
502 heap_type->ht_name = name_obj.inc_ref().ptr();
503 #ifdef PYBIND11_BUILTIN_QUALNAME
504 heap_type->ht_qualname = name_obj.inc_ref().ptr();
507 auto *
type = &heap_type->ht_type;
511 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
520 if (PyType_Ready(
type) < 0) {
524 setattr((PyObject *)
type,
"__module__",
str(
"pybind11_builtins"));
527 assert(!PyType_HasFeature(
type, Py_TPFLAGS_HAVE_GC));
528 return (PyObject *) heap_type;
533 #if PY_VERSION_HEX >= 0x030D0000
534 PyObject_VisitManagedDict(
self, visit,
arg);
536 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
540 #if PY_VERSION_HEX >= 0x03090000
541 Py_VISIT(Py_TYPE(
self));
548 #if PY_VERSION_HEX >= 0x030D0000
549 PyObject_ClearManagedDict(
self);
551 PyObject *&
dict = *_PyObject_GetDictPtr(
self);
559 auto *
type = &heap_type->ht_type;
560 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
561 #if PY_VERSION_HEX < 0x030B0000
562 type->tp_dictoffset =
type->tp_basicsize;
563 type->tp_basicsize += (
ssize_t)
sizeof(PyObject *);
565 type->tp_flags |= Py_TPFLAGS_MANAGED_DICT;
570 static PyGetSetDef getset[]
571 = {{
"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict,
nullptr,
nullptr},
572 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}};
573 type->tp_getset = getset;
580 for (
auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
590 set_error(PyExc_BufferError,
"pybind11_getbuffer(): Internal error");
593 std::memset(
view, 0,
sizeof(Py_buffer));
595 if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE &&
info->readonly) {
598 set_error(PyExc_BufferError,
"Writable buffer requested for readonly storage");
607 for (
auto s :
info->shape) {
610 view->readonly =
static_cast<int>(
info->readonly);
611 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
612 view->format =
const_cast<char *
>(
info->format.c_str());
614 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
616 view->strides =
info->strides.data();
619 Py_INCREF(
view->obj);
630 heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer;
641 auto qualname =
name;
643 qualname = reinterpret_steal<object>(
644 PyUnicode_FromFormat(
"%U.%U", rec.
scope.attr(
"__qualname__").
ptr(),
name.ptr()));
656 const auto *full_name =
c_str(
657 #
if !defined(PYPY_VERSION)
662 char *tp_doc =
nullptr;
665 size_t size = std::strlen(rec.
doc) + 1;
666 #if PY_VERSION_HEX >= 0x030D0000
667 tp_doc = (
char *) PyMem_MALLOC(
size);
669 tp_doc = (
char *) PyObject_MALLOC(
size);
671 std::memcpy((
void *) tp_doc, rec.
doc,
size);
690 heap_type->ht_name =
name.release().ptr();
691 #ifdef PYBIND11_BUILTIN_QUALNAME
692 heap_type->ht_qualname = qualname.inc_ref().ptr();
695 auto *
type = &heap_type->ht_type;
696 type->tp_name = full_name;
697 type->tp_doc = tp_doc;
700 if (!bases.empty()) {
708 type->tp_as_number = &heap_type->as_number;
709 type->tp_as_sequence = &heap_type->as_sequence;
710 type->tp_as_mapping = &heap_type->as_mapping;
711 type->tp_as_async = &heap_type->as_async;
714 type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE;
716 type->tp_flags |= Py_TPFLAGS_BASETYPE;
731 if (PyType_Ready(
type) < 0) {
750 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)
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 Sat Nov 16 2024 04:01:58