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

Function _function_getstate

cloudpickle/cloudpickle.py:714–754  ·  view source on GitHub ↗
(func)

Source from the content-addressed store, hash-verified

712
713
714def _function_getstate(func):
715 # - Put func's dynamic attributes (stored in func.__dict__) in state. These
716 # attributes will be restored at unpickling time using
717 # f.__dict__.update(state)
718 # - Put func's members into slotstate. Such attributes will be restored at
719 # unpickling time by iterating over slotstate and calling setattr(func,
720 # slotname, slotvalue)
721 slotstate = {
722 # Hack to circumvent non-predictable memoization caused by string interning.
723 # See the inline comment in _class_setstate for details.
724 "__name__": "".join(func.__name__),
725 "__qualname__": "".join(func.__qualname__),
726 "__annotations__": func.__annotations__,
727 "__kwdefaults__": func.__kwdefaults__,
728 "__defaults__": func.__defaults__,
729 "__module__": func.__module__,
730 "__doc__": func.__doc__,
731 "__closure__": func.__closure__,
732 }
733
734 f_globals_ref = _extract_code_globals(func.__code__)
735 f_globals = {k: func.__globals__[k] for k in f_globals_ref if k in func.__globals__}
736
737 if func.__closure__ is not None:
738 closure_values = list(map(_get_cell_contents, func.__closure__))
739 else:
740 closure_values = ()
741
742 # Extract currently-imported submodules used by func. Storing these modules
743 # in a smoke _cloudpickle_subimports attribute of the object's state will
744 # trigger the side effect of importing these modules at unpickling time
745 # (which is necessary for func to work correctly once depickled)
746 slotstate["_cloudpickle_submodules"] = _find_imported_submodules(
747 func.__code__, itertools.chain(f_globals.values(), closure_values)
748 )
749 slotstate["__globals__"] = f_globals
750
751 # Hack to circumvent non-predictable memoization caused by string interning.
752 # See the inline comment in _class_setstate for details.
753 state = {"".join(k): v for k, v in func.__dict__.items()}
754 return state, slotstate
755
756
757def _class_getstate(obj):

Callers 1

Calls 3

_extract_code_globalsFunction · 0.85
joinMethod · 0.80

Tested by

no test coverage detected