由于“TypeError:unhashable type:'bytearray'”(在raspberry pi中)

时间:2017-10-10 16:51:39

标签: python raspberry-pi arm protocol-buffers raspbian

我仅在Raspberry Pis中收到这些错误:"TypeError: unhashable type: 'bytearray'"

  • Python 3.5.3
  • Raspbian Stretch 9.1
  • 以下套餐: grpcio(1.6.3) grpcio-reflection(1.6.3) grpcio-tools(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')

反序列化只在Raspberry Pi中失败(Ubuntu很好):

  

field1:“AAAA”

     

Traceback(最近一次调用最后一次):文件   “/home/pi/.local/lib/python3.5/site-packages/google/protobuf/internal/python_message.py”   第1069行,在MergeFromString中       if self._InternalParse(serialized,0,length)!= length:File“/home/pi/.local/lib/python3.5/site-packages/google/protobuf/internal/python_message.py”,   第1092行,在InternalParse中       field_decoder,field_desc = decoders_by_tag.get(tag_bytes,(None,None))TypeError:unhashable type:'bytearray'

     

在处理上述异常期间,发生了另一个异常:

     

Traceback(最近一次调用最后一次):文件“test.py”,第13行,in          foo.ParseFromString(REF)文件“/home/pi/.local/lib/python3.5/site-packages/google/protobuf/message.py”,   第185行,在ParseFromString中       self.MergeFromString(序列化)文件“/home/pi/.local/lib/python3.5/site-packages/google/protobuf/internal/python_message.py”,   第1075行,在MergeFromString中       提出message_mod.DecodeError('截断的消息。')google.protobuf.message.DecodeError:截断的消息。

1 个答案:

答案 0 :(得分:1)

我设法解决了Raspberry Pi中的问题。

问题在于,在Pi反序列化中运行时bytearray失败,但是,如果数据传递为bytes,则一切正常。

所以我目前的解决方法是做类似的事情:

foo.ParseFromString( bytes(EXPECTED) )

我仍然不确定为什么在台式机中不会发生同样的问题。我注意到的是,在桌面调试时,foo.ParseFromString显示为<built-in method ParseFromString of Foo object at 0x7fedcc0b3fa8>,因此我认为桌面中存在一些在Pi中运行时无法使用的原生优化。

当我有更多时间时,我会尝试更多地研究如何部署protobuf。也许在此期间,有人可以分享一些细节。