1"""Run Python's test suite in a fast, rigorous way.
2
3The defaults are meant to be reasonably thorough, while skipping certain
4tests that can be time-consuming or resource-intensive (e.g. largefile),
5or distracting (e.g. audio and gui). These defaults can be overridden by
6simply passing a -u option to this script.
7
8"""
9
10import os
11import shlex
12import sys
13import sysconfig
14import test.support
15
16
17def is_multiprocess_flag(arg):
18    return arg.startswith('-j') or arg.startswith('--multiprocess')
19
20
21def is_resource_use_flag(arg):
22    return arg.startswith('-u') or arg.startswith('--use')
23
24def is_python_flag(arg):
25    return arg.startswith('-p') or arg.startswith('--python')
26
27
28def main(regrtest_args):
29    args = [sys.executable,
30            '-u',                 # Unbuffered stdout and stderr
31            '-W', 'default',      # Warnings set to 'default'
32            '-bb',                # Warnings about bytes/bytearray
33            ]
34
35    cross_compile = '_PYTHON_HOST_PLATFORM' in os.environ
36    if (hostrunner := os.environ.get("_PYTHON_HOSTRUNNER")) is None:
37        hostrunner = sysconfig.get_config_var("HOSTRUNNER")
38    if cross_compile:
39        # emulate -E, but keep PYTHONPATH + cross compile env vars, so
40        # test executable can load correct sysconfigdata file.
41        keep = {
42            '_PYTHON_PROJECT_BASE',
43            '_PYTHON_HOST_PLATFORM',
44            '_PYTHON_SYSCONFIGDATA_NAME',
45            'PYTHONPATH'
46        }
47        environ = {
48            name: value for name, value in os.environ.items()
49            if not name.startswith(('PYTHON', '_PYTHON')) or name in keep
50        }
51    else:
52        environ = os.environ.copy()
53        args.append("-E")
54
55    # Allow user-specified interpreter options to override our defaults.
56    args.extend(test.support.args_from_interpreter_flags())
57
58    args.extend(['-m', 'test',    # Run the test suite
59                 '-r',            # Randomize test order
60                 '-w',            # Re-run failed tests in verbose mode
61                 ])
62    if sys.platform == 'win32':
63        args.append('-n')         # Silence alerts under Windows
64    if not any(is_multiprocess_flag(arg) for arg in regrtest_args):
65        if cross_compile and hostrunner:
66            # For now use only two cores for cross-compiled builds;
67            # hostrunner can be expensive.
68            args.extend(['-j', '2'])
69        else:
70            args.extend(['-j', '0'])  # Use all CPU cores
71    if not any(is_resource_use_flag(arg) for arg in regrtest_args):
72        args.extend(['-u', 'all,-largefile,-audio,-gui'])
73
74    if cross_compile and hostrunner:
75        # If HOSTRUNNER is set and -p/--python option is not given, then
76        # use hostrunner to execute python binary for tests.
77        if not any(is_python_flag(arg) for arg in regrtest_args):
78            buildpython = sysconfig.get_config_var("BUILDPYTHON")
79            args.extend(["--python", f"{hostrunner} {buildpython}"])
80
81    args.extend(regrtest_args)
82
83    print(shlex.join(args))
84    if sys.platform == 'win32':
85        from subprocess import call
86        sys.exit(call(args))
87    else:
88        os.execve(sys.executable, args, environ)
89
90
91if __name__ == '__main__':
92    main(sys.argv[1:])
93