200200RESOURCE_NAMES = ('audio' , 'curses' , 'largefile' , 'network' ,
201201 'decimal' , 'cpu' , 'subprocess' , 'urlfetch' , 'gui' )
202202
203- TEMPDIR = os .path .abspath (tempfile .gettempdir ())
203+ # When tests are run from the Python build directory, it is best practice
204+ # to keep the test files in a subfolder. This eases the cleanup of leftover
205+ # files using the "make distclean" command.
206+ if sysconfig .is_python_build ():
207+ TEMPDIR = os .path .join (sysconfig .get_config_var ('srcdir' ), 'build' )
208+ else :
209+ TEMPDIR = tempfile .gettempdir ()
210+ TEMPDIR = os .path .abspath (TEMPDIR )
204211
205212class _ArgParser (argparse .ArgumentParser ):
206213
@@ -1543,13 +1550,9 @@ def printlist(x, width=70, indent=4):
15431550 initial_indent = blanks , subsequent_indent = blanks ))
15441551
15451552
1546- def _make_temp_dir_for_build (TEMPDIR ):
1547- # When tests are run from the Python build directory, it is best practice
1548- # to keep the test files in a subfolder. It eases the cleanup of leftover
1549- # files using command "make distclean".
1553+ def main_in_temp_cwd ():
1554+ """Run main() in a temporary working directory."""
15501555 if sysconfig .is_python_build ():
1551- TEMPDIR = os .path .join (sysconfig .get_config_var ('srcdir' ), 'build' )
1552- TEMPDIR = os .path .abspath (TEMPDIR )
15531556 try :
15541557 os .mkdir (TEMPDIR )
15551558 except FileExistsError :
@@ -1558,10 +1561,16 @@ def _make_temp_dir_for_build(TEMPDIR):
15581561 # Define a writable temp dir that will be used as cwd while running
15591562 # the tests. The name of the dir includes the pid to allow parallel
15601563 # testing (see the -j option).
1561- TESTCWD = 'test_python_{}' .format (os .getpid ())
1564+ test_cwd = 'test_python_{}' .format (os .getpid ())
1565+ test_cwd = os .path .join (TEMPDIR , test_cwd )
1566+
1567+ # Run the tests in a context manager that temporarily changes the CWD to a
1568+ # temporary and writable directory. If it's not possible to create or
1569+ # change the CWD, the original CWD will be used. The original CWD is
1570+ # available from support.SAVEDCWD.
1571+ with support .temp_cwd (test_cwd , quiet = True ):
1572+ main ()
15621573
1563- TESTCWD = os .path .join (TEMPDIR , TESTCWD )
1564- return TEMPDIR , TESTCWD
15651574
15661575if __name__ == '__main__' :
15671576 # Remove regrtest.py's own directory from the module search path. Despite
@@ -1585,11 +1594,4 @@ def _make_temp_dir_for_build(TEMPDIR):
15851594 # sanity check
15861595 assert __file__ == os .path .abspath (sys .argv [0 ])
15871596
1588- TEMPDIR , TESTCWD = _make_temp_dir_for_build (TEMPDIR )
1589-
1590- # Run the tests in a context manager that temporary changes the CWD to a
1591- # temporary and writable directory. If it's not possible to create or
1592- # change the CWD, the original CWD will be used. The original CWD is
1593- # available from support.SAVEDCWD.
1594- with support .temp_cwd (TESTCWD , quiet = True ):
1595- main ()
1597+ main_in_temp_cwd ()
0 commit comments