Mercurial > piecrust2
annotate piecrust/fastpickle.py @ 459:2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 11 Jul 2015 00:44:58 -0700 |
parents | 298f8f46432a |
children | b015e38d4ee1 |
rev | line source |
---|---|
450
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import sys |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import datetime |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import collections |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 def pickle(obj): |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 return _pickle_object(obj) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 def unpickle(data): |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 return _unpickle_object(data) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 |
459
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
14 _PICKLING = 0 |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
15 _UNPICKLING = 1 |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
16 |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
17 |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
18 def _tuple_dispatch(obj, func, op): |
450
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 res = [None] * len(obj) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 for i, c in enumerate(obj): |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 res[i] = func(c) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 return tuple(res) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 |
459
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
25 def _list_dispatch(obj, func, op): |
450
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 res = [None] * len(obj) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 for i, c in enumerate(obj): |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 res[i] = func(c) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 return res |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 |
459
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
32 def _dict_dispatch(obj, func, op): |
450
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 res = {} |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 for k, v in obj.items(): |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 res[k] = func(v) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 return res |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 |
459
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
39 def _set_dispatch(obj, func, op): |
450
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 res = set() |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 for v in obj: |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 res.add(func(v)) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 return res |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 |
459
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
46 def _date_convert(obj, op): |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
47 if op == _PICKLING: |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
48 return {'__class__': 'date', |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
49 'year': obj.year, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
50 'month': obj.month, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
51 'day': obj.day} |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
52 elif op == _UNPICKLING: |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
53 return datetime.date( |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
54 obj['year'], obj['month'], obj['day']) |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
55 |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
56 |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
57 def _datetime_convert(obj, op): |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
58 if op == _PICKLING: |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
59 return {'__class__': 'datetime', |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
60 'year': obj.year, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
61 'month': obj.month, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
62 'day': obj.day, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
63 'hour': obj.hour, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
64 'minute': obj.minute, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
65 'second': obj.second, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
66 'microsecond': obj.microsecond} |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
67 elif op == _UNPICKLING: |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
68 return datetime.datetime( |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
69 obj['year'], obj['month'], obj['day'], |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
70 obj['hour'], obj['minute'], obj['second'], obj['microsecond']) |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
71 |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
72 |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
73 def _time_convert(obj, op): |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
74 if op == _PICKLING: |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
75 return {'__class__': 'time', |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
76 'hour': obj.hour, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
77 'minute': obj.minute, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
78 'second': obj.second, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
79 'microsecond': obj.microsecond} |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
80 elif op == _UNPICKLING: |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
81 return datetime.time( |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
82 obj['hour'], obj['minute'], obj['second'], obj['microsecond']) |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
83 |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
84 |
450
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 _identity_dispatch = object() |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 _type_dispatch = { |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
88 type(None): _identity_dispatch, |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 bool: _identity_dispatch, |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
90 int: _identity_dispatch, |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
91 float: _identity_dispatch, |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
92 str: _identity_dispatch, |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 tuple: _tuple_dispatch, |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 list: _list_dispatch, |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
95 dict: _dict_dispatch, |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 collections.OrderedDict: _dict_dispatch, |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
97 set: _set_dispatch |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
98 } |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 |
459
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
101 _type_convert = { |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
102 datetime.date: _date_convert, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
103 datetime.datetime: _datetime_convert, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
104 datetime.time: _time_convert |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
105 } |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
106 |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
107 |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
108 _type_unconvert = { |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
109 'date': _date_convert, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
110 'datetime': _datetime_convert, |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
111 'time': _time_convert |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
112 } |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
113 |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
114 |
450
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
115 def _pickle_object(obj): |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
116 t = type(obj) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
117 disp = _type_dispatch.get(t) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
118 if disp is _identity_dispatch: |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
119 return obj |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
120 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
121 if disp is not None: |
459
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
122 return disp(obj, _pickle_object, _PICKLING) |
450
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
123 |
459
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
124 conv = _type_convert.get(t) |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
125 if conv is not None: |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
126 return conv(obj, _PICKLING) |
450
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
128 getter = getattr(obj, '__getstate__', None) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
129 if getter is not None: |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
130 state = getter() |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
131 else: |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
132 state = obj.__dict__ |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
133 |
459
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
134 state = _dict_dispatch(state, _pickle_object, _PICKLING) |
450
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
135 state['__class__'] = obj.__class__.__name__ |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
136 state['__module__'] = obj.__class__.__module__ |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
137 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
138 return state |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
139 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
140 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
141 def _unpickle_object(state): |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
142 t = type(state) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
143 disp = _type_dispatch.get(t) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
144 if disp is _identity_dispatch: |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
145 return state |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
146 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
147 if (disp is not None and |
459
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
148 (t != dict or '__class__' not in state)): |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
149 return disp(state, _unpickle_object, _UNPICKLING) |
450
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
150 |
459
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
151 class_name = state['__class__'] |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
152 conv = _type_unconvert.get(class_name) |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
153 if conv is not None: |
2ef04e16f0b9
internal: Add support for fake pickling of date/time structures.
Ludovic Chabant <ludovic@chabant.com>
parents:
450
diff
changeset
|
154 return conv(state, _UNPICKLING) |
450
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
155 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
156 mod_name = state['__module__'] |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
157 mod = sys.modules[mod_name] |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
158 class_def = getattr(mod, class_name) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
159 obj = class_def.__new__(class_def) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
160 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
161 del state['__class__'] |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
162 del state['__module__'] |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
163 attr_names = list(state.keys()) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
164 for name in attr_names: |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
165 state[name] = _unpickle_object(state[name]) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
166 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
167 obj.__dict__.update(state) |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
168 |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
169 return obj |
298f8f46432a
internal: Add a `fastpickle` module to help with multiprocess serialization.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
170 |