MCPcopy Create free account
hub / github.com/cloudpipe/cloudpickle / Pickler

Class Pickler

cloudpickle/cloudpickle.py:1238–1521  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 2

dumpFunction · 0.85
dumpsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected