在 C++ 中嵌入 Python 并使用 Boost.Python 从 C++ 代码调用方法

2023-11-24

我尝试将 Python 脚本嵌入到我的 C++ 程序中。在阅读了一些有关嵌入和扩展的内容后,我了解了如何打开自己的 python 脚本以及如何向其传递一些整数。但现在我不明白如何解决我的问题。我必须同时执行这两项操作,从 C++ 调用 Python 函数,并从嵌入的 Python 脚本调用 C++ 函数。但我不知道该从哪里开始。我知道我必须编译一个 .so 文件才能将我的 C++ 函数公开给 Python,但这我无能为力,因为我必须嵌入我的 Python 文件并使用 C++ 代码来控制它(我必须使用 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() before 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对于 Python 2 和&PyInit_hello对于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

Usage:

$ ./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 的未来版本中得到修复。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 C++ 中嵌入 Python 并使用 Boost.Python 从 C++ 代码调用方法 的相关文章

随机推荐

  • Google Maps API 自动完成同一页面上的第二个地址字段

    我在我的页面上使用 Google Maps API 该页面要求用户填写您的 当前地址 和 新地址 我可以让自动完成功能在第一个地址上工作 但它不适用于第二个地址 我做了很多研究并查看了 stackoverflow 上的类似帖子 但我找不到任
  • 在 try/catch 块中等待两个承诺会导致“未处理的承诺拒绝”[重复]

    这个问题在这里已经有答案了 我想等待两个并行运行的承诺 我不想连续等待每个承诺 这有效但速度较慢 出于这个原因 我认为我可以首先创建两个承诺来让它们滚动 比如说两个网络请求 然后等待它们并能够在 catch 块中捕获错误 这个假设似乎是不正
  • Django - 按模板中的某个字段对查询集进行分组

    我有一张桌子Events 按字段排序date 我想打印模板中的事件 但为每个日期使用单独的 div 例如 div class content h1 December 30th h1 div div class content h1 Dece
  • #1025 - mysql 中重命名错误(errno:150)

    我试图在一个表 misc 中删除一个外键 id 它是表 main 中的主键 id 数据库名称 xxx alter table misc drop FOREIGN KEY id 我收到这个错误 1025 将 interview sql edc
  • 如何使用 dotnet CLI 构建 .NET Framework 4.8 应用程序?

    我继承了一个 NET Framework 4 8 应用程序 我可以使用 Visual Studio 对其进行编译 但是当尝试使用dotnetCLI 应用程序 对于我从外部依赖项使用的所有类型 我都会收到 CS0246 错误 原来的错误消息
  • Spring授权服务器:如何使用托管在单独应用程序上的登录表单?

    我正在使用 Spring Security 和 Spring Authorization Server 并尝试创建身份验证服务器 我有一个基本流程 允许我使用预先构建的登录页面 从拜尔东指南 这是我正在处理的代码 我假设这个登录页面表单来自
  • 学习 LINQ [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 目前不接受答案 Overview 我在这个网站上问过很多次的事情之一是LINQ 我提出的问题广泛而多样 而且往往没有太多背景 因此 为了巩固我在 Linq 上获得的
  • ASP.NET 传递 Windows 身份验证凭据

    我有一个使用 Windows 身份验证的 ASP NET Web 应用程序 此应用程序需要连接到另一个 ASP NET Web 服务 也使用 Windows 身份验证 并使用从用户浏览器收到的相同凭据 这可以做到吗 如何做到 我不相信没有
  • C++ 错误:转换为执行字符集

    问题是我试图打印 ASCII 之外的一些字符 例如德语元音变音字符 等 这些字符不适合普通的 char 变量 因此显然我尝试将它们放入 wchar t 中并使用 L 初始化字符串 但是每次这个字符串包含上面的一个字符时 我都会收到上述错误
  • 从 CakePHP 2.1.2 控制台 Shell 创建完整 url

    我正在尝试通过控制台 shell 从 CakePHP 2 1 2 发送电子邮件 最终通过 cron 作业 我发送的视图是一个日历 其中包含返回应用程序网页的链接 我发现的问题是网址不包含正确的路径 从我读到的内容来看 这是因为我使用控制台后
  • GIF/JPEG 文件可以包含可运行的 PHP 代码吗?

    测试 Web 应用程序 我可以上传 GIF JPEG 文件 并且我知道在该 GIF Jpeg 中正确包含 PHP 代码时可能存在威胁 因为它是使用图像创建真彩色 and 图像jpeg 我正在寻找包含简单 PHP 代码 如 phpinfo 或
  • GroovyWS 和复杂请求

    我遇到了使用 GroovyWS 发送复杂请求的问题 这是由soapUI生成的示例请求
  • ggplot2 中两个不同组的不同调色板

    我正在尝试创建一个图 显示多个地点的观测数据和建模数据的每月土壤湿度垂直剖面 到目前为止 我只能绘制一组值 无论是观察值还是建模值 如下例所示 library ggplot2 library RColorBrewer Create cust
  • Spring 3 MVC - 高级数据绑定 - 带有简单对象列表的表单请求

    我已经阅读了所有 Spring 3 Web 文档 http static springsource org spring docs 3 0 x spring framework reference html spring web html但
  • 关闭 C# 表单应用程序

    我有 2 个表单 当我启动应用程序时 并使用标题栏中的关闭 X 整个应用程序将关闭 现在 当我从第一个表单中选择一个选项时 在我的情况下它是一个按钮 ADD 作为它的电话簿应用程序 它进入第二种形式 因为我使用了 1stform hide
  • 如何以通用方式禁用 cookie,直到用户接受 cookie

    有没有一种奇特的方法来禁用 cookie 直到用户接受它们 以下问题 我有一个使用大量 cookie 的网上商店 为了符合 GDPR 我们需要 禁用 cookie 直到用户接受它们 我不想重写整个商店系统因此我正在寻找一个通用的解决方案 我
  • Java:查找列表中字符串的索引

    我有一个清单 public static List
  • 在 Android Studio 中运行模拟器时出错

    我刚刚在我的计算机 Windows 8 上安装了 Android Studio 但是当我尝试运行程序时出现错误 错误 无法访问包管理器 系统是否正在运行 我已经尝试了几乎所有模拟器 但仍然收到此错误 或者甚至无法启动 我做错了什么 您需要等
  • 如何使用 PHP 读取 .tar.gz 文件?

    我正在构建一个系统 供人们用 PHP 上传 tar 和 tar gz tar bz2 zip 等 文件 上传文件没问题 但我想在上传后列出存档中包含的文件 有人可以推荐一个可以读取文件档案的好的 PHP 库吗 I found 文件 存档在
  • 在 C++ 中嵌入 Python 并使用 Boost.Python 从 C++ 代码调用方法

    我尝试将 Python 脚本嵌入到我的 C 程序中 在阅读了一些有关嵌入和扩展的内容后 我了解了如何打开自己的 python 脚本以及如何向其传递一些整数 但现在我不明白如何解决我的问题 我必须同时执行这两项操作 从 C 调用 Python