Skip to content

Commit f799b2c

Browse files
committed
Cleanup regrtest "main()" function
* Rename libregrtest.main_in_temp_cwd() to libregrtest.main() * Add regrtest.main_in_temp_cwd() alias to libregrtest.main() * Move old main_in_temp_cwd() code into libregrtest.Regrtest.main() * Update multiple scripts to call libregrtest.main()
1 parent 1ea9027 commit f799b2c

File tree

7 files changed

+38
-36
lines changed

7 files changed

+38
-36
lines changed

Lib/test/__main__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
from test import regrtest
2-
3-
regrtest.main_in_temp_cwd()
1+
from test.libregrtest import main
2+
main()

Lib/test/autotest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# This should be equivalent to running regrtest.py from the cmdline.
22
# It can be especially handy if you're in an interactive shell, e.g.,
33
# from test import autotest.
4-
5-
from test import regrtest
6-
regrtest.main()
4+
from test.libregrtest import main
5+
main()

Lib/test/libregrtest/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
# We import importlib *ASAP* in order to test #15386
2+
import importlib
3+
14
from test.libregrtest.cmdline import _parse_args, RESOURCE_NAMES
2-
from test.libregrtest.main import main, main_in_temp_cwd
5+
from test.libregrtest.main import main

Lib/test/libregrtest/main.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,28 @@ def finalize(self):
415415
os.system("leaks %d" % os.getpid())
416416

417417
def main(self, tests=None, **kwargs):
418+
global TEMPDIR
419+
420+
if sysconfig.is_python_build():
421+
try:
422+
os.mkdir(TEMPDIR)
423+
except FileExistsError:
424+
pass
425+
426+
# Define a writable temp dir that will be used as cwd while running
427+
# the tests. The name of the dir includes the pid to allow parallel
428+
# testing (see the -j option).
429+
test_cwd = 'test_python_{}'.format(os.getpid())
430+
test_cwd = os.path.join(TEMPDIR, test_cwd)
431+
432+
# Run the tests in a context manager that temporarily changes the CWD to a
433+
# temporary and writable directory. If it's not possible to create or
434+
# change the CWD, the original CWD will be used. The original CWD is
435+
# available from support.SAVEDCWD.
436+
with support.temp_cwd(test_cwd, quiet=True):
437+
self._main(tests, kwargs)
438+
439+
def _main(self, tests, kwargs):
418440
self.ns = self.parse_args(kwargs)
419441

420442
if self.ns.slaveargs is not None:
@@ -473,26 +495,5 @@ def printlist(x, width=70, indent=4):
473495

474496

475497
def main(tests=None, **kwargs):
498+
"""Run the Python suite."""
476499
Regrtest().main(tests=tests, **kwargs)
477-
478-
479-
def main_in_temp_cwd():
480-
"""Run main() in a temporary working directory."""
481-
if sysconfig.is_python_build():
482-
try:
483-
os.mkdir(TEMPDIR)
484-
except FileExistsError:
485-
pass
486-
487-
# Define a writable temp dir that will be used as cwd while running
488-
# the tests. The name of the dir includes the pid to allow parallel
489-
# testing (see the -j option).
490-
test_cwd = 'test_python_{}'.format(os.getpid())
491-
test_cwd = os.path.join(TEMPDIR, test_cwd)
492-
493-
# Run the tests in a context manager that temporarily changes the CWD to a
494-
# temporary and writable directory. If it's not possible to create or
495-
# change the CWD, the original CWD will be used. The original CWD is
496-
# available from support.SAVEDCWD.
497-
with support.temp_cwd(test_cwd, quiet=True):
498-
main()

Lib/test/regrtest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
import os
1313
import sys
14-
from test.libregrtest import main_in_temp_cwd
14+
from test.libregrtest import main
1515

1616

17-
# alias needed by other scripts
18-
main = main_in_temp_cwd
17+
# Alias for backward compatibility (just in case)
18+
main_in_temp_cwd = main
1919

2020

2121
def _main():

Lib/test/test_importlib/regrtest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"""
99
import importlib
1010
import sys
11-
from test import regrtest
11+
from test import libregrtest
1212

1313
if __name__ == '__main__':
1414
__builtins__.__import__ = importlib.__import__
1515
sys.path_importer_cache.clear()
1616

17-
regrtest.main(quiet=True, verbose2=True)
17+
libregrtest.main(quiet=True, verbose2=True)

PC/testpy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
# Add the "test" directory to PYTHONPATH.
2727
sys.path = sys.path + [test]
2828

29-
import regrtest # Standard Python tester.
30-
regrtest.main()
29+
import libregrtest # Standard Python tester.
30+
libregrtest.main()

0 commit comments

Comments
 (0)