| 1222 | |
| 1223 | |
| 1224 | class Pickler(pickle.Pickler): |
| 1225 | # set of reducers defined and used by cloudpickle (private) |
| 1226 | _dispatch_table = {} |
| 1227 | _dispatch_table[classmethod] = _classmethod_reduce |
| 1228 | _dispatch_table[io.TextIOWrapper] = _file_reduce |
| 1229 | _dispatch_table[logging.Logger] = _logger_reduce |
| 1230 | _dispatch_table[logging.RootLogger] = _root_logger_reduce |
| 1231 | _dispatch_table[memoryview] = _memoryview_reduce |
| 1232 | _dispatch_table[property] = _property_reduce |
| 1233 | _dispatch_table[staticmethod] = _classmethod_reduce |
| 1234 | _dispatch_table[CellType] = _cell_reduce |
| 1235 | _dispatch_table[types.CodeType] = _code_reduce |
| 1236 | _dispatch_table[types.GetSetDescriptorType] = _getset_descriptor_reduce |
| 1237 | _dispatch_table[types.ModuleType] = _module_reduce |
| 1238 | _dispatch_table[types.MethodType] = _method_reduce |
| 1239 | _dispatch_table[types.MappingProxyType] = _mappingproxy_reduce |
| 1240 | _dispatch_table[weakref.WeakSet] = _weakset_reduce |
| 1241 | _dispatch_table[typing.TypeVar] = _typevar_reduce |
| 1242 | _dispatch_table[_collections_abc.dict_keys] = _dict_keys_reduce |
| 1243 | _dispatch_table[_collections_abc.dict_values] = _dict_values_reduce |
| 1244 | _dispatch_table[_collections_abc.dict_items] = _dict_items_reduce |
| 1245 | _dispatch_table[type(OrderedDict().keys())] = _odict_keys_reduce |
| 1246 | _dispatch_table[type(OrderedDict().values())] = _odict_values_reduce |
| 1247 | _dispatch_table[type(OrderedDict().items())] = _odict_items_reduce |
| 1248 | _dispatch_table[abc.abstractmethod] = _classmethod_reduce |
| 1249 | _dispatch_table[abc.abstractclassmethod] = _classmethod_reduce |
| 1250 | _dispatch_table[abc.abstractstaticmethod] = _classmethod_reduce |
| 1251 | _dispatch_table[abc.abstractproperty] = _property_reduce |
| 1252 | _dispatch_table[dataclasses._FIELD_BASE] = _dataclass_field_base_reduce |
| 1253 | |
| 1254 | dispatch_table = ChainMap(_dispatch_table, copyreg.dispatch_table) |
| 1255 | |
| 1256 | # function reducers are defined as instance methods of cloudpickle.Pickler |
| 1257 | # objects, as they rely on a cloudpickle.Pickler attribute (globals_ref) |
| 1258 | def _dynamic_function_reduce(self, func): |
| 1259 | """Reduce a function that is not pickleable via attribute lookup.""" |
| 1260 | newargs = self._function_getnewargs(func) |
| 1261 | state = _function_getstate(func) |
| 1262 | return (_make_function, newargs, state, None, None, _function_setstate) |
| 1263 | |
| 1264 | def _function_reduce(self, obj): |
| 1265 | """Reducer for function objects. |
| 1266 | |
| 1267 | If obj is a top-level attribute of a file-backed module, this reducer |
| 1268 | returns NotImplemented, making the cloudpickle.Pickler fall back to |
| 1269 | traditional pickle.Pickler routines to save obj. Otherwise, it reduces |
| 1270 | obj using a custom cloudpickle reducer designed specifically to handle |
| 1271 | dynamic functions. |
| 1272 | """ |
| 1273 | if _should_pickle_by_reference(obj): |
| 1274 | return NotImplemented |
| 1275 | else: |
| 1276 | return self._dynamic_function_reduce(obj) |
| 1277 | |
| 1278 | def _function_getnewargs(self, func): |
| 1279 | code = func.__code__ |
| 1280 | |
| 1281 | # base_globals represents the future global namespace of func at |