DEV Community

slowpy
slowpy

Posted on

error by wrong protobuf version

File "/home/xxxx/.local/lib/python3.8/site-packages/google/protobuf/descriptor.py", line 755, in new
_message.Message._CheckCalledFromGeneratedFile()
TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:

  1. Downgrade the protobuf package to 3.20.x or lower.
  2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

刨根问底
https://github.com/protocolbuffers/protobuf/issues/10048
https://github.com/protocolbuffers/upb/blob/3c76a26b3666cb74d7e8850cd0b0923bc4621f86/python/message.c#L1971

def __new__(cls, name, index, number,
            type=None,  # pylint: disable=redefined-builtin
            options=None, serialized_options=None, create_key=None):
  _message.Message._CheckCalledFromGeneratedFile()
  # There is no way we can build a complete EnumValueDescriptor with the
  # given parameters (the name of the Enum is not known, for example).
  # Fortunately generated files just pass it to the EnumDescriptor()
  # constructor, which will ignore it, so returning None is good enough.
  return None
Enter fullscreen mode Exit fullscreen mode

# pylint: disable=protected-access
_message = api_implementation._c_module
# TODO(jieluo): Remove this import after fix api_implementation
if _message is None:
from google.protobuf.pyext import _message
_USE_C_DESCRIPTORS = True

https://github.com/protocolbuffers/protobuf/blob/ad677f3215de203695ade78a25f79fa61010f770/python/google/protobuf/internal/api_implementation.py#L53

if _implementation_type == 'upb':
try:
# pylint: disable=g-import-not-at-top
from google._upb import _message
_c_module = _message
del _message

https://protobuf.dev/news/2022-05-06/#python-updates
upb

bool PyUpb_InitMessage(PyObject* m) {
if (!PyUpb_CPythonBits_Init(&cpython_bits)) return false;
PyObject* message_meta_type = PyUpb_MessageMeta_CreateType();

PyUpb_ModuleState* state = PyUpb_ModuleState_GetFromModule(m);
state->cmessage_type = PyUpb_AddClass(m, &PyUpb_Message_Spec);
state->message_meta_type = (PyTypeObject*)message_meta_type;

PyTypeObject* PyUpb_AddClass(PyObject* m, PyType_Spec* spec) {
PyObject* type = PyType_FromSpec(spec);
const char* name = PyUpb_GetClassName(spec);
if (PyModule_AddObject(m, name, type) < 0) {
Py_XDECREF(type);
return NULL;
}
return (PyTypeObject*)type;
}
PyModule_AddObject

Top comments (0)