'save targetfd descriptor, and open a new
temporary file there.  If no tmpfile is
specified a tempfile.Tempfile() will be opened
in text mode.'

def __init__(self, targetfd, tmpfile=None, now=True, patchsys=False):

    self.targetfd = targetfd
    if ((tmpfile is None) and (targetfd != 0)):
        f = tempfile.TemporaryFile('wb+')
        tmpfile = dupfile(f, encoding='UTF-8')
        f.close()
    self.tmpfile = tmpfile
    self._savefd = os.dup(self.targetfd)
    if patchsys:
        self._oldsys = getattr(sys, patchsysdict[targetfd])
    if now:
        self.start()
