Skip to content

Commit

Permalink
Replace use of mktemp
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EliahKagan committed Dec 13, 2023
1 parent 7d6f97c commit a315725
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions smmap/test/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a315725

Please sign in to comment.