common: clean up failed atomic writes (#38552)

* common: clean up failed atomic writes

* common: remove atomic write regression test

---------

Co-authored-by: GavinnnK <239280171+GavinnnK@users.noreply.github.com>
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
This commit is contained in:
GavinnnK
2026-08-07 16:34:11 -04:00
committed by GitHub
parent 66b3590b63
commit 0819f5c0fd
+8 -4
View File
@@ -108,10 +108,14 @@ def atomic_write(path: str, mode: str = 'w', buffering: int = -1, encoding: str
if not overwrite and os.path.exists(path):
raise FileExistsError(f"File '{path}' already exists. To overwrite it, set 'overwrite' to True.")
with tempfile.NamedTemporaryFile(mode=mode, buffering=buffering, encoding=encoding, newline=newline, dir=dir_name, delete=False) as tmp_file:
yield tmp_file
tmp_file_name = tmp_file.name
os.replace(tmp_file_name, path)
tmp_file = tempfile.NamedTemporaryFile(mode=mode, buffering=buffering, encoding=encoding, newline=newline, dir=dir_name, delete=False)
try:
with tmp_file:
yield tmp_file
os.replace(tmp_file.name, path)
finally:
with contextlib.suppress(FileNotFoundError):
os.unlink(tmp_file.name)
def get_upload_stream(filepath: str, should_compress: bool) -> tuple[io.BufferedIOBase, int]: