'unpatch and clean up, returns the self.tmpfile (file object)'

def done(self):

    os.dup2(self._savefd, self.targetfd)
    os.close(self._savefd)
    if (self.targetfd != 0):
        self.tmpfile.seek(0)
    if hasattr(self, '_oldsys'):
        setattr(sys, patchsysdict[self.targetfd], self._oldsys)
    return self.tmpfile
