So we were using the "feature" that if you open a message with unknown
fields in it from python, the unknown data would get dropped when you
serialized it again. I added some new functions to python-proto2.cc
and cpp_message.py to be able to clear unknown fields from python. If
anyone else was using that feature, here is a way to get it back, just
add this code and then call ".ClearUnknownFieldsRecursive()" on the
object after you deserialize it:
static PyObject* CMessage_ClearUnknownFields(CMessage* self, PyObject*
args);
static PyObject* CMessage_ClearUnknownFieldsRecursive(CMessage* self,
PyObject* args);
static PyMethodDef CMessageMethods[] = {
...
CMETHOD(ClearUnknownFields, METH_NOARGS,
"Clears the unknown fields from a protocol message."),
CMETHOD(ClearUnknownFieldsRecursive, METH_NOARGS,
"Recursively clears the unknown fields from a protocol
message and all its sub messages."),
...
}
static PyObject* CMessage_ClearUnknownFields(CMessage* self, PyObject*
args) {
AssureWritable(self);
self->message->GetReflection()->MutableUnknownFields(self->message)-
>Clear();
Py_RETURN_NONE;
}
static void ClearUnknownFieldsRecursive(google::protobuf::Message*
message) {
const google::protobuf::Reflection* reflection = message-
>GetReflection();
reflection->MutableUnknownFields(message)->Clear();
vector<const google::protobuf::FieldDescriptor*> fields;
reflection->ListFields(*message, &fields);
for(vector<const google::protobuf::FieldDescriptor*>::iterator it =
fields.begin(); it != fields.end(); ++it)
{
if (google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE == (*it)-
>cpp_type()) {
if (google::protobuf::FieldDescriptor::LABEL_REPEATED == (*it)-
>label()) {
const int numItems = reflection->FieldSize(*message, (*it));
for (int index = 0; index < numItems; ++index)
{
google::protobuf::Message* msg = reflection-
>MutableRepeatedMessage(message, (*it), index);
ClearUnknownFieldsRecursive(msg);
}
}
}
}
}
static PyObject* CMessage_ClearUnknownFieldsRecursive(CMessage* self,
PyObject* args) {
AssureWritable(self);
ClearUnknownFieldsRecursive(self->message);
Py_RETURN_NONE;
}
cpp_message.py:
def _AddMessageMethods(message_descriptor, cls):
...
def ClearUnknownFields(self):
return self._cmsg.ClearUnknownFields()
def ClearUnknownFieldsRecursive(self):
return self._cmsg.ClearUnknownFieldsRecursive()
...
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/protobuf?hl=en.