C implementation _pickle doesn't respect dispatch_table for native types in Python 3.6

87 Views Asked by At

I am trying to pickle lambdas with the native pickle module by providing custom serializing function as:

from six.moves import copyreg
import pickle, types
copyreg.pickle(types.FunctionType, _custom_pickle_func)

However, this doesn't work for following types which pickle supports natively:

import pickle
pickle._Pickler.dispatch

{NoneType: <function pickle._Pickler.save_none>,
 bool: <function pickle._Pickler.save_bool>,
 bytes: <function pickle._Pickler.save_bytes>,
 dict: <function pickle._Pickler.save_dict>,
 float: <function pickle._Pickler.save_float>,
 frozenset: <function pickle._Pickler.save_frozenset>,
 function: <function pickle._Pickler.save_global>,
 int: <function pickle._Pickler.save_long>,
 list: <function pickle._Pickler.save_list>,
 set: <function pickle._Pickler.save_set>,
 str: <function pickle._Pickler.save_str>,
 tuple: <function pickle._Pickler.save_tuple>,
 type: <function pickle._Pickler.save_type>}

This is because C{pickle._Pickler} looks for entries in its own dispatch table C{pickle.dispatch} first and then in the C{copyreg.dispath_table}. This prevents our registered handlers to come into play. A workaround here would be to override dispatch functions for native types in both _pickle and pickle. We can do this for pure Python C{pickle._Pickler} as is done by dill, but overriding these entries for _pickle seems impossible as type of _pickle.Pickler.dispatch_table is member_descriptor which doesn’t support assignment from Python interpreter. Please correct me if I am wrong or if there is any way to override for _pickle.

0

There are 0 best solutions below