class.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  2. pybind11/detail/class.h: Python C API implementation details for py::class_
  3. Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #pragma once
  8. #include "../attr.h"
  9. #include "../options.h"
  10. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  11. PYBIND11_NAMESPACE_BEGIN(detail)
  12. #if !defined(PYPY_VERSION)
  13. # define PYBIND11_BUILTIN_QUALNAME
  14. # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
  15. #else
  16. // In PyPy, we still set __qualname__ so that we can produce reliable function type
  17. // signatures; in CPython this macro expands to nothing:
  18. # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) \
  19. setattr((PyObject *) obj, "__qualname__", nameobj)
  20. #endif
  21. inline std::string get_fully_qualified_tp_name(PyTypeObject *type) {
  22. #if !defined(PYPY_VERSION)
  23. return type->tp_name;
  24. #else
  25. auto module_name = handle((PyObject *) type).attr("__module__").cast<std::string>();
  26. if (module_name == PYBIND11_BUILTINS_MODULE)
  27. return type->tp_name;
  28. else
  29. return std::move(module_name) + "." + type->tp_name;
  30. #endif
  31. }
  32. inline PyTypeObject *type_incref(PyTypeObject *type) {
  33. Py_INCREF(type);
  34. return type;
  35. }
  36. #if !defined(PYPY_VERSION)
  37. /// `pybind11_static_property.__get__()`: Always pass the class instead of the instance.
  38. extern "C" inline PyObject *pybind11_static_get(PyObject *self, PyObject * /*ob*/, PyObject *cls) {
  39. return PyProperty_Type.tp_descr_get(self, cls, cls);
  40. }
  41. /// `pybind11_static_property.__set__()`: Just like the above `__get__()`.
  42. extern "C" inline int pybind11_static_set(PyObject *self, PyObject *obj, PyObject *value) {
  43. PyObject *cls = PyType_Check(obj) ? obj : (PyObject *) Py_TYPE(obj);
  44. return PyProperty_Type.tp_descr_set(self, cls, value);
  45. }
  46. // Forward declaration to use in `make_static_property_type()`
  47. inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type);
  48. /** A `static_property` is the same as a `property` but the `__get__()` and `__set__()`
  49. methods are modified to always use the object type instead of a concrete instance.
  50. Return value: New reference. */
  51. inline PyTypeObject *make_static_property_type() {
  52. constexpr auto *name = "pybind11_static_property";
  53. auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
  54. /* Danger zone: from now (and until PyType_Ready), make sure to
  55. issue no Python C API calls which could potentially invoke the
  56. garbage collector (the GC will call type_traverse(), which will in
  57. turn find the newly constructed type in an invalid state) */
  58. auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
  59. if (!heap_type) {
  60. pybind11_fail("make_static_property_type(): error allocating type!");
  61. }
  62. heap_type->ht_name = name_obj.inc_ref().ptr();
  63. # ifdef PYBIND11_BUILTIN_QUALNAME
  64. heap_type->ht_qualname = name_obj.inc_ref().ptr();
  65. # endif
  66. auto *type = &heap_type->ht_type;
  67. type->tp_name = name;
  68. type->tp_base = type_incref(&PyProperty_Type);
  69. type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
  70. type->tp_descr_get = pybind11_static_get;
  71. type->tp_descr_set = pybind11_static_set;
  72. # if PY_VERSION_HEX >= 0x030C0000
  73. // Since Python-3.12 property-derived types are required to
  74. // have dynamic attributes (to set `__doc__`)
  75. enable_dynamic_attributes(heap_type);
  76. # endif
  77. if (PyType_Ready(type) < 0) {
  78. pybind11_fail("make_static_property_type(): failure in PyType_Ready()!");
  79. }
  80. setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
  81. PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
  82. return type;
  83. }
  84. #else // PYPY
  85. /** PyPy has some issues with the above C API, so we evaluate Python code instead.
  86. This function will only be called once so performance isn't really a concern.
  87. Return value: New reference. */
  88. inline PyTypeObject *make_static_property_type() {
  89. auto d = dict();
  90. PyObject *result = PyRun_String(R"(\
  91. class pybind11_static_property(property):
  92. def __get__(self, obj, cls):
  93. return property.__get__(self, cls, cls)
  94. def __set__(self, obj, value):
  95. cls = obj if isinstance(obj, type) else type(obj)
  96. property.__set__(self, cls, value)
  97. )",
  98. Py_file_input,
  99. d.ptr(),
  100. d.ptr());
  101. if (result == nullptr)
  102. throw error_already_set();
  103. Py_DECREF(result);
  104. return (PyTypeObject *) d["pybind11_static_property"].cast<object>().release().ptr();
  105. }
  106. #endif // PYPY
  107. /** Types with static properties need to handle `Type.static_prop = x` in a specific way.
  108. By default, Python replaces the `static_property` itself, but for wrapped C++ types
  109. we need to call `static_property.__set__()` in order to propagate the new value to
  110. the underlying C++ data structure. */
  111. extern "C" inline int pybind11_meta_setattro(PyObject *obj, PyObject *name, PyObject *value) {
  112. // Use `_PyType_Lookup()` instead of `PyObject_GetAttr()` in order to get the raw
  113. // descriptor (`property`) instead of calling `tp_descr_get` (`property.__get__()`).
  114. PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name);
  115. // The following assignment combinations are possible:
  116. // 1. `Type.static_prop = value` --> descr_set: `Type.static_prop.__set__(value)`
  117. // 2. `Type.static_prop = other_static_prop` --> setattro: replace existing `static_prop`
  118. // 3. `Type.regular_attribute = value` --> setattro: regular attribute assignment
  119. auto *const static_prop = (PyObject *) get_internals().static_property_type;
  120. const auto call_descr_set = (descr != nullptr) && (value != nullptr)
  121. && (PyObject_IsInstance(descr, static_prop) != 0)
  122. && (PyObject_IsInstance(value, static_prop) == 0);
  123. if (call_descr_set) {
  124. // Call `static_property.__set__()` instead of replacing the `static_property`.
  125. #if !defined(PYPY_VERSION)
  126. return Py_TYPE(descr)->tp_descr_set(descr, obj, value);
  127. #else
  128. if (PyObject *result = PyObject_CallMethod(descr, "__set__", "OO", obj, value)) {
  129. Py_DECREF(result);
  130. return 0;
  131. } else {
  132. return -1;
  133. }
  134. #endif
  135. } else {
  136. // Replace existing attribute.
  137. return PyType_Type.tp_setattro(obj, name, value);
  138. }
  139. }
  140. /**
  141. * Python 3's PyInstanceMethod_Type hides itself via its tp_descr_get, which prevents aliasing
  142. * methods via cls.attr("m2") = cls.attr("m1"): instead the tp_descr_get returns a plain function,
  143. * when called on a class, or a PyMethod, when called on an instance. Override that behaviour here
  144. * to do a special case bypass for PyInstanceMethod_Types.
  145. */
  146. extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name) {
  147. PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name);
  148. if (descr && PyInstanceMethod_Check(descr)) {
  149. Py_INCREF(descr);
  150. return descr;
  151. }
  152. return PyType_Type.tp_getattro(obj, name);
  153. }
  154. /// metaclass `__call__` function that is used to create all pybind11 objects.
  155. extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs) {
  156. // use the default metaclass call to create/initialize the object
  157. PyObject *self = PyType_Type.tp_call(type, args, kwargs);
  158. if (self == nullptr) {
  159. return nullptr;
  160. }
  161. // Ensure that the base __init__ function(s) were called
  162. values_and_holders vhs(self);
  163. for (const auto &vh : vhs) {
  164. if (!vh.holder_constructed() && !vhs.is_redundant_value_and_holder(vh)) {
  165. PyErr_Format(PyExc_TypeError,
  166. "%.200s.__init__() must be called when overriding __init__",
  167. get_fully_qualified_tp_name(vh.type->type).c_str());
  168. Py_DECREF(self);
  169. return nullptr;
  170. }
  171. }
  172. return self;
  173. }
  174. /// Cleanup the type-info for a pybind11-registered type.
  175. extern "C" inline void pybind11_meta_dealloc(PyObject *obj) {
  176. auto *type = (PyTypeObject *) obj;
  177. auto &internals = get_internals();
  178. // A pybind11-registered type will:
  179. // 1) be found in internals.registered_types_py
  180. // 2) have exactly one associated `detail::type_info`
  181. auto found_type = internals.registered_types_py.find(type);
  182. if (found_type != internals.registered_types_py.end() && found_type->second.size() == 1
  183. && found_type->second[0]->type == type) {
  184. auto *tinfo = found_type->second[0];
  185. auto tindex = std::type_index(*tinfo->cpptype);
  186. internals.direct_conversions.erase(tindex);
  187. if (tinfo->module_local) {
  188. get_local_internals().registered_types_cpp.erase(tindex);
  189. } else {
  190. internals.registered_types_cpp.erase(tindex);
  191. }
  192. internals.registered_types_py.erase(tinfo->type);
  193. // Actually just `std::erase_if`, but that's only available in C++20
  194. auto &cache = internals.inactive_override_cache;
  195. for (auto it = cache.begin(), last = cache.end(); it != last;) {
  196. if (it->first == (PyObject *) tinfo->type) {
  197. it = cache.erase(it);
  198. } else {
  199. ++it;
  200. }
  201. }
  202. delete tinfo;
  203. }
  204. PyType_Type.tp_dealloc(obj);
  205. }
  206. /** This metaclass is assigned by default to all pybind11 types and is required in order
  207. for static properties to function correctly. Users may override this using `py::metaclass`.
  208. Return value: New reference. */
  209. inline PyTypeObject *make_default_metaclass() {
  210. constexpr auto *name = "pybind11_type";
  211. auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
  212. /* Danger zone: from now (and until PyType_Ready), make sure to
  213. issue no Python C API calls which could potentially invoke the
  214. garbage collector (the GC will call type_traverse(), which will in
  215. turn find the newly constructed type in an invalid state) */
  216. auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
  217. if (!heap_type) {
  218. pybind11_fail("make_default_metaclass(): error allocating metaclass!");
  219. }
  220. heap_type->ht_name = name_obj.inc_ref().ptr();
  221. #ifdef PYBIND11_BUILTIN_QUALNAME
  222. heap_type->ht_qualname = name_obj.inc_ref().ptr();
  223. #endif
  224. auto *type = &heap_type->ht_type;
  225. type->tp_name = name;
  226. type->tp_base = type_incref(&PyType_Type);
  227. type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
  228. type->tp_call = pybind11_meta_call;
  229. type->tp_setattro = pybind11_meta_setattro;
  230. type->tp_getattro = pybind11_meta_getattro;
  231. type->tp_dealloc = pybind11_meta_dealloc;
  232. if (PyType_Ready(type) < 0) {
  233. pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!");
  234. }
  235. setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
  236. PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
  237. return type;
  238. }
  239. /// For multiple inheritance types we need to recursively register/deregister base pointers for any
  240. /// base classes with pointers that are difference from the instance value pointer so that we can
  241. /// correctly recognize an offset base class pointer. This calls a function with any offset base
  242. /// ptrs.
  243. inline void traverse_offset_bases(void *valueptr,
  244. const detail::type_info *tinfo,
  245. instance *self,
  246. bool (*f)(void * /*parentptr*/, instance * /*self*/)) {
  247. for (handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
  248. if (auto *parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) {
  249. for (auto &c : parent_tinfo->implicit_casts) {
  250. if (c.first == tinfo->cpptype) {
  251. auto *parentptr = c.second(valueptr);
  252. if (parentptr != valueptr) {
  253. f(parentptr, self);
  254. }
  255. traverse_offset_bases(parentptr, parent_tinfo, self, f);
  256. break;
  257. }
  258. }
  259. }
  260. }
  261. }
  262. inline bool register_instance_impl(void *ptr, instance *self) {
  263. get_internals().registered_instances.emplace(ptr, self);
  264. return true; // unused, but gives the same signature as the deregister func
  265. }
  266. inline bool deregister_instance_impl(void *ptr, instance *self) {
  267. auto &registered_instances = get_internals().registered_instances;
  268. auto range = registered_instances.equal_range(ptr);
  269. for (auto it = range.first; it != range.second; ++it) {
  270. if (self == it->second) {
  271. registered_instances.erase(it);
  272. return true;
  273. }
  274. }
  275. return false;
  276. }
  277. inline void register_instance(instance *self, void *valptr, const type_info *tinfo) {
  278. register_instance_impl(valptr, self);
  279. if (!tinfo->simple_ancestors) {
  280. traverse_offset_bases(valptr, tinfo, self, register_instance_impl);
  281. }
  282. }
  283. inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo) {
  284. bool ret = deregister_instance_impl(valptr, self);
  285. if (!tinfo->simple_ancestors) {
  286. traverse_offset_bases(valptr, tinfo, self, deregister_instance_impl);
  287. }
  288. return ret;
  289. }
  290. /// Instance creation function for all pybind11 types. It allocates the internal instance layout
  291. /// for holding C++ objects and holders. Allocation is done lazily (the first time the instance is
  292. /// cast to a reference or pointer), and initialization is done by an `__init__` function.
  293. inline PyObject *make_new_instance(PyTypeObject *type) {
  294. #if defined(PYPY_VERSION)
  295. // PyPy gets tp_basicsize wrong (issue 2482) under multiple inheritance when the first
  296. // inherited object is a plain Python type (i.e. not derived from an extension type). Fix it.
  297. ssize_t instance_size = static_cast<ssize_t>(sizeof(instance));
  298. if (type->tp_basicsize < instance_size) {
  299. type->tp_basicsize = instance_size;
  300. }
  301. #endif
  302. PyObject *self = type->tp_alloc(type, 0);
  303. auto *inst = reinterpret_cast<instance *>(self);
  304. // Allocate the value/holder internals:
  305. inst->allocate_layout();
  306. return self;
  307. }
  308. /// Instance creation function for all pybind11 types. It only allocates space for the
  309. /// C++ object, but doesn't call the constructor -- an `__init__` function must do that.
  310. extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *) {
  311. return make_new_instance(type);
  312. }
  313. /// An `__init__` function constructs the C++ object. Users should provide at least one
  314. /// of these using `py::init` or directly with `.def(__init__, ...)`. Otherwise, the
  315. /// following default function will be used which simply throws an exception.
  316. extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject *) {
  317. PyTypeObject *type = Py_TYPE(self);
  318. std::string msg = get_fully_qualified_tp_name(type) + ": No constructor defined!";
  319. set_error(PyExc_TypeError, msg.c_str());
  320. return -1;
  321. }
  322. inline void add_patient(PyObject *nurse, PyObject *patient) {
  323. auto &internals = get_internals();
  324. auto *instance = reinterpret_cast<detail::instance *>(nurse);
  325. instance->has_patients = true;
  326. Py_INCREF(patient);
  327. internals.patients[nurse].push_back(patient);
  328. }
  329. inline void clear_patients(PyObject *self) {
  330. auto *instance = reinterpret_cast<detail::instance *>(self);
  331. auto &internals = get_internals();
  332. auto pos = internals.patients.find(self);
  333. assert(pos != internals.patients.end());
  334. // Clearing the patients can cause more Python code to run, which
  335. // can invalidate the iterator. Extract the vector of patients
  336. // from the unordered_map first.
  337. auto patients = std::move(pos->second);
  338. internals.patients.erase(pos);
  339. instance->has_patients = false;
  340. for (PyObject *&patient : patients) {
  341. Py_CLEAR(patient);
  342. }
  343. }
  344. /// Clears all internal data from the instance and removes it from registered instances in
  345. /// preparation for deallocation.
  346. inline void clear_instance(PyObject *self) {
  347. auto *instance = reinterpret_cast<detail::instance *>(self);
  348. // Deallocate any values/holders, if present:
  349. for (auto &v_h : values_and_holders(instance)) {
  350. if (v_h) {
  351. // We have to deregister before we call dealloc because, for virtual MI types, we still
  352. // need to be able to get the parent pointers.
  353. if (v_h.instance_registered()
  354. && !deregister_instance(instance, v_h.value_ptr(), v_h.type)) {
  355. pybind11_fail(
  356. "pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
  357. }
  358. if (instance->owned || v_h.holder_constructed()) {
  359. v_h.type->dealloc(v_h);
  360. }
  361. }
  362. }
  363. // Deallocate the value/holder layout internals:
  364. instance->deallocate_layout();
  365. if (instance->weakrefs) {
  366. PyObject_ClearWeakRefs(self);
  367. }
  368. PyObject **dict_ptr = _PyObject_GetDictPtr(self);
  369. if (dict_ptr) {
  370. Py_CLEAR(*dict_ptr);
  371. }
  372. if (instance->has_patients) {
  373. clear_patients(self);
  374. }
  375. }
  376. /// Instance destructor function for all pybind11 types. It calls `type_info.dealloc`
  377. /// to destroy the C++ object itself, while the rest is Python bookkeeping.
  378. extern "C" inline void pybind11_object_dealloc(PyObject *self) {
  379. auto *type = Py_TYPE(self);
  380. // If this is a GC tracked object, untrack it first
  381. // Note that the track call is implicitly done by the
  382. // default tp_alloc, which we never override.
  383. if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC) != 0) {
  384. PyObject_GC_UnTrack(self);
  385. }
  386. clear_instance(self);
  387. type->tp_free(self);
  388. #if PY_VERSION_HEX < 0x03080000
  389. // `type->tp_dealloc != pybind11_object_dealloc` means that we're being called
  390. // as part of a derived type's dealloc, in which case we're not allowed to decref
  391. // the type here. For cross-module compatibility, we shouldn't compare directly
  392. // with `pybind11_object_dealloc`, but with the common one stashed in internals.
  393. auto pybind11_object_type = (PyTypeObject *) get_internals().instance_base;
  394. if (type->tp_dealloc == pybind11_object_type->tp_dealloc)
  395. Py_DECREF(type);
  396. #else
  397. // This was not needed before Python 3.8 (Python issue 35810)
  398. // https://github.com/pybind/pybind11/issues/1946
  399. Py_DECREF(type);
  400. #endif
  401. }
  402. std::string error_string();
  403. /** Create the type which can be used as a common base for all classes. This is
  404. needed in order to satisfy Python's requirements for multiple inheritance.
  405. Return value: New reference. */
  406. inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
  407. constexpr auto *name = "pybind11_object";
  408. auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
  409. /* Danger zone: from now (and until PyType_Ready), make sure to
  410. issue no Python C API calls which could potentially invoke the
  411. garbage collector (the GC will call type_traverse(), which will in
  412. turn find the newly constructed type in an invalid state) */
  413. auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
  414. if (!heap_type) {
  415. pybind11_fail("make_object_base_type(): error allocating type!");
  416. }
  417. heap_type->ht_name = name_obj.inc_ref().ptr();
  418. #ifdef PYBIND11_BUILTIN_QUALNAME
  419. heap_type->ht_qualname = name_obj.inc_ref().ptr();
  420. #endif
  421. auto *type = &heap_type->ht_type;
  422. type->tp_name = name;
  423. type->tp_base = type_incref(&PyBaseObject_Type);
  424. type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
  425. type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
  426. type->tp_new = pybind11_object_new;
  427. type->tp_init = pybind11_object_init;
  428. type->tp_dealloc = pybind11_object_dealloc;
  429. /* Support weak references (needed for the keep_alive feature) */
  430. type->tp_weaklistoffset = offsetof(instance, weakrefs);
  431. if (PyType_Ready(type) < 0) {
  432. pybind11_fail("PyType_Ready failed in make_object_base_type(): " + error_string());
  433. }
  434. setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
  435. PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
  436. assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
  437. return (PyObject *) heap_type;
  438. }
  439. /// dynamic_attr: Allow the garbage collector to traverse the internal instance `__dict__`.
  440. extern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *arg) {
  441. #if PY_VERSION_HEX >= 0x030D0000
  442. PyObject_VisitManagedDict(self, visit, arg);
  443. #else
  444. PyObject *&dict = *_PyObject_GetDictPtr(self);
  445. Py_VISIT(dict);
  446. #endif
  447. // https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_traverse
  448. #if PY_VERSION_HEX >= 0x03090000
  449. Py_VISIT(Py_TYPE(self));
  450. #endif
  451. return 0;
  452. }
  453. /// dynamic_attr: Allow the GC to clear the dictionary.
  454. extern "C" inline int pybind11_clear(PyObject *self) {
  455. #if PY_VERSION_HEX >= 0x030D0000
  456. PyObject_ClearManagedDict(self);
  457. #else
  458. PyObject *&dict = *_PyObject_GetDictPtr(self);
  459. Py_CLEAR(dict);
  460. #endif
  461. return 0;
  462. }
  463. /// Give instances of this type a `__dict__` and opt into garbage collection.
  464. inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
  465. auto *type = &heap_type->ht_type;
  466. type->tp_flags |= Py_TPFLAGS_HAVE_GC;
  467. #if PY_VERSION_HEX < 0x030B0000
  468. type->tp_dictoffset = type->tp_basicsize; // place dict at the end
  469. type->tp_basicsize += (ssize_t) sizeof(PyObject *); // and allocate enough space for it
  470. #else
  471. type->tp_flags |= Py_TPFLAGS_MANAGED_DICT;
  472. #endif
  473. type->tp_traverse = pybind11_traverse;
  474. type->tp_clear = pybind11_clear;
  475. static PyGetSetDef getset[] = {{
  476. #if PY_VERSION_HEX < 0x03070000
  477. const_cast<char *>("__dict__"),
  478. #else
  479. "__dict__",
  480. #endif
  481. PyObject_GenericGetDict,
  482. PyObject_GenericSetDict,
  483. nullptr,
  484. nullptr},
  485. {nullptr, nullptr, nullptr, nullptr, nullptr}};
  486. type->tp_getset = getset;
  487. }
  488. /// buffer_protocol: Fill in the view as specified by flags.
  489. extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int flags) {
  490. // Look for a `get_buffer` implementation in this type's info or any bases (following MRO).
  491. type_info *tinfo = nullptr;
  492. for (auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
  493. tinfo = get_type_info((PyTypeObject *) type.ptr());
  494. if (tinfo && tinfo->get_buffer) {
  495. break;
  496. }
  497. }
  498. if (view == nullptr || !tinfo || !tinfo->get_buffer) {
  499. if (view) {
  500. view->obj = nullptr;
  501. }
  502. set_error(PyExc_BufferError, "pybind11_getbuffer(): Internal error");
  503. return -1;
  504. }
  505. std::memset(view, 0, sizeof(Py_buffer));
  506. buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data);
  507. if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->readonly) {
  508. delete info;
  509. // view->obj = nullptr; // Was just memset to 0, so not necessary
  510. set_error(PyExc_BufferError, "Writable buffer requested for readonly storage");
  511. return -1;
  512. }
  513. view->obj = obj;
  514. view->ndim = 1;
  515. view->internal = info;
  516. view->buf = info->ptr;
  517. view->itemsize = info->itemsize;
  518. view->len = view->itemsize;
  519. for (auto s : info->shape) {
  520. view->len *= s;
  521. }
  522. view->readonly = static_cast<int>(info->readonly);
  523. if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
  524. view->format = const_cast<char *>(info->format.c_str());
  525. }
  526. if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
  527. view->ndim = (int) info->ndim;
  528. view->strides = info->strides.data();
  529. view->shape = info->shape.data();
  530. }
  531. Py_INCREF(view->obj);
  532. return 0;
  533. }
  534. /// buffer_protocol: Release the resources of the buffer.
  535. extern "C" inline void pybind11_releasebuffer(PyObject *, Py_buffer *view) {
  536. delete (buffer_info *) view->internal;
  537. }
  538. /// Give this type a buffer interface.
  539. inline void enable_buffer_protocol(PyHeapTypeObject *heap_type) {
  540. heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer;
  541. heap_type->as_buffer.bf_getbuffer = pybind11_getbuffer;
  542. heap_type->as_buffer.bf_releasebuffer = pybind11_releasebuffer;
  543. }
  544. /** Create a brand new Python type according to the `type_record` specification.
  545. Return value: New reference. */
  546. inline PyObject *make_new_python_type(const type_record &rec) {
  547. auto name = reinterpret_steal<object>(PYBIND11_FROM_STRING(rec.name));
  548. auto qualname = name;
  549. if (rec.scope && !PyModule_Check(rec.scope.ptr()) && hasattr(rec.scope, "__qualname__")) {
  550. qualname = reinterpret_steal<object>(
  551. PyUnicode_FromFormat("%U.%U", rec.scope.attr("__qualname__").ptr(), name.ptr()));
  552. }
  553. object module_;
  554. if (rec.scope) {
  555. if (hasattr(rec.scope, "__module__")) {
  556. module_ = rec.scope.attr("__module__");
  557. } else if (hasattr(rec.scope, "__name__")) {
  558. module_ = rec.scope.attr("__name__");
  559. }
  560. }
  561. const auto *full_name = c_str(
  562. #if !defined(PYPY_VERSION)
  563. module_ ? str(module_).cast<std::string>() + "." + rec.name :
  564. #endif
  565. rec.name);
  566. char *tp_doc = nullptr;
  567. if (rec.doc && options::show_user_defined_docstrings()) {
  568. /* Allocate memory for docstring (using PyObject_MALLOC, since
  569. Python will free this later on) */
  570. size_t size = std::strlen(rec.doc) + 1;
  571. tp_doc = (char *) PyObject_MALLOC(size);
  572. std::memcpy((void *) tp_doc, rec.doc, size);
  573. }
  574. auto &internals = get_internals();
  575. auto bases = tuple(rec.bases);
  576. auto *base = (bases.empty()) ? internals.instance_base : bases[0].ptr();
  577. /* Danger zone: from now (and until PyType_Ready), make sure to
  578. issue no Python C API calls which could potentially invoke the
  579. garbage collector (the GC will call type_traverse(), which will in
  580. turn find the newly constructed type in an invalid state) */
  581. auto *metaclass
  582. = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr() : internals.default_metaclass;
  583. auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
  584. if (!heap_type) {
  585. pybind11_fail(std::string(rec.name) + ": Unable to create type object!");
  586. }
  587. heap_type->ht_name = name.release().ptr();
  588. #ifdef PYBIND11_BUILTIN_QUALNAME
  589. heap_type->ht_qualname = qualname.inc_ref().ptr();
  590. #endif
  591. auto *type = &heap_type->ht_type;
  592. type->tp_name = full_name;
  593. type->tp_doc = tp_doc;
  594. type->tp_base = type_incref((PyTypeObject *) base);
  595. type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
  596. if (!bases.empty()) {
  597. type->tp_bases = bases.release().ptr();
  598. }
  599. /* Don't inherit base __init__ */
  600. type->tp_init = pybind11_object_init;
  601. /* Supported protocols */
  602. type->tp_as_number = &heap_type->as_number;
  603. type->tp_as_sequence = &heap_type->as_sequence;
  604. type->tp_as_mapping = &heap_type->as_mapping;
  605. type->tp_as_async = &heap_type->as_async;
  606. /* Flags */
  607. type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE;
  608. if (!rec.is_final) {
  609. type->tp_flags |= Py_TPFLAGS_BASETYPE;
  610. }
  611. if (rec.dynamic_attr) {
  612. enable_dynamic_attributes(heap_type);
  613. }
  614. if (rec.buffer_protocol) {
  615. enable_buffer_protocol(heap_type);
  616. }
  617. if (rec.custom_type_setup_callback) {
  618. rec.custom_type_setup_callback(heap_type);
  619. }
  620. if (PyType_Ready(type) < 0) {
  621. pybind11_fail(std::string(rec.name) + ": PyType_Ready failed: " + error_string());
  622. }
  623. assert(!rec.dynamic_attr || PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
  624. /* Register type with the parent scope */
  625. if (rec.scope) {
  626. setattr(rec.scope, rec.name, (PyObject *) type);
  627. } else {
  628. Py_INCREF(type); // Keep it alive forever (reference leak)
  629. }
  630. if (module_) { // Needed by pydoc
  631. setattr((PyObject *) type, "__module__", module_);
  632. }
  633. PYBIND11_SET_OLDPY_QUALNAME(type, qualname);
  634. return (PyObject *) type;
  635. }
  636. PYBIND11_NAMESPACE_END(detail)
  637. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)