mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Relands #103907 after it was reverted. This PR makes the new `ignore_hermetic_tls` argument of `check_pyobj` optional to avoid causing a compilation error in torchdistx Part of #91395 Pull Request resolved: https://github.com/pytorch/pytorch/pull/109039 Approved by: https://github.com/ezyang
20 lines
508 B
C++
20 lines
508 B
C++
#include <torch/csrc/utils/pyobject_preservation.h>
|
|
|
|
#include <structmember.h>
|
|
|
|
void clear_slots(PyTypeObject* type, PyObject* self) {
|
|
Py_ssize_t n = Py_SIZE(type);
|
|
PyMemberDef* mp = type->tp_members;
|
|
|
|
for (Py_ssize_t i = 0; i < n; i++, mp++) {
|
|
if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
|
|
char* addr = (char*)self + mp->offset;
|
|
PyObject* obj = *(PyObject**)addr;
|
|
if (obj != nullptr) {
|
|
*(PyObject**)addr = nullptr;
|
|
Py_DECREF(obj);
|
|
}
|
|
}
|
|
}
|
|
}
|