Mercurial > piecrust2
comparison piecrust/fastpickle.py @ 717:fcfbe103cfd1
internal: Fix some bugs with the `fastpickle` module.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 28 May 2016 15:41:05 -0700 |
parents | 76a799eae824 |
children | 4850f8c21b6e |
comparison
equal
deleted
inserted
replaced
716:42efc3634763 | 717:fcfbe103cfd1 |
---|---|
9 data = _pickle_object(obj) | 9 data = _pickle_object(obj) |
10 data = json.dumps(data, indent=None, separators=(',', ':')) | 10 data = json.dumps(data, indent=None, separators=(',', ':')) |
11 return data.encode('utf8') | 11 return data.encode('utf8') |
12 | 12 |
13 | 13 |
14 def pickle_obj(obj): | |
15 if obj is not None: | |
16 return _pickle_object(obj) | |
17 return None | |
18 | |
19 | |
14 def pickle_intob(obj, buf): | 20 def pickle_intob(obj, buf): |
15 data = _pickle_object(obj) | 21 data = _pickle_object(obj) |
16 buf = _WriteWrapper(buf) | 22 buf = _WriteWrapper(buf) |
17 json.dump(data, buf, indent=None, separators=(',', ':')) | 23 json.dump(data, buf, indent=None, separators=(',', ':')) |
18 | 24 |
19 | 25 |
20 def unpickle(data): | 26 def unpickle(data): |
21 data = json.loads(data.decode('utf8')) | 27 data = json.loads(data.decode('utf8')) |
22 return _unpickle_object(data) | 28 return _unpickle_object(data) |
29 | |
30 | |
31 def unpickle_obj(data): | |
32 if data is not None: | |
33 return _unpickle_object(data) | |
34 return None | |
23 | 35 |
24 | 36 |
25 def unpickle_fromb(buf, bufsize): | 37 def unpickle_fromb(buf, bufsize): |
26 with buf.getbuffer() as innerbuf: | 38 with buf.getbuffer() as innerbuf: |
27 data = codecs.decode(innerbuf[:bufsize], 'utf8') | 39 data = codecs.decode(innerbuf[:bufsize], 'utf8') |
68 res[k] = func(v) | 80 res[k] = func(v) |
69 return res | 81 return res |
70 elif op == _UNPICKLING: | 82 elif op == _UNPICKLING: |
71 res = collections.OrderedDict() | 83 res = collections.OrderedDict() |
72 for k, v in obj.items(): | 84 for k, v in obj.items(): |
73 res[k] = func(v) | 85 if k != '__type__': |
86 res[k] = func(v) | |
74 return res | 87 return res |
75 | 88 |
76 | 89 |
77 def _set_convert(obj, func, op): | 90 def _set_convert(obj, func, op): |
78 if op == _PICKLING: | 91 if op == _PICKLING: |
228 mod_name = state['__module__'] | 241 mod_name = state['__module__'] |
229 mod = sys.modules[mod_name] | 242 mod = sys.modules[mod_name] |
230 class_def = getattr(mod, class_name) | 243 class_def = getattr(mod, class_name) |
231 obj = class_def.__new__(class_def) | 244 obj = class_def.__new__(class_def) |
232 | 245 |
233 del state['__class__'] | |
234 del state['__module__'] | |
235 attr_names = list(state.keys()) | 246 attr_names = list(state.keys()) |
236 for name in attr_names: | 247 for name in attr_names: |
237 state[name] = _unpickle_object(state[name]) | 248 if name == '__class__' or name == '__module__': |
238 | 249 continue |
239 obj.__dict__.update(state) | 250 obj.__dict__[name] = _unpickle_object(state[name]) |
240 | 251 |
241 return obj | 252 return obj |
242 | 253 |