Skip to content

Commit

Permalink
Merge pull request #14674 from anandolee/25.x
Browse files Browse the repository at this point in the history
Cherry pick to 25.x: Raise warnings for python syntax usages
  • Loading branch information
mkruskal-google committed Nov 7, 2023
2 parents edb1afd + 74f5cf4 commit f36432a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
10 changes: 10 additions & 0 deletions python/descriptor.c
Expand Up @@ -620,6 +620,11 @@ static PyObject* PyUpb_Descriptor_GetOneofsByName(PyObject* _self,
}

static PyObject* PyUpb_Descriptor_GetSyntax(PyObject* self, void* closure) {
PyErr_WarnEx(NULL,
"descriptor.syntax is deprecated. It will be removed soon. "
"Most usages are checking field descriptors. Consider to use "
"has_presence, is_packed on field descriptors.",
1);
const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self);
const char* syntax =
upb_MessageDef_Syntax(msgdef) == kUpb_Syntax_Proto2 ? "proto2" : "proto3";
Expand Down Expand Up @@ -1309,6 +1314,11 @@ static PyObject* PyUpb_FileDescriptor_GetPublicDependencies(PyObject* _self,

static PyObject* PyUpb_FileDescriptor_GetSyntax(PyObject* _self,
void* closure) {
PyErr_WarnEx(NULL,
"descriptor.syntax is deprecated. It will be removed soon. "
"Most usages are checking field descriptors. Consider to use "
"has_presence, is_packed on field descriptors.",
1);
PyUpb_DescriptorBase* self = (void*)_self;
const char* syntax =
upb_FileDef_Syntax(self->def) == kUpb_Syntax_Proto2 ? "proto2" : "proto3";
Expand Down
28 changes: 23 additions & 5 deletions python/google/protobuf/descriptor.py
Expand Up @@ -353,9 +353,18 @@ def __init__(self, name, full_name, filename, containing_type, fields,
self.oneofs_by_name = dict((o.name, o) for o in self.oneofs)
for oneof in self.oneofs:
oneof.containing_type = self
self.syntax = syntax or "proto2"
self._deprecated_syntax = syntax or "proto2"
self._is_map_entry = is_map_entry

@property
def syntax(self):
warnings.warn(
'descriptor.syntax is deprecated. It will be removed'
' soon. Most usages are checking field descriptors. Consider to use'
' has_presence, is_packed on field descriptors.'
)
return self._deprecated_syntax

@property
def fields_by_camelcase_name(self):
"""Same FieldDescriptor objects as in :attr:`fields`, but indexed by
Expand Down Expand Up @@ -621,7 +630,7 @@ def has_presence(self):
# compatibility. FieldDescriptor.file was added in cl/153110619
# Some old/generated code didn't link file to FieldDescriptor.
# TODO: remove syntax usage b/240619313
return self.containing_type.syntax == 'proto2'
return self.containing_type._deprecated_syntax == 'proto2'

@property
def is_packed(self):
Expand All @@ -634,7 +643,7 @@ def is_packed(self):
field_type == FieldDescriptor.TYPE_MESSAGE or
field_type == FieldDescriptor.TYPE_BYTES):
return False
if self.containing_type.syntax == 'proto2':
if self.containing_type._deprecated_syntax == 'proto2':
return self.has_options and self.GetOptions().packed
else:
return (not self.has_options or
Expand Down Expand Up @@ -743,7 +752,7 @@ def is_closed(self):
Care should be taken when using this function to respect the target
runtime's enum handling quirks.
"""
return self.file.syntax == 'proto2'
return self.file._deprecated_syntax == 'proto2'

def CopyToProto(self, proto):
"""Copies this to a descriptor_pb2.EnumDescriptorProto.
Expand Down Expand Up @@ -1083,7 +1092,7 @@ def __init__(self, name, package, options=None,
self.message_types_by_name = {}
self.name = name
self.package = package
self.syntax = syntax or "proto2"
self._deprecated_syntax = syntax or "proto2"
self.serialized_pb = serialized_pb

self.enum_types_by_name = {}
Expand All @@ -1092,6 +1101,15 @@ def __init__(self, name, package, options=None,
self.dependencies = (dependencies or [])
self.public_dependencies = (public_dependencies or [])

@property
def syntax(self):
warnings.warn(
'descriptor.syntax is deprecated. It will be removed'
' soon. Most usages are checking field descriptors. Consider to use'
' has_presence, is_packed on field descriptors.'
)
return self._deprecated_syntax

def CopyToProto(self, proto):
"""Copies this to a descriptor_pb2.FileDescriptorProto.
Expand Down
10 changes: 10 additions & 0 deletions python/google/protobuf/pyext/descriptor.cc
Expand Up @@ -672,6 +672,11 @@ static PyObject* EnumValueName(PyBaseDescriptor *self, PyObject *args) {
}

static PyObject* GetSyntax(PyBaseDescriptor *self, void *closure) {
PyErr_WarnEx(nullptr,
"descriptor.syntax is deprecated. It will be removed soon. "
"Most usages are checking field descriptors. Consider to use "
"has_presence, is_packed on field descriptors.",
1);
std::string syntax(FileDescriptorLegacy::SyntaxName(
FileDescriptorLegacy(_GetDescriptor(self)->file()).syntax()));
return PyUnicode_InternFromString(syntax.c_str());
Expand Down Expand Up @@ -1493,6 +1498,11 @@ static int SetSerializedOptions(PyFileDescriptor *self, PyObject *value,
}

static PyObject* GetSyntax(PyFileDescriptor *self, void *closure) {
PyErr_WarnEx(nullptr,
"descriptor.syntax is deprecated. It will be removed soon. "
"Most usages are checking field descriptors. Consider to use "
"has_presence, is_packed on field descriptors.",
1);
std::string syntax(FileDescriptorLegacy::SyntaxName(
FileDescriptorLegacy(_GetDescriptor(self)).syntax()));
return PyUnicode_InternFromString(syntax.c_str());
Expand Down

0 comments on commit f36432a

Please sign in to comment.