【发布时间】:2020-10-05 16:04:37
【问题描述】:
我正在尝试多种消息格式来解码持久的二进制 protobuf 消息(我认为 protobuf 二进制文件无法为您提供找出消息类型的方法)。
for message_type in message_types:
try:
with open(bin_file, 'rb') as stream:
message_type.FromString(stream.read())
except google.protobuf.message.DecodeError:
pass
这可行,但是所有解析二进制流失败的message_types 都会将错误打印到stderr,例如
[libprotobuf ERROR T:\src\github\protobuf\protobuf\src\google\protobuf\wire_format_lite.cc:577] String field 'MyMessage.WarningMessages' contains invalid UTF-8 data when parsing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes.
由于它们不打印到 python 对象 sys.stderr,因此更改该对象没有帮助。
我可以防止 protobuf 打印错误吗?
我正在使用全新的 protobuf==3.13.0 安装。
【问题讨论】: