I have to check whether passed object type exists in python using boost python library:
// bp = boost::python
bool TypeExists(const std::string &typeName)
{
bp::object ret = bp::exec(
(boost::format("'%1%' in globals()") % typeName).str().c_str()
);
return bp::extract<bool>(ret);
}
So, in this code I run python expression which looks like: 'TypeName' in globals(). This should return object with Boolean type. But extract always returns 0. Type 100% exists because in next line after TypeExists call I create the object of this type. What’s wrong?
The problem is that exec executes Python code and returns execution result (whether it executed or had errors); and you need the result of expression evaluation.
You must use eval here to get expression evaluation result.
This is described in documentation.