fix Python bug with required fields

https://github.com/protocolbuffers/upb/issues/1220

There were two bugs here: Python was incorrectly mandating that a required
field be set during assignment, and it was also incorrectly assuming a non-NULL
return from an internal function call.

PiperOrigin-RevId: 518561818
pull/12298/head
Eric Salo 2 years ago committed by Copybara-Service
parent dbc918c6d0
commit 579f4ab70d
  1. 8
      python/google/protobuf/internal/message_test.py
  2. 8
      python/google/protobuf/internal/more_messages.proto

@ -53,6 +53,7 @@ cmp = lambda x, y: (x > y) - (x < y)
from google.protobuf.internal import api_implementation # pylint: disable=g-import-not-at-top from google.protobuf.internal import api_implementation # pylint: disable=g-import-not-at-top
from google.protobuf.internal import encoder from google.protobuf.internal import encoder
from google.protobuf.internal import more_extensions_pb2 from google.protobuf.internal import more_extensions_pb2
from google.protobuf.internal import more_messages_pb2
from google.protobuf.internal import packed_field_test_pb2 from google.protobuf.internal import packed_field_test_pb2
from google.protobuf.internal import test_proto3_optional_pb2 from google.protobuf.internal import test_proto3_optional_pb2
from google.protobuf.internal import test_util from google.protobuf.internal import test_util
@ -496,6 +497,13 @@ class MessageTest(unittest.TestCase):
msg.repeated_nested_message.MergeFrom(other_msg.repeated_nested_message) msg.repeated_nested_message.MergeFrom(other_msg.repeated_nested_message)
self.assertEqual([1, 2, 3, 4], [m.bb for m in msg.repeated_nested_message]) self.assertEqual([1, 2, 3, 4], [m.bb for m in msg.repeated_nested_message])
@unittest.skipIf(
api_implementation.Type() == 'upb',
"We can enable after protobuf updates to use the current upb repo")
def testInternalMergeWithMissingRequiredField(self, message_module):
req = more_messages_pb2.RequiredField()
more_messages_pb2.RequiredWrapper(request=req)
def testAddWrongRepeatedNestedField(self, message_module): def testAddWrongRepeatedNestedField(self, message_module):
msg = message_module.TestAllTypes() msg = message_module.TestAllTypes()
try: try:

@ -358,3 +358,11 @@ message LotsNestedMessage {
message B254 {} message B254 {}
message B255 {} message B255 {}
} }
message RequiredField {
required int32 id = 1;
}
message RequiredWrapper {
optional RequiredField request = 1;
}

Loading…
Cancel
Save