Skip to content

Commit 6162a4c

Browse files
authored
Merge pull request #4756 from mwichmann/tempdir-reorg
One more bit of TEMPDIR rearranging
2 parents 14d0280 + 02931da commit 6162a4c

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

SCons/Platform/__init__.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@
4343
import SCons.compat
4444

4545
import atexit
46+
import contextlib
4647
import importlib
48+
import locale
4749
import os
4850
import sys
4951
import tempfile
50-
import locale
5152

5253
import SCons.Action
5354
import SCons.Errors
@@ -249,12 +250,13 @@ def __call__(self, target, source, env, for_signature):
249250
if cmdlist is not None:
250251
return cmdlist
251252

253+
# try encoding the tempfile data before creating the file -
254+
# avoid orphaned files
252255
tempfile_esc_func = env.get('TEMPFILEARGESCFUNC', SCons.Subst.quote_spaces)
253256
args = [tempfile_esc_func(arg) for arg in cmd[1:]]
254257
join_char = env.get('TEMPFILEARGJOIN', ' ')
255258
contents = join_char.join(args) + "\n"
256259
encoding = env.get('TEMPFILEENCODING', TEMPFILE_DEFAULT_ENCODING)
257-
258260
try:
259261
tempfile_contents = bytes(contents, encoding=encoding)
260262
except (UnicodeError, LookupError, TypeError):
@@ -281,33 +283,21 @@ def __call__(self, target, source, env, for_signature):
281283
else:
282284
tempfile_dir = None
283285

284-
# default is binary - encode the tempfile contents later
285286
fd, tmp = tempfile.mkstemp(suffix, dir=tempfile_dir)
286-
287287
try:
288288
os.write(fd, tempfile_contents)
289289
finally:
290290
os.close(fd)
291-
292291
native_tmp = SCons.Util.get_native_path(tmp)
293292

294293
# arrange for cleanup on exit:
295294

296295
def tmpfile_cleanup(file) -> None:
297-
os.remove(file)
296+
with contextlib.suppress(FileNotFoundError):
297+
os.remove(file)
298298

299299
atexit.register(tmpfile_cleanup, tmp)
300300

301-
if env.get('SHELL', None) == 'sh':
302-
# The sh shell will try to escape the backslashes in the
303-
# path, so unescape them.
304-
native_tmp = native_tmp.replace('\\', r'\\\\')
305-
306-
if 'TEMPFILEPREFIX' in env:
307-
prefix = env.subst('$TEMPFILEPREFIX')
308-
else:
309-
prefix = "@"
310-
311301
# XXX Using the SCons.Action.print_actions value directly
312302
# like this is bogus, but expedient. This class should
313303
# really be rewritten as an Action that defines the
@@ -337,10 +327,18 @@ def tmpfile_cleanup(file) -> None:
337327
)
338328
self._print_cmd_str(target, source, env, cmdstr)
339329

330+
if env.get('SHELL', None) == 'sh':
331+
# The sh shell will try to escape the backslashes in the
332+
# path, so unescape them.
333+
native_tmp = native_tmp.replace('\\', r'\\\\')
334+
if 'TEMPFILEPREFIX' in env:
335+
prefix = env.subst('$TEMPFILEPREFIX')
336+
else:
337+
prefix = "@"
340338
cmdlist = [cmd[0], prefix + native_tmp]
341339

342340
# Store the temporary file command list into the target Node.attributes
343-
# to avoid creating two temporary files one for print and one for execute.
341+
# to avoid creating separate temporary files for print and execute.
344342
if node is not None:
345343
try:
346344
# Storing in tempfile_cmdlist by self.cmd provided when intializing

0 commit comments

Comments
 (0)