'Open a file named `filename` in a temporary directory.
This context manager is preferred over `NamedTemporaryFile` in
stdlib `tempfile` when one needs to reopen the file.
Arguments `mode` and `bufsize` are passed to `open`.
Rest of the arguments are passed to `TemporaryDirectory`.'

def __init__(self, filename, mode='w+b', bufsize=(-1), **kwds):

    self._tmpdir = TemporaryDirectory(**kwds)
    path = _os.path.join(self._tmpdir.name, filename)
    self.file = open(path, mode, bufsize)
