From 86fc32cedc7aa01798938d5f860094fc21874b93 Mon Sep 17 00:00:00 2001
From: Jie Luo <jieluo@google.com>
Date: Tue, 23 May 2023 12:18:17 -0700
Subject: [PATCH] Removed message._extensions_by_number

PiperOrigin-RevId: 534514738
---
 .../protobuf/internal/extension_dict.py       |  4 +-
 .../protobuf/internal/python_message.py       |  1 -
 python/google/protobuf/pyext/message.cc       | 40 -------------------
 3 files changed, 3 insertions(+), 42 deletions(-)

diff --git a/python/google/protobuf/internal/extension_dict.py b/python/google/protobuf/internal/extension_dict.py
index 289f3b363ef1a..a016f1be230cf 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 58e5b2019c73f..7bb8cf91f583b 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 22e47250dccd9..dec6311828c40 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<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},
 };
 
@@ -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},
 };