1111import zipfile
1212
1313from importlib .util import source_from_cache
14+ from test import support
1415from test .support .import_helper import make_legacy_pyc
1516
1617
1718# Cached result of the expensive test performed in the function below.
1819__cached_interp_requires_environment = None
1920
21+
2022def interpreter_requires_environment ():
2123 """
2224 Returns True if our sys.executable interpreter requires environment
@@ -136,12 +138,14 @@ def run_python_until_end(*args, **env_vars):
136138 rc = proc .returncode
137139 return _PythonRunResult (rc , out , err ), cmd_line
138140
141+
139142def _assert_python (expected_success , / , * args , ** env_vars ):
140143 res , cmd_line = run_python_until_end (* args , ** env_vars )
141144 if (res .rc and expected_success ) or (not res .rc and not expected_success ):
142145 res .fail (cmd_line )
143146 return res
144147
148+
145149def assert_python_ok (* args , ** env_vars ):
146150 """
147151 Assert that running the interpreter with `args` and optional environment
@@ -155,6 +159,7 @@ def assert_python_ok(*args, **env_vars):
155159 """
156160 return _assert_python (True , * args , ** env_vars )
157161
162+
158163def assert_python_failure (* args , ** env_vars ):
159164 """
160165 Assert that running the interpreter with `args` and optional environment
@@ -165,6 +170,7 @@ def assert_python_failure(*args, **env_vars):
165170 """
166171 return _assert_python (False , * args , ** env_vars )
167172
173+
168174def spawn_python (* args , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , ** kw ):
169175 """Run a Python subprocess with the given arguments.
170176
@@ -187,6 +193,7 @@ def spawn_python(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw):
187193 stdout = stdout , stderr = stderr ,
188194 ** kw )
189195
196+
190197def kill_python (p ):
191198 """Run the given Popen process until completion and return stdout."""
192199 p .stdin .close ()
@@ -198,6 +205,7 @@ def kill_python(p):
198205 subprocess ._cleanup ()
199206 return data
200207
208+
201209def make_script (script_dir , script_basename , source , omit_suffix = False ):
202210 script_filename = script_basename
203211 if not omit_suffix :
@@ -209,6 +217,7 @@ def make_script(script_dir, script_basename, source, omit_suffix=False):
209217 importlib .invalidate_caches ()
210218 return script_name
211219
220+
212221def make_zip_script (zip_dir , zip_basename , script_name , name_in_zip = None ):
213222 zip_filename = zip_basename + os .extsep + 'zip'
214223 zip_name = os .path .join (zip_dir , zip_filename )
@@ -228,10 +237,12 @@ def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
228237 # zip_file.printdir()
229238 return zip_name , os .path .join (zip_name , name_in_zip )
230239
240+
231241def make_pkg (pkg_dir , init_source = '' ):
232242 os .mkdir (pkg_dir )
233243 make_script (pkg_dir , '__init__' , init_source )
234244
245+
235246def make_zip_pkg (zip_dir , zip_basename , pkg_name , script_basename ,
236247 source , depth = 1 , compiled = False ):
237248 unlink = []
@@ -260,3 +271,24 @@ def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
260271 # print 'Contents of %r:' % zip_name
261272 # zip_file.printdir()
262273 return zip_name , os .path .join (zip_name , script_name_in_zip )
274+
275+
276+ def run_test_script (script ):
277+ # use -u to try to get the full output if the test hangs or crash
278+ if support .verbose :
279+ def title (text ):
280+ return f"===== { text } ======"
281+
282+ name = f"script { os .path .basename (script )} "
283+ print ()
284+ print (title (name ), flush = True )
285+ # In verbose mode, the child process inherit stdout and stdout,
286+ # to see output in realtime and reduce the risk of losing output.
287+ args = [sys .executable , "-E" , "-X" , "faulthandler" , "-u" , script , "-v" ]
288+ proc = subprocess .run (args )
289+ print (title (f"{ name } completed: exit code { proc .returncode } " ),
290+ flush = True )
291+ if proc .returncode :
292+ raise AssertionError (f"{ name } failed" )
293+ else :
294+ assert_python_ok ("-u" , script , "-v" )
0 commit comments