Re: Pickling python protocol puffers

78 views
Skip to first unread message

Alex Roper

unread,
Sep 19, 2012, 10:21:31 AM9/19/12
to prot...@googlegroups.com
This will be fixed in 2.5.0, see issue 418 http://code.google.com/p/protobuf/issues/detail?id=418

On Tuesday, July 10, 2012 9:55:15 PM UTC-4, Alex Roper wrote:
Hi,

As part of a research project I'm working on, I found it convenient to be able to pickle messages (or more specifically Python structures that contain them). You can do this without patching protobuf, but it is a bit clumsier and (unless I'm missing something) requires registering a handler for every kind of message to be handled with copy_reg, or breaking down the structures manually (especially annoying when operating interactively).

The attached patch adds __getstate__ and __setstate__ methods to generated classes, hooking into pickle and using SerializePartialToString to return an object pickle knows how to deal with, allowing them to be pickled out of the box. This seems to work for both standard Python messages and the experimental C++ bindings.

I'm not sure whether this is a common use case, but it solved my problem nicely, and seems like something that should just work, and therefore I contribute it upstream.

Alex
Reply all
Reply to author
Forward
0 new messages