【问题标题】:Can I suppress protobuf deserialization error messages in Python我可以在 Python 中抑制 protobuf 反序列化错误消息吗
【发布时间】: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 安装。

【问题讨论】:

    标签: python protocol-buffers


    【解决方案1】:

    您使用的是哪个 protobuf 版本?我正在使用3.12.4,但我在 stderr 上看不到任何喋喋不休。您是否设置了任何可能导致底层库发出消息的环境变量或日志记录级别标志?

    尝试使用更独立的代码在更裸露的环境中。

    编辑:我现在明白发生了什么。不知道如何重现它(尝试提供 b'\xff' 这应该是无效的 unicode 但仍然看不到库错误),但通常你想寻找有关如何从外部库中抑制 stderr 的答案: Disable stderr from external library

    【讨论】:

    • 我自己使用3.13.0。也许是因为我使用的消息非常相似,足以让 protobuf 绊倒? (用我得到的那种 stderr 噪音更新了问题)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多