Skip to content

Commit

Permalink
Removed message._extensions_by_number
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 534514738
  • Loading branch information
anandolee authored and copybara-github committed May 23, 2023
1 parent 20604b4 commit 86fc32c
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 42 deletions.
4 changes: 3 additions & 1 deletion python/google/protobuf/internal/extension_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ def _FindExtensionByNumber(self, number):
Returns:
Extension field descriptor.
"""
return self._extended_message._extensions_by_number.get(number, None)
descriptor = self._extended_message.DESCRIPTOR
extensions = descriptor.file.pool._extensions_by_number[descriptor]
return extensions.get(number, None)

def __iter__(self):
# Return a generator over the populated extension fields
Expand Down
1 change: 0 additions & 1 deletion python/google/protobuf/internal/python_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,6 @@ def _AddPropertiesForExtensions(descriptor, cls):
if descriptor.file is not None:
# TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available.
pool = descriptor.file.pool
cls._extensions_by_number = pool._extensions_by_number[descriptor]

def _AddStaticMethods(cls):
# TODO(robinson): This probably needs to be thread-safe(?)
Expand Down
40 changes: 0 additions & 40 deletions python/google/protobuf/pyext/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -332,41 +332,7 @@ static int GcClear(PyObject* pself) {
return PyType_Type.tp_clear(pself);
}

// The _extensions_by_number dictionary is built on every access.
// TODO(amauryfa): Migrate all users to pool.FindExtensionByNumber()
static PyObject* GetExtensionsByNumber(CMessageClass *self, void *closure) {
if (self->message_descriptor == nullptr) {
// This is the base Message object, simply raise AttributeError.
PyErr_SetString(PyExc_AttributeError,
"Base Message class has no DESCRIPTOR");
return nullptr;
}

const PyDescriptorPool* pool = self->py_message_factory->pool;

std::vector<const FieldDescriptor*> extensions;
pool->pool->FindAllExtensions(self->message_descriptor, &extensions);

ScopedPyObjectPtr result(PyDict_New());
for (int i = 0; i < extensions.size(); i++) {
ScopedPyObjectPtr extension(
PyFieldDescriptor_FromDescriptor(extensions[i]));
if (extension == nullptr) {
return nullptr;
}
ScopedPyObjectPtr number(PyLong_FromLong(extensions[i]->number()));
if (number == nullptr) {
return nullptr;
}
if (PyDict_SetItem(result.get(), number.get(), extension.get()) < 0) {
return nullptr;
}
}
return result.release();
}

static PyGetSetDef Getters[] = {
{"_extensions_by_number", (getter)GetExtensionsByNumber, nullptr},
{nullptr},
};

Expand Down Expand Up @@ -2410,14 +2376,8 @@ static PyObject* GetUnknownFields(CMessage* self) {
return self->unknown_field_set;
}

static PyObject* GetExtensionsByNumber(CMessage *self, void *closure) {
return message_meta::GetExtensionsByNumber(
reinterpret_cast<CMessageClass*>(Py_TYPE(self)), closure);
}

static PyGetSetDef Getters[] = {
{"Extensions", (getter)GetExtensionDict, nullptr, "Extension dict"},
{"_extensions_by_number", (getter)GetExtensionsByNumber, nullptr},
{nullptr},
};

Expand Down

0 comments on commit 86fc32c

Please sign in to comment.