如何使用 PyBind11 实现从 C++ 返回 Python Protobuf 对象的绑定?

use*_*737 5 c++ python protocol-buffers pybind11

我正在为一个 C++ 类编写 pybindngs,该类将 Google Protobuf 对象作为成员变量。我希望这些绑定函数返回(在 Python 领域)protobuf 对象的 Python 版本(Google 的 proto 编译器本机生成)。

如果我尝试在 pybind 定义中天真地返回 protobuf 的 C++ 版本

.def_readwrite("my_protobuf", &my_protobuf_)
Run Code Online (Sandbox Code Playgroud)

,我收到以下错误:

TypeError: Unable to convert function return value to a Python type! The signature was
        (self: MyClass) -> MyProtobufDef
Run Code Online (Sandbox Code Playgroud)

这是有道理的 - 返回类型是 C++ protobuf 对象,而不是 Python protobuf 对象。如何将其转换为 Python 版本的 protobuf?