comparison 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
comparison
equal deleted inserted replaced
696:3bc9f857eb48 697:9e5393fcfab2
1 import sys 1 import sys
2 import json 2 import json
3 import codecs
3 import datetime 4 import datetime
4 import collections 5 import collections
5 6
6 7
7 def pickle(obj): 8 def pickle(obj):
8 data = _pickle_object(obj) 9 data = _pickle_object(obj)
9 data = json.dumps(data, indent=None, separators=(',', ':')) 10 data = json.dumps(data, indent=None, separators=(',', ':'))
10 return data.encode('utf8') 11 return data.encode('utf8')
11 12
12 13
14 def pickle_intob(obj, buf):
15 data = _pickle_object(obj)
16 buf = _WriteWrapper(buf)
17 json.dump(data, buf, indent=None, separators=(',', ':'))
18
19
13 def unpickle(data): 20 def unpickle(data):
14 data = data.decode('utf8') 21 data = json.loads(data.decode('utf8'))
22 return _unpickle_object(data)
23
24
25 def unpickle_fromb(buf, bufsize):
26 with buf.getbuffer() as innerbuf:
27 data = codecs.decode(innerbuf[:bufsize], encoding='utf8')
15 data = json.loads(data) 28 data = json.loads(data)
16 return _unpickle_object(data) 29 return _unpickle_object(data)
30
31
32 class _WriteWrapper(object):
33 def __init__(self, buf):
34 self._buf = buf
35
36 def write(self, data):
37 self._buf.write(data.encode('utf8'))
17 38
18 39
19 _PICKLING = 0 40 _PICKLING = 0
20 _UNPICKLING = 1 41 _UNPICKLING = 1
21 42