Skip to content

Commit

Permalink
Use has_presence instead of syntax check in text_format.
Browse files Browse the repository at this point in the history
Remove syntax check in descriptor_test.py

PiperOrigin-RevId: 487362335
  • Loading branch information
anandolee authored and copybara-github committed Nov 9, 2022
1 parent f1998d9 commit f6f7046
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
1 change: 0 additions & 1 deletion python/google/protobuf/internal/descriptor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ def testFileDescriptor(self):
self.assertEqual(self.my_file.package, 'protobuf_unittest')
self.assertEqual(self.my_file.pool, self.pool)
self.assertFalse(self.my_file.has_options)
self.assertEqual('proto2', self.my_file.syntax)
file_proto = descriptor_pb2.FileDescriptorProto()
self.my_file.CopyToProto(file_proto)
self.assertEqual(self.my_file.serialized_pb,
Expand Down
18 changes: 6 additions & 12 deletions python/google/protobuf/text_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,12 +1091,6 @@ def _MergeMessageField(self, tokenizer, message, field):
else:
getattr(message, field.name)[sub_message.key] = sub_message.value

@staticmethod
def _IsProto3Syntax(message):
message_descriptor = message.DESCRIPTOR
return (hasattr(message_descriptor, 'syntax') and
message_descriptor.syntax == 'proto3')

def _MergeScalarField(self, tokenizer, message, field):
"""Merges a single scalar field into a message.
Expand Down Expand Up @@ -1148,7 +1142,7 @@ def _MergeScalarField(self, tokenizer, message, field):
else:
if field.is_extension:
if (not self._allow_multiple_scalars and
not self._IsProto3Syntax(message) and
field.has_presence and
message.HasExtension(field)):
raise tokenizer.ParseErrorPreviousToken(
'Message type "%s" should not have multiple "%s" extensions.' %
Expand All @@ -1158,12 +1152,12 @@ def _MergeScalarField(self, tokenizer, message, field):
else:
duplicate_error = False
if not self._allow_multiple_scalars:
if self._IsProto3Syntax(message):
# Proto3 doesn't represent presence so we try best effort to check
# multiple scalars by compare to default values.
duplicate_error = bool(getattr(message, field.name))
else:
if field.has_presence:
duplicate_error = message.HasField(field.name)
else:
# For field that doesn't represent presence, try best effort to
# check multiple scalars by compare to default values.
duplicate_error = bool(getattr(message, field.name))

if duplicate_error:
raise tokenizer.ParseErrorPreviousToken(
Expand Down

0 comments on commit f6f7046

Please sign in to comment.