diff --git a/python/google/protobuf/pyext/repeated_scalar_container.cc b/python/google/protobuf/pyext/repeated_scalar_container.cc index edbb75d4d0..8cf236bf7a 100644 --- a/python/google/protobuf/pyext/repeated_scalar_container.cc +++ b/python/google/protobuf/pyext/repeated_scalar_container.cc @@ -57,6 +57,7 @@ namespace repeated_scalar_container { static int InternalAssignRepeatedField(RepeatedScalarContainer* self, PyObject* list) { + cmessage::AssureWritable(self->parent); Message* message = self->parent->message; message->GetReflection()->ClearField(message, self->parent_field_descriptor); for (Py_ssize_t i = 0; i < PyList_GET_SIZE(list); ++i) { @@ -589,6 +590,9 @@ static PyObject* Sort(PyObject* pself, PyObject* args, PyObject* kwds) { if (list == nullptr) { return nullptr; } + if (PyList_GET_SIZE(list.get()) == 0) { + Py_RETURN_NONE; + } ScopedPyObjectPtr m(PyObject_GetAttrString(list.get(), "sort")); if (m == nullptr) { return nullptr;