summaryrefslogtreecommitdiffstats
path: root/Lib/test/script_helper.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-10-08 18:05:42 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-10-08 18:05:42 (GMT)
commitf51d8d3a2ede7ff3330e2a868feb4ecdda0b92c0 (patch)
tree8a5a261dc53a7b2a20c5bc12c48c25f77122eea9 /Lib/test/script_helper.py
parent0ee4c9f274960bb717492b0827f6d22af0b1e60b (diff)
downloadcpython-f51d8d3a2ede7ff3330e2a868feb4ecdda0b92c0.zip
cpython-f51d8d3a2ede7ff3330e2a868feb4ecdda0b92c0.tar.gz
cpython-f51d8d3a2ede7ff3330e2a868feb4ecdda0b92c0.tar.bz2
Better Python spawning primitives in test.script_helper, for
easier writing of unit tests and better error reporting.
Diffstat (limited to 'Lib/test/script_helper.py')
-rw-r--r--Lib/test/script_helper.py31
1 files changed, 19 insertions, 12 deletions
diff --git a/Lib/test/script_helper.py b/Lib/test/script_helper.py
index 7a29ec6..f40a676 100644
--- a/Lib/test/script_helper.py
+++ b/Lib/test/script_helper.py
@@ -15,12 +15,27 @@ from imp import source_from_cache
from test.support import make_legacy_pyc
# Executing the interpreter in a subprocess
-def python_exit_code(*args):
+def _assert_python(expected_success, *args):
cmd_line = [sys.executable, '-E']
cmd_line.extend(args)
- with open(os.devnull, 'w') as devnull:
- return subprocess.call(cmd_line, stdout=devnull,
- stderr=subprocess.STDOUT)
+ p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ try:
+ out, err = p.communicate()
+ finally:
+ subprocess._cleanup()
+ rc = p.returncode
+ if (rc and expected_success) or (not rc and not expected_success):
+ raise AssertionError(
+ "Process return code is %d, "
+ "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
+ return rc, out, err
+
+def assert_python_ok(*args):
+ return _assert_python(True, *args)
+
+def assert_python_failure(*args):
+ return _assert_python(False, *args)
def spawn_python(*args):
cmd_line = [sys.executable, '-E']
@@ -38,14 +53,6 @@ def kill_python(p):
subprocess._cleanup()
return data
-def run_python(*args):
- if __debug__:
- p = spawn_python(*args)
- else:
- p = spawn_python('-O', *args)
- stdout_data = kill_python(p)
- return p.wait(), stdout_data
-
# Script creation utilities
@contextlib.contextmanager
def temp_dir():