Python中C++与PybDun11反序列化原BuffF缓冲区

2024-10-04 11:22:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个^ {CD1>},我将它转换成C++字符串^ {CD2>},因为我想把它传递给Python。在

C++可以与:

protoObj.ParseFromArray(buffer, sbuffer.size());

我通过以下方式将buffer传递给python:

^{pr2}$

python文件看起来像这样:

import proto.protobuf_pb2


class Calculation:
    def funcName(self, sbuffer, sbuffer_size):
        protoObj = ProtoBuffClass()
        protoObj.ParseFromString(sbuffer.encode('utf-8'))

如果我运行代码,会收到以下错误消息:

terminate called after throwing an instance of 'pybind11::error_already_set'
  what():  DecodeError: Truncated message.

At:
  /usr/local/lib/python3.6/dist-packages/google/protobuf/internal/decoder.py(721): DecodeField
  /usr/local/lib/python3.6/dist-packages/google/protobuf/internal/python_message.py(1189): InternalParse
  /usr/local/lib/python3.6/dist-packages/google/protobuf/internal/python_message.py(1132): MergeFromString
  /usr/local/lib/python3.6/dist-packages/google/protobuf/message.py(187): ParseFromString
  ./Calculation.py(31): funcName

Aborted (core dumped)

我是否犯了一些根本性的错误或如何解决这个问题?是sbuffer的编码吗(当我不编码时,我得到错误:TypeError: memoryview: a bytes-like object is required, not 'str')?提前谢谢。在


Tags: pymessagesizelibpackagesusrlocaldist