summaryrefslogtreecommitdiffstats
path: root/Lib/test/script_helper.py
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2015-02-04 08:59:40 (GMT)
committerGregory P. Smith <greg@krypto.org>2015-02-04 08:59:40 (GMT)
commitb9a3dd9dfb780b9691f8a1859fd67cf9eadbd2d0 (patch)
treec186f9c50fd386da260b0c345550df9f0a65ac81 /Lib/test/script_helper.py
parent17d87f8ae42122025b5ce41230df6ba9e042ec40 (diff)
downloadcpython-b9a3dd9dfb780b9691f8a1859fd67cf9eadbd2d0.zip
cpython-b9a3dd9dfb780b9691f8a1859fd67cf9eadbd2d0.tar.gz
cpython-b9a3dd9dfb780b9691f8a1859fd67cf9eadbd2d0.tar.bz2
Skip some tests that require a subinterpreter launched with -E or -I when the
interpreter under test is being run in an environment that requires the use of environment variables such as PYTHONHOME in order to function at all. Adds a private test.script_helper._interpreter_requires_environment() function to be used with @unittest.skipIf on stdlib test methods requiring this.
Diffstat (limited to 'Lib/test/script_helper.py')
-rw-r--r--Lib/test/script_helper.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/script_helper.py b/Lib/test/script_helper.py
index 87a781e..6a8bea6 100644
--- a/Lib/test/script_helper.py
+++ b/Lib/test/script_helper.py
@@ -15,6 +15,41 @@ import zipfile
from importlib.util import source_from_cache
from test.support import make_legacy_pyc, strip_python_stderr, temp_dir
+
+# 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
+ variables in order to be able to run at all.
+
+ This is designed to be used with @unittest.skipIf() to annotate tests
+ that need to use an assert_python*() function to launch an isolated
+ mode (-I) or no environment mode (-E) sub-interpreter process.
+
+ A normal build & test does not run into this situation but it can happen
+ when trying to run the standard library test suite from an interpreter that
+ doesn't have an obvious home with Python's current home finding logic.
+
+ Setting PYTHONHOME is one way to get most of the testsuite to run in that
+ situation. PYTHONPATH or PYTHONUSERSITE are other common envirnonment
+ variables that might impact whether or not the interpreter can start.
+ """
+ global __cached_interp_requires_environment
+ if __cached_interp_requires_environment is None:
+ # Try running an interpreter with -E to see if it works or not.
+ try:
+ subprocess.check_call([sys.executable, '-E',
+ '-c', 'import sys; sys.exit(0)'])
+ except subprocess.CalledProcessError:
+ __cached_interp_requires_environment = True
+ else:
+ __cached_interp_requires_environment = False
+
+ return __cached_interp_requires_environment
+
+
# Executing the interpreter in a subprocess
def _assert_python(expected_success, *args, **env_vars):
if '__isolated' in env_vars: