File indexing completed on 2026-01-07 10:14:59
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #pragma once
0011
0012 #include <pybind11/attr.h>
0013 #include <pybind11/options.h>
0014
0015 #include "exception_translation.h"
0016
0017 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0018 PYBIND11_NAMESPACE_BEGIN(detail)
0019
0020 #if !defined(PYPY_VERSION)
0021 # define PYBIND11_BUILTIN_QUALNAME
0022 # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
0023 #else
0024
0025
0026 # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) \
0027 setattr((PyObject *) obj, "__qualname__", nameobj)
0028 #endif
0029
0030 inline std::string get_fully_qualified_tp_name(PyTypeObject *type) {
0031 #if !defined(PYPY_VERSION)
0032 return type->tp_name;
0033 #else
0034 auto module_name = handle((PyObject *) type).attr("__module__").cast<std::string>();
0035 if (module_name == PYBIND11_BUILTINS_MODULE)
0036 return type->tp_name;
0037 else
0038 return std::move(module_name) + "." + type->tp_name;
0039 #endif
0040 }
0041
0042 inline PyTypeObject *type_incref(PyTypeObject *type) {
0043 Py_INCREF(type);
0044 return type;
0045 }
0046
0047 #if !defined(PYPY_VERSION)
0048
0049
0050 extern "C" inline PyObject *pybind11_static_get(PyObject *self, PyObject * , PyObject *cls) {
0051 return PyProperty_Type.tp_descr_get(self, cls, cls);
0052 }
0053
0054
0055 extern "C" inline int pybind11_static_set(PyObject *self, PyObject *obj, PyObject *value) {
0056 PyObject *cls = PyType_Check(obj) ? obj : (PyObject *) Py_TYPE(obj);
0057 return PyProperty_Type.tp_descr_set(self, cls, value);
0058 }
0059
0060
0061 inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type);
0062
0063
0064
0065
0066 inline PyTypeObject *make_static_property_type() {
0067 constexpr auto *name = "pybind11_static_property";
0068 auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
0069
0070
0071
0072
0073
0074 auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
0075 if (!heap_type) {
0076 pybind11_fail("make_static_property_type(): error allocating type!");
0077 }
0078
0079 heap_type->ht_name = name_obj.inc_ref().ptr();
0080 # ifdef PYBIND11_BUILTIN_QUALNAME
0081 heap_type->ht_qualname = name_obj.inc_ref().ptr();
0082 # endif
0083
0084 auto *type = &heap_type->ht_type;
0085 type->tp_name = name;
0086 type->tp_base = type_incref(&PyProperty_Type);
0087 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
0088 type->tp_descr_get = pybind11_static_get;
0089 type->tp_descr_set = pybind11_static_set;
0090
0091 # if PY_VERSION_HEX >= 0x030C0000
0092
0093
0094 enable_dynamic_attributes(heap_type);
0095 # endif
0096
0097 if (PyType_Ready(type) < 0) {
0098 pybind11_fail("make_static_property_type(): failure in PyType_Ready()!");
0099 }
0100
0101 setattr((PyObject *) type, "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
0102 PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
0103
0104 return type;
0105 }
0106
0107 #else
0108
0109
0110
0111
0112 inline PyTypeObject *make_static_property_type() {
0113 auto d = dict();
0114 PyObject *result = PyRun_String(R"(\
0115 class pybind11_static_property(property):
0116 def __get__(self, obj, cls):
0117 return property.__get__(self, cls, cls)
0118
0119 def __set__(self, obj, value):
0120 cls = obj if isinstance(obj, type) else type(obj)
0121 property.__set__(self, cls, value)
0122 )",
0123 Py_file_input,
0124 d.ptr(),
0125 d.ptr());
0126 if (result == nullptr)
0127 throw error_already_set();
0128 Py_DECREF(result);
0129 return (PyTypeObject *) d["pybind11_static_property"].cast<object>().release().ptr();
0130 }
0131
0132 #endif
0133
0134
0135
0136
0137
0138 extern "C" inline int pybind11_meta_setattro(PyObject *obj, PyObject *name, PyObject *value) {
0139
0140
0141 PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name);
0142
0143
0144
0145
0146
0147 auto *const static_prop = (PyObject *) get_internals().static_property_type;
0148 const auto call_descr_set = (descr != nullptr) && (value != nullptr)
0149 && (PyObject_IsInstance(descr, static_prop) != 0)
0150 && (PyObject_IsInstance(value, static_prop) == 0);
0151 if (call_descr_set) {
0152
0153 #if !defined(PYPY_VERSION)
0154 return Py_TYPE(descr)->tp_descr_set(descr, obj, value);
0155 #else
0156 if (PyObject *result = PyObject_CallMethod(descr, "__set__", "OO", obj, value)) {
0157 Py_DECREF(result);
0158 return 0;
0159 } else {
0160 return -1;
0161 }
0162 #endif
0163 } else {
0164
0165 return PyType_Type.tp_setattro(obj, name, value);
0166 }
0167 }
0168
0169
0170
0171
0172
0173
0174
0175 extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name) {
0176 PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name);
0177 if (descr && PyInstanceMethod_Check(descr)) {
0178 Py_INCREF(descr);
0179 return descr;
0180 }
0181 return PyType_Type.tp_getattro(obj, name);
0182 }
0183
0184
0185 extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs) {
0186
0187
0188 PyObject *self = PyType_Type.tp_call(type, args, kwargs);
0189 if (self == nullptr) {
0190 return nullptr;
0191 }
0192
0193
0194 values_and_holders vhs(self);
0195 for (const auto &vh : vhs) {
0196 if (!vh.holder_constructed() && !vhs.is_redundant_value_and_holder(vh)) {
0197 PyErr_Format(PyExc_TypeError,
0198 "%.200s.__init__() must be called when overriding __init__",
0199 get_fully_qualified_tp_name(vh.type->type).c_str());
0200 Py_DECREF(self);
0201 return nullptr;
0202 }
0203 }
0204
0205 return self;
0206 }
0207
0208
0209 extern "C" inline void pybind11_meta_dealloc(PyObject *obj) {
0210 with_internals([obj](internals &internals) {
0211 auto *type = (PyTypeObject *) obj;
0212
0213
0214
0215
0216 auto found_type = internals.registered_types_py.find(type);
0217 if (found_type != internals.registered_types_py.end() && found_type->second.size() == 1
0218 && found_type->second[0]->type == type) {
0219
0220 auto *tinfo = found_type->second[0];
0221 auto tindex = std::type_index(*tinfo->cpptype);
0222 internals.direct_conversions.erase(tindex);
0223
0224 if (tinfo->module_local) {
0225 get_local_internals().registered_types_cpp.erase(tindex);
0226 } else {
0227 internals.registered_types_cpp.erase(tindex);
0228 }
0229 internals.registered_types_py.erase(tinfo->type);
0230
0231
0232 auto &cache = internals.inactive_override_cache;
0233 for (auto it = cache.begin(), last = cache.end(); it != last;) {
0234 if (it->first == (PyObject *) tinfo->type) {
0235 it = cache.erase(it);
0236 } else {
0237 ++it;
0238 }
0239 }
0240
0241 delete tinfo;
0242 }
0243 });
0244
0245 PyType_Type.tp_dealloc(obj);
0246 }
0247
0248
0249
0250
0251 inline PyTypeObject *make_default_metaclass() {
0252 constexpr auto *name = "pybind11_type";
0253 auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
0254
0255
0256
0257
0258
0259 auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
0260 if (!heap_type) {
0261 pybind11_fail("make_default_metaclass(): error allocating metaclass!");
0262 }
0263
0264 heap_type->ht_name = name_obj.inc_ref().ptr();
0265 #ifdef PYBIND11_BUILTIN_QUALNAME
0266 heap_type->ht_qualname = name_obj.inc_ref().ptr();
0267 #endif
0268
0269 auto *type = &heap_type->ht_type;
0270 type->tp_name = name;
0271 type->tp_base = type_incref(&PyType_Type);
0272 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
0273
0274 type->tp_call = pybind11_meta_call;
0275
0276 type->tp_setattro = pybind11_meta_setattro;
0277 type->tp_getattro = pybind11_meta_getattro;
0278
0279 type->tp_dealloc = pybind11_meta_dealloc;
0280
0281 if (PyType_Ready(type) < 0) {
0282 pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!");
0283 }
0284
0285 setattr((PyObject *) type, "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
0286 PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
0287
0288 return type;
0289 }
0290
0291
0292
0293
0294
0295 inline void traverse_offset_bases(void *valueptr,
0296 const detail::type_info *tinfo,
0297 instance *self,
0298 bool (*f)(void * , instance * )) {
0299 for (handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
0300 if (auto *parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) {
0301 for (auto &c : parent_tinfo->implicit_casts) {
0302 if (c.first == tinfo->cpptype) {
0303 auto *parentptr = c.second(valueptr);
0304 if (parentptr != valueptr) {
0305 f(parentptr, self);
0306 }
0307 traverse_offset_bases(parentptr, parent_tinfo, self, f);
0308 break;
0309 }
0310 }
0311 }
0312 }
0313 }
0314
0315 #ifdef Py_GIL_DISABLED
0316 inline void enable_try_inc_ref(PyObject *obj) {
0317
0318
0319 if (_Py_IsImmortal(obj)) {
0320 return;
0321 }
0322 for (;;) {
0323 Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&obj->ob_ref_shared);
0324 if ((shared & _Py_REF_SHARED_FLAG_MASK) != 0) {
0325
0326 return;
0327 }
0328 if (_Py_atomic_compare_exchange_ssize(
0329 &obj->ob_ref_shared, &shared, shared | _Py_REF_MAYBE_WEAKREF)) {
0330 return;
0331 }
0332 }
0333 }
0334 #endif
0335
0336 inline bool register_instance_impl(void *ptr, instance *self) {
0337 #ifdef Py_GIL_DISABLED
0338 enable_try_inc_ref(reinterpret_cast<PyObject *>(self));
0339 #endif
0340 with_instance_map(ptr, [&](instance_map &instances) { instances.emplace(ptr, self); });
0341 return true;
0342 }
0343 inline bool deregister_instance_impl(void *ptr, instance *self) {
0344 return with_instance_map(ptr, [&](instance_map &instances) {
0345 auto range = instances.equal_range(ptr);
0346 for (auto it = range.first; it != range.second; ++it) {
0347 if (self == it->second) {
0348 instances.erase(it);
0349 return true;
0350 }
0351 }
0352 return false;
0353 });
0354 }
0355
0356 inline void register_instance(instance *self, void *valptr, const type_info *tinfo) {
0357 register_instance_impl(valptr, self);
0358 if (!tinfo->simple_ancestors) {
0359 traverse_offset_bases(valptr, tinfo, self, register_instance_impl);
0360 }
0361 }
0362
0363 inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo) {
0364 bool ret = deregister_instance_impl(valptr, self);
0365 if (!tinfo->simple_ancestors) {
0366 traverse_offset_bases(valptr, tinfo, self, deregister_instance_impl);
0367 }
0368 return ret;
0369 }
0370
0371
0372
0373
0374 inline PyObject *make_new_instance(PyTypeObject *type) {
0375 #if defined(PYPY_VERSION)
0376
0377
0378 ssize_t instance_size = static_cast<ssize_t>(sizeof(instance));
0379 if (type->tp_basicsize < instance_size) {
0380 type->tp_basicsize = instance_size;
0381 }
0382 #endif
0383 PyObject *self = type->tp_alloc(type, 0);
0384 auto *inst = reinterpret_cast<instance *>(self);
0385
0386 inst->allocate_layout();
0387
0388 return self;
0389 }
0390
0391
0392
0393 extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *) {
0394 return make_new_instance(type);
0395 }
0396
0397
0398
0399
0400 extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject *) {
0401 PyTypeObject *type = Py_TYPE(self);
0402 std::string msg = get_fully_qualified_tp_name(type) + ": No constructor defined!";
0403 set_error(PyExc_TypeError, msg.c_str());
0404 return -1;
0405 }
0406
0407 inline void add_patient(PyObject *nurse, PyObject *patient) {
0408 auto *instance = reinterpret_cast<detail::instance *>(nurse);
0409 instance->has_patients = true;
0410 Py_INCREF(patient);
0411
0412 with_internals([&](internals &internals) { internals.patients[nurse].push_back(patient); });
0413 }
0414
0415 inline void clear_patients(PyObject *self) {
0416 auto *instance = reinterpret_cast<detail::instance *>(self);
0417 std::vector<PyObject *> patients;
0418
0419 with_internals([&](internals &internals) {
0420 auto pos = internals.patients.find(self);
0421
0422 if (pos == internals.patients.end()) {
0423 pybind11_fail(
0424 "FATAL: Internal consistency check failed: Invalid clear_patients() call.");
0425 }
0426
0427
0428
0429
0430 patients = std::move(pos->second);
0431 internals.patients.erase(pos);
0432 });
0433
0434 instance->has_patients = false;
0435 for (PyObject *&patient : patients) {
0436 Py_CLEAR(patient);
0437 }
0438 }
0439
0440
0441
0442 inline void clear_instance(PyObject *self) {
0443 auto *instance = reinterpret_cast<detail::instance *>(self);
0444
0445
0446 for (auto &v_h : values_and_holders(instance)) {
0447 if (v_h) {
0448
0449
0450
0451 if (v_h.instance_registered()
0452 && !deregister_instance(instance, v_h.value_ptr(), v_h.type)) {
0453 pybind11_fail(
0454 "pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
0455 }
0456
0457 if (instance->owned || v_h.holder_constructed()) {
0458 v_h.type->dealloc(v_h);
0459 }
0460 } else if (v_h.holder_constructed()) {
0461 v_h.type->dealloc(v_h);
0462 }
0463 }
0464
0465 instance->deallocate_layout();
0466
0467 if (instance->weakrefs) {
0468 PyObject_ClearWeakRefs(self);
0469 }
0470
0471 PyObject **dict_ptr = _PyObject_GetDictPtr(self);
0472 if (dict_ptr) {
0473 Py_CLEAR(*dict_ptr);
0474 }
0475
0476 if (instance->has_patients) {
0477 clear_patients(self);
0478 }
0479 }
0480
0481
0482
0483 extern "C" inline void pybind11_object_dealloc(PyObject *self) {
0484 auto *type = Py_TYPE(self);
0485
0486
0487
0488
0489 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC) != 0) {
0490 PyObject_GC_UnTrack(self);
0491 }
0492
0493 clear_instance(self);
0494
0495 type->tp_free(self);
0496
0497
0498
0499 Py_DECREF(type);
0500 }
0501
0502 PYBIND11_WARNING_PUSH
0503 PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls")
0504
0505 std::string error_string();
0506
0507 PYBIND11_WARNING_POP
0508
0509
0510
0511
0512 inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
0513 constexpr auto *name = "pybind11_object";
0514 auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
0515
0516
0517
0518
0519
0520 auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
0521 if (!heap_type) {
0522 pybind11_fail("make_object_base_type(): error allocating type!");
0523 }
0524
0525 heap_type->ht_name = name_obj.inc_ref().ptr();
0526 #ifdef PYBIND11_BUILTIN_QUALNAME
0527 heap_type->ht_qualname = name_obj.inc_ref().ptr();
0528 #endif
0529
0530 auto *type = &heap_type->ht_type;
0531 type->tp_name = name;
0532 type->tp_base = type_incref(&PyBaseObject_Type);
0533 type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
0534 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
0535
0536 type->tp_new = pybind11_object_new;
0537 type->tp_init = pybind11_object_init;
0538 type->tp_dealloc = pybind11_object_dealloc;
0539
0540
0541 type->tp_weaklistoffset = offsetof(instance, weakrefs);
0542
0543 if (PyType_Ready(type) < 0) {
0544 pybind11_fail("PyType_Ready failed in make_object_base_type(): " + error_string());
0545 }
0546
0547 setattr((PyObject *) type, "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
0548 PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
0549
0550 assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
0551 return (PyObject *) heap_type;
0552 }
0553
0554
0555 extern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *arg) {
0556 #if PY_VERSION_HEX >= 0x030D0000
0557 PyObject_VisitManagedDict(self, visit, arg);
0558 #else
0559 PyObject *&dict = *_PyObject_GetDictPtr(self);
0560 Py_VISIT(dict);
0561 #endif
0562
0563 #if PY_VERSION_HEX >= 0x03090000
0564 Py_VISIT(Py_TYPE(self));
0565 #endif
0566 return 0;
0567 }
0568
0569
0570 extern "C" inline int pybind11_clear(PyObject *self) {
0571 #if PY_VERSION_HEX >= 0x030D0000
0572 PyObject_ClearManagedDict(self);
0573 #else
0574 PyObject *&dict = *_PyObject_GetDictPtr(self);
0575 Py_CLEAR(dict);
0576 #endif
0577 return 0;
0578 }
0579
0580
0581 inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
0582 auto *type = &heap_type->ht_type;
0583 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
0584 #ifdef PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET
0585 type->tp_dictoffset = type->tp_basicsize;
0586 type->tp_basicsize += (ssize_t) sizeof(PyObject *);
0587 #else
0588 type->tp_flags |= Py_TPFLAGS_MANAGED_DICT;
0589 #endif
0590 type->tp_traverse = pybind11_traverse;
0591 type->tp_clear = pybind11_clear;
0592
0593 static PyGetSetDef getset[]
0594 = {{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, nullptr, nullptr},
0595 {nullptr, nullptr, nullptr, nullptr, nullptr}};
0596 type->tp_getset = getset;
0597 }
0598
0599
0600 extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int flags) {
0601
0602 type_info *tinfo = nullptr;
0603 for (auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
0604 tinfo = get_type_info((PyTypeObject *) type.ptr());
0605 if (tinfo && tinfo->get_buffer) {
0606 break;
0607 }
0608 }
0609 if (view == nullptr || !tinfo || !tinfo->get_buffer) {
0610 if (view) {
0611 view->obj = nullptr;
0612 }
0613 set_error(PyExc_BufferError, "pybind11_getbuffer(): Internal error");
0614 return -1;
0615 }
0616 std::memset(view, 0, sizeof(Py_buffer));
0617 std::unique_ptr<buffer_info> info = nullptr;
0618 try {
0619 info.reset(tinfo->get_buffer(obj, tinfo->get_buffer_data));
0620 } catch (...) {
0621 try_translate_exceptions();
0622 raise_from(PyExc_BufferError, "Error getting buffer");
0623 return -1;
0624 }
0625 if (info == nullptr) {
0626 pybind11_fail("FATAL UNEXPECTED SITUATION: tinfo->get_buffer() returned nullptr.");
0627 }
0628
0629 if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->readonly) {
0630
0631 set_error(PyExc_BufferError, "Writable buffer requested for readonly storage");
0632 return -1;
0633 }
0634
0635
0636
0637 view->itemsize = info->itemsize;
0638 view->len = view->itemsize;
0639 for (auto s : info->shape) {
0640 view->len *= s;
0641 }
0642 view->ndim = static_cast<int>(info->ndim);
0643 view->shape = info->shape.data();
0644 view->strides = info->strides.data();
0645 view->readonly = static_cast<int>(info->readonly);
0646 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
0647 view->format = const_cast<char *>(info->format.c_str());
0648 }
0649
0650
0651 if ((flags & PyBUF_C_CONTIGUOUS) == PyBUF_C_CONTIGUOUS) {
0652 if (PyBuffer_IsContiguous(view, 'C') == 0) {
0653 std::memset(view, 0, sizeof(Py_buffer));
0654 set_error(PyExc_BufferError,
0655 "C-contiguous buffer requested for discontiguous storage");
0656 return -1;
0657 }
0658 } else if ((flags & PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS) {
0659 if (PyBuffer_IsContiguous(view, 'F') == 0) {
0660 std::memset(view, 0, sizeof(Py_buffer));
0661 set_error(PyExc_BufferError,
0662 "Fortran-contiguous buffer requested for discontiguous storage");
0663 return -1;
0664 }
0665 } else if ((flags & PyBUF_ANY_CONTIGUOUS) == PyBUF_ANY_CONTIGUOUS) {
0666 if (PyBuffer_IsContiguous(view, 'A') == 0) {
0667 std::memset(view, 0, sizeof(Py_buffer));
0668 set_error(PyExc_BufferError, "Contiguous buffer requested for discontiguous storage");
0669 return -1;
0670 }
0671
0672 } else if ((flags & PyBUF_STRIDES) != PyBUF_STRIDES) {
0673
0674
0675 if (PyBuffer_IsContiguous(view, 'C') == 0) {
0676 std::memset(view, 0, sizeof(Py_buffer));
0677 set_error(PyExc_BufferError,
0678 "C-contiguous buffer requested for discontiguous storage");
0679 return -1;
0680 }
0681
0682 view->strides = nullptr;
0683
0684
0685 if ((flags & PyBUF_ND) != PyBUF_ND) {
0686 view->shape = nullptr;
0687 view->ndim = 0;
0688 }
0689 }
0690
0691
0692
0693 view->buf = info->ptr;
0694 view->internal = info.release();
0695 view->obj = obj;
0696 Py_INCREF(view->obj);
0697 return 0;
0698 }
0699
0700
0701 extern "C" inline void pybind11_releasebuffer(PyObject *, Py_buffer *view) {
0702 delete (buffer_info *) view->internal;
0703 }
0704
0705
0706 inline void enable_buffer_protocol(PyHeapTypeObject *heap_type) {
0707 heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer;
0708
0709 heap_type->as_buffer.bf_getbuffer = pybind11_getbuffer;
0710 heap_type->as_buffer.bf_releasebuffer = pybind11_releasebuffer;
0711 }
0712
0713
0714
0715 inline PyObject *make_new_python_type(const type_record &rec) {
0716 auto name = reinterpret_steal<object>(PYBIND11_FROM_STRING(rec.name));
0717
0718 auto qualname = name;
0719 if (rec.scope && !PyModule_Check(rec.scope.ptr()) && hasattr(rec.scope, "__qualname__")) {
0720 qualname = reinterpret_steal<object>(
0721 PyUnicode_FromFormat("%U.%U", rec.scope.attr("__qualname__").ptr(), name.ptr()));
0722 }
0723
0724 object module_ = get_module_name_if_available(rec.scope);
0725 const auto *full_name = c_str(
0726 #if !defined(PYPY_VERSION)
0727 module_ ? str(module_).cast<std::string>() + "." + rec.name :
0728 #endif
0729 rec.name);
0730
0731 char *tp_doc = nullptr;
0732 if (rec.doc && options::show_user_defined_docstrings()) {
0733
0734 size_t size = std::strlen(rec.doc) + 1;
0735 #if PY_VERSION_HEX >= 0x030D0000
0736 tp_doc = (char *) PyMem_MALLOC(size);
0737 #else
0738 tp_doc = (char *) PyObject_MALLOC(size);
0739 #endif
0740 std::memcpy((void *) tp_doc, rec.doc, size);
0741 }
0742
0743 auto &internals = get_internals();
0744 auto bases = tuple(rec.bases);
0745 auto *base = (bases.empty()) ? internals.instance_base : bases[0].ptr();
0746
0747
0748
0749
0750
0751 auto *metaclass
0752 = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr() : internals.default_metaclass;
0753
0754 auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
0755 if (!heap_type) {
0756 pybind11_fail(std::string(rec.name) + ": Unable to create type object!");
0757 }
0758
0759 heap_type->ht_name = name.release().ptr();
0760 #ifdef PYBIND11_BUILTIN_QUALNAME
0761 heap_type->ht_qualname = qualname.inc_ref().ptr();
0762 #endif
0763
0764 auto *type = &heap_type->ht_type;
0765 type->tp_name = full_name;
0766 type->tp_doc = tp_doc;
0767 type->tp_base = type_incref((PyTypeObject *) base);
0768 type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
0769 if (!bases.empty()) {
0770 type->tp_bases = bases.release().ptr();
0771 }
0772
0773
0774 type->tp_init = pybind11_object_init;
0775
0776
0777 type->tp_as_number = &heap_type->as_number;
0778 type->tp_as_sequence = &heap_type->as_sequence;
0779 type->tp_as_mapping = &heap_type->as_mapping;
0780 type->tp_as_async = &heap_type->as_async;
0781
0782
0783 type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE;
0784 if (!rec.is_final) {
0785 type->tp_flags |= Py_TPFLAGS_BASETYPE;
0786 }
0787
0788 if (rec.dynamic_attr) {
0789 enable_dynamic_attributes(heap_type);
0790 }
0791
0792 if (rec.buffer_protocol) {
0793 enable_buffer_protocol(heap_type);
0794 }
0795
0796 if (rec.custom_type_setup_callback) {
0797 rec.custom_type_setup_callback(heap_type);
0798 }
0799
0800 if (PyType_Ready(type) < 0) {
0801 pybind11_fail(std::string(rec.name) + ": PyType_Ready failed: " + error_string());
0802 }
0803
0804 assert(!rec.dynamic_attr || PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
0805
0806
0807 if (rec.scope) {
0808 setattr(rec.scope, rec.name, (PyObject *) type);
0809 } else {
0810 Py_INCREF(type);
0811 }
0812
0813 if (module_) {
0814 setattr((PyObject *) type, "__module__", module_);
0815 }
0816
0817 PYBIND11_SET_OLDPY_QUALNAME(type, qualname);
0818
0819 return (PyObject *) type;
0820 }
0821
0822 PYBIND11_NAMESPACE_END(detail)
0823 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)