Author Olivier.Grisel
Recipients Olivier.Grisel, alexandre.vassalotti, pierreglaser, pitrou, serhiy.storchaka
Date 2019-02-13.10:54:45
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Adding such a hook would make it possible to reimplement cloudpickle.CloudPickler by deriving from the fast _pickle.Pickler class (instead of the slow pickle._Pickler as done currently). This would mean rewriting most of the CloudPickler method to only rely on a save_reduce-style design instead of directly calling pickle._Pickler.write and This is tedious but doable.

There is however a blocker with the current way closures are set: when we pickle a dynamically defined function (e.g. lambda, nested function or function in __main__), we currently use a direct call to memoize ( so as to be able to refer to the function itself in its own closure without causing an infinite loop in CloudPickler.dump. This also makes possible to pickle mutually recursive functions.

The easiest way to avoid having to call memoize explicitly would be to be able to pass the full __closure__ attribute in the state dict of the reduce call. Indeed the save_reduce function calls memoize automatically after saving the reconstructor and its args but prior to saving the state:

It would therefore be possible to pass a (state, slotstate) tuple with the closure in slotstate that so it could be reconstructed at unpickling time with a setattr:

However, it is currently not possible to setattr __closure__ at the moment. We can only set individual closure cell contents (which is not compatible with the setattr state trick described above).

To summarize, we need to implement the setter function for the __closure__ attribute of functions and methods to make it natural to reimplement the CloudPickler by inheriting from _pickle.Pickler using the hook described in this issue.
Date User Action Args
2019-02-13 10:54:45Olivier.Griselsetrecipients: + Olivier.Grisel, pitrou, alexandre.vassalotti, serhiy.storchaka, pierreglaser
2019-02-13 10:54:45Olivier.Griselsetmessageid: <>
2019-02-13 10:54:45Olivier.Grisellinkissue35900 messages
2019-02-13 10:54:45Olivier.Griselcreate