Use exclusive open x when writing a file
- ID
b7fe765- date
2024-09-07 19:58:47+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
2c9ba9c- message
Use exclusive open `x` when writing a file This is almost certainly overkill because the UUID suffix should ensure nobody else is trying to write to the same path, but it's a useful defensive measure.- changed files
1 file, 4 additions, 1 deletion
Changed files
src/javascript/__init__.py (5886) → src/javascript/__init__.py (5933)
diff --git a/src/javascript/__init__.py b/src/javascript/__init__.py
index 6f44d4f..54e993b 100644
--- a/src/javascript/__init__.py
+++ b/src/javascript/__init__.py
@@ -88,7 +88,10 @@ def write_js(p: pathlib.Path | str, *, value: typing.Any, varname: str) -> None:
# The UUID is probably overkill because it would be very unusual for
# me to have multiple, concurrent writes going on, but it doesn't hurt.
tmp_p = p.with_suffix(f".{uuid.uuid4()}.js.tmp")
- tmp_p.write_text(js_string)
+
+ with tmp_p.open("x") as out_file:
+ out_file.write_text(js_string)
+
tmp_p.rename(p)