Replace all direct cdata access with THPVariable_Unpack (#55799)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55799

I'm going to change the implementation of cdata soon so I need to
abstract over cdata access with a function.  Additionally, many
users are casting manually casting to THPVariable to access
the member so I can remove these unsafe casts in the client code
(the implementation, of course, is still doing an unsafe cast.)

Signed-off-by: Edward Z. Yang <ezyang@fb.com>

Test Plan: Imported from OSS

Reviewed By: albanD

Differential Revision: D27712130

Pulled By: ezyang

fbshipit-source-id: 95fcc013bf3913d67f2c634068eb5b3aab144cb3
This commit is contained in:
Edward Yang
2021-04-15 08:48:00 -07:00
committed by Facebook GitHub Bot
parent 61418aa069
commit 6ec71ed4f9
24 changed files with 183 additions and 181 deletions

View File

@ -43,7 +43,7 @@ PyObject* THPCppFunction_call(PyObject* self, PyObject* args, PyObject *kwargs)
if (!THPVariable_Check(arg)) {
return PyErr_Format(PyExc_TypeError, "argument %d is not a Variable", i);
}
vars[i] = ((THPVariable*)arg)->cdata;
vars[i] = THPVariable_Unpack(arg);
}
variable_list output;
@ -143,7 +143,7 @@ PyObject* THPCppFunction_register_hook_dict(PyObject* self, PyObject* _var)
auto var = (THPVariable*)_var;
auto& fn = *((THPCppFunction*)self)->cdata;
std::unique_ptr<FunctionPreHook> hook(
new PyFunctionPreHook(var->backward_hooks, var->cdata.output_nr()));
new PyFunctionPreHook(var->backward_hooks, THPVariable_Unpack(var).output_nr()));
fn.add_pre_hook(std::move(hook));
Py_RETURN_NONE;
}