In Python 3.1, the following hack works:
import copyreg
def functionpickler(f):
print('pickling', f.__name__)
return f.__name__
ft = type(functionpickler)
copyreg.pickle(ft, functionpickler)
import pickle
pickle.Pickler = pickle._Pickler
del pickle.Pickler.dispatch[ft]
s = pickle.dumps(functionpickler)
print('Result is', s)
From this, two hacker lines:
pickle.Pickler = pickle._Pickler
del pickle.Pickler.dispatch[ft]
You need to delete the entry dispatchfor the function type, because otherwise it unloads the registration of copyreg; and I don’t think you can do it on a C-encoded Pickler, so you need to install it on a Python-encoded one.
It would be a little less hacking a subclass _Picklerwith its own class that does its own dispatch(copying the parent element and deleting the entry for the function type), and then use your subclass specifically (and its dump method), rather than pickle.dump; however, it would also be a little less convenient if this monkey were to pickle.