def save_file(self, obj): 
try:<EOL><INDENT>import StringIO as pystringIO <EOL><DEDENT>except ImportError:<EOL><INDENT>import io as pystringIO <EOL><DEDENT>if not hasattr(obj, '<STR_LIT:name>') or  not hasattr(obj, '<STR_LIT>'):<EOL><INDENT>raise pickle.PicklingError("<STR_LIT>")<EOL><DEDENT>if obj is sys.stdout:<EOL><INDENT>return self.save_reduce(getattr, (sys, '<STR_LIT>'), obj=obj)<EOL><DEDENT>if obj is sys.stderr:<EOL><INDENT>return self.save_reduce(getattr, (sys, '<STR_LIT>'), obj=obj)<EOL><DEDENT>if obj is sys.stdin:<EOL><INDENT>raise pickle.PicklingError("<STR_LIT>")<EOL><DEDENT>if  hasattr(obj, '<STR_LIT>') and obj.isatty():<EOL><INDENT>raise pickle.PicklingError("<STR_LIT>")<EOL><DEDENT>if '<STR_LIT:r>' not in obj.mode:<EOL><INDENT>raise pickle.PicklingError("<STR_LIT>")<EOL><DEDENT>name = obj.name<EOL>try:<EOL><INDENT>fsize = os.stat(name).st_size<EOL><DEDENT>except OSError:<EOL><INDENT>raise pickle.PicklingError("<STR_LIT>" % name)<EOL><DEDENT>if obj.closed:<EOL><INDENT>retval = pystringIO.StringIO("<STR_LIT>")<EOL>retval.close()<EOL><DEDENT>elif not fsize: <EOL><INDENT>retval = pystringIO.StringIO("<STR_LIT>")<EOL>try:<EOL><INDENT>tmpfile = file(name)<EOL>tst = tmpfile.read(<NUM_LIT:1>)<EOL><DEDENT>except IOError:<EOL><INDENT>raise pickle.PicklingError("<STR_LIT>" % name)<EOL><DEDENT>tmpfile.close()<EOL>if tst != '<STR_LIT>':<EOL><INDENT>raise pickle.PicklingError(<EOL>"<STR_LIT>" % name)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>tmpfile = file(name)<EOL>contents = tmpfile.read()<EOL>tmpfile.close()<EOL><DEDENT>except IOError:<EOL><INDENT>raise pickle.PicklingError("<STR_LIT>" % name)<EOL><DEDENT>retval = pystringIO.StringIO(contents)<EOL>curloc = obj.tell()<EOL>retval.seek(curloc)<EOL><DEDENT>retval.name = name<EOL>self.save(retval)<EOL>self.memoize(obj)<EOL>
Save a file