2015-07-09 46 views
0

我将一个python模块传递给C作为PyObject。我要检查,看看是否这个值是无我的C代码,使用这种形式:错误:类型'PyObject'(aka'_object')的值不能上下文转换为'bool'

int func(PyObject tmp) 
{ 
    if(tmp) 
    { 
    // etc 

我收到以下错误。我如何从PyObject转换为布尔值,与Python的if函数行为相似。值得注意的是,当tmpboost::python::object变量时,此命令按预期工作。

ex_program.cpp:72:7: error: value of type 'PyObject' (aka '_object') is not contextually convertible to 'bool' 
    if (tmp) 
     ^~~ 
+0

NULL或无?他们是非常不同的。 –

+0

无。谢谢你的澄清,我已经编辑了我的备忘录来反映这一点。 – kilojoules

回答

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. 
+0

tmp是否必须是'PyObject *'类型?我用tmp作为'PyObject'实现这个问题...... – kilojoules

+0

我用'if(PyObject_IsTrue(&tmp))'克服了错误' – kilojoules

相关问题