error: value of type 'PyObject' (aka '_object') is not contextually convertible to 'bool'

喜夏-厌秋 提交于 2019-12-25 02:55:08

问题


I am passing a python module to C as a PyObject. I want to check to see if this value is 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 a PyObject to boolean value, simillar to the way Python's if function behaves. It is worth noting that when tmp is a boost::python::object variable 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)
      ^~~

回答1:


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.


来源:https://stackoverflow.com/questions/31329062/error-value-of-type-pyobject-aka-object-is-not-contextually-convertible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!