mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
Same treatment as many other objects such as https://github.com/pytorch/pytorch/blob/main/torch/csrc/autograd/python_hook.cpp#L99
This one can outlive the python runtime due to structs like: 2f35715f0d/torch/csrc/autograd/python_cpp_function.cpp (L232)
With the pybind patch and this one, the 3.12 build at https://github.com/pytorch/pytorch/pull/106083 stops segfaulting and runs test_autograd.py just fine.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/106334
Approved by: https://github.com/ezyang
29 lines
592 B
C++
29 lines
592 B
C++
#include <c10/macros/Macros.h>
|
|
#include <torch/csrc/utils/object_ptr.h>
|
|
|
|
#include <torch/csrc/python_headers.h>
|
|
|
|
template <>
|
|
void THPPointer<PyObject>::free() {
|
|
if (ptr && C10_LIKELY(Py_IsInitialized()))
|
|
Py_DECREF(ptr);
|
|
}
|
|
|
|
template class THPPointer<PyObject>;
|
|
|
|
template <>
|
|
void THPPointer<PyCodeObject>::free() {
|
|
if (ptr && C10_LIKELY(Py_IsInitialized()))
|
|
Py_DECREF(ptr);
|
|
}
|
|
|
|
template class THPPointer<PyCodeObject>;
|
|
|
|
template <>
|
|
void THPPointer<PyFrameObject>::free() {
|
|
if (ptr && C10_LIKELY(Py_IsInitialized()))
|
|
Py_DECREF(ptr);
|
|
}
|
|
|
|
template class THPPointer<PyFrameObject>;
|