From a315725c68295a02f3d007409ea3101430543289 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Wed, 13 Dec 2023 03:04:54 -0500 Subject: [PATCH] Replace use of mktemp This uses NamedTemporaryFile with delete=False to replace the one use of the deprecated mktemp function in smmap (reported in #41). This avoids the race condition inherent to mktemp, as the file is named and created together in a way that is effectively atomic. Because NamedTemporaryFile is not being used to automatically delete the file, it use and cleanup are unaffected by the change. --- smmap/test/lib.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/smmap/test/lib.py b/smmap/test/lib.py index ca91ee9..b15b0ec 100644 --- a/smmap/test/lib.py +++ b/smmap/test/lib.py @@ -18,12 +18,12 @@ class FileCreator: def __init__(self, size, prefix=''): assert size, "Require size to be larger 0" - self._path = tempfile.mktemp(prefix=prefix) self._size = size - with open(self._path, "wb") as fp: - fp.seek(size - 1) - fp.write(b'1') + with tempfile.NamedTemporaryFile("wb", prefix=prefix, delete=False) as file: + self._path = file.name + file.seek(size - 1) + file.write(b'1') assert os.path.getsize(self.path) == size