Skip to content
Snippets Groups Projects
Commit 11161f99 authored by Tristan Van Berkom's avatar Tristan Van Berkom Committed by Tristan Van Berkom
Browse files

utils.py: Give save_file_atomic() a tempdir argument

Allow callers to decide where the temporary file will be created.
parent 0a1f8e3c
No related tags found
1 merge request!854fix cache size race
......@@ -502,7 +502,7 @@ def get_bst_version():
@contextmanager
def save_file_atomic(filename, mode='w', *, buffering=-1, encoding=None,
errors=None, newline=None, closefd=True, opener=None):
errors=None, newline=None, closefd=True, opener=None, tempdir=None):
"""Save a file with a temporary name and rename it into place when ready.
This is a context manager which is meant for saving data to files.
......@@ -529,8 +529,9 @@ def save_file_atomic(filename, mode='w', *, buffering=-1, encoding=None,
# https://bugs.python.org/issue8604
assert os.path.isabs(filename), "The utils.save_file_atomic() parameter ``filename`` must be an absolute path"
dirname = os.path.dirname(filename)
fd, tempname = tempfile.mkstemp(dir=dirname)
if tempdir is None:
tempdir = os.path.dirname(filename)
fd, tempname = tempfile.mkstemp(dir=tempdir)
os.close(fd)
f = open(tempname, mode=mode, buffering=buffering, encoding=encoding,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment