python - 由于 "TypeError: unhashable type: ' bytearray'"(在树莓派中)导致消息被截断

标签 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 反射 (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/

相关文章:

raspberry-pi - 树莓派杰西 : Cannot mount NTFS external harddrive anymore after 'apt-get upgrade' (fuse device is missing)

iOS - 如何从热点 (iPhone) 向连接的客户端 (IoT 设备) 发送消息

android - 什么是 ELF 二进制文件中的 .init_array 部分?

c++ - PendSV/SVC 异常是否立即引发?

c - 为 C 程序分析 ARM 上的功耗

Python比较2个csv文件的列并写入新的csv

python - 如何使用 Django 文件存储在 API 和 Worker 之间共享文件

python - 在另一个函数调用中使用返回元组的 Python 函数

python - 防止 lxml 创建自闭合标签

Python 不重复连续行