我只在 Raspberry Pis 中收到这些错误:"TypeError: unhashable type: 'bytearray'"
- python 3.5.3
- Raspbian Stretch 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')
反序列化仅在 Raspberry Pi 中失败(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.
最佳答案
我设法解决了 Raspberry Pi 中的问题。
问题是在 Pi 中运行时反序列化失败 bytearray
, 但是,如果数据作为 bytes
传递一切正常。
所以我目前的解决方法是做类似的事情:
foo.ParseFromString( bytes(EXPECTED) )
我仍然不确定为什么同样的问题不会在桌面上发生。我注意到在桌面上调试时,foo.ParseFromString
显示为<built-in method ParseFromString of Foo object at 0x7fedcc0b3fa8>
所以我想桌面上有一些 native 优化在 Pi 中运行时不可用。
当我有更多时间时,我将尝试更多地调查 protobuf 的部署方式。也许在此期间有人可以分享一些细节。
关于python - 由于 "TypeError: unhashable type: ' bytearray'"(在树莓派中)导致消息被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46671972/