Removed message._extensions_by_name

PiperOrigin-RevId: 533501166
pull/12850/head
Jie Luo 2 years ago committed by Copybara-Service
parent 711a749dd1
commit e737c8108a
  1. 4
      python/google/protobuf/internal/extension_dict.py
  2. 1
      python/google/protobuf/internal/python_message.py
  3. 37
      python/google/protobuf/pyext/message.cc

@ -179,7 +179,9 @@ class _ExtensionDict(object):
Returns:
Extension field descriptor.
"""
return self._extended_message._extensions_by_name.get(name, None)
descriptor = self._extended_message.DESCRIPTOR
extensions = descriptor.file.pool._extensions_by_name[descriptor]
return extensions.get(name, None)
def _FindExtensionByNumber(self, number):
"""Tries to find a known extension with the field number.

@ -765,7 +765,6 @@ def _AddPropertiesForExtensions(descriptor, cls):
# TODO(amauryfa): Use cls.MESSAGE_FACTORY.pool when available.
pool = descriptor.file.pool
cls._extensions_by_number = pool._extensions_by_number[descriptor]
cls._extensions_by_name = pool._extensions_by_name[descriptor]
def _AddStaticMethods(cls):
# TODO(robinson): This probably needs to be thread-safe(?)

@ -332,36 +332,6 @@ static int GcClear(PyObject* pself) {
return PyType_Type.tp_clear(pself);
}
// The _extensions_by_name dictionary is built on every access.
// TODO(amauryfa): Migrate all users to pool.FindAllExtensions()
static PyObject* GetExtensionsByName(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;
}
if (PyDict_SetItemString(result.get(), extensions[i]->full_name().c_str(),
extension.get()) < 0) {
return nullptr;
}
}
return result.release();
}
// 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) {
@ -396,7 +366,6 @@ static PyObject* GetExtensionsByNumber(CMessageClass *self, void *closure) {
}
static PyGetSetDef Getters[] = {
{"_extensions_by_name", (getter)GetExtensionsByName, nullptr},
{"_extensions_by_number", (getter)GetExtensionsByNumber, nullptr},
{nullptr},
};
@ -2441,11 +2410,6 @@ static PyObject* GetUnknownFields(CMessage* self) {
return self->unknown_field_set;
}
static PyObject* GetExtensionsByName(CMessage *self, void *closure) {
return message_meta::GetExtensionsByName(
reinterpret_cast<CMessageClass*>(Py_TYPE(self)), closure);
}
static PyObject* GetExtensionsByNumber(CMessage *self, void *closure) {
return message_meta::GetExtensionsByNumber(
reinterpret_cast<CMessageClass*>(Py_TYPE(self)), closure);
@ -2453,7 +2417,6 @@ static PyObject* GetExtensionsByNumber(CMessage *self, void *closure) {
static PyGetSetDef Getters[] = {
{"Extensions", (getter)GetExtensionDict, nullptr, "Extension dict"},
{"_extensions_by_name", (getter)GetExtensionsByName, nullptr},
{"_extensions_by_number", (getter)GetExtensionsByNumber, nullptr},
{nullptr},
};

Loading…
Cancel
Save