Skip to content

Commit

Permalink
make bytearray work (again) (#16691)
Browse files Browse the repository at this point in the history
fix is pretty simple, just check if the type is bytearray and get the bytes if it is

addresses issue: #15911

Closes #16691

COPYBARA_INTEGRATE_REVIEW=#16691 from jensbjorgensen:main 6249e62
PiperOrigin-RevId: 642623917
  • Loading branch information
jensbjorgensen authored and copybara-github committed Jun 12, 2024
1 parent 41315fd commit eb67a91
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions python/google/protobuf/internal/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ def testGoldenMessage(self, message_module):
golden_copy = copy.deepcopy(golden_message)
self.assertEqual(golden_data, golden_copy.SerializeToString())

def testGoldenMessageBytearray(self, message_module):
# bytearray was broken, test that it works again
if message_module is unittest_pb2:
golden_data = test_util.GoldenFileData('golden_message_oneof_implemented')
else:
golden_data = test_util.GoldenFileData('golden_message_proto3')

golden_message = message_module.TestAllTypes()
golden_message.ParseFromString(bytearray(golden_data))
if message_module is unittest_pb2:
test_util.ExpectAllFieldsSet(self, golden_message)
self.assertEqual(golden_data, golden_message.SerializeToString())

def testGoldenPackedMessage(self, message_module):
golden_data = test_util.GoldenFileData('golden_packed_fields_message')
golden_message = message_module.TestPackedTypes()
Expand Down
3 changes: 3 additions & 0 deletions python/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,9 @@ PyObject* PyUpb_Message_MergeFromString(PyObject* _self, PyObject* arg) {
int err = PyBytes_AsStringAndSize(bytes, &buf, &size);
(void)err;
assert(err >= 0);
} else if (PyByteArray_Check(arg)) {
buf = PyByteArray_AS_STRING(arg);
size = PyByteArray_GET_SIZE(arg);
} else if (PyBytes_AsStringAndSize(arg, &buf, &size) < 0) {
return NULL;
}
Expand Down

0 comments on commit eb67a91

Please sign in to comment.