aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Gilbert <floppym@gentoo.org>2018-01-05 13:39:02 -0500
committerMike Gilbert <floppym@gentoo.org>2018-01-05 13:39:02 -0500
commit57b0073cf623ae29b50fe88ce165709779a95a31 (patch)
treeb7d415f0bd5b78e232c7c48cb108a3ce85e7ad62
parentImprove distutils C++ support (diff)
downloadcpython-57b0073cf623ae29b50fe88ce165709779a95a31.tar.gz
cpython-57b0073cf623ae29b50fe88ce165709779a95a31.tar.bz2
cpython-57b0073cf623ae29b50fe88ce165709779a95a31.zip
tests environment
https://bugs.python.org/issue1674555
-rw-r--r--Lib/site.py8
-rw-r--r--Lib/test/libregrtest/main.py4
-rw-r--r--Lib/test/libregrtest/runtest.py57
-rw-r--r--Lib/test/libregrtest/runtest_mp.py41
-rw-r--r--Lib/test/test_site.py13
-rw-r--r--Makefile.pre.in2
6 files changed, 80 insertions, 45 deletions
diff --git a/Lib/site.py b/Lib/site.py
index 6d0fe698795..107f75e7ce2 100644
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -527,8 +527,12 @@ def main():
known_paths = venv(known_paths)
if ENABLE_USER_SITE is None:
ENABLE_USER_SITE = check_enableusersite()
- known_paths = addusersitepackages(known_paths)
- known_paths = addsitepackages(known_paths)
+ if os.environ.get("_PYTHONNOSITEPACKAGES") is None:
+ known_paths = addusersitepackages(known_paths)
+ known_paths = addsitepackages(known_paths)
+ else:
+ # Initialize USER_BASE and USER_SITE.
+ getusersitepackages()
setquit()
setcopyright()
sethelper()
diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py
index 527de177792..b8d29a0dea3 100644
--- a/Lib/test/libregrtest/main.py
+++ b/Lib/test/libregrtest/main.py
@@ -110,7 +110,7 @@ class Regrtest:
def accumulate_result(self, test, result):
ok, test_time = result
- if ok not in (CHILD_ERROR, INTERRUPTED):
+ if ok not in (None, CHILD_ERROR, INTERRUPTED):
self.test_times.append((test_time, test))
if ok == PASSED:
self.good.append(test)
@@ -123,7 +123,7 @@ class Regrtest:
elif ok == RESOURCE_DENIED:
self.skipped.append(test)
self.resource_denieds.append(test)
- elif ok != INTERRUPTED:
+ elif ok not in (None, INTERRUPTED):
raise ValueError("invalid test result: %r" % ok)
def display_progress(self, test_index, test):
diff --git a/Lib/test/libregrtest/runtest.py b/Lib/test/libregrtest/runtest.py
index fda4ca1ebca..107b04cf9dc 100644
--- a/Lib/test/libregrtest/runtest.py
+++ b/Lib/test/libregrtest/runtest.py
@@ -1,6 +1,7 @@
import faulthandler
import importlib
import io
+import json
import os
import sys
import time
@@ -79,7 +80,7 @@ def get_abs_module(ns, test):
return 'test.' + test
-def runtest(ns, test):
+def runtest(ns, test, *, slave=False):
"""Run a single test.
ns -- regrtest namespace of options
@@ -98,6 +99,25 @@ def runtest(ns, test):
output_on_failure = ns.verbose3
+ if not slave and test == "test_site":
+ retcode, stdout, stderr = run_test_in_subprocess(test, ns)
+ if retcode != 0:
+ result = (CHILD_ERROR, None)
+ else:
+ stdout, _, result = stdout.strip().rpartition("\n")
+ if not result:
+ return (None, None)
+ result = json.loads(result)
+ stdout = stdout.rstrip()
+ stderr = stderr.rstrip()
+ if stdout:
+ print(stdout, flush=True)
+ if stderr and not ns.pgo:
+ print(stderr, file=sys.stderr, flush=True)
+ if result[0] == INTERRUPTED:
+ raise KeyboardInterrupt
+ return result
+
use_timeout = (ns.timeout is not None)
if use_timeout:
faulthandler.dump_traceback_later(ns.timeout, exit=True)
@@ -143,6 +163,41 @@ def runtest(ns, test):
runtest.stringio = None
+def run_test_in_subprocess(testname, ns):
+ """Run the given test in a subprocess with --slaveargs.
+
+ ns is the option Namespace parsed from command-line arguments. regrtest
+ is invoked in a subprocess with the --slaveargs argument; when the
+ subprocess exits, its return code, stdout and stderr are returned as a
+ 3-tuple.
+ """
+ from subprocess import Popen, PIPE
+
+ ns_dict = vars(ns)
+ slaveargs = (ns_dict, testname)
+ slaveargs = json.dumps(slaveargs)
+
+ cmd = [sys.executable, *support.args_from_interpreter_flags(),
+ '-u', # Unbuffered stdout and stderr
+ '-m', 'test.regrtest',
+ '--slaveargs', slaveargs]
+ if ns.pgo:
+ cmd += ['--pgo']
+
+ # Running the child from the same working directory as regrtest's original
+ # invocation ensures that TEMPDIR for the child is the same when
+ # sysconfig.is_python_build() is true. See issue 15300.
+ popen = Popen(cmd,
+ stdout=PIPE, stderr=PIPE,
+ universal_newlines=True,
+ close_fds=(os.name != 'nt'),
+ cwd=support.SAVEDCWD)
+ with popen:
+ stdout, stderr = popen.communicate()
+ retcode = popen.wait()
+ return retcode, stdout, stderr
+
+
def runtest_inner(ns, test, display_failure=True):
support.unload(test)
diff --git a/Lib/test/libregrtest/runtest_mp.py b/Lib/test/libregrtest/runtest_mp.py
index 779ff01a649..a32c9b3e01c 100644
--- a/Lib/test/libregrtest/runtest_mp.py
+++ b/Lib/test/libregrtest/runtest_mp.py
@@ -1,12 +1,10 @@
import faulthandler
import json
-import os
import queue
import sys
import time
import traceback
import types
-from test import support
try:
import threading
except ImportError:
@@ -14,7 +12,7 @@ except ImportError:
sys.exit(2)
from test.libregrtest.runtest import (
- runtest, INTERRUPTED, CHILD_ERROR, PROGRESS_MIN_TIME,
+ runtest, run_test_in_subprocess, INTERRUPTED, CHILD_ERROR, PROGRESS_MIN_TIME,
format_test_result)
from test.libregrtest.setup import setup_tests
@@ -26,41 +24,6 @@ PROGRESS_UPDATE = 30.0 # seconds
WAIT_PROGRESS = 2.0 # seconds
-def run_test_in_subprocess(testname, ns):
- """Run the given test in a subprocess with --slaveargs.
-
- ns is the option Namespace parsed from command-line arguments. regrtest
- is invoked in a subprocess with the --slaveargs argument; when the
- subprocess exits, its return code, stdout and stderr are returned as a
- 3-tuple.
- """
- from subprocess import Popen, PIPE
-
- ns_dict = vars(ns)
- slaveargs = (ns_dict, testname)
- slaveargs = json.dumps(slaveargs)
-
- cmd = [sys.executable, *support.args_from_interpreter_flags(),
- '-u', # Unbuffered stdout and stderr
- '-m', 'test.regrtest',
- '--slaveargs', slaveargs]
- if ns.pgo:
- cmd += ['--pgo']
-
- # Running the child from the same working directory as regrtest's original
- # invocation ensures that TEMPDIR for the child is the same when
- # sysconfig.is_python_build() is true. See issue 15300.
- popen = Popen(cmd,
- stdout=PIPE, stderr=PIPE,
- universal_newlines=True,
- close_fds=(os.name != 'nt'),
- cwd=support.SAVEDCWD)
- with popen:
- stdout, stderr = popen.communicate()
- retcode = popen.wait()
- return retcode, stdout, stderr
-
-
def run_tests_slave(slaveargs):
ns_dict, testname = json.loads(slaveargs)
ns = types.SimpleNamespace(**ns_dict)
@@ -68,7 +31,7 @@ def run_tests_slave(slaveargs):
setup_tests(ns)
try:
- result = runtest(ns, testname)
+ result = runtest(ns, testname, slave=True)
except KeyboardInterrupt:
result = INTERRUPTED, ''
except BaseException as e:
diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
index 159d9ad9bfc..5394476b24f 100644
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
@@ -8,6 +8,7 @@ import unittest
import test.support
from test.support import captured_stderr, TESTFN, EnvironmentVarGuard
import builtins
+import importlib
import os
import sys
import re
@@ -29,12 +30,19 @@ import site
OLD_SYS_PATH = None
+OLD__PYTHONNOSITEPACKAGES = None
def setUpModule():
global OLD_SYS_PATH
OLD_SYS_PATH = sys.path[:]
+ if "_PYTHONNOSITEPACKAGES" in os.environ:
+ global OLD__PYTHONNOSITEPACKAGES
+ OLD__PYTHONNOSITEPACKAGES = os.environ.get("_PYTHONNOSITEPACKAGES")
+ del os.environ["_PYTHONNOSITEPACKAGES"]
+ importlib.reload(site)
+
if site.ENABLE_USER_SITE and not os.path.isdir(site.USER_SITE):
# need to add user site directory for tests
try:
@@ -48,6 +56,8 @@ def setUpModule():
def tearDownModule():
sys.path[:] = OLD_SYS_PATH
+ if OLD__PYTHONNOSITEPACKAGES is not None:
+ os.environ["_PYTHONNOSITEPACKAGES"] = OLD__PYTHONNOSITEPACKAGES
class HelperFunctionsTests(unittest.TestCase):
@@ -456,8 +466,11 @@ class StartupImportTests(unittest.TestCase):
def test_startup_imports(self):
# This tests checks which modules are loaded by Python when it
# initially starts upon startup.
+ env = os.environ.copy()
+ env["_PYTHONNOSITEPACKAGES"] = "1"
popen = subprocess.Popen([sys.executable, '-I', '-v', '-c',
'import sys; print(set(sys.modules))'],
+ env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='utf-8')
diff --git a/Makefile.pre.in b/Makefile.pre.in
index 283790b6a47..7ad02e85cc9 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -997,7 +997,7 @@ $(LIBRARY_OBJS) $(MODOBJS) Programs/python.o: $(PYTHON_HEADERS)
######################################################################
TESTOPTS= $(EXTRATESTOPTS)
-TESTPYTHON= $(RUNSHARED) ./$(BUILDPYTHON) $(TESTPYTHONOPTS)
+TESTPYTHON= _PYTHONNOSITEPACKAGES=1 $(RUNSHARED) ./$(BUILDPYTHON) $(TESTPYTHONOPTS)
TESTRUNNER= $(TESTPYTHON) $(srcdir)/Tools/scripts/run_tests.py
TESTTIMEOUT= 1200