88"""
99
1010import os
11+ import shlex
1112import sys
13+ import sysconfig
1214import test .support
1315
1416
@@ -19,15 +21,37 @@ def is_multiprocess_flag(arg):
1921def is_resource_use_flag (arg ):
2022 return arg .startswith ('-u' ) or arg .startswith ('--use' )
2123
24+ def is_python_flag (arg ):
25+ return arg .startswith ('-p' ) or arg .startswith ('--python' )
26+
2227
2328def main (regrtest_args ):
2429 args = [sys .executable ,
2530 '-u' , # Unbuffered stdout and stderr
2631 '-W' , 'default' , # Warnings set to 'default'
2732 '-bb' , # Warnings about bytes/bytearray
28- '-E' , # Ignore environment variables
2933 ]
3034
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+
3155 # Allow user-specified interpreter options to override our defaults.
3256 args .extend (test .support .args_from_interpreter_flags ())
3357
@@ -38,16 +62,30 @@ def main(regrtest_args):
3862 if sys .platform == 'win32' :
3963 args .append ('-n' ) # Silence alerts under Windows
4064 if not any (is_multiprocess_flag (arg ) for arg in regrtest_args ):
41- args .extend (['-j' , '0' ]) # Use all CPU cores
65+ if cross_compile and hostrunner :
66+ # For now use only one core for cross-compiled builds;
67+ # hostrunner can be expensive.
68+ args .extend (['-j' , '1' ])
69+ else :
70+ args .extend (['-j' , '0' ]) # Use all CPU cores
4271 if not any (is_resource_use_flag (arg ) for arg in regrtest_args ):
4372 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+
4481 args .extend (regrtest_args )
45- print (' ' .join (args ))
82+
83+ print (shlex .join (args ))
4684 if sys .platform == 'win32' :
4785 from subprocess import call
4886 sys .exit (call (args ))
4987 else :
50- os .execv (sys .executable , args )
88+ os .execve (sys .executable , args , environ )
5189
5290
5391if __name__ == '__main__' :
0 commit comments