File size: 1,088 Bytes
7f436a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Copyright (c) Facebook, Inc. and its affiliates.
import cloudpickle


class PicklableWrapper:
    """

    Wrap an object to make it more picklable, note that it uses

    heavy weight serialization libraries that are slower than pickle.

    It's best to use it only on closures (which are usually not picklable).



    This is a simplified version of

    https://github.com/joblib/joblib/blob/master/joblib/externals/loky/cloudpickle_wrapper.py

    """

    def __init__(self, obj):
        while isinstance(obj, PicklableWrapper):
            # Wrapping an object twice is no-op
            obj = obj._obj
        self._obj = obj

    def __reduce__(self):
        s = cloudpickle.dumps(self._obj)
        return cloudpickle.loads, (s,)

    def __call__(self, *args, **kwargs):
        return self._obj(*args, **kwargs)

    def __getattr__(self, attr):
        # Ensure that the wrapped object can be used seamlessly as the previous object.
        if attr not in ["_obj"]:
            return getattr(self._obj, attr)
        return getattr(self, attr)