class.h 31 KB

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