Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow merging of dynamic and static Python proto messages. #16651

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 additions & 8 deletions python/google/protobuf/pyext/message.cc
Expand Up @@ -1711,13 +1711,15 @@ PyObject* MergeFrom(CMessage* self, PyObject* arg) {
}

other_message = reinterpret_cast<CMessage*>(arg);
if (other_message->message->GetDescriptor() !=
self->message->GetDescriptor()) {
const Descriptor* self_descriptor = self->message->GetDescriptor();
const Descriptor* other_descriptor = other_message->message->GetDescriptor();
if (other_descriptor != self_descriptor &&
other_descriptor->full_name() != self_descriptor->full_name()) {
PyErr_Format(PyExc_TypeError,
"Parameter to MergeFrom() must be instance of same class: "
"expected %s got %s.",
self->message->GetDescriptor()->full_name().c_str(),
other_message->message->GetDescriptor()->full_name().c_str());
self_descriptor->full_name().c_str(),
other_descriptor->full_name().c_str());
return nullptr;
}
AssureWritable(self);
Expand Down Expand Up @@ -1749,13 +1751,15 @@ static PyObject* CopyFrom(CMessage* self, PyObject* arg) {
Py_RETURN_NONE;
}

if (other_message->message->GetDescriptor() !=
self->message->GetDescriptor()) {
const Descriptor* self_descriptor = self->message->GetDescriptor();
const Descriptor* other_descriptor = other_message->message->GetDescriptor();
if (other_descriptor != self_descriptor &&
other_descriptor->full_name() != self_descriptor->full_name()) {
PyErr_Format(PyExc_TypeError,
"Parameter to CopyFrom() must be instance of same class: "
"expected %s got %s.",
self->message->GetDescriptor()->full_name().c_str(),
other_message->message->GetDescriptor()->full_name().c_str());
self_descriptor->full_name().c_str(),
other_descriptor->full_name().c_str());
return nullptr;
}

Expand Down
8 changes: 5 additions & 3 deletions src/google/protobuf/generated_message_reflection.cc
Expand Up @@ -284,9 +284,11 @@ static void ReportReflectionUsageEnumTypeError(
field, #METHOD)
#endif

#define USAGE_CHECK_MESSAGE_TYPE(METHOD) \
USAGE_CHECK_EQ(field->containing_type(), descriptor_, METHOD, \
"Field does not match message type.");
#define USAGE_CHECK_MESSAGE_TYPE(METHOD) \
USAGE_CHECK( \
field->containing_type() == descriptor_ || \
field->containing_type()->full_name() == descriptor_->full_name(), \
METHOD, "Field does not match message type.");
#define USAGE_CHECK_SINGULAR(METHOD) \
USAGE_CHECK_NE(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \
"Field is repeated; the method requires a singular field.")
Expand Down
6 changes: 4 additions & 2 deletions src/google/protobuf/message.cc
Expand Up @@ -99,13 +99,15 @@ void Message::CopyFrom(const Message& from) {
class_to->full().merge_to_from(*this, from);
} else {
const Descriptor* descriptor = GetDescriptor();
ABSL_CHECK_EQ(from.GetDescriptor(), descriptor)
const Descriptor* from_descriptor = from.GetDescriptor();
ABSL_CHECK(from_descriptor == descriptor ||
from_descriptor->full_name() == descriptor->full_name())
<< ": Tried to copy from a message with a different type. "
"to: "
<< descriptor->full_name()
<< ", "
"from: "
<< from.GetDescriptor()->full_name();
<< from_descriptor->full_name();
ReflectionOps::Copy(from, this);
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/google/protobuf/reflection_ops.cc
Expand Up @@ -51,10 +51,11 @@ void ReflectionOps::Merge(const Message& from, Message* to) {
ABSL_CHECK_NE(&from, to);

const Descriptor* descriptor = from.GetDescriptor();
ABSL_CHECK_EQ(to->GetDescriptor(), descriptor)
<< "Tried to merge messages of different types "
<< "(merge " << descriptor->full_name() << " to "
<< to->GetDescriptor()->full_name() << ")";
ABSL_CHECK(to->GetDescriptor() == descriptor ||
to->GetDescriptor()->full_name() == descriptor->full_name())
<< "Tried to merge messages of different types " << "(merge "
<< descriptor->full_name() << " to " << to->GetDescriptor()->full_name()
<< ")";

const Reflection* from_reflection = GetReflectionOrDie(from);
const Reflection* to_reflection = GetReflectionOrDie(*to);
Expand Down