(self, obj)
Save a file
Save a file
Save a file
def save_file(self, obj):
        """Save a file"""
        try:
            import StringIO as pystringIO  # we can't use cStringIO as it lacks the name attribute
        except ImportError:
            import io as pystringIO

        if not hasattr(obj, "name") or not hasattr(obj, "mode"):
            raise pickle.PicklingError(
                "Cannot pickle files that do not map to an actual file"
            )
        if obj is sys.stdout:
            return self.save_reduce(getattr, (sys, "stdout"), obj=obj)
        if obj is sys.stderr:
            return self.save_reduce(getattr, (sys, "stderr"), obj=obj)
        if obj is sys.stdin:
            raise pickle.PicklingError("Cannot pickle standard input")
        if obj.closed:
            raise pickle.PicklingError("Cannot pickle closed files")
        if hasattr(obj, "isatty") and obj.isatty():
            raise pickle.PicklingError("Cannot pickle files that map to tty objects")
        if "r" not in obj.mode and "+" not in obj.mode:
            raise pickle.PicklingError(
                "Cannot pickle files that are not opened for reading: %s" % obj.mode
            )

        name = obj.name

        retval = pystringIO.StringIO()

        try:
            # Read the whole file
            curloc = obj.tell()
            obj.seek(0)
            contents = obj.read()
            obj.seek(curloc)
        except IOError:
            raise pickle.PicklingError(
                "Cannot pickle file %s as it cannot be read" % name
            )
        retval.write(contents)
        retval.seek(curloc)

        retval.name = name
        self.save(retval)
        self.memoize(obj)
def save_file ( self , obj ) : try : import StringIO as pystringIO # we can't use cStringIO as it lacks the name attribute except ImportError : import io as pystringIO if not hasattr ( obj , "name" ) or not hasattr ( obj , "mode" ) : raise pickle . PicklingError ( "Cannot pickle files that do not map to an actual file" ) if obj is sys . stdout : return self . save_reduce ( getattr , ( sys , "stdout" ) , obj = obj ) if obj is sys . stderr : return self . save_reduce ( getattr , ( sys , "stderr" ) , obj = obj ) if obj is sys . stdin : raise pickle . PicklingError ( "Cannot pickle standard input" ) if obj . closed : raise pickle . PicklingError ( "Cannot pickle closed files" ) if hasattr ( obj , "isatty" ) and obj . isatty ( ) : raise pickle . PicklingError ( "Cannot pickle files that map to tty objects" ) if "r" not in obj . mode and "+" not in obj . mode : raise pickle . PicklingError ( "Cannot pickle files that are not opened for reading: %s" % obj . mode ) name = obj . name retval = pystringIO . StringIO ( ) try : # Read the whole file curloc = obj . tell ( ) obj . seek ( 0 ) contents = obj . read ( ) obj . seek ( curloc ) except IOError : raise pickle . PicklingError ( "Cannot pickle file %s as it cannot be read" % name ) retval . write ( contents ) retval . seek ( curloc ) retval . name = name self . save ( retval ) self . memoize ( obj )