Skip to content

Commit

Permalink
fix upb python: DO NOT create empty optional field after sorting empt…
Browse files Browse the repository at this point in the history
…y repeated field

PiperOrigin-RevId: 618350866
  • Loading branch information
anandolee authored and copybara-github committed Mar 23, 2024
1 parent 41aeb3c commit 7cf0238
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/google/protobuf/internal/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,15 @@ def testRepeatedNestedFieldIteration(self, message_module):
self.assertEqual([4, 3, 2, 1],
[m.bb for m in msg.repeated_nested_message[::-1]])

def testSortEmptyRepeated(self, message_module):
message = message_module.NestedTestAllTypes()
self.assertFalse(message.HasField('child'))
self.assertFalse(message.HasField('payload'))
message.child.repeated_child.sort()
message.payload.repeated_int32.sort()
self.assertFalse(message.HasField('child'))
self.assertFalse(message.HasField('payload'))

def testSortingRepeatedScalarFieldsDefaultComparator(self, message_module):
"""Check some different types with the default comparator."""
message = message_module.TestAllTypes()
Expand Down
3 changes: 3 additions & 0 deletions python/repeated.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,14 @@ static PyObject* PyUpb_RepeatedContainer_Sort(PyObject* pself, PyObject* args,
}
}

if (PyUpb_RepeatedContainer_Length(pself) == 0) Py_RETURN_NONE;

PyObject* ret = NULL;
PyObject* full_slice = NULL;
PyObject* list = NULL;
PyObject* m = NULL;
PyObject* res = NULL;

if ((full_slice = PySlice_New(NULL, NULL, NULL)) &&
(list = PyUpb_RepeatedContainer_Subscript(pself, full_slice)) &&
(m = PyObject_GetAttrString(list, "sort")) &&
Expand Down

0 comments on commit 7cf0238

Please sign in to comment.