Raise warnings for python syntax usages

PiperOrigin-RevId: 579344748
pull/14674/head
Jie Luo 1 year ago
parent edb1afd049
commit 74f5cf4106
  1. 10
      python/descriptor.c
  2. 28
      python/google/protobuf/descriptor.py
  3. 10
      python/google/protobuf/pyext/descriptor.cc

@ -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";
@ -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";

@ -353,9 +353,18 @@ class Descriptor(_NestedDescriptorBase):
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
@ -621,7 +630,7 @@ class FieldDescriptor(DescriptorBase):
# 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):
@ -634,7 +643,7 @@ class FieldDescriptor(DescriptorBase):
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
@ -743,7 +752,7 @@ class EnumDescriptor(_NestedDescriptorBase):
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.
@ -1083,7 +1092,7 @@ class FileDescriptor(DescriptorBase):
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 = {}
@ -1092,6 +1101,15 @@ class FileDescriptor(DescriptorBase):
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.

@ -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());
@ -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());

Loading…
Cancel
Save