43
43
import SCons .compat
44
44
45
45
import atexit
46
+ import contextlib
46
47
import importlib
48
+ import locale
47
49
import os
48
50
import sys
49
51
import tempfile
50
- import locale
51
52
52
53
import SCons .Action
53
54
import SCons .Errors
@@ -249,12 +250,13 @@ def __call__(self, target, source, env, for_signature):
249
250
if cmdlist is not None :
250
251
return cmdlist
251
252
253
+ # try encoding the tempfile data before creating the file -
254
+ # avoid orphaned files
252
255
tempfile_esc_func = env .get ('TEMPFILEARGESCFUNC' , SCons .Subst .quote_spaces )
253
256
args = [tempfile_esc_func (arg ) for arg in cmd [1 :]]
254
257
join_char = env .get ('TEMPFILEARGJOIN' , ' ' )
255
258
contents = join_char .join (args ) + "\n "
256
259
encoding = env .get ('TEMPFILEENCODING' , TEMPFILE_DEFAULT_ENCODING )
257
-
258
260
try :
259
261
tempfile_contents = bytes (contents , encoding = encoding )
260
262
except (UnicodeError , LookupError , TypeError ):
@@ -281,33 +283,21 @@ def __call__(self, target, source, env, for_signature):
281
283
else :
282
284
tempfile_dir = None
283
285
284
- # default is binary - encode the tempfile contents later
285
286
fd , tmp = tempfile .mkstemp (suffix , dir = tempfile_dir )
286
-
287
287
try :
288
288
os .write (fd , tempfile_contents )
289
289
finally :
290
290
os .close (fd )
291
-
292
291
native_tmp = SCons .Util .get_native_path (tmp )
293
292
294
293
# arrange for cleanup on exit:
295
294
296
295
def tmpfile_cleanup (file ) -> None :
297
- os .remove (file )
296
+ with contextlib .suppress (FileNotFoundError ):
297
+ os .remove (file )
298
298
299
299
atexit .register (tmpfile_cleanup , tmp )
300
300
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
-
311
301
# XXX Using the SCons.Action.print_actions value directly
312
302
# like this is bogus, but expedient. This class should
313
303
# really be rewritten as an Action that defines the
@@ -337,10 +327,18 @@ def tmpfile_cleanup(file) -> None:
337
327
)
338
328
self ._print_cmd_str (target , source , env , cmdstr )
339
329
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 = "@"
340
338
cmdlist = [cmd [0 ], prefix + native_tmp ]
341
339
342
340
# 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.
344
342
if node is not None :
345
343
try :
346
344
# Storing in tempfile_cmdlist by self.cmd provided when intializing
0 commit comments