I’m using cPickle to serialize data that’s used for logging.
I’d like to be able to throw whatever I want into an object, then serialize it. Usually this is fine with cPickle, but just ran into a problem where one of the objects I wanted to serialize contained a function. This caused cPickle to raise an exception.
I would rather cPickle just skipped over stuff it can’t deal with instead of causing the whole process to implode.
What is a good way to make this happen?
I’m assuming that you’re looking for a best-effort solution and you’re okay if the unpickled results don’t function properly.
For your particular use case, you may want to register a pickle handler for function objects. Just make it a dummy handler that’s good enough for your best-effort purposes. Making a handler for functions is possible, it’s rather tricky. To avoid affecting other code that pickles, you’ll probably want to deregister the handler when exiting your logging code.
Here’s an example (without any deregistration):
It prints: