From 4ebba684c791416a9131dd01d4c5175a2beb3566 Mon Sep 17 00:00:00 2001 From: Jie Luo Date: Fri, 5 Jan 2024 07:22:32 -0800 Subject: [PATCH] BREAKING CHANGE in v26: Remove RegisterExtension in message class PiperOrigin-RevId: 595989309 --- .../protobuf/internal/python_message.py | 9 ------ python/google/protobuf/message.py | 5 ---- python/google/protobuf/pyext/message.cc | 28 ------------------- python/google/protobuf/pyext/message.h | 4 --- 4 files changed, 46 deletions(-) diff --git a/python/google/protobuf/internal/python_message.py b/python/google/protobuf/internal/python_message.py index 870ff185f09b8..e9e5cd5e40ebc 100755 --- a/python/google/protobuf/internal/python_message.py +++ b/python/google/protobuf/internal/python_message.py @@ -779,15 +779,6 @@ def _AddPropertiesForExtensions(descriptor, cls): pool = descriptor.file.pool def _AddStaticMethods(cls): - # TODO: This probably needs to be thread-safe(?) - def RegisterExtension(field_descriptor): - field_descriptor.containing_type = cls.DESCRIPTOR - # TODO: Use cls.MESSAGE_FACTORY.pool when available. - # pylint: disable=protected-access - cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) - _AttachFieldHelpers(cls, field_descriptor) - cls.RegisterExtension = staticmethod(RegisterExtension) - def FromString(s): message = cls() message.MergeFromString(s) diff --git a/python/google/protobuf/message.py b/python/google/protobuf/message.py index 29ebd7b0e2e7c..3226b6e776cef 100755 --- a/python/google/protobuf/message.py +++ b/python/google/protobuf/message.py @@ -340,11 +340,6 @@ def ByteSize(self): def FromString(cls, s): raise NotImplementedError -# TODO: Remove it in OSS - @staticmethod - def RegisterExtension(field_descriptor): - raise NotImplementedError - def _SetListener(self, message_listener): """Internal method used by the protocol message implementation. Clients should not call this directly. diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc index 7e01f64ae8781..42bb20da042da 100644 --- a/python/google/protobuf/pyext/message.cc +++ b/python/google/protobuf/pyext/message.cc @@ -1855,32 +1855,6 @@ static PyObject* ByteSize(CMessage* self, PyObject* args) { return PyLong_FromLong(self->message->ByteSizeLong()); } -PyObject* RegisterExtension(PyObject* cls, PyObject* extension_handle) { - const FieldDescriptor* descriptor = - GetExtensionDescriptor(extension_handle); - if (descriptor == nullptr) { - return nullptr; - } - if (!PyObject_TypeCheck(cls, CMessageClass_Type)) { - PyErr_Format(PyExc_TypeError, "Expected a message class, got %s", - cls->ob_type->tp_name); - return nullptr; - } - CMessageClass *message_class = reinterpret_cast(cls); - if (message_class == nullptr) { - return nullptr; - } - // If the extension was already registered, check that it is the same. - const FieldDescriptor* existing_extension = - message_class->py_message_factory->pool->pool->FindExtensionByNumber( - descriptor->containing_type(), descriptor->number()); - if (existing_extension != nullptr && existing_extension != descriptor) { - PyErr_SetString(PyExc_ValueError, "Double registration of Extensions"); - return nullptr; - } - Py_RETURN_NONE; -} - static PyObject* SetInParent(CMessage* self, PyObject* args) { AssureWritable(self); Py_RETURN_NONE; @@ -2391,8 +2365,6 @@ static PyMethodDef Methods[] = { "Merges a serialized message into the current message."}, {"ParseFromString", (PyCFunction)ParseFromString, METH_O, "Parses a serialized message into the current message."}, - {"RegisterExtension", (PyCFunction)RegisterExtension, METH_O | METH_CLASS, - "Registers an extension with the current message."}, {"SerializePartialToString", (PyCFunction)SerializePartialToString, METH_VARARGS | METH_KEYWORDS, "Serializes the message to a string, even if it isn't initialized."}, diff --git a/python/google/protobuf/pyext/message.h b/python/google/protobuf/pyext/message.h index 3d1336b860bdf..dc5018227a296 100644 --- a/python/google/protobuf/pyext/message.h +++ b/python/google/protobuf/pyext/message.h @@ -229,10 +229,6 @@ int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs); PyObject* MergeFrom(CMessage* self, PyObject* arg); -// This method does not do anything beyond checking that no other extension -// has been registered with the same field number on this class. -PyObject* RegisterExtension(PyObject* cls, PyObject* extension_handle); - // Get a field from a message. PyObject* GetFieldValue(CMessage* self, const FieldDescriptor* field_descriptor);