class.h 31 KB

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