aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJie Luo <jieluo@google.com>2023-11-03 17:15:12 -0700
committerJie Luo <jieluo@google.com>2023-11-07 18:50:09 +0000
commit74f5cf4106d7bcdd4612caed9f85af5096f4a390 (patch)
tree0bc2e362acb254efeb85e183a839c11187e019b4
parentedb1afd0490d202d78eb0e8a275fef72077a3eae (diff)
downloadprotobuf-74f5cf4106d7bcdd4612caed9f85af5096f4a390.tar.gz
Raise warnings for python syntax usages
PiperOrigin-RevId: 579344748
-rw-r--r--python/descriptor.c10
-rwxr-xr-xpython/google/protobuf/descriptor.py28
-rw-r--r--python/google/protobuf/pyext/descriptor.cc10
3 files changed, 43 insertions, 5 deletions
diff --git a/python/descriptor.c b/python/descriptor.c
index be1a839ac..023e94360 100644
--- a/python/descriptor.c
+++ b/python/descriptor.c
@@ -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";
diff --git a/python/google/protobuf/descriptor.py b/python/google/protobuf/descriptor.py
index f8b12ff13..5b32e5e15 100755
--- a/python/google/protobuf/descriptor.py
+++ b/python/google/protobuf/descriptor.py
@@ -353,10 +353,19 @@ 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
:attr:`FieldDescriptor.camelcase_name`.
@@ -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.
diff --git a/python/google/protobuf/pyext/descriptor.cc b/python/google/protobuf/pyext/descriptor.cc
index 44d9e4517..734aea66a 100644
--- a/python/google/protobuf/pyext/descriptor.cc
+++ b/python/google/protobuf/pyext/descriptor.cc
@@ -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());