Do not check required field for upb python MergeFrom

PiperOrigin-RevId: 568577335
pull/14219/head
Jie Luo 1 year ago committed by Copybara-Service
parent ab58f8fbab
commit 1e0bc565a0
  1. 6
      python/google/protobuf/internal/message_test.py
  2. 19
      upb/python/message.c

@ -495,6 +495,12 @@ class MessageTest(unittest.TestCase):
req = more_messages_pb2.RequiredField() req = more_messages_pb2.RequiredField()
more_messages_pb2.RequiredWrapper(request=req) more_messages_pb2.RequiredWrapper(request=req)
def testMergeFromMissingRequiredField(self, message_module):
msg = more_messages_pb2.RequiredField()
message = more_messages_pb2.RequiredField()
message.MergeFrom(msg)
self.assertEqual(msg, message)
def testAddWrongRepeatedNestedField(self, message_module): def testAddWrongRepeatedNestedField(self, message_module):
msg = message_module.TestAllTypes() msg = message_module.TestAllTypes()
try: try:

@ -454,8 +454,6 @@ err:
return ok; return ok;
} }
static PyObject* PyUpb_Message_MergePartialFrom(PyObject*, PyObject*);
static bool PyUpb_Message_InitMessageAttribute(PyObject* _self, PyObject* name, static bool PyUpb_Message_InitMessageAttribute(PyObject* _self, PyObject* name,
PyObject* value) { PyObject* value) {
PyObject* submsg = PyUpb_Message_GetAttr(_self, name); PyObject* submsg = PyUpb_Message_GetAttr(_self, name);
@ -463,7 +461,7 @@ static bool PyUpb_Message_InitMessageAttribute(PyObject* _self, PyObject* name,
assert(!PyErr_Occurred()); assert(!PyErr_Occurred());
bool ok; bool ok;
if (PyUpb_Message_TryCheck(value)) { if (PyUpb_Message_TryCheck(value)) {
PyObject* tmp = PyUpb_Message_MergePartialFrom(submsg, value); PyObject* tmp = PyUpb_Message_MergeFrom(submsg, value);
ok = tmp != NULL; ok = tmp != NULL;
Py_XDECREF(tmp); Py_XDECREF(tmp);
} else if (PyDict_Check(value)) { } else if (PyDict_Check(value)) {
@ -1197,8 +1195,7 @@ err:
return NULL; return NULL;
} }
static PyObject* PyUpb_Message_MergeInternal(PyObject* self, PyObject* arg, PyObject* PyUpb_Message_MergeFrom(PyObject* self, PyObject* arg) {
bool check_required) {
if (self->ob_type != arg->ob_type) { if (self->ob_type != arg->ob_type) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"Parameter to MergeFrom() must be instance of same class: " "Parameter to MergeFrom() must be instance of same class: "
@ -1209,9 +1206,7 @@ static PyObject* PyUpb_Message_MergeInternal(PyObject* self, PyObject* arg,
// OPT: exit if src is empty. // OPT: exit if src is empty.
PyObject* subargs = PyTuple_New(0); PyObject* subargs = PyTuple_New(0);
PyObject* serialized = PyObject* serialized =
check_required PyUpb_Message_SerializePartialToString(arg, subargs, NULL);
? PyUpb_Message_SerializeToString(arg, subargs, NULL)
: PyUpb_Message_SerializePartialToString(arg, subargs, NULL);
Py_DECREF(subargs); Py_DECREF(subargs);
if (!serialized) return NULL; if (!serialized) return NULL;
PyObject* ret = PyUpb_Message_MergeFromString(self, serialized); PyObject* ret = PyUpb_Message_MergeFromString(self, serialized);
@ -1220,14 +1215,6 @@ static PyObject* PyUpb_Message_MergeInternal(PyObject* self, PyObject* arg,
Py_RETURN_NONE; Py_RETURN_NONE;
} }
PyObject* PyUpb_Message_MergeFrom(PyObject* self, PyObject* arg) {
return PyUpb_Message_MergeInternal(self, arg, true);
}
static PyObject* PyUpb_Message_MergePartialFrom(PyObject* self, PyObject* arg) {
return PyUpb_Message_MergeInternal(self, arg, false);
}
static PyObject* PyUpb_Message_Clear(PyUpb_Message* self); static PyObject* PyUpb_Message_Clear(PyUpb_Message* self);
static PyObject* PyUpb_Message_CopyFrom(PyObject* _self, PyObject* arg) { static PyObject* PyUpb_Message_CopyFrom(PyObject* _self, PyObject* arg) {

Loading…
Cancel
Save