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

Function _should_pickle_by_reference

cloudpickle/cloudpickle.py:239–272  ·  view source on GitHub ↗

Test whether an function or a class should be pickled by reference Pickling by reference means by that the object (typically a function or a class) is an attribute of a module that is assumed to be importable in the target Python environment. Loading will therefore rely on importing the

(obj, name=None)

Source from the content-addressed store, hash-verified

237
238
239def _should_pickle_by_reference(obj, name=None):
240 """Test whether an function or a class should be pickled by reference
241
242 Pickling by reference means by that the object (typically a function or a
243 class) is an attribute of a module that is assumed to be importable in the
244 target Python environment. Loading will therefore rely on importing the
245 module and then calling `getattr` on it to access the function or class.
246
247 Pickling by reference is the only option to pickle functions and classes
248 in the standard library. In cloudpickle the alternative option is to
249 pickle by value (for instance for interactively or locally defined
250 functions and classes or for attributes of modules that have been
251 explicitly registered to be pickled by value.
252 """
253 if isinstance(obj, types.FunctionType) or issubclass(type(obj), type):
254 module_and_name = _lookup_module_and_qualname(obj, name=name)
255 if module_and_name is None:
256 return False
257 module, name = module_and_name
258 return not _is_registered_pickle_by_value(module)
259
260 elif isinstance(obj, types.ModuleType):
261 # We assume that sys.modules is primarily used as a cache mechanism for
262 # the Python import machinery. Checking if a module has been added in
263 # is sys.modules therefore a cheap and simple heuristic to tell us
264 # whether we can assume that a given module could be imported by name
265 # in another Python process.
266 if _is_registered_pickle_by_value(obj):
267 return False
268 return obj.__name__ in sys.modules
269 else:
270 raise TypeError(
271 "cannot check importability of {} instances".format(type(obj).__name__)
272 )
273
274
275def _lookup_module_and_qualname(obj, name=None):

Callers 6

_module_reduceFunction · 0.85
_class_reduceFunction · 0.85
_function_reduceMethod · 0.85
save_globalMethod · 0.85
save_functionMethod · 0.85

Calls 2

Tested by 1