You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3.10搭配Protobuf 3.19.1调用MessageFactory报错问题咨询

本文所述问题出现在Linux系统下(已在Fedora 35、Ubuntu 20.04验证)使用Python 3.10搭配Python Protobuf 3.19.1版本的场景中。

问题复现

该问题会导致相关依赖库无法正常运行,可通过以下步骤快速复现:
使用Protobuf官方教程中的addressbook.proto文件,运行如下代码尝试获取proto2消息类:

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),代码可正常运行。

排查进展

  • 已向官方提交相关Issue,暂未被认定为bug
  • 初步推断问题原因是Python Protobuf 3.19.1版本引入PY_SSIZE_T_CLEAN宏时出现缺陷,大概率是在使用#格式时用int类型替代了Py_ssize_t类型导致的异常。

请问是否有其他开发者遇到过同类问题,或是可以帮忙复现确认该问题?


内容的提问来源于stack exchange,提问作者Nuno Mariz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 12:45:03