reconstruct python method with kwargs with marshal and types?

278 views Asked by At

I am using the marshal module to serialize some Python methods, and reconstruct them using types module (Is there an easy way to pickle a python function (or otherwise serialize its code)?). I am having trouble getting it to work with optional kwargs. E.g.

import marshal
import types

def test_func(x, y, kw='asdf'):
   return x, y, kw
serialized_code_string = marshal.dumps(test_func.func_code)
# In real code there is a bunch of file I/O here instead
unserialized_code_string = marshal.load(code_string)
new_func = types.FunctionType(code, globals(), "deserialized_function")

When I run new_func(1,2) I get the error that new_func() takes exactly 3 arguments (2 given), even though the kwarg is optional. I think that the issue arises in reconstructing the function from the func_code, but I am not sure how to fix this. If there isn't a way, I would be interested in alternative way to reconstruct the function that keeps the kwarg functionality.

1

There are 1 answers

0
Andrew On BEST ANSWER

After some more searching I discovered the dill package, which allows you to pickle more things than cPickle (the reason I was using marshal). When you directly reconstruct a serialized function (not using the func_code), you avoid this keyword issue I was having.