pybind11 embedded in C++: C++内使用pybind11功能

C++内使用pybind11嵌入功能

C++源码内可以使用pybind11嵌入方式提供的功能:构建Python解释器,执行Python命令、脚本,构造pybind11嵌入模块,加载调用Python模块等。源码来源于pybind11使用手册。

借用此方式,可以在C++代码中使用Python特色模块。同时需要考虑安全问题,防止一些恶意代码注入。


示例源码(embed.cpp)
#include<pybind11/embed.h>
#include<iostream>
#include<string>
#include<cassert>

namespace py=pybind11;
using namespace py::literals;

//定义多个embedded模块
PYBIND11_EMBEDDED_MODULE(fast_calc,m){
    m.def("add",[](int i,int j){
        return i+j;
    });
}

PYBIND11_EMBEDDED_MODULE(cpp_module,m){
    m.attr("a")=1;
}

int main(int argc, char **argv)
{
    //-----------------------------------------------------------------
    //启动Python解释器
    py::scoped_interpreter guard{};
    //-------------------------------------------------------------------

    //调用Python函数
    py::print("Hello,World");

    //-------------------------------------------------------------------
    //执行Python脚本
    py::exec(R"(
        kwargs=dict(name="world",number=42)
        message="Hello,{name}! The answer is {number}".format(**kwargs)
        print(message)
    )");

    //--------------------------------------------------------------------

    auto kwargs=py::dict("name"_a="world","number"_a=42);
    auto message="Hello,{name}! The answer is {number}"_s.format(**kwargs);
    py::print(message);

    //-------------------------------------------------------------------

    //初始化脚本空间状态    
    auto locals=py::dict("name"_a="world","number"_a=42);
    //运行脚本
    py::exec(R"(
        message="Hello,{name}! The answer is {number}".format(**locals())
    )",py::globals(),locals);
    
    std::cout<<locals["message"].cast<std::string>()<<std::endl;

    //---------------------------------------------------------------------
    //加载Python模块,获取模块变量(py::object)
    auto sys=py::module_::import("sys");
    py::print(sys.attr("path"));

    //---------------------------------------------------------------------

    //加载pybind11嵌入模块,运行命令
    auto calc=py::module_::import("calc");
    auto result=calc.attr("add")(1,2);

    int n=result.cast<int>();

    assert(n==3);
    //-------------------------------------------------------------------------

    {
        //加载embedded模块
        auto fast_calc=py::module_::import("fast_calc");
        auto result=fast_calc.attr("add")(1,2).cast<int>();
        assert(result==3);
    }

    //-------------------------------------------------------------------------
    {
        //加载外部模块,该模块引用内部embedded模块
        auto py_module=py::module_::import("py_module");
        auto locals=py::dict("fmt"_a="{}+{}={}",**py_module.attr("__dict__"));
        assert(locals["a"].cast<int>()==1);
        assert(locals["b"].cast<int>()==2);

        py::exec(R"(
            c=a+b
            message=fmt.format(a,b,c)
        )",py::globals(),locals);

        assert(locals["c"].cast<int>()==3);
        py::print(locals["message"]);

    }
    //------------------------------------------------------------------------
    return(0);
}

CMakeLists.txt
cmake_minimum_required(VERSION 3.4)
project(embed)
find_package(pybind11 REQUIRED) # or `add_subdirectory(pybind11)`
add_executable(embed embed.cpp)
target_link_libraries(embed PRIVATE pybind11::embed)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值