From 915799a2b9e59a3b32d596a216893554f9c87f07 Mon Sep 17 00:00:00 2001 From: nicoo Date: Fri, 13 Sep 2024 08:00:12 +0000 Subject: [PATCH] maintainers/scripts/sha-to-sri: fix file-descriptor leak --- maintainers/scripts/sha-to-sri.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/maintainers/scripts/sha-to-sri.py b/maintainers/scripts/sha-to-sri.py index 657d5ac5d87f..881e03071913 100755 --- a/maintainers/scripts/sha-to-sri.py +++ b/maintainers/scripts/sha-to-sri.py @@ -173,17 +173,18 @@ def atomicFileUpdate(target: Path): raised, `new` (atomically) replaces the `target`, otherwise it is deleted. """ # That's mostly copied from noto-emoji.py, should DRY it out - from tempfile import mkstemp - - fd, _p = mkstemp( - dir = target.parent, - prefix = target.name, - ) - tmpPath = Path(_p) + from tempfile import NamedTemporaryFile try: with target.open() as original: - with tmpPath.open("w") as new: + with NamedTemporaryFile( + dir = target.parent, + prefix = target.stem, + suffix = target.suffix, + delete = False, + mode="w", # otherwise the file would be opened in binary mode by default + ) as new: + tmpPath = Path(new.name) yield (original, new) tmpPath.replace(target)