On 07/18/2017 01:07 AM, dieter wrote:
"Jan Gosmann" <j...@hyper-world.de> writes:
[...]
fn = load_pyfile('fn.py')['fn']
[...]
"pickle" (and "cpickle") are serializing functions as so called
"global"s, i.e. as a module reference together with a name.
This means, they cannot handle functions computed in a module
(as in your case).
Note that I'm assigning the computed function to a global/module level
variable. As far as I understand the documentation
<https://docs.python.org/2/library/pickle.html#what-can-be-pickled-and-unpickled>
that should be all that matters because only the function name will be
serialized.
I am quite convinced that "pickle" will not be able to deserialize (i.e. load)
your function (even though it appears to perform the serialization
(i.e. dump).
Actually the deserialization works fine with either module. That is both
pickle.loads(pickle.dumps(fn)) and cPickle.loads(pickle.dumps(fn)) give
me back the function.
By now I realized that a pretty simple workaround works. Instead of
doing `fn = load_pyfile('fn.py')['fn']` the following function
definition works with both pickle modules:
_fn = load_pyfile('fn.py')['fn']
def fn(*args, **kwargs):
return _fn(*args, **kwargs)
--
https://mail.python.org/mailman/listinfo/python-list