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

Function _function_setstate

cloudpickle/cloudpickle.py:1144–1175  ·  view source on GitHub ↗

Update the state of a dynamic function. As __closure__ and __globals__ are readonly attributes of a function, we cannot rely on the native setstate routine of pickle.load_build, that calls setattr on items of the slotstate. Instead, we have to modify them inplace.

(obj, state)

Source from the content-addressed store, hash-verified

1142
1143
1144def _function_setstate(obj, state):
1145 """Update the state of a dynamic function.
1146
1147 As __closure__ and __globals__ are readonly attributes of a function, we
1148 cannot rely on the native setstate routine of pickle.load_build, that calls
1149 setattr on items of the slotstate. Instead, we have to modify them inplace.
1150 """
1151 state, slotstate = state
1152 obj.__dict__.update(state)
1153
1154 obj_globals = slotstate.pop("__globals__")
1155 obj_closure = slotstate.pop("__closure__")
1156 # _cloudpickle_subimports is a set of submodules that must be loaded for
1157 # the pickled function to work correctly at unpickling time. Now that these
1158 # submodules are depickled (hence imported), they can be removed from the
1159 # object's state (the object state only served as a reference holder to
1160 # these submodules)
1161 slotstate.pop("_cloudpickle_submodules")
1162
1163 obj.__globals__.update(obj_globals)
1164 obj.__globals__["__builtins__"] = __builtins__
1165
1166 if obj_closure is not None:
1167 for i, cell in enumerate(obj_closure):
1168 try:
1169 value = cell.cell_contents
1170 except ValueError: # cell is empty
1171 continue
1172 obj.__closure__[i].cell_contents = value
1173
1174 for k, v in slotstate.items():
1175 setattr(obj, k, v)
1176
1177
1178def _class_setstate(obj, state):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected