Skip to content

Commit 7601d78

Browse files
committed
Closes python#24875: Merged fix from 3.6.
2 parents e1af696 + 993f535 commit 7601d78

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

Lib/test/test_venv.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,7 @@ def test_devnull(self):
330330
else:
331331
self.assertTrue(os.path.exists(os.devnull))
332332

333-
334-
@unittest.skipUnless(threading, 'some dependencies of pip import threading'
335-
' module unconditionally')
336-
# Issue #26610: pip/pep425tags.py requires ctypes
337-
@unittest.skipUnless(ctypes, 'pip requires ctypes')
338-
def test_with_pip(self):
333+
def do_test_with_pip(self, system_site_packages):
339334
rmtree(self.env_dir)
340335
with EnvironmentVarGuard() as envvars:
341336
# pip's cross-version compatibility may trigger deprecation
@@ -369,6 +364,7 @@ def test_with_pip(self):
369364
# config in place to ensure we ignore it
370365
try:
371366
self.run_with_capture(venv.create, self.env_dir,
367+
system_site_packages=system_site_packages,
372368
with_pip=True)
373369
except subprocess.CalledProcessError as exc:
374370
# The output this produces can be a little hard to read,
@@ -418,9 +414,19 @@ def test_with_pip(self):
418414
out = out.decode("latin-1") # Force to text, prevent decoding errors
419415
self.assertIn("Successfully uninstalled pip", out)
420416
self.assertIn("Successfully uninstalled setuptools", out)
421-
# Check pip is now gone from the virtual environment
422-
self.assert_pip_not_installed()
417+
# Check pip is now gone from the virtual environment. This only
418+
# applies in the system_site_packages=False case, because in the
419+
# other case, pip may still be available in the system site-packages
420+
if not system_site_packages:
421+
self.assert_pip_not_installed()
423422

423+
@unittest.skipUnless(threading, 'some dependencies of pip import threading'
424+
' module unconditionally')
425+
# Issue #26610: pip/pep425tags.py requires ctypes
426+
@unittest.skipUnless(ctypes, 'pip requires ctypes')
427+
def test_with_pip(self):
428+
self.do_test_with_pip(False)
429+
self.do_test_with_pip(True)
424430

425431
if __name__ == "__main__":
426432
unittest.main()

Lib/venv/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,22 @@ def create(self, env_dir):
5757
"""
5858
env_dir = os.path.abspath(env_dir)
5959
context = self.ensure_directories(env_dir)
60+
# See issue 24875. We need system_site_packages to be False
61+
# until after pip is installed.
62+
true_system_site_packages = self.system_site_packages
63+
self.system_site_packages = False
6064
self.create_configuration(context)
6165
self.setup_python(context)
6266
if self.with_pip:
6367
self._setup_pip(context)
6468
if not self.upgrade:
6569
self.setup_scripts(context)
6670
self.post_setup(context)
71+
if true_system_site_packages:
72+
# We had set it to False before, now
73+
# restore it and rewrite the configuration
74+
self.system_site_packages = True
75+
self.create_configuration(context)
6776

6877
def clear_directory(self, path):
6978
for fn in os.listdir(path):

0 commit comments

Comments
 (0)