Skip to content

Commit

Permalink
BREAKING CHANGE in v26: Remove RegisterExtension in message class
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 595989309
  • Loading branch information
anandolee authored and copybara-github committed Jan 5, 2024
1 parent 7bbc0b6 commit 4ebba68
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 46 deletions.
9 changes: 0 additions & 9 deletions python/google/protobuf/internal/python_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions python/google/protobuf/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 0 additions & 28 deletions python/google/protobuf/pyext/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<CMessageClass*>(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;
Expand Down Expand Up @@ -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."},
Expand Down
4 changes: 0 additions & 4 deletions python/google/protobuf/pyext/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4ebba68

Please sign in to comment.