diff --git a/torch/csrc/StorageMethods.cpp b/torch/csrc/StorageMethods.cpp index 19f02b46453..a9d3f64f914 100644 --- a/torch/csrc/StorageMethods.cpp +++ b/torch/csrc/StorageMethods.cpp @@ -49,9 +49,6 @@ static PyObject* THPStorage_nbytes(PyObject* self, PyObject* noargs) { static PyObject* THPStorage_dataPtr(PyObject* self, PyObject* noargs) { HANDLE_TH_ERRORS - // PyLong_FromVoidPtr should not need to mutate the pointer in order - // to extract a new long object from it. - auto self_ = THPStorage_Unpack(self); // See Note [Invalid Python Storages] auto invalid = self_.data() == nullptr && @@ -59,7 +56,7 @@ static PyObject* THPStorage_dataPtr(PyObject* self, PyObject* noargs) { TORCH_CHECK( !invalid, "Attempted to access the data pointer on an invalid python storage.") - return PyLong_FromVoidPtr(self_.mutable_data()); + return torch::autograd::utils::wrap(self_.mutable_data()); END_HANDLE_TH_ERRORS } diff --git a/torch/csrc/autograd/utils/wrap_outputs.h b/torch/csrc/autograd/utils/wrap_outputs.h index 5d1d17c597a..72d7a6c76d7 100644 --- a/torch/csrc/autograd/utils/wrap_outputs.h +++ b/torch/csrc/autograd/utils/wrap_outputs.h @@ -47,7 +47,7 @@ inline PyObject* wrap(c10::complex value) { } inline PyObject* wrap(void* value) { - return THPUtils_packInt64(reinterpret_cast(value)); + return PyLong_FromVoidPtr(value); } inline PyObject* wrap(THPDtype* dtype) {