Skip to content

Commit e4b48f1

Browse files
author
zhangyuncong
committed
support deepcopy & copy by serialize and deserialize
1 parent 1cfa795 commit e4b48f1

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/fastpb/template/module.jinjacc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,25 @@ namespace {
203203
return PyString_FromStringAndSize(result.data(), result.length());
204204
}
205205

206+
PyObject *
207+
{{ message.name }}_Copy({{ message.name }}* self)
208+
{
209+
std::string result;
210+
{{ message.name }}* cloned = NULL;
211+
Py_BEGIN_ALLOW_THREADS
212+
self->protobuf->SerializeToString(&result);
213+
cloned = ({{ message.name }}*){{ message.name }}_new(&{{ message.name }}Type, NULL, NULL);
214+
cloned->protobuf->ParseFromString(result);
215+
Py_END_ALLOW_THREADS
216+
return (PyObject*)cloned;
217+
}
218+
219+
PyObject *
220+
{{ message.name }}_DeepCopy({{ message.name }}* self, PyObject* /*memo*/)
221+
{
222+
return {{ message.name }}_Copy(self);
223+
}
224+
206225

207226
PyObject *
208227
{{ message.name }}_ParseFromString({{ message.name }}* self, PyObject *value)
@@ -658,6 +677,12 @@ namespace {
658677
{"ParseMany", (PyCFunction){{ message.name }}_ParseMany, METH_VARARGS | METH_CLASS,
659678
"Parses many protocol buffers of this type from a string."
660679
},
680+
{"__copy__", (PyCFunction){{ message.name }}_DeepCopy, METH_NOARGS,
681+
"copy a pb message."
682+
},
683+
{"__deepcopy__", (PyCFunction){{ message.name }}_DeepCopy, METH_O,
684+
"deep copy a pb message."
685+
},
661686
{NULL} // Sentinel
662687
};
663688

0 commit comments

Comments
 (0)