Skip to content

Commit 21dbfdf

Browse files
committed
Rewrite CacheDir _mkdir_atomic to include old version
The failure on Python 3.7 caused a rewrite. Redo that so the version released with SCons 4.9.0 is still present, but commented out, so it's easier to restore it in future. tempfile.TemporaryDirectory has better cleanup logic, so we'll want to flip to that when Python 3.7 support is retired. Signed-off-by: Mats Wichmann <[email protected]>
1 parent acab1e7 commit 21dbfdf

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

SCons/CacheDir.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,31 @@ def _mkdir_atomic(self, path: str) -> bool:
209209
if os.path.exists(directory):
210210
return False
211211

212-
# TODO: tried to use TemporaryDirectory() here and the result as a
213-
# context manager, but that fails on Python 3.7 (works on 3.8+) after
214-
# the directory has successfully been renamed. Not sure why.
215212
try:
213+
# TODO: Python 3.7. See comment below.
214+
# tempdir = tempfile.TemporaryDirectory(dir=os.path.dirname(directory))
216215
tempdir = tempfile.mkdtemp(dir=os.path.dirname(directory))
217216
except OSError as e:
218217
msg = "Failed to create cache directory " + path
219218
raise SCons.Errors.SConsEnvironmentError(msg) from e
219+
220+
# TODO: Python 3.7: the context manager raises exception on cleanup
221+
# if the temporary was moved successfully (File Not Found).
222+
# Fixed in 3.8+. In the replacement below we manually clean up if
223+
# the move failed as mkdtemp() does not. TemporaryDirectory's
224+
# cleanup is more sophisitcated so prefer when we can use it.
225+
# self._add_config(tempdir.name)
226+
# with tempdir:
227+
# try:
228+
# os.replace(tempdir.name, directory)
229+
# return True
230+
# except OSError as e:
231+
# # did someone else get there first?
232+
# if os.path.isdir(directory):
233+
# return False # context manager cleans up
234+
# msg = "Failed to create cache directory " + path
235+
# raise SCons.Errors.SConsEnvironmentError(msg) from e
236+
220237
self._add_config(tempdir)
221238
try:
222239
os.replace(tempdir, directory)

0 commit comments

Comments
 (0)