if ( 0 == r ) { PyErr_Print(); return false;}
// release used objects
Py_XDECREF( r ) ;
//
return r;
First, if r == 0, that doesn’t mean false. It means there is some problem in your python code and you may want to let the code return a python exception instead (throw TPyException, from $ROOTSYS/include/TPyException.h).
Second, you don’t need Py_XDECREF, as you just checked for nullptr, so use Py_DECREF.
Third, technically you can’t rely on what ‘r’ is anymore when returning as the macro could have set it to nullptr. It doesn’t, but you can’t know, so you should consider ‘r’ undefined after the DECREF.
Fourth, ‘r’ will be C++ non-zero if there is no python error, but that just means it will be Py_True or Py_False. You should either pointer-compare it directly or check for Python non-zero using PyObject_IsTrue().