Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db6322c

Browse files
committed
Fixes python#24875: pip can now be installed in a venv with --system-site-packages.
1 parent a5917d1 commit db6322c

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

Lib/test/test_venv.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,7 @@ def test_devnull_exists_and_is_empty(self):
328328
with open(os.devnull, "rb") as f:
329329
self.assertEqual(f.read(), b"")
330330

331-
# Requesting pip fails without SSL (http://bugs.python.org/issue19744)
332-
@unittest.skipIf(ssl is None, ensurepip._MISSING_SSL_MESSAGE)
333-
@unittest.skipUnless(threading, 'some dependencies of pip import threading'
334-
' module unconditionally')
335-
# Issue #26610: pip/pep425tags.py requires ctypes
336-
@unittest.skipUnless(ctypes, 'pip requires ctypes')
337-
def test_with_pip(self):
331+
def do_test_with_pip(self, system_site_packages):
338332
rmtree(self.env_dir)
339333
with EnvironmentVarGuard() as envvars:
340334
# pip's cross-version compatibility may trigger deprecation
@@ -368,6 +362,7 @@ def test_with_pip(self):
368362
# config in place to ensure we ignore it
369363
try:
370364
self.run_with_capture(venv.create, self.env_dir,
365+
system_site_packages=system_site_packages,
371366
with_pip=True)
372367
except subprocess.CalledProcessError as exc:
373368
# The output this produces can be a little hard to read,
@@ -417,9 +412,21 @@ def test_with_pip(self):
417412
out = out.decode("latin-1") # Force to text, prevent decoding errors
418413
self.assertIn("Successfully uninstalled pip", out)
419414
self.assertIn("Successfully uninstalled setuptools", out)
420-
# Check pip is now gone from the virtual environment
421-
self.assert_pip_not_installed()
415+
# Check pip is now gone from the virtual environment. This only
416+
# applies in the system_site_packages=False case, because in the
417+
# other case, pip may still be available in the system site-packages
418+
if not system_site_packages:
419+
self.assert_pip_not_installed()
422420

421+
# Requesting pip fails without SSL (http://bugs.python.org/issue19744)
422+
@unittest.skipIf(ssl is None, ensurepip._MISSING_SSL_MESSAGE)
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)
423430

424431
if __name__ == "__main__":
425432
unittest.main()

Lib/venv/__init__.py

+9
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,22 @@ def create(self, env_dir):
7777
"""
7878
env_dir = os.path.abspath(env_dir)
7979
context = self.ensure_directories(env_dir)
80+
# See issue 24875. We need system_site_packages to be False
81+
# until after pip is installed.
82+
true_system_site_packages = self.system_site_packages
83+
self.system_site_packages = False
8084
self.create_configuration(context)
8185
self.setup_python(context)
8286
if self.with_pip:
8387
self._setup_pip(context)
8488
if not self.upgrade:
8589
self.setup_scripts(context)
8690
self.post_setup(context)
91+
if true_system_site_packages:
92+
# We had set it to False before, now
93+
# restore it and rewrite the configuration
94+
self.system_site_packages = True
95+
self.create_configuration(context)
8796

8897
def clear_directory(self, path):
8998
for fn in os.listdir(path):

0 commit comments

Comments
 (0)