def NamedTemporaryFile( mode="w+b", buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, wrapper_class_override=None, ): """Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to io.open (default "w+b"). 'buffering' -- the buffer size argument to io.open (default -1). 'encoding' -- the encoding argument to io.open (default None) 'newline' -- the newline argument to io.open (default None) 'delete' -- whether the file is deleted on close (default True). The file is created as mkstemp() would do it. Returns an object with a file-like interface; the name of the file is accessible as its 'name' attribute. The file will be automatically deleted when it is closed unless the 'delete' argument is set to False. """ prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir) flags = _bin_openflags # Setting O_TEMPORARY in the flags causes the OS to delete # the file when it is closed. This is only supported by Windows. if not wrapper_class_override: wrapper_class_override = _TemporaryFileWrapper if os.name == "nt" and delete: flags |= os.O_TEMPORARY if sys.version_info < (3, 5): (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags) else: (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type) try: file = io.open(fd, mode, buffering=buffering, newline=newline, encoding=encoding) if wrapper_class_override is not None: return type(str("_TempFileWrapper"), (wrapper_class_override, object), {})(file, name, delete) else: return _TemporaryFileWrapper(file, name, delete) except BaseException: os.unlink(name) os.close(fd) raise
def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="", prefix=template, dir=None, delete=True): """Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to os.fdopen (default "w+b"). 'bufsize' -- the buffer size argument to os.fdopen (default -1). 'delete' -- whether the file is deleted on close (default True). The file is created as mkstemp() would do it. Returns an object with a file-like interface; the name of the file is accessible as file.name. The file will be automatically deleted when it is closed unless the 'delete' argument is set to False. """ if dir is None: dir = gettempdir() if 'b' in mode: flags = _bin_openflags else: flags = _text_openflags # Setting O_TEMPORARY in the flags causes the OS to delete # the file when it is closed. This is only supported by Windows. if _os.name == 'nt' and delete: flags |= _os.O_TEMPORARY (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags) file = _os.fdopen(fd, mode, bufsize) return _TemporaryFileWrapper(file, name, delete)
def __init__(self, dir, pre, suf, bin): if bin: flags = self._bflags else: flags = self._tflags (self.fd, self.name) = tempfile._mkstemp_inner(dir, pre, suf, flags)
def test_collision_with_existing_directory(self): # _mkstemp_inner tries another name when a directory with # the chosen name already exists container_dir = tempfile.mkdtemp() try: def mock_get_candidate_names(): return iter(['aaa', 'aaa', 'bbb']) with support.swap_attr(tempfile, '_get_candidate_names', mock_get_candidate_names): dir = tempfile.mkdtemp(dir=container_dir) self.assertTrue(dir.endswith('aaa')) flags = tempfile._bin_openflags (fd, name) = tempfile._mkstemp_inner(container_dir, tempfile.template, '', flags) try: self.assertTrue(name.endswith('bbb')) finally: os.close(fd) os.unlink(name) finally: support.rmtree(container_dir)
def __init__(self, dir: str, pre: str, suf: str, bin: int) -> None: if bin: flags = self._bflags else: flags = self._tflags (self.fd, self.name) = tempfile._mkstemp_inner(dir, pre, suf, flags) self._close = os.close self._unlink = os.unlink
def __init__(self, dir, pre, suf, bin): if bin: flags = self._bflags else: flags = self._tflags output_type = tempfile._infer_return_type(dir, pre, suf) self.fd, self.name = tempfile._mkstemp_inner( dir, pre, suf, flags, output_type)
def NamedTemporaryFile( mode="w+b", buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, ): """Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to io.open (default "w+b"). 'buffering' -- the buffer size argument to io.open (default -1). 'encoding' -- the encoding argument to io.open (default None) 'newline' -- the newline argument to io.open (default None) 'delete' -- whether the file is deleted on close (default True). The file is created as mkstemp() would do it. Returns an object with a file-like interface; the name of the file is accessible as its 'name' attribute. The file will be automatically deleted when it is closed unless the 'delete' argument is set to False. """ prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir) flags = _bin_openflags # Setting O_TEMPORARY in the flags causes the OS to delete # the file when it is closed. This is only supported by Windows. if os.name == "nt" and delete: flags |= os.O_TEMPORARY if sys.version_info < (3, 5): (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags) else: (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type) try: file = io.open( fd, mode, buffering=buffering, newline=newline, encoding=encoding ) return _TemporaryFileWrapper(file, name, delete) except BaseException: os.unlink(name) os.close(fd) raise
def __init__(self, dir, pre, suf, bin): if bin: flags = self._bflags else: flags = self._tflags # XXX: CPython assigns _close/_unlink as class vars but this # would rebind Jython's close/unlink (to be classmethods) # because they're not built-in functions (unfortunately # built-in functions act differently when binding: # http://mail.python.org/pipermail/python-dev/2003-April/034749.html) self._close = os.close self._unlink = os.unlink (self.fd, self.name) = tempfile._mkstemp_inner(dir, pre, suf, flags)
def get_named_tempfile(delete): try: return tempfile.NamedTemporaryFile(delete=delete) except TypeError: if os.name == "nt": dir_ = tempfile.gettempdir() flags = tempfile._bin_openflags if delete: flags |= os.O_TEMPORARY (fd, name) = tempfile._mkstemp_inner(dir_, "tmp", "", flags) f = os.fdopen(fd, "w+b", -1) return tempfile._TemporaryFileWrapper(f, name) else: f = tempfile.NamedTemporaryFile() if not delete: def close(): f.close_called = True f.file.close() f.close = close return f
def default_mkstemp_inner(self): return tempfile._mkstemp_inner(tempfile.gettempdir(), tempfile.template, "", tempfile._bin_openflags)
def update_event(self, inp=-1): self.set_output_val( 0, tempfile._mkstemp_inner(self.input(0), self.input(1), self.input(2), self.input(3), self.input(4)))
def make_temp(self): return tempfile._mkstemp_inner(tempfile.gettempdir(), tempfile.template, '', tempfile._bin_openflags)
def make_temp(self): return tempfile._mkstemp_inner(tempfile.gettempdir(), tempfile.gettempprefix(), '', tempfile._bin_openflags, str)
def __init__(self, dir, pre, suf, bin): if bin: flags = self._bflags else: flags = self._tflags output_type = tempfile._infer_return_type(dir, pre, suf) (self.fd, self.name) = tempfile._mkstemp_inner(dir, pre, suf, flags, output_type)