Skip to content

Commit 517e925

Browse files
committed
Issue #17283: Share code between __main__.py and regrtest.py in Lib/test.
This commit also removes TESTCWD from regrtest.py's global namespace.
1 parent 4cbd293 commit 517e925

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

Lib/test/__main__.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
from test import regrtest, support
1+
from test import regrtest
22

3-
4-
TEMPDIR, TESTCWD = regrtest._make_temp_dir_for_build(regrtest.TEMPDIR)
5-
regrtest.TEMPDIR = TEMPDIR
6-
regrtest.TESTCWD = TESTCWD
7-
8-
# Run the tests in a context manager that temporary changes the CWD to a
9-
# temporary and writable directory. If it's not possible to create or
10-
# change the CWD, the original CWD will be used. The original CWD is
11-
# available from support.SAVEDCWD.
12-
with support.temp_cwd(TESTCWD, quiet=True):
13-
regrtest.main()
3+
regrtest.main_in_temp_cwd()

Lib/test/regrtest.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,14 @@
200200
RESOURCE_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

205212
class _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

15661575
if __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()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,9 @@ Extension Modules
878878
Tests
879879
-----
880880

881+
- Issue #17283: Share code between `__main__.py` and `regrtest.py` in
882+
`Lib/test`.
883+
881884
- Issue #17249: convert a test in test_capi to use unittest and reap threads.
882885

883886
- Issue #17107: Test client-side SNI support in urllib.request thanks to

0 commit comments

Comments
 (0)