From 372fc496e8e7e194e13eaf6fcadf4c340e20fede Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Sat, 19 Apr 2014 17:00:19 -0700 Subject: [PATCH] Fix pickling for pypy --- capnp/lib/capnp.pyx | 11 +++++++---- capnp/lib/pickle_helper.py | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 capnp/lib/pickle_helper.py diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index 610feb1..5fe8971 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -845,8 +845,11 @@ cdef class _MessageSize: self.word_count = word_count self.cap_count = cap_count -def _struct_reducer(schema_id, data): - return _global_schema_parser.modules_by_id[schema_id].from_bytes(data) +if getattr(_sys, 'subversion', [''])[0] == 'PyPy': + from pickle_helper import _struct_reducer +else: + def _struct_reducer(schema_id, data): + return _global_schema_parser.modules_by_id[schema_id].from_bytes(data) cdef class _DynamicStructReader: """Reads Cap'n Proto structs @@ -930,7 +933,7 @@ cdef class _DynamicStructReader: size = self.thisptr.totalSize() return _MessageSize(size.wordCount, size.capCount) - def __reduce__(self): + def __reduce_ex__(self, proto): return _struct_reducer, (self.schema.node.id, self.as_builder().to_bytes()) cdef class _DynamicStructBuilder: @@ -1191,7 +1194,7 @@ cdef class _DynamicStructBuilder: size = self.thisptr.totalSize() return _MessageSize(size.wordCount, size.capCount) - def __reduce__(self): + def __reduce_ex__(self, proto): return _struct_reducer, (self.schema.node.id, self.to_bytes()) cdef class _DynamicStructPipeline: diff --git a/capnp/lib/pickle_helper.py b/capnp/lib/pickle_helper.py new file mode 100644 index 0000000..c28d46a --- /dev/null +++ b/capnp/lib/pickle_helper.py @@ -0,0 +1,6 @@ +import capnp + + +def _struct_reducer(schema_id, data): + 'Hack to deal with pypy not allowing reduce functions to be "built-in" methods (ie. compiled from a .pyx)' + return capnp._global_schema_parser.modules_by_id[schema_id].from_bytes(data)