[英]Embedding Python in C++ and calling methods from the C++ code with Boost.Python
我嘗試將 Python 腳本嵌入到我的 C++ 程序中。 在閱讀了一些關於嵌入和擴展的內容后,我了解了如何打開我自己的 Python 腳本以及如何將一些整數傳遞給它。 但現在我有點不明白如何解決我的問題。 我必須同時執行這兩項操作,從 C++ 調用 Python 函數和從我的嵌入式 Python 腳本調用 C++ 函數。 但我不知道我必須從哪里開始。 我知道我必須編譯一個 .so 文件才能將我的 C++ 函數公開給 Python 但這我無能為力,因為我必須嵌入我的 Python 文件並使用 C++ 代碼控制它(我必須使用腳本語言,使一些邏輯易於編輯)。
那么,有沒有辦法做到這兩件事呢? 從 C++ 調用 Python 函數和從 Python 調用 C++ 函數?
這是我的 C++ 代碼
#include <Python.h>
#include <boost/python.hpp>
using namespace boost::python;
// <----------I want to use this struct in my python file---------
struct World
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
// Exposing the function like its explained in the boost.python manual
// but this needs to be compiled to a .so to be read from the multiply.py
BOOST_PYTHON_MODULE(hello)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}
// <---------------------------------------------------------------
int
main(int argc, char *argv[]) // in the main function is only code for embedding the python file, its not relevant to this question
{
setenv("PYTHONPATH",".",1);
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;
if (argc < 3) {
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
return 1;
}
Py_Initialize();
pName = PyString_FromString(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, argv[2]);
/* pFunc is a new reference */
if (pFunc && PyCallable_Check(pFunc)) {
pArgs = PyTuple_New(argc - 3);
for (i = 0; i < argc - 3; ++i) {
pValue = PyInt_FromLong(atoi(argv[i + 3]));
if (!pValue) {
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Cannot convert argument\n");
return 1;
}
/* pValue reference stolen here: */
PyTuple_SetItem(pArgs, i, pValue);
}
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL) {
printf("Result of call: %ld\n", PyInt_AsLong(pValue));
Py_DECREF(pValue);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
return 1;
}
Py_Finalize();
return 0;
}
這是我的 Python 文件
import hello_ext #importing the C++ file works only if its compiled as a .so
planet = hello.World() #this class should be exposed to python
planet.set('foo')
def multiply(a,b):
planet.greet()
print "Will compute", a, "times", b
c = 0
for i in range(0, a):
c = c + b
return c
簡而言之,與嵌入式 Python 靜態鏈接的 Python 擴展需要在初始化解釋器之前將其模塊初始化函數顯式添加到初始化表中。
PyImport_AppendInittab("hello", &inithello);
Py_Initialize();
Boost.Python 使用BOOST_PYTHON_MODULE
宏來定義 Python 模塊初始值設定項。 結果函數不是模塊導入器。 這種區別類似於創建example.py
模塊並調用import example
區別。
導入模塊時,Python 會首先檢查該模塊是否為內置模塊。 如果模塊不存在,則 Python 將搜索模塊搜索路徑,嘗試根據模塊名稱查找 Python 文件或庫。 如果找到了一個庫,那么 Python 期望該庫提供一個函數來初始化模塊。 一旦找到,導入將在模塊表中創建一個空模塊,然后對其進行初始化。 對於靜態鏈接的模塊,例如原始代碼中的hello
,模塊搜索路徑將無濟於事,因為沒有可供它查找的庫。
對於嵌入, 模塊表和初始化函數文檔指出,對於靜態模塊,除非初始化表中有條目,否則不會自動調用模塊初始化函數。 對於Python 2和Python 3,一個可以通過調用完成此PyImport_AppendInittab()
之前Py_Initialize()
BOOST_PYTHON_MODULE(hello)
{
// ...
}
PyImport_AppendInittab("hello", &inithello);
Py_Initialize();
// ...
boost::python::object hello = boost::python::import("hello");
另請注意,Python 的 C API 用於在 Python 2 和 3 之間嵌入已更改的模塊初始化函數的命名約定,因此對於BOOST_PYTHON_MODULE(hello)
,可能需要使用&inithello
for Python 2 和&PyInit_hello
for Python 3。
這是一個完整的示例,演示如何使用嵌入式 Python 導入demo
用戶模塊,然后導入靜態鏈接的hello
模塊。 它還調用用戶模塊demo.multiply
的函數,然后調用通過靜態鏈接模塊公開的方法。
#include <cstdlib> // setenv, atoi
#include <iostream> // cerr, cout, endl
#include <boost/python.hpp>
struct World
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
/// Staticly linking a Python extension for embedded Python.
BOOST_PYTHON_MODULE(hello)
{
namespace python = boost::python;
python::class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "Usage: call pythonfile funcname [args]" << std::endl;
return 1;
}
char* module_name = argv[1];
char* function_name = argv[2];
// Explicitly add initializers for staticly linked modules.
PyImport_AppendInittab("hello", &inithello);
// Initialize Python.
setenv("PYTHONPATH", ".", 1);
Py_Initialize();
namespace python = boost::python;
try
{
// Convert remaining args into a Python list of integers.
python::list args;
for (int i=3; i < argc; ++i)
{
args.append(std::atoi(argv[i]));
}
// Import the user requested module.
// >>> import module
python::object module = python::import(module_name);
// Invoke the user requested function with the provided arguments.
// >>> result = module.fn(*args)
python::object result = module.attr(function_name)(*python::tuple(args));
// Print the result.
std::cout << python::extract<int>(result)() << std::endl;
}
catch (const python::error_already_set&)
{
PyErr_Print();
return 1;
}
// Do not call Py_Finalize() with Boost.Python.
}
demo.py
內容:
import hello
planet = hello.World()
planet.set('foo')
def multiply(a,b):
print planet.greet()
print "Will compute", a, "times", b
c = 0
for i in range(0, a):
c = c + b
return c
用法:
$ ./a.out demo multiply 21 2
foo
Will compute 21 times 2
42
在上面的代碼中,我選擇使用 Boost.Python 而不是 Python/C API,並使用等效的 Python 代碼注釋 C++ 注釋。 我發現它更簡潔,更不容易出錯。 如果發生 Python 錯誤,Boost.Python 將拋出異常並且所有引用計數都將得到適當處理。
另外,在使用 Boost.Python 時,不要調用Py_Finalize()
。 根據嵌入 - 入門部分:
請注意,此時您不能調用
Py_Finalize()
來停止解釋器。 這可能會在 boost.python 的未來版本中修復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.