xref: /aosp_15_r20/external/pytorch/torch/csrc/utils/pyobject_preservation.cpp (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #include <torch/csrc/utils/pyobject_preservation.h>
2 
3 #include <structmember.h>
4 
clear_slots(PyTypeObject * type,PyObject * self)5 void clear_slots(PyTypeObject* type, PyObject* self) {
6   Py_ssize_t n = Py_SIZE(type);
7   PyMemberDef* mp = type->tp_members;
8 
9   for (Py_ssize_t i = 0; i < n; i++, mp++) {
10     if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
11       char* addr = (char*)self + mp->offset;
12       PyObject* obj = *(PyObject**)addr;
13       if (obj != nullptr) {
14         *(PyObject**)addr = nullptr;
15         Py_DECREF(obj);
16       }
17     }
18   }
19 }
20