I'm trying to pickle quite an involved object hierarchy and getting the exception:
pickle.PicklingError: Can't pickle <class 'function'>: attribute lookup builtins.function failed
Are there any reasonable methods one can use to test the pickleablility of an object hierarchy? My aim would be to find the location of the offending function
To do this, I'd use dill, which can serialize almost anything in python. Dill also has some good tools for helping you understand what is causing your pickling to fail when your code fails.
If you absolutely wanted to, you could use dill's
badobjects
(or one of the other detection functions) to dive recursively into your object's reference chain, and pop out the unpickleable objects, instead of calling it at at every depth, as above.Also, objgraph is a pretty handy compliment to the test suite too.
Here is a slightly tweaked version of @Sheena's code that also works with python 2 and some additional types:
I found this the most useful option (also from the far more forgiving
dill
at places where pickle wasn't). You can simply run it withI did this, it does the trick for me a lot of the time... I'll update this once I have found something totally foolproof
It makes a bunch of prints then raises an exception if there is one to be raised so you can see what part of the object hierarchy is causing the problem.