Skip to content

Commit f7049b5

Browse files
authored
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().
1 parent 6a02b38 commit f7049b5

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

‎Lib/test/support/script_helper.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
import zipfile
1212

1313
from importlib.util import source_from_cache
14+
from test import support
1415
from 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+
2022
def 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+
139142
def _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+
145149
def 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+
158163
def 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+
168174
def 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+
190197
def 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+
201209
def 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+
212221
def 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+
231241
def make_pkg(pkg_dir, init_source=''):
232242
os.mkdir(pkg_dir)
233243
make_script(pkg_dir, '__init__', init_source)
234244

245+
235246
def 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")

‎Lib/test/test_eintr.py‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,8 @@ class EINTRTests(unittest.TestCase):
1515
def test_all(self):
1616
# Run the tester in a sub-process, to make sure there is only one
1717
# thread (for reliable signal delivery).
18-
tester = support.findfile("eintr_tester.py", subdir="eintrdata")
19-
# use -u to try to get the full output if the test hangs or crash
20-
args = ["-u", tester, "-v"]
21-
if support.verbose:
22-
print()
23-
print("--- run eintr_tester.py ---", flush=True)
24-
# In verbose mode, the child process inherit stdout and stdout,
25-
# to see output in realtime and reduce the risk of losing output.
26-
args = [sys.executable, "-E", "-X", "faulthandler", *args]
27-
proc = subprocess.run(args)
28-
print(f"--- eintr_tester.py completed: "
29-
f"exit code {proc.returncode} ---", flush=True)
30-
if proc.returncode:
31-
self.fail("eintr_tester.py failed")
32-
else:
33-
script_helper.assert_python_ok("-u", tester, "-v")
18+
script = support.findfile("_test_eintr.py")
19+
script_helper.run_test_script(script)
3420

3521

3622
if __name__ == "__main__":

0 commit comments

Comments
 (0)