Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-picked Python tests and updated upb dep #14443

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions protobuf_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def protobuf_deps():
_github_archive(
name = "upb",
repo = "https://github.com/protocolbuffers/upb",
commit = "7af7938239bf027022e90438d4c6dbcdce007fb9",
sha256 = "99b44fe57266c897ca1d2af96e4ced314344d40f34f7f99d458ddcc76b56f827",
commit = "a71ec4b0e0c01ce2f02c461511302efe97d58727",
sha256 = "3ad3f339ca65fccbde566b6b8c56f23743cebac83eef4e047cfd924ce9ce08b6",
patches = ["@com_google_protobuf//build_defs:upb.patch"],
)
1 change: 1 addition & 0 deletions python/google/protobuf/internal/factory_test1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ message Factory1Message {
optional NestedFactory1Message nested_factory_1_message = 3;
optional int32 scalar_value = 4;
repeated string list_value = 5;
map<string, string> map_field = 6;

extensions 1000 to max;
}
Expand Down
25 changes: 25 additions & 0 deletions python/google/protobuf/internal/message_factory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
__author__ = '[email protected] (Matt Toia)'

import unittest
import gc

from google.protobuf import descriptor_pb2
from google.protobuf.internal import api_implementation
Expand Down Expand Up @@ -285,5 +286,29 @@ def FindFileByName(self, name):
self.assertEqual(345, m.Extensions[ext2].setting)


def testOndemandCreateMetaClass(self):
def loadFile():
f = descriptor_pb2.FileDescriptorProto.FromString(
factory_test1_pb2.DESCRIPTOR.serialized_pb)
return message_factory.GetMessages([f])

messages = loadFile()
data = factory_test1_pb2.Factory1Message()
data.map_field['hello'] = 'welcome'
# Force GC to collect. UPB python will clean up the map entry class.
# cpp extension and pure python will still keep the map entry class.
gc.collect()
message = messages['google.protobuf.python.internal.Factory1Message']()
message.ParseFromString(data.SerializeToString())
value = message.map_field
values = [
# The entry class will be created on demand in upb python.
value.GetEntryClass()(key=k, value=value[k]) for k in sorted(value)
]
gc.collect()
self.assertEqual(1, len(values))
self.assertEqual('hello', values[0].key)
self.assertEqual('welcome', values[0].value)

if __name__ == '__main__':
unittest.main()
Loading