Use macros instead of directly accessing Python object fields (#31388)
Summary:
The Python C API documentation states "Access to the [PyObject]
members must be done by using the macros Py_REFCNT and Py_TYPE."
Pull Request resolved: https://github.com/pytorch/pytorch/pull/31388
Differential Revision: D19161790
Pulled By: colesbury
fbshipit-source-id: ac9a3738c913ad290a6d3460d0d657ec5c13b711