Truncated Message due to "TypeError: unhashable type: 'bytearray'" (in raspberry pi)
Truncated Message due to "TypeError: unhashable type: 'bytearray'" (in raspberry pi)
我仅在 Raspberry Pi 秒后收到这些错误:"TypeError: unhashable type: 'bytearray'"
- 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')
反序列化仅在 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>
,所以我认为桌面上有一些本机优化在 Pi 中 运行 时不可用。
当我有更多时间时,我将尝试更多地调查 protobuf 的部署方式。也许在此期间有人可以分享一些细节。
我仅在 Raspberry Pi 秒后收到这些错误:"TypeError: unhashable type: 'bytearray'"
- 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')
反序列化仅在 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>
,所以我认为桌面上有一些本机优化在 Pi 中 运行 时不可用。
当我有更多时间时,我将尝试更多地调查 protobuf 的部署方式。也许在此期间有人可以分享一些细节。