diff --git a/python/google/protobuf/internal/extension_dict.py b/python/google/protobuf/internal/extension_dict.py index 289f3b363ef1..a016f1be230c 100644 --- a/python/google/protobuf/internal/extension_dict.py +++ b/python/google/protobuf/internal/extension_dict.py @@ -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 diff --git a/python/google/protobuf/internal/python_message.py b/python/google/protobuf/internal/python_message.py index 58e5b2019c73..7bb8cf91f583 100755 --- a/python/google/protobuf/internal/python_message.py +++ b/python/google/protobuf/internal/python_message.py @@ -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(?) diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc index 22e47250dccd..dec6311828c4 100644 --- a/python/google/protobuf/pyext/message.cc +++ b/python/google/protobuf/pyext/message.cc @@ -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 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}, }; @@ -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(Py_TYPE(self)), closure); -} - static PyGetSetDef Getters[] = { {"Extensions", (getter)GetExtensionDict, nullptr, "Extension dict"}, - {"_extensions_by_number", (getter)GetExtensionsByNumber, nullptr}, {nullptr}, };