pytorch/c10/core/SafePyObject.cpp
Edward Z. Yang de6353ba88 Introduce SafePyObject, make TorchDispatchTypeObject use it
The pattern of a PyObject* bundled with a PyInterpreter* is pretty
useful in many contexts (e.g., TorchDispatchTypeObject) so I have turned
it into a dedicated class SafePyObject.  In the process I fixed a
bug with the old TorchDispatchTypeObject (copy constructor/assignment
was not deleted), made the API more safe (retrieving the PyObject*
pointer requires verification that the PyInterpreter* matches) and
fixed some minor inefficiencies in C++ code.

Signed-off-by: Edward Z. Yang <ezyangfb.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/75142

Approved by: https://github.com/zou3519
2022-04-04 14:35:01 +00:00

12 lines
261 B
C++

#include <c10/core/SafePyObject.h>
#include <c10/core/TensorImpl.h>
namespace c10 {
PyObject* SafePyObject::ptr(const c10::impl::PyInterpreter* interpreter) const {
TORCH_INTERNAL_ASSERT(interpreter == pyinterpreter_);
return data_;
}
} // namespace c10