summaryrefslogtreecommitdiff
blob: 3cd7c7cfb6a0e82fd7aea0578df8cc22e9595a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
https://bugs.python.org/issue1674555

--- Lib/site.py
+++ Lib/site.py
@@ -546,8 +546,12 @@
     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()
--- Lib/test/regrtest.py
+++ Lib/test/regrtest.py
@@ -152,6 +152,7 @@
 import unittest
 import warnings
 from inspect import isabstract
+from subprocess import Popen, PIPE
 
 try:
     import threading
@@ -478,7 +479,6 @@
     subprocess exits, its return code, stdout and stderr are returned as a
     3-tuple.
     """
-    from subprocess import Popen, PIPE
     base_cmd = ([sys.executable] + support.args_from_interpreter_flags() +
                 ['-X', 'faulthandler', '-m', 'test.regrtest'])
     # required to spawn a new process with PGO flag on/off
@@ -711,9 +711,57 @@
     support.use_resources = ns.use_resources
     save_modules = sys.modules.keys()
 
+    def _runtest(ns, test, verbose, quiet, huntrleaks=False, use_resources=None,
+                 output_on_failure=False, failfast=False, match_tests=None,
+                 timeout=None, *, pgo=False):
+        if test == "test_site":
+            base_cmd = ([sys.executable] + support.args_from_interpreter_flags() +
+                        ['-X', 'faulthandler', '-m', 'test.regrtest'])
+            # required to spawn a new process with PGO flag on/off
+            if pgo:
+                base_cmd = base_cmd + ['--pgo']
+            ns_dict = vars(ns)
+            slaveargs = (ns_dict, test)
+            slaveargs = json.dumps(slaveargs)
+            env = os.environ.copy()
+            try:
+                del env["_PYTHONNOSITEPACKAGES"]
+            except KeyError:
+                pass
+            popen = Popen(base_cmd + ['--slaveargs', slaveargs],
+                          stdout=PIPE, stderr=PIPE,
+                          universal_newlines=True,
+                          close_fds=(os.name != 'nt'),
+                          cwd=support.SAVEDCWD,
+                          env=env)
+            stdout, stderr = popen.communicate()
+            retcode = popen.wait()
+            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 pgo:
+                print(stderr, file=sys.stderr, flush=True)
+            if result[0] == INTERRUPTED:
+                raise KeyboardInterrupt
+            return result
+        else:
+            return runtest(ns, test, verbose, quiet, huntrleaks=huntrleaks,
+                           use_resources=use_resources,
+                           output_on_failure=output_on_failure,
+                           failfast=failfast, match_tests=match_tests,
+                           timeout=timeout, pgo=pgo)
+
     def accumulate_result(test, result):
         ok, test_time = result
-        if ok not in (CHILD_ERROR, INTERRUPTED):
+        if ok not in (None, CHILD_ERROR, INTERRUPTED):
             test_times.append((test_time, test))
         if ok == PASSED:
             good.append(test)
@@ -726,7 +774,7 @@
         elif ok == RESOURCE_DENIED:
             skipped.append(test)
             resource_denieds.append(test)
-        elif ok != INTERRUPTED:
+        elif ok not in (None, INTERRUPTED):
             raise ValueError("invalid test result: %r" % ok)
 
     if ns.list_tests:
@@ -856,11 +904,11 @@
             display_progress(test_index, test)
 
             def runtest_accumulate():
-                result = runtest(ns, test, ns.verbose, ns.quiet,
-                                 ns.huntrleaks,
-                                 output_on_failure=ns.verbose3,
-                                 timeout=ns.timeout, failfast=ns.failfast,
-                                 match_tests=ns.match_tests, pgo=ns.pgo)
+                result = _runtest(ns, test, ns.verbose, ns.quiet,
+                                  ns.huntrleaks,
+                                  output_on_failure=ns.verbose3,
+                                  timeout=ns.timeout, failfast=ns.failfast,
+                                  match_tests=ns.match_tests, pgo=ns.pgo)
                 accumulate_result(test, result)
 
             if ns.trace:
@@ -923,8 +971,8 @@
             sys.stdout.flush()
             try:
                 ns.verbose = True
-                ok = runtest(ns, test, True, ns.quiet, ns.huntrleaks,
-                             timeout=ns.timeout, pgo=ns.pgo)
+                ok = _runtest(ns, test, True, ns.quiet, ns.huntrleaks,
+                              timeout=ns.timeout, pgo=ns.pgo)
             except KeyboardInterrupt:
                 # print a newline separate from the ^C
                 print()
--- Lib/test/test_site.py
+++ Lib/test/test_site.py
@@ -8,6 +8,7 @@
 import test.support
 from test.support import captured_stderr, TESTFN, EnvironmentVarGuard
 import builtins
+import importlib
 import os
 import sys
 import re
@@ -28,12 +29,19 @@
 
 
 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:
@@ -47,6 +55,8 @@
 
 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 @@
     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)
         stdout, stderr = popen.communicate()
--- Makefile.pre.in
+++ Makefile.pre.in
@@ -1004,7 +1004,7 @@
 ######################################################################
 
 TESTOPTS=	$(EXTRATESTOPTS)
-TESTPYTHON=	$(RUNSHARED) ./$(BUILDPYTHON) $(TESTPYTHONOPTS)
+TESTPYTHON=	_PYTHONNOSITEPACKAGES=1 $(RUNSHARED) ./$(BUILDPYTHON) $(TESTPYTHONOPTS)
 TESTRUNNER=	$(TESTPYTHON) $(srcdir)/Tools/scripts/run_tests.py
 TESTTIMEOUT=	3600