是什么导致 Python 的 __main__ 在嵌入 Boost.Python 时消失
What causes Python's __main__ to disappear when embedding with Boost.Python
我正在将 Python 嵌入到 C++ 应用程序中。我想在主模块中定义一个函数 V
,它接受一个字符串和 returns class A
.
的一个实例
问题是 A
需要一些在 class 嵌入 Python 实例中可用的数据,在示例中作为 _env
传递 - 所以我想我可以使用 def(...) 在方法中定义 V
,使用 lambda 捕获所需的数据。
但是,当我这样做时,当我尝试获取主模块的字典时,我最终得到 AttributeError: 'NoneType' object has no attribute '__dict__'
。
无需调用 boost 的 def(...),我能够获取并添加到主模块。
是不是我做错了什么导致 __main__
在我尝试访问它时丢失并生成 None?关于如何实现这一目标的任何建议?
void B::processRequest(Ptr<protocol::Message> msg, const std::function<void(const std::string &)> &send) {
try {
//make my_module module available to python
PyImport_AppendInittab("my_module", &initmy_module);
//
Py_Initialize();
//get the main module
py::object main_module((py::handle<>(py::borrowed(PyImport_AddModule("__main__")))));
py::object main_namespace = main_module.attr("__dict__");
py::object my_moduleNamespace((py::handle<>(PyImport_ImportModule("my_module"))));
//add the module to the main namespace
main_namespace["my_module"] = my_moduleNamespace;
//add attribute to namespace
// main_namespace["V"] = py::ptr(this);
auto AFn([this](std::string label) { return ptr<A>(_env, label); });
typedef boost::mpl::vector<Ptr<A>, std::string> AFnSig;
const auto policies = py::default_call_policies();
py::def("V", boost::python::make_function(AFn, policies, AFnSig()));
py::handle<> ignored((PyRun_String((*msg->action).c_str(), Py_file_input, main_namespace.ptr(), main_namespace.ptr())));
} catch (py::error_already_set) {
PyErr_Print();
}
Py_Finalize();
}
我唯一能想到的解决方法是使 B
可调用定义 operator(std::stirng)
但这不起作用,因为我还有另外两个函数需要 _env
其中之一与 V
具有相同的签名,因此据我所知,无法区分调用。
编辑:
更改了标题,以便更清楚地说明我指的是什么。
似乎找不到使用 bp 的方法。所以我想出了使用普通 python 在命名空间中定义我想要的函数并注册 python 函数使用的环境对象。因此,围绕 C++ API 编写一个 python 包装器,我能够创建。
BOOST_PYTHON_MODULE (my_module) {
//env is now exposed to the user but can't be constructed and none of its methods are exposed so it's not so bad. Just the symbol exists.
py::class_<Ptr<Environment>>("Environment", py::no_init);
py::class_<A>("A", py::init<const Ptr<Environment>, std::string>());
}
void B::processRequest(Ptr<protocol::Message> msg, const std::function<void(const std::string &)> &send) {
try {
//make my_module module available to python
PyImport_AppendInittab("my_module", &initmy_module);
//
Py_Initialize();
//get the main module
py::object main_module((py::handle<>(py::borrowed(PyImport_AddModule("__main__")))));
py::object main_namespace = main_module.attr("__dict__");
py::object my_moduleNamespace((py::handle<>(PyImport_ImportModule("my_module"))));
//add the module to the main namespace
main_namespace["my_module"] = my_moduleNamespace;
my_moduleNamespace.attr("environment") = _env;
//HERE - provide the API I want using a native Python function
py::handle<> envRun((PyRun_String("def V(label):\n return my_module.A(my_module.environment,label)", Py_file_input, main_namespace.ptr(), main_namespace.ptr())));
py::handle<> ignored((PyRun_String((*msg->action).c_str(), Py_file_input, main_namespace.ptr(), main_namespace.ptr())));
} catch (py::error_already_set) {
PyErr_Print();
}
Py_Finalize();
}
问题不是 __main__
正在消失;而是问题是 V
方法从未在 __main__
的范围内定义。 python::def()
function only operates on the current python::scope
:
def()
is the function which can be used to expose C++ functions and callable objects as Python functions in the current scope
.
要解决这个问题,可以将 Python 可调用对象直接分配到 Python 命名空间中:
namespace python = boost::python;
python::object main = python::import("__main__");
python::object main_namespace = main.attr("__dict__");
main_namespace["V"] = python::make_function(...);
或将当前作用域设置为__main__
,然后通过def()
:
公开Python可调用对象
namespace python = boost::python;
python::object main = python::import("__main__");
python::object main_namespace = main.attr("__dict__");
python::scope main_scope(main);
python::def("V", python::make_function(...)); // defined in __main__
这是一个完整的例子demonstrating这个:
#include <boost/python.hpp>
int main()
{
namespace python = boost::python;
try
{
Py_Initialize(); // Start interpreter.
// Create the __main__ module.
python::object main = python::import("__main__");
python::object main_namespace = main.attr("__dict__");
// >>> def func1(): return 100
main_namespace["func1"] = python::make_function(
[]() { return 100; },
python::default_call_policies(),
boost::mpl::vector<int>());
// >>> def func2(): return 100
{
// Set __main__.__dict__ as current scope.
python::scope main_scope(main);
// def will define in current scope.
python::def("func2", python::make_function(
[]() { return 200; },
python::default_call_policies(),
boost::mpl::vector<int>())
);
}
// Execute func1 and func2 via a python string.
python::exec(
"print dir()\n"
"assert(100 == func1())\n"
"assert(200 == func2())\n"
, main_namespace
);
}
catch (const python::error_already_set&)
{
PyErr_Print();
}
}
输出:
['__builtins__', '__doc__', '__name__', '__package__', 'func1', 'func2']
此外,您可能需要更改解释器的生命周期或不使用 Boost.Python。不应调用 documentation warns that Py_Finalize
:
Note that at this time you must not call Py_Finalize()
to stop the interpreter.
我正在将 Python 嵌入到 C++ 应用程序中。我想在主模块中定义一个函数 V
,它接受一个字符串和 returns class A
.
问题是 A
需要一些在 class 嵌入 Python 实例中可用的数据,在示例中作为 _env
传递 - 所以我想我可以使用 def(...) 在方法中定义 V
,使用 lambda 捕获所需的数据。
但是,当我这样做时,当我尝试获取主模块的字典时,我最终得到 AttributeError: 'NoneType' object has no attribute '__dict__'
。
无需调用 boost 的 def(...),我能够获取并添加到主模块。
是不是我做错了什么导致 __main__
在我尝试访问它时丢失并生成 None?关于如何实现这一目标的任何建议?
void B::processRequest(Ptr<protocol::Message> msg, const std::function<void(const std::string &)> &send) {
try {
//make my_module module available to python
PyImport_AppendInittab("my_module", &initmy_module);
//
Py_Initialize();
//get the main module
py::object main_module((py::handle<>(py::borrowed(PyImport_AddModule("__main__")))));
py::object main_namespace = main_module.attr("__dict__");
py::object my_moduleNamespace((py::handle<>(PyImport_ImportModule("my_module"))));
//add the module to the main namespace
main_namespace["my_module"] = my_moduleNamespace;
//add attribute to namespace
// main_namespace["V"] = py::ptr(this);
auto AFn([this](std::string label) { return ptr<A>(_env, label); });
typedef boost::mpl::vector<Ptr<A>, std::string> AFnSig;
const auto policies = py::default_call_policies();
py::def("V", boost::python::make_function(AFn, policies, AFnSig()));
py::handle<> ignored((PyRun_String((*msg->action).c_str(), Py_file_input, main_namespace.ptr(), main_namespace.ptr())));
} catch (py::error_already_set) {
PyErr_Print();
}
Py_Finalize();
}
我唯一能想到的解决方法是使 B
可调用定义 operator(std::stirng)
但这不起作用,因为我还有另外两个函数需要 _env
其中之一与 V
具有相同的签名,因此据我所知,无法区分调用。
编辑: 更改了标题,以便更清楚地说明我指的是什么。
似乎找不到使用 bp 的方法。所以我想出了使用普通 python 在命名空间中定义我想要的函数并注册 python 函数使用的环境对象。因此,围绕 C++ API 编写一个 python 包装器,我能够创建。
BOOST_PYTHON_MODULE (my_module) {
//env is now exposed to the user but can't be constructed and none of its methods are exposed so it's not so bad. Just the symbol exists.
py::class_<Ptr<Environment>>("Environment", py::no_init);
py::class_<A>("A", py::init<const Ptr<Environment>, std::string>());
}
void B::processRequest(Ptr<protocol::Message> msg, const std::function<void(const std::string &)> &send) {
try {
//make my_module module available to python
PyImport_AppendInittab("my_module", &initmy_module);
//
Py_Initialize();
//get the main module
py::object main_module((py::handle<>(py::borrowed(PyImport_AddModule("__main__")))));
py::object main_namespace = main_module.attr("__dict__");
py::object my_moduleNamespace((py::handle<>(PyImport_ImportModule("my_module"))));
//add the module to the main namespace
main_namespace["my_module"] = my_moduleNamespace;
my_moduleNamespace.attr("environment") = _env;
//HERE - provide the API I want using a native Python function
py::handle<> envRun((PyRun_String("def V(label):\n return my_module.A(my_module.environment,label)", Py_file_input, main_namespace.ptr(), main_namespace.ptr())));
py::handle<> ignored((PyRun_String((*msg->action).c_str(), Py_file_input, main_namespace.ptr(), main_namespace.ptr())));
} catch (py::error_already_set) {
PyErr_Print();
}
Py_Finalize();
}
问题不是 __main__
正在消失;而是问题是 V
方法从未在 __main__
的范围内定义。 python::def()
function only operates on the current python::scope
:
def()
is the function which can be used to expose C++ functions and callable objects as Python functions in the currentscope
.
要解决这个问题,可以将 Python 可调用对象直接分配到 Python 命名空间中:
namespace python = boost::python;
python::object main = python::import("__main__");
python::object main_namespace = main.attr("__dict__");
main_namespace["V"] = python::make_function(...);
或将当前作用域设置为__main__
,然后通过def()
:
namespace python = boost::python;
python::object main = python::import("__main__");
python::object main_namespace = main.attr("__dict__");
python::scope main_scope(main);
python::def("V", python::make_function(...)); // defined in __main__
这是一个完整的例子demonstrating这个:
#include <boost/python.hpp>
int main()
{
namespace python = boost::python;
try
{
Py_Initialize(); // Start interpreter.
// Create the __main__ module.
python::object main = python::import("__main__");
python::object main_namespace = main.attr("__dict__");
// >>> def func1(): return 100
main_namespace["func1"] = python::make_function(
[]() { return 100; },
python::default_call_policies(),
boost::mpl::vector<int>());
// >>> def func2(): return 100
{
// Set __main__.__dict__ as current scope.
python::scope main_scope(main);
// def will define in current scope.
python::def("func2", python::make_function(
[]() { return 200; },
python::default_call_policies(),
boost::mpl::vector<int>())
);
}
// Execute func1 and func2 via a python string.
python::exec(
"print dir()\n"
"assert(100 == func1())\n"
"assert(200 == func2())\n"
, main_namespace
);
}
catch (const python::error_already_set&)
{
PyErr_Print();
}
}
输出:
['__builtins__', '__doc__', '__name__', '__package__', 'func1', 'func2']
此外,您可能需要更改解释器的生命周期或不使用 Boost.Python。不应调用 documentation warns that Py_Finalize
:
Note that at this time you must not call
Py_Finalize()
to stop the interpreter.