Skip to content

Commit

Permalink
BREAKING CHANGE in v26: Remove Deprecated APIs that add non top descr…
Browse files Browse the repository at this point in the history
…iptor.

Include AddFileDescriptor, AddDescriptor, AddEnumDescriptor,
AddExtensionDescriptor, AddServiceDescriptor.

Those Deprecated APIs may add unlinked descriptors to descriptor_pool which is
is wrong. Should use Add() or AddSerializedFile() instead. Those APIs were
raising deprecated warnings since 2019

PiperOrigin-RevId: 595831718
  • Loading branch information
anandolee authored and Copybara-Service committed Jan 5, 2024
1 parent 181bcee commit c51f111
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 143 deletions.
38 changes: 0 additions & 38 deletions python/google/protobuf/descriptor_pool.py
Expand Up @@ -47,22 +47,6 @@
_USE_C_DESCRIPTORS = descriptor._USE_C_DESCRIPTORS # pylint: disable=protected-access


def _Deprecated(func):
"""Mark functions as deprecated."""

def NewFunc(*args, **kwargs):
warnings.warn(
'Call to deprecated function %s(). Note: Do add unlinked descriptors '
'to descriptor_pool is wrong. Please use Add() or AddSerializedFile() '
'instead. This function will be removed soon.' % func.__name__,
category=DeprecationWarning)
return func(*args, **kwargs)
NewFunc.__name__ = func.__name__
NewFunc.__doc__ = func.__doc__
NewFunc.__dict__.update(func.__dict__)
return NewFunc


def _NormalizeFullyQualifiedName(name):
"""Remove leading period from fully-qualified type name.
Expand Down Expand Up @@ -207,12 +191,6 @@ def AddSerializedFile(self, serialized_file_desc_proto):
file_desc.serialized_pb = serialized_file_desc_proto
return file_desc

# Add Descriptor to descriptor pool is deprecated. Please use Add()
# or AddSerializedFile() to add a FileDescriptorProto instead.
@_Deprecated
def AddDescriptor(self, desc):
self._AddDescriptor(desc)

# Never call this method. It is for internal usage only.
def _AddDescriptor(self, desc):
"""Adds a Descriptor to the pool, non-recursively.
Expand Down Expand Up @@ -267,12 +245,6 @@ def _AddEnumDescriptor(self, enum_desc):
self._top_enum_values[full_name] = enum_value
self._AddFileDescriptor(enum_desc.file)

# Add ServiceDescriptor to descriptor pool is deprecated. Please use Add()
# or AddSerializedFile() to add a FileDescriptorProto instead.
@_Deprecated
def AddServiceDescriptor(self, service_desc):
self._AddServiceDescriptor(service_desc)

# Never call this method. It is for internal usage only.
def _AddServiceDescriptor(self, service_desc):
"""Adds a ServiceDescriptor to the pool.
Expand All @@ -288,12 +260,6 @@ def _AddServiceDescriptor(self, service_desc):
service_desc.file.name)
self._service_descriptors[service_desc.full_name] = service_desc

# Add ExtensionDescriptor to descriptor pool is deprecated. Please use Add()
# or AddSerializedFile() to add a FileDescriptorProto instead.
@_Deprecated
def AddExtensionDescriptor(self, extension):
self._AddExtensionDescriptor(extension)

# Never call this method. It is for internal usage only.
def _AddExtensionDescriptor(self, extension):
"""Adds a FieldDescriptor describing an extension to the pool.
Expand Down Expand Up @@ -343,10 +309,6 @@ def _AddExtensionDescriptor(self, extension):
python_message._AttachFieldHelpers(
extension.containing_type._concrete_class, extension)

@_Deprecated
def AddFileDescriptor(self, file_desc):
self._InternalAddFileDescriptor(file_desc)

