2017-10-10 223 views
2

我只在树莓派接收这些错误:"TypeError: unhashable type: 'bytearray'"截断的消息由于 “类型错误:unhashable类型: '的bytearray'”(在覆盆子PI)

  • 的Python 3.5.3
  • Raspbian拉伸9.1
  • 下面的包: grpcio(1.6.3) grpcio反射(1.6.3) grpcio工具(1.6.3)

test.proto (很简单):

syntax = "proto3"; 

package bug; 

message Foo 
{ 
    string field1 = 1; 
} 

和Python代码:

from test_pb2 import Foo 

EXPECTED = bytearray(b'\n\x04AAAA') 
foo = Foo() 
foo.field1 = 'AAAA' 
print(foo) 

data = foo.SerializeToString() 
print(data) 
assert (data == EXPECTED) 

foo.ParseFromString(EXPECTED) 
assert (foo.field1 == 'AAAA') 

反序列化不但不能在树莓派(Ubuntu是罚款):

field1: "AAAA"

Traceback (most recent call last): File "/home/pi/.local/lib/python3.5/site-packages/google/protobuf/internal/python_message.py", line 1069, in MergeFromString if self._InternalParse(serialized, 0, length) != length: File "/home/pi/.local/lib/python3.5/site-packages/google/protobuf/internal/python_message.py", line 1092, in InternalParse field_decoder, field_desc = decoders_by_tag.get(tag_bytes, (None, None)) TypeError: unhashable type: 'bytearray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "test.py", line 13, in foo.ParseFromString(REF) File "/home/pi/.local/lib/python3.5/site-packages/google/protobuf/message.py", line 185, in ParseFromString self.MergeFromString(serialized) File "/home/pi/.local/lib/python3.5/site-packages/google/protobuf/internal/python_message.py", line 1075, in MergeFromString raise message_mod.DecodeError('Truncated message.') google.protobuf.message.DecodeError: Truncated message.

回答

1

我设法解决这个问题在树莓派。

问题是,当运行在Pi反序列化失败的bytearray时,但是,如果数据以bytes的形式传递,则事情很好。

所以我目前的解决办法是做这样的事情:

foo.ParseFromString(bytes(EXPECTED)) 

我仍然不知道为什么同样的问题不会在桌面出现。我注意到的是,在台式机上进行调试时,foo.ParseFromString显示为<built-in method ParseFromString of Foo object at 0x7fedcc0b3fa8>,所以我想在桌面上有一些本地优化,在Pi中运行时不可用。

当我有更多时间时,我会尝试调查一下protobuf的部署方式。也许在此期间有人可以分享一些细节。