diff piecrust/fastpickle.py @ 697:9e5393fcfab2

bake: Re-enable faster serialization between processes.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 23 Mar 2016 12:19:35 -0700
parents b015e38d4ee1
children 76a799eae824
line wrap: on
line diff
--- a/piecrust/fastpickle.py	Wed Mar 23 10:54:03 2016 -0700
+++ b/piecrust/fastpickle.py	Wed Mar 23 12:19:35 2016 -0700
@@ -1,5 +1,6 @@
 import sys
 import json
+import codecs
 import datetime
 import collections
 
@@ -10,12 +11,32 @@
     return data.encode('utf8')
 
 
+def pickle_intob(obj, buf):
+    data = _pickle_object(obj)
+    buf = _WriteWrapper(buf)
+    json.dump(data, buf, indent=None, separators=(',', ':'))
+
+
 def unpickle(data):
-    data = data.decode('utf8')
+    data = json.loads(data.decode('utf8'))
+    return _unpickle_object(data)
+
+
+def unpickle_fromb(buf, bufsize):
+    with buf.getbuffer() as innerbuf:
+        data = codecs.decode(innerbuf[:bufsize], encoding='utf8')
     data = json.loads(data)
     return _unpickle_object(data)
 
 
+class _WriteWrapper(object):
+    def __init__(self, buf):
+        self._buf = buf
+
+    def write(self, data):
+        self._buf.write(data.encode('utf8'))
+
+
 _PICKLING = 0
 _UNPICKLING = 1