Skip to content

Commit a315725

Browse files
committed
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.
1 parent 7d6f97c commit a315725

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Diff for: smmap/test/lib.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ class FileCreator:
1818
def __init__(self, size, prefix=''):
1919
assert size, "Require size to be larger 0"
2020

21-
self._path = tempfile.mktemp(prefix=prefix)
2221
self._size = size
2322

24-
with open(self._path, "wb") as fp:
25-
fp.seek(size - 1)
26-
fp.write(b'1')
23+
with tempfile.NamedTemporaryFile("wb", prefix=prefix, delete=False) as file:
24+
self._path = file.name
25+
file.seek(size - 1)
26+
file.write(b'1')
2727

2828
assert os.path.getsize(self.path) == size
2929

0 commit comments

Comments
 (0)