MCPcopy Index your code
hub / github.com/cloudpipe/cloudpickle / _function_getstate

Function _function_getstate

cloudpickle/cloudpickle.py:707–747  ·  view source on GitHub ↗
(func)

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 3

_extract_code_globalsFunction · 0.85
joinMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…