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

Function _class_getstate

cloudpickle/cloudpickle.py:757–799  ·  view source on GitHub ↗
(obj)

Source from the content-addressed store, hash-verified

755
756
757def _class_getstate(obj):
758 clsdict = _extract_class_dict(obj)
759 clsdict.pop("__weakref__", None)
760
761 if issubclass(type(obj), abc.ABCMeta):
762 # If obj is an instance of an ABCMeta subclass, don't pickle the
763 # cache/negative caches populated during isinstance/issubclass
764 # checks, but pickle the list of registered subclasses of obj.
765 clsdict.pop("_abc_cache", None)
766 clsdict.pop("_abc_negative_cache", None)
767 clsdict.pop("_abc_negative_cache_version", None)
768 registry = clsdict.pop("_abc_registry", None)
769 if registry is None:
770 # The abc caches and registered subclasses of a
771 # class are bundled into the single _abc_impl attribute
772 clsdict.pop("_abc_impl", None)
773 (registry, _, _, _) = abc._get_dump(obj)
774
775 clsdict["_abc_impl"] = [subclass_weakref() for subclass_weakref in registry]
776 else:
777 # In the above if clause, registry is a set of weakrefs -- in
778 # this case, registry is a WeakSet
779 clsdict["_abc_impl"] = [type_ for type_ in registry]
780
781 if "__slots__" in clsdict:
782 # pickle string length optimization: member descriptors of obj are
783 # created automatically from obj's __slots__ attribute, no need to
784 # save them in obj's state
785 if isinstance(obj.__slots__, str):
786 clsdict.pop(obj.__slots__)
787 else:
788 for k in obj.__slots__:
789 clsdict.pop(k, None)
790
791 clsdict.pop("__dict__", None) # unpicklable property object
792
793 if sys.version_info >= (3, 14):
794 # PEP-649/749: __annotate_func__ contains a closure that references the class
795 # dict. We need to exclude it from pickling. Python will recreate it when
796 # __annotations__ is accessed at unpickling time.
797 clsdict.pop("__annotate_func__", None)
798
799 return (clsdict, {})
800
801
802def _enum_getstate(obj):

Callers 2

_enum_getstateFunction · 0.85
_dynamic_class_reduceFunction · 0.85

Calls 1

_extract_class_dictFunction · 0.85

Tested by

no test coverage detected