Skip to content

Commit

Permalink
support deepcopy & copy by serialize and deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuncong committed Nov 21, 2017
1 parent 1cfa795 commit e4b48f1
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/fastpb/template/module.jinjacc
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,25 @@ namespace {
return PyString_FromStringAndSize(result.data(), result.length());
}

PyObject *
{{ message.name }}_Copy({{ message.name }}* self)
{
std::string result;
{{ message.name }}* cloned = NULL;
Py_BEGIN_ALLOW_THREADS
self->protobuf->SerializeToString(&result);
cloned = ({{ message.name }}*){{ message.name }}_new(&{{ message.name }}Type, NULL, NULL);
cloned->protobuf->ParseFromString(result);
Py_END_ALLOW_THREADS
return (PyObject*)cloned;
}

PyObject *
{{ message.name }}_DeepCopy({{ message.name }}* self, PyObject* /*memo*/)
{
return {{ message.name }}_Copy(self);
}


PyObject *
{{ message.name }}_ParseFromString({{ message.name }}* self, PyObject *value)
Expand Down Expand Up @@ -658,6 +677,12 @@ namespace {
{"ParseMany", (PyCFunction){{ message.name }}_ParseMany, METH_VARARGS | METH_CLASS,
"Parses many protocol buffers of this type from a string."
},
{"__copy__", (PyCFunction){{ message.name }}_DeepCopy, METH_NOARGS,
"copy a pb message."
},
{"__deepcopy__", (PyCFunction){{ message.name }}_DeepCopy, METH_O,
"deep copy a pb message."
},
{NULL} // Sentinel
};

Expand Down

0 comments on commit e4b48f1

Please sign in to comment.