(self, obj)
Save a class that can't be stored as module global.

        This method is used to serialize classes that are defined inside
        functions, or that otherwise can't be serialized as attribute lookups
        from global modules.
Save a class that can't be stored as module global.
Save a class that can t be stored as module global .
def save_dynamic_class(self, obj):
        """Save a class that can't be stored as module global.

        This method is used to serialize classes that are defined inside
        functions, or that otherwise can't be serialized as attribute lookups
        from global modules.
        """
        clsdict = _extract_class_dict(obj)
        clsdict.pop("__weakref__", None)

        # For ABCMeta in python3.7+, remove _abc_impl as it is not picklable.
        # This is a fix which breaks the cache but this only makes the first
        # calls to issubclass slower.
        if "_abc_impl" in clsdict:
            import abc

            (registry, _, _, _) = abc._get_dump(obj)
            clsdict["_abc_impl"] = [subclass_weakref() for subclass_weakref in registry]

        # On PyPy, __doc__ is a readonly attribute, so we need to include it in
        # the initial skeleton class.  This is safe because we know that the
        # doc can't participate in a cycle with the original class.
        type_kwargs = {"__doc__": clsdict.pop("__doc__", None)}

        if hasattr(obj, "__slots__"):
            type_kwargs["__slots__"] = obj.__slots__
            # pickle string length optimization: member descriptors of obj are
            # created automatically from obj's __slots__ attribute, no need to
            # save them in obj's state
            if isinstance(obj.__slots__, string_types):
                clsdict.pop(obj.__slots__)
            else:
                for k in obj.__slots__:
                    clsdict.pop(k, None)

        # If type overrides __dict__ as a property, include it in the type
        # kwargs. In Python 2, we can't set this attribute after construction.
        __dict__ = clsdict.pop("__dict__", None)
        if isinstance(__dict__, property):
            type_kwargs["__dict__"] = __dict__

        save = self.save
        write = self.write

        # We write pickle instructions explicitly here to handle the
        # possibility that the type object participates in a cycle with its own
        # __dict__. We first write an empty "skeleton" version of the class and
        # memoize it before writing the class' __dict__ itself. We then write
        # instructions to "rehydrate" the skeleton class by restoring the
        # attributes from the __dict__.
        #
        # A type can appear in a cycle with its __dict__ if an instance of the
        # type appears in the type's __dict__ (which happens for the stdlib
        # Enum class), or if the type defines methods that close over the name
        # of the type, (which is common for Python 2-style super() calls).

        # Push the rehydration function.
        save(_rehydrate_skeleton_class)

        # Mark the start of the args tuple for the rehydration function.
        write(pickle.MARK)

        # Create and memoize an skeleton class with obj's name and bases.
        if Enum is not None and issubclass(obj, Enum):
            # Special handling of Enum subclasses
            self._save_dynamic_enum(obj, clsdict)
        else:
            # "Regular" class definition:
            tp = type(obj)
            self.save_reduce(
                _make_skeleton_class,
                (
                    tp,
                    obj.__name__,
                    obj.__bases__,
                    type_kwargs,
                    _ensure_tracking(obj),
                    None,
                ),
                obj=obj,
            )

        # Now save the rest of obj's __dict__. Any references to obj
        # encountered while saving will point to the skeleton class.
        save(clsdict)

        # Write a tuple of (skeleton_class, clsdict).
        write(pickle.TUPLE)

        # Call _rehydrate_skeleton_class(skeleton_class, clsdict)
        write(pickle.REDUCE)
def save_dynamic_class ( self , obj ) : clsdict = _extract_class_dict ( obj ) clsdict . pop ( "__weakref__" , None ) # For ABCMeta in python3.7+, remove _abc_impl as it is not picklable. # This is a fix which breaks the cache but this only makes the first # calls to issubclass slower. if "_abc_impl" in clsdict : import abc ( registry , _ , _ , _ ) = abc . _get_dump ( obj ) clsdict [ "_abc_impl" ] = [ subclass_weakref ( ) for subclass_weakref in registry ] # On PyPy, __doc__ is a readonly attribute, so we need to include it in # the initial skeleton class.  This is safe because we know that the # doc can't participate in a cycle with the original class. type_kwargs = { "__doc__" : clsdict . pop ( "__doc__" , None ) } if hasattr ( obj , "__slots__" ) : type_kwargs [ "__slots__" ] = obj . __slots__ # pickle string length optimization: member descriptors of obj are # created automatically from obj's __slots__ attribute, no need to # save them in obj's state if isinstance ( obj . __slots__ , string_types ) : clsdict . pop ( obj . __slots__ ) else : for k in obj . __slots__ : clsdict . pop ( k , None ) # If type overrides __dict__ as a property, include it in the type # kwargs. In Python 2, we can't set this attribute after construction. __dict__ = clsdict . pop ( "__dict__" , None ) if isinstance ( __dict__ , property ) : type_kwargs [ "__dict__" ] = __dict__ save = self . save write = self . write # We write pickle instructions explicitly here to handle the # possibility that the type object participates in a cycle with its own # __dict__. We first write an empty "skeleton" version of the class and # memoize it before writing the class' __dict__ itself. We then write # instructions to "rehydrate" the skeleton class by restoring the # attributes from the __dict__. # # A type can appear in a cycle with its __dict__ if an instance of the # type appears in the type's __dict__ (which happens for the stdlib # Enum class), or if the type defines methods that close over the name # of the type, (which is common for Python 2-style super() calls). # Push the rehydration function. save ( _rehydrate_skeleton_class ) # Mark the start of the args tuple for the rehydration function. write ( pickle . MARK ) # Create and memoize an skeleton class with obj's name and bases. if Enum is not None and issubclass ( obj , Enum ) : # Special handling of Enum subclasses self . _save_dynamic_enum ( obj , clsdict ) else : # "Regular" class definition: tp = type ( obj ) self . save_reduce ( _make_skeleton_class , ( tp , obj . __name__ , obj . __bases__ , type_kwargs , _ensure_tracking ( obj ) , None , ) , obj = obj , ) # Now save the rest of obj's __dict__. Any references to obj # encountered while saving will point to the skeleton class. save ( clsdict ) # Write a tuple of (skeleton_class, clsdict). write ( pickle . TUPLE ) # Call _rehydrate_skeleton_class(skeleton_class, clsdict) write ( pickle . REDUCE )