Prevent out of scope access when clearing an object
Releasing an object and then passing the original pointer to Py_CLEAR results in an out of scope access which is undefined behaviour. So instead call Py_CLEAR on the object before calling its release function.
This behaviour was seen with the address sanitizer.

Fixes: https://github.com/tensorflow/tensorflow/issues/63012