Python 反序列化包含嵌套类型的protobuf描述符时出错

Python 反序列化包含嵌套类型的protobuf描述符时出错,python,serialization,protocol-buffers,Python,Serialization,Protocol Buffers,我需要向我的服务器发送protobuf描述符,这样它就可以用来序列化/反序列化包含嵌套类型的对象。我写了一个简单的例子来说明我的问题: test.proto: message A { message B { required string data = 1; } repeated B bs = 1; } test.py: from descriptor_pb2 import DescriptorProto from google.protobuf import descri

我需要向我的服务器发送protobuf描述符,这样它就可以用来序列化/反序列化包含嵌套类型的对象。我写了一个简单的例子来说明我的问题:

test.proto:

message A {
  message B {
    required string data = 1;
  }
  repeated B bs = 1;
}
test.py:

from descriptor_pb2 import DescriptorProto
from google.protobuf import descriptor,reflection,message

from test_pb2 import *

a = A()
b = a.bs.add()
b.data = "stuff"

d1 = a.DESCRIPTOR

dp1 = DescriptorProto()
d1.CopyToProto(dp1)

# dp1 serialized, sent over network, deserialized

d2 = descriptor.MakeDescriptor(dp1)

## This code fixes the problem
#for desc in dp1.nested_type:
#    d2.nested_types.append( descriptor.MakeDescriptor(desc) )
#d2.nested_types_by_name = dict((t.name, t) for t in d2.nested_types)
#    
#for f in dp1.field:
#    d2.fields_by_number[f.number].message_type = d2.nested_types_by_name[f.type_name.split('.')[-1]]
#
## This line cannot be run on the server side
#d2.fields[0].message_type._concrete_class = d1.fields[0].message_type._concrete_class 

reflection.ParseMessage(d1, a.SerializeToString())
reflection.ParseMessage(d2, a.SerializeToString())
运行此代码时,最后一行出现错误:

Traceback (most recent call last):
  File "test.py", line 29, in <module>
    reflection.ParseMessage(d2, a.SerializeToString())
  File "build\bdist.win32\egg\google\protobuf\reflection.py", line 168, in ParseMessage
    new_msg.ParseFromString(byte_str)
  File "build\bdist.win32\egg\google\protobuf\message.py", line 182, in ParseFromString
    self.MergeFromString(serialized)
  File "build\bdist.win32\egg\google\protobuf\internal\python_message.py", line 795, in MergeFromString
    if self._InternalParse(serialized, 0, length) != length:
  File "build\bdist.win32\egg\google\protobuf\internal\python_message.py", line 827, in InternalParse
    pos = field_decoder(buffer, new_pos, end, self, field_dict)
  File "build\bdist.win32\egg\google\protobuf\internal\decoder.py", line 523, in DecodeRepeatedField
    if value.add()._InternalParse(buffer, pos, new_pos) != new_pos:
  File "build\bdist.win32\egg\google\protobuf\internal\containers.py", line 216, in add
    new_element = self._message_descriptor._concrete_class(**kwargs)
AttributeError: 'NoneType' object has no attribute '_concrete_class'
回溯(最近一次呼叫最后一次):
文件“test.py”,第29行,在
ParseMessage(d2,a.SerializeToString())
ParseMessage中第168行的文件“build\bdist.win32\egg\google\protobuf\reflection.py”
新建\u msg.ParseFromString(字节\u str)
文件“build\bdist.win32\egg\google\protobuf\message.py”,第182行,ParseFromString格式
self.MergeFromString(序列化)
文件“build\bdist.win32\egg\google\protobuf\internal\python\u message.py”,第795行,位于MergeFromString中
if self.\u InternalParse(序列化,0,长度)!=长度:
文件“build\bdist.win32\egg\google\protobuf\internal\python\u message.py”,第827行,在InternalParse中
pos=字段解码器(缓冲区、新字段、结束、自身、字段dict)
文件“build\bdist.win32\egg\google\protobuf\internal\decoder.py”,第523行,在DecodeRepeatedField中
if value.add()。_InternalParse(缓冲区、位置、新位置)!=新职位:
文件“build\bdist.win32\egg\google\protobuf\internal\containers.py”,第216行,添加
new\u element=self.\u message\u描述符。\ u具体\u类(**kwargs)
AttributeError:“非类型”对象没有属性“\u具体\u类”
我发现发生错误的原因是,当descriptor.MakeDescriptor()从反序列化的DescriptorProto复制时,它并没有完全复制所有嵌套类型,因此我在注释块中编写了代码来补偿它。但是,最后一行使用了对原始描述符中的类的引用,因此我无法在服务器上这样做


我花了无数个小时试图解决这个问题,这是我的系统中非常关键的一部分。任何帮助都将不胜感激。

您好,我很乐意与您联系,了解如何通过Tearray反序列化protobug。我自己也在挣扎。如果你能亲切地指导我,如果有什么东西能帮助你并与我分享,那将是非常有帮助的。谢谢