Skip to content

Commit 7cf0238

Browse files
anandoleecopybara-github
authored andcommitted
fix upb python: DO NOT create empty optional field after sorting empty repeated field
PiperOrigin-RevId: 618350866
1 parent 41aeb3c commit 7cf0238

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

python/google/protobuf/internal/message_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,15 @@ def testRepeatedNestedFieldIteration(self, message_module):
549549
self.assertEqual([4, 3, 2, 1],
550550
[m.bb for m in msg.repeated_nested_message[::-1]])
551551

552+
def testSortEmptyRepeated(self, message_module):
553+
message = message_module.NestedTestAllTypes()
554+
self.assertFalse(message.HasField('child'))
555+
self.assertFalse(message.HasField('payload'))
556+
message.child.repeated_child.sort()
557+
message.payload.repeated_int32.sort()
558+
self.assertFalse(message.HasField('child'))
559+
self.assertFalse(message.HasField('payload'))
560+
552561
def testSortingRepeatedScalarFieldsDefaultComparator(self, message_module):
553562
"""Check some different types with the default comparator."""
554563
message = message_module.TestAllTypes()

python/repeated.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,14 @@ static PyObject* PyUpb_RepeatedContainer_Sort(PyObject* pself, PyObject* args,
473473
}
474474
}
475475

476+
if (PyUpb_RepeatedContainer_Length(pself) == 0) Py_RETURN_NONE;
477+
476478
PyObject* ret = NULL;
477479
PyObject* full_slice = NULL;
478480
PyObject* list = NULL;
479481
PyObject* m = NULL;
480482
PyObject* res = NULL;
483+
481484
if ((full_slice = PySlice_New(NULL, NULL, NULL)) &&
482485
(list = PyUpb_RepeatedContainer_Subscript(pself, full_slice)) &&
483486
(m = PyObject_GetAttrString(list, "sort")) &&

0 commit comments

Comments
 (0)