[Python-checkins] bpo-42639: Add script_helper.run_test_script() (GH-23777)

vstinner webhook-mailer at python.org
Tue Dec 15 10:08:25 EST 2020


https://github.com/python/cpython/commit/f7049b5fb680c774e4950d10be62859a749f4e79
commit: f7049b5fb680c774e4950d10be62859a749f4e79
branch: master
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2020-12-15T16:08:16+01:00
summary:

bpo-42639: Add script_helper.run_test_script() (GH-23777)

* Add run_test_script() function to test.support.script_helper.
* Rename Lib/test/eintrdata/eintr_tester.py to
  Lib/test/_test_eintr.py.
* test_eintr.py uses run_test_script().

files:
A Lib/test/_test_eintr.py
D Lib/test/eintrdata/eintr_tester.py
M Lib/test/support/script_helper.py
M Lib/test/test_eintr.py

diff --git a/Lib/test/eintrdata/eintr_tester.py b/Lib/test/_test_eintr.py
similarity index 100%
rename from Lib/test/eintrdata/eintr_tester.py
rename to Lib/test/_test_eintr.py
diff --git a/Lib/test/support/script_helper.py b/Lib/test/support/script_helper.py
index 09bb586dcf79d..6d699c8486cd2 100644
--- a/Lib/test/support/script_helper.py
+++ b/Lib/test/support/script_helper.py
@@ -11,12 +11,14 @@
 import zipfile
 
 from importlib.util import source_from_cache
+from test import support
 from test.support.import_helper import make_legacy_pyc
 
 
 # Cached result of the expensive test performed in the function below.
 __cached_interp_requires_environment = None
 
+
 def interpreter_requires_environment():
     """
     Returns True if our sys.executable interpreter requires environment
@@ -136,12 +138,14 @@ def run_python_until_end(*args, **env_vars):
     rc = proc.returncode
     return _PythonRunResult(rc, out, err), cmd_line
 
+
 def _assert_python(expected_success, /, *args, **env_vars):
     res, cmd_line = run_python_until_end(*args, **env_vars)
     if (res.rc and expected_success) or (not res.rc and not expected_success):
         res.fail(cmd_line)
     return res
 
+
 def assert_python_ok(*args, **env_vars):
     """
     Assert that running the interpreter with `args` and optional environment
@@ -155,6 +159,7 @@ def assert_python_ok(*args, **env_vars):
     """
     return _assert_python(True, *args, **env_vars)
 
+
 def assert_python_failure(*args, **env_vars):
     """
     Assert that running the interpreter with `args` and optional environment
@@ -165,6 +170,7 @@ def assert_python_failure(*args, **env_vars):
     """
     return _assert_python(False, *args, **env_vars)
 
+
 def spawn_python(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw):
     """Run a Python subprocess with the given arguments.
 
@@ -187,6 +193,7 @@ def spawn_python(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw):
                             stdout=stdout, stderr=stderr,
                             **kw)
 
+
 def kill_python(p):
     """Run the given Popen process until completion and return stdout."""
     p.stdin.close()
@@ -198,6 +205,7 @@ def kill_python(p):
     subprocess._cleanup()
     return data
 
+
 def make_script(script_dir, script_basename, source, omit_suffix=False):
     script_filename = script_basename
     if not omit_suffix:
@@ -209,6 +217,7 @@ def make_script(script_dir, script_basename, source, omit_suffix=False):
     importlib.invalidate_caches()
     return script_name
 
+
 def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
     zip_filename = zip_basename+os.extsep+'zip'
     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):
     #        zip_file.printdir()
     return zip_name, os.path.join(zip_name, name_in_zip)
 
+
 def make_pkg(pkg_dir, init_source=''):
     os.mkdir(pkg_dir)
     make_script(pkg_dir, '__init__', init_source)
 
+
 def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
                  source, depth=1, compiled=False):
     unlink = []
@@ -260,3 +271,24 @@ def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
     #        print 'Contents of %r:' % zip_name
     #        zip_file.printdir()
     return zip_name, os.path.join(zip_name, script_name_in_zip)
+
+
+def run_test_script(script):
+    # use -u to try to get the full output if the test hangs or crash
+    if support.verbose:
+        def title(text):
+            return f"===== {text} ======"
+
+        name = f"script {os.path.basename(script)}"
+        print()
+        print(title(name), flush=True)
+        # In verbose mode, the child process inherit stdout and stdout,
+        # to see output in realtime and reduce the risk of losing output.
+        args = [sys.executable, "-E", "-X", "faulthandler", "-u", script, "-v"]
+        proc = subprocess.run(args)
+        print(title(f"{name} completed: exit code {proc.returncode}"),
+              flush=True)
+        if proc.returncode:
+            raise AssertionError(f"{name} failed")
+    else:
+        assert_python_ok("-u", script, "-v")
diff --git a/Lib/test/test_eintr.py b/Lib/test/test_eintr.py
index a5f8f6465e88b..b61cdfa0a122d 100644
--- a/Lib/test/test_eintr.py
+++ b/Lib/test/test_eintr.py
@@ -15,22 +15,8 @@ class EINTRTests(unittest.TestCase):
     def test_all(self):
         # Run the tester in a sub-process, to make sure there is only one
         # thread (for reliable signal delivery).
-        tester = support.findfile("eintr_tester.py", subdir="eintrdata")
-        # use -u to try to get the full output if the test hangs or crash
-        args = ["-u", tester, "-v"]
-        if support.verbose:
-            print()
-            print("--- run eintr_tester.py ---", flush=True)
-            # In verbose mode, the child process inherit stdout and stdout,
-            # to see output in realtime and reduce the risk of losing output.
-            args = [sys.executable, "-E", "-X", "faulthandler", *args]
-            proc = subprocess.run(args)
-            print(f"--- eintr_tester.py completed: "
-                  f"exit code {proc.returncode} ---", flush=True)
-            if proc.returncode:
-                self.fail("eintr_tester.py failed")
-        else:
-            script_helper.assert_python_ok("-u", tester, "-v")
+        script = support.findfile("_test_eintr.py")
+        script_helper.run_test_script(script)
 
 
 if __name__ == "__main__":



More information about the Python-checkins mailing list