Nov*_*udi 4 python boost boost-python
我在使用Boost-Python为Python封装枚举时遇到问题。
最初,我打算在try-catch(我在下面插入了整个代码)语句中执行以下操作:
main_namespace["Motion"] = enum_<TestClass::Motion>("Motion")
.value("walk", TestClass::walk)
.value("bike", TestClass::bike)
;
Run Code Online (Sandbox Code Playgroud)
一切都很好,编译已经完成。在运行时,我收到此错误(对我来说这没有意义):
AttributeError: 'NoneType' object has no attribute 'Motion'
Run Code Online (Sandbox Code Playgroud)
之后,我决定在代码中使用BOOST_PYTHON_MODULE编写一个Python模块。初始化Python解释器后,我想立即使用此模块,但不知道how(?)。以下是我的整个代码:
#include <boost/python.hpp>
#include <iostream>
using namespace std;
using namespace boost::python;
BOOST_PYTHON_MODULE(test)
{
enum_<TestClass::Motion>("Motion")
.value("walk", TestClass::walk)
.value("bike", TestClass::bike)
;
}
int main()
{
Py_Initialize();
try
{
object pyMainModule = import("__main__");
object main_namespace = pyMainModule.attr("__dict__");
//What previously I intended to do
//main_namespace["Motion"] = enum_<TestClass::Motion>("Motion")
// .value("walk", TestClass::walk)
// .value("bike", TestClass::bike)
//;
//I want to use my enum here
//I need something like line below which makes me able to use the enum!
exec("print 'hello world'", main_namespace, main_namespace);
}
catch(error_already_set const&)
{
PyErr_Print();
}
Py_Finalize();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
任何有关在Python中包装和使用Enum的有用知识将不胜感激!提前致谢
这AttributeError是尝试在没有第一个设置范围的情况下创建Python扩展类型的结果。该boost::python::enum_构造的状态:
构造一个
enum_对象,该对象包含从中派生的Python扩展类型int,命名为name。name当前作用域的d属性绑定到新的扩展类型。
嵌入Python时,要使用自定义Python模块,通常最容易使用PyImport_AppendInittab,然后按名称导入该模块。
PyImport_AppendInittab("example", &initexample);
...
boost::python::object example = boost::python::import("example");
Run Code Online (Sandbox Code Playgroud)
这是一个完整的示例,显示了通过Boost.Python公开的两个枚举。一个包含在example由导入的单独模块()中,main另一个直接在中公开main。
#include <iostream>
#include <boost/python.hpp>
/// @brief Mockup class with a nested enum.
struct TestClass
{
/// @brief Mocked enum.
enum Motion
{
walk,
bike
};
// @brief Mocked enum.
enum Color
{
red,
blue
};
};
/// @brief Python example module.
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::enum_<TestClass::Motion>("Motion")
.value("walk", TestClass::walk)
.value("bike", TestClass::bike)
;
}
int main()
{
PyImport_AppendInittab("example", &initexample); // Add example to built-in.
Py_Initialize(); // Start interpreter.
// Create the __main__ module.
namespace python = boost::python;
try
{
python::object main = python::import("__main__");
python::object main_namespace = main.attr("__dict__");
python::scope scope(main); // Force main scope
// Expose TestClass::Color as Color
python::enum_<TestClass::Color>("Color")
.value("red", TestClass::red)
.value("blue", TestClass::blue)
;
// Print values of Color enumeration.
python::exec(
"print Color.values",
main_namespace, main_namespace);
// Get a handle to the Color enumeration.
python::object color = main_namespace["Color"];
python::object blue = color.attr("blue");
if (TestClass::blue == python::extract<TestClass::Color>(blue))
std::cout << "blue enum values matched." << std::endl;
// Import example module into main namespace.
main_namespace["example"] = python::import("example");
// Print the values of the Motion enumeration.
python::exec(
"print example.Motion.values",
main_namespace, main_namespace);
// Check if the Python enums match the C++ enum values.
if (TestClass::bike == python::extract<TestClass::Motion>(
main_namespace["example"].attr("Motion").attr("bike")))
std::cout << "bike enum values matched." << std::endl;
}
catch (const python::error_already_set&)
{
PyErr_Print();
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
PyImport_AppendInittab("example", &initexample);
...
boost::python::object example = boost::python::import("example");
Run Code Online (Sandbox Code Playgroud)