# Never call this method. It is for internal usage only.
def _InternalAddFileDescriptor(self, file_desc):
"""Adds a FileDescriptor to the pool, non-recursively.
Expand Down
105 changes: 0 additions & 105 deletions python/google/protobuf/pyext/descriptor_pool.cc
Expand Up @@ -482,100 +482,6 @@ static PyObject* FindAllExtensions(PyObject* self, PyObject* arg) {
return result.release();
}

// These functions should not exist -- the only valid way to create
// descriptors is to call Add() or AddSerializedFile().
// But these AddDescriptor() functions were created in Python and some people
// call them, so we support them for now for compatibility.
// However we do check that the existing descriptor already exists in the pool,
// which appears to always be true for existing calls -- but then why do people
// call a function that will just be a no-op?
// TODO: Need to investigate further.

static PyObject* AddFileDescriptor(PyObject* self, PyObject* descriptor) {
const FileDescriptor* file_descriptor =
PyFileDescriptor_AsDescriptor(descriptor);
if (!file_descriptor) {
return nullptr;
}
if (file_descriptor !=
reinterpret_cast<PyDescriptorPool*>(self)->pool->FindFileByName(
file_descriptor->name())) {
PyErr_Format(PyExc_ValueError,
"The file descriptor %s does not belong to this pool",
file_descriptor->name().c_str());
return nullptr;
}
Py_RETURN_NONE;
}

static PyObject* AddDescriptor(PyObject* self, PyObject* descriptor) {
const Descriptor* message_descriptor =
PyMessageDescriptor_AsDescriptor(descriptor);
if (!message_descriptor) {
return nullptr;
}
if (message_descriptor !=
reinterpret_cast<PyDescriptorPool*>(self)->pool->FindMessageTypeByName(
message_descriptor->full_name())) {
PyErr_Format(PyExc_ValueError,
"The message descriptor %s does not belong to this pool",
message_descriptor->full_name().c_str());
return nullptr;
}
Py_RETURN_NONE;
}

static PyObject* AddEnumDescriptor(PyObject* self, PyObject* descriptor) {
const EnumDescriptor* enum_descriptor =
PyEnumDescriptor_AsDescriptor(descriptor);
if (!enum_descriptor) {
return nullptr;
}
if (enum_descriptor !=
reinterpret_cast<PyDescriptorPool*>(self)->pool->FindEnumTypeByName(
enum_descriptor->full_name())) {
PyErr_Format(PyExc_ValueError,
"The enum descriptor %s does not belong to this pool",
enum_descriptor->full_name().c_str());
return nullptr;
}
Py_RETURN_NONE;
}

static PyObject* AddExtensionDescriptor(PyObject* self, PyObject* descriptor) {
const FieldDescriptor* extension_descriptor =
PyFieldDescriptor_AsDescriptor(descriptor);
if (!extension_descriptor) {
return nullptr;
}
if (extension_descriptor !=
reinterpret_cast<PyDescriptorPool*>(self)->pool->FindExtensionByName(
extension_descriptor->full_name())) {
PyErr_Format(PyExc_ValueError,
"The extension descriptor %s does not belong to this pool",
extension_descriptor->full_name().c_str());
return nullptr;
}
Py_RETURN_NONE;
}

static PyObject* AddServiceDescriptor(PyObject* self, PyObject* descriptor) {
const ServiceDescriptor* service_descriptor =
PyServiceDescriptor_AsDescriptor(descriptor);
if (!service_descriptor) {
return nullptr;
}
if (service_descriptor !=
reinterpret_cast<PyDescriptorPool*>(self)->pool->FindServiceByName(
service_descriptor->full_name())) {
PyErr_Format(PyExc_ValueError,
"The service descriptor %s does not belong to this pool",
service_descriptor->full_name().c_str());
return nullptr;
}
Py_RETURN_NONE;
}

// The code below loads new Descriptors from a serialized FileDescriptorProto.
static PyObject* AddSerializedFile(PyObject* pself, PyObject* serialized_pb) {
PyDescriptorPool* self = reinterpret_cast<PyDescriptorPool*>(pself);
Expand Down Expand Up @@ -689,17 +595,6 @@ static PyMethodDef Methods[] = {
{"SetFeatureSetDefaults", SetFeatureSetDefaults, METH_O,
"Sets the default feature mappings used during the build."},

{"AddFileDescriptor", AddFileDescriptor, METH_O,
"No-op. Add() must have been called before."},
{"AddDescriptor", AddDescriptor, METH_O,
"No-op. Add() must have been called before."},
{"AddEnumDescriptor", AddEnumDescriptor, METH_O,
"No-op. Add() must have been called before."},
{"AddExtensionDescriptor", AddExtensionDescriptor, METH_O,
"No-op. Add() must have been called before."},
{"AddServiceDescriptor", AddServiceDescriptor, METH_O,
"No-op. Add() must have been called before."},

{"FindFileByName", FindFileByName, METH_O,
"Searches for a file descriptor by its .proto name."},
{"FindMessageTypeByName", FindMessageByName, METH_O,
Expand Down

0 comments on commit c51f111

Please sign in to comment.