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

Function _class_setstate

cloudpickle/cloudpickle.py:1178–1217  ·  view source on GitHub ↗
(obj, state)

Source from the content-addressed store, hash-verified

1176
1177
1178def _class_setstate(obj, state):
1179 state, slotstate = state
1180 registry = None
1181 for attrname, attr in state.items():
1182 if attrname == "_abc_impl":
1183 registry = attr
1184 else:
1185 # Note: setting attribute names on a class automatically triggers their
1186 # interning in CPython:
1187 # https://github.com/python/cpython/blob/v3.12.0/Objects/object.c#L957
1188 #
1189 # This means that to get deterministic pickling for a dynamic class that
1190 # was initially defined in a different Python process, the pickler
1191 # needs to ensure that dynamic class and function attribute names are
1192 # systematically copied into a non-interned version to avoid
1193 # unpredictable pickle payloads.
1194 #
1195 # Indeed the Pickler's memoizer relies on physical object identity to break
1196 # cycles in the reference graph of the object being serialized.
1197 setattr(obj, attrname, attr)
1198
1199 if sys.version_info >= (3, 13) and "__firstlineno__" in state:
1200 # Set the Python 3.13+ only __firstlineno__ attribute one more time, as it
1201 # will be automatically deleted by the `setattr(obj, attrname, attr)` call
1202 # above when `attrname` is "__firstlineno__". We assume that preserving this
1203 # information might be important for some users and that it not stale in the
1204 # context of cloudpickle usage, hence legitimate to propagate. Furthermore it
1205 # is necessary to do so to keep deterministic chained pickling as tested in
1206 # test_deterministic_str_interning_for_chained_dynamic_class_pickling.
1207 obj.__firstlineno__ = state["__firstlineno__"]
1208
1209 if registry is not None:
1210 for subclass in registry:
1211 obj.register(subclass)
1212
1213 # PEP-649/749: During pickling, we excluded the __annotate_func__ attribute but it
1214 # will be created by Python. Subsequently, annotations will be recreated when
1215 # __annotations__ is accessed.
1216
1217 return obj
1218
1219
1220# COLLECTION OF DATACLASS UTILITIES

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected