Skip to content

Commit

Permalink
make bytearray work (again) (protocolbuffers#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: protocolbuffers#15911

Closes protocolbuffers#16691

COPYBARA_INTEGRATE_REVIEW=protocolbuffers#16691 from jensbjorgensen:main 6249e62
PiperOrigin-RevId: 642623917
  • Loading branch information
jensbjorgensen authored and deannagarcia committed Jun 20, 2024
1 parent 8622deb commit 75f8019
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 75f8019

Please sign in to comment.