Skip to content

Commit

Permalink
fix Python bug with required fields
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ericsalo authored and copybara-github committed Mar 22, 2023
1 parent dbc918c commit 579f4ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/google/protobuf/internal/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
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 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 test_proto3_optional_pb2
from google.protobuf.internal import test_util
Expand Down Expand Up @@ -496,6 +497,13 @@ def testMergeFromRepeatedField(self, message_module):
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])

@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):
msg = message_module.TestAllTypes()
try:
Expand Down
8 changes: 8 additions & 0 deletions python/google/protobuf/internal/more_messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,11 @@ message LotsNestedMessage {
message B254 {}
message B255 {}
}

message RequiredField {
required int32 id = 1;
}

message RequiredWrapper {
optional RequiredField request = 1;
}

0 comments on commit 579f4ab

Please sign in to comment.