Error: value of type 'PyObject' (aka '_object') is not converted by context to 'bool'
I am passing python module in C as PyObject
. I want to check if this is not NONE in my C code using this form:
int func(PyObject tmp)
{
if(tmp)
{
// etc
I am getting the following error. How can I convert from PyObject to boolean value, similar to how Python if works. It's worth noting that when set to tmp
a variable boost::python::object
, this command works as expected.
ex_program.cpp:72:7: error: value of type 'PyObject' (aka '_object') is not contextually convertible to 'bool'
if (tmp)
^~~
0
source to share
1 answer
PyObject_IsTrue
seems to do what you want :
int PyObject_IsTrue(PyObject *o)
Returns 1 if the object o is considered to be true, and 0 otherwise. This is equivalent to the Python expression not not o. On failure, return -1.
+1
source to share