I just realized, that I can simplify mktemp
by directly returning the tempfile._TemporaryFileWrapper
's buffer, which can be used in a file context for open and closing without deleting the superordinate _TemporaryFileWrapper
:
def mktemp(self, mode='w+b', encoding=None, suffix=''):
"""Creates and registers a named temporary
file and returns the file object.
"""
return self.enter_context(NamedTemporaryFile(
mode=mode, encoding=encoding, suffix=suffix, prefix=self.prefix,
dir=self.root)).file