簡體   English   中英

Protobuf 3.19.1 和 Python 3.10 問題

[英]Protobuf 3.19.1 and Python 3.10 issue

This is all about and issue when using the latest Python Protobuf (3.19.1) and Python 3.10, in Linux (tested in Fedora 35 and Ubuntu 20.04.

它破壞了我們的庫,但可以使用 Python Protobuf 教程中的addressbook.proto輕松測試,並嘗試獲取 proto2 消息 class 如下:

import addressbook_pb2

from google.protobuf import (
    descriptor_database,
    descriptor_pb2,
    descriptor_pool,
    message_factory,
)

_DESCRIPTOR_DB = descriptor_database.DescriptorDatabase()
_DESCRIPTOR_POOL = descriptor_pool.DescriptorPool(_DESCRIPTOR_DB)

_DESCRIPTOR_DB.Add(
    descriptor_pb2.FileDescriptorProto.FromString(
        addressbook_pb2.DESCRIPTOR.serialized_pb
    )
)
factory = message_factory.MessageFactory()
cls = factory.GetPrototype(_DESCRIPTOR_POOL.FindMessageTypeByName("tutorial.Person"))

它引發以下錯誤:

[libprotobuf ERROR google/protobuf/pyext/descriptor_database.cc:64] DescriptorDatabase method raised an error
SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats
Traceback (most recent call last):
  File "/dev/protobuf/test/test.py", line 21, in <module>
    ls = factory.GetPrototype(_DESCRIPTOR_POOL.FindMessageTypeByName("tutorial.Person"))
`KeyError: "Couldn't find message tutorial.Person"

現在,如果我使用較舊的 Python Protobuf 版本(例如 3.18.1),它可以按預期工作。

我打開了一個錯誤https://github.com/protocolbuffers/protobuf/issues/9245 ,但顯然,它不被認為是一個錯誤。

Python Protobuf 在 3.19.1 中引入了PY_SSIZE_T_CLEAN宏並破壞了一些東西,可能是在使用#格式時使用int而不是Py_ssize_t

有人有這個問題或可以確認嗎?

是的,我也遇到了同樣的問題。

我們將版本更改如下:

*protobuf >= 3.19* # Not working

*protobuf >= 3.15.6, <= 3.20.1* # Working 

這里實際上有兩個錯誤:

SystemError:必須為“#”格式定義 PY_SSIZE_T_CLEAN 宏

此錯誤是由Python 3.10 在將數據從 C 傳遞到 Python 端時放棄對舊默認轉換的支持引起的。 在這種情況下,在 protobuf 庫中,僅當將異常從 C 代碼傳遞到 Python 時才會發生錯誤。

早在2021 年 10 月,python-protobuf 庫就已修復以與 Python 3.10 一起使用,並且該修復程序應包含在 python-protobuf 3.20.0 及更高版本中。

嘗試將此添加到您的腳本以檢查版本:

import google.protobuf
print(google.protobuf.__version__)

對我來說,最新版本3.19.43.20.14.21.1不會發生該錯誤,但3.19.2及更早版本會發生錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM