I would like to serialize on machine A and deserialize on machine B a python lambda. There are a couple of obvious problems with that:

  • the pickle module does not serialize or deserialize code. It only serializes the names of classes/methods/functions
  • some of the answers I found with google suggest the use of the low-level marshal module to serialize the func_code attribute of the lambda but they fail to describe how one could reconstruct a function object from the deserialized code object
  • marhshal(l.func_code) will not serialize the closure associated with the lambda which leads to the problem of detecting when a given lambda really needs a closure and warning the user that he is trying to serialize a lambda that uses a closure

Hence, my question(s):

  • how would one reconstruct a function from the deserialized (demarshaled) code object ?
  • how would one detect that a given lambda will not work properly without the associated closure ?
Edit
Report