mirror of
https://github.com/python/cpython.git
synced 2025-08-29 13:15:11 +00:00
Issue #19744: improve ensurepip error when ssl is missing
This commit is contained in:
parent
f71cae0a93
commit
ae2ee96ad7
4 changed files with 81 additions and 2 deletions
|
@ -14,6 +14,19 @@ _SETUPTOOLS_VERSION = "2.0.1"
|
||||||
|
|
||||||
_PIP_VERSION = "1.5rc2"
|
_PIP_VERSION = "1.5rc2"
|
||||||
|
|
||||||
|
# pip currently requires ssl support, so we try to provide a nicer
|
||||||
|
# error message when that is missing (http://bugs.python.org/issue19744)
|
||||||
|
_MISSING_SSL_MESSAGE = ("pip {} requires SSL/TLS".format(_PIP_VERSION))
|
||||||
|
try:
|
||||||
|
import ssl
|
||||||
|
except ImportError:
|
||||||
|
ssl = None
|
||||||
|
def _require_ssl_for_pip():
|
||||||
|
raise RuntimeError(_MISSING_SSL_MESSAGE)
|
||||||
|
else:
|
||||||
|
def _require_ssl_for_pip():
|
||||||
|
pass
|
||||||
|
|
||||||
_PROJECTS = [
|
_PROJECTS = [
|
||||||
("setuptools", _SETUPTOOLS_VERSION),
|
("setuptools", _SETUPTOOLS_VERSION),
|
||||||
("pip", _PIP_VERSION),
|
("pip", _PIP_VERSION),
|
||||||
|
@ -57,6 +70,7 @@ def bootstrap(*, root=None, upgrade=False, user=False,
|
||||||
if altinstall and default_pip:
|
if altinstall and default_pip:
|
||||||
raise ValueError("Cannot use altinstall and default_pip together")
|
raise ValueError("Cannot use altinstall and default_pip together")
|
||||||
|
|
||||||
|
_require_ssl_for_pip()
|
||||||
_clear_pip_environment_variables()
|
_clear_pip_environment_variables()
|
||||||
|
|
||||||
# By default, installing pip and setuptools installs all of the
|
# By default, installing pip and setuptools installs all of the
|
||||||
|
@ -121,6 +135,7 @@ def _uninstall_helper(*, verbosity=0):
|
||||||
"({!r} installed, {!r} bundled)")
|
"({!r} installed, {!r} bundled)")
|
||||||
raise RuntimeError(msg.format(pip.__version__, _PIP_VERSION))
|
raise RuntimeError(msg.format(pip.__version__, _PIP_VERSION))
|
||||||
|
|
||||||
|
_require_ssl_for_pip()
|
||||||
_clear_pip_environment_variables()
|
_clear_pip_environment_variables()
|
||||||
|
|
||||||
# Construct the arguments to be passed to the pip command
|
# Construct the arguments to be passed to the pip command
|
||||||
|
|
|
@ -9,6 +9,20 @@ import sys
|
||||||
import ensurepip
|
import ensurepip
|
||||||
import ensurepip._uninstall
|
import ensurepip._uninstall
|
||||||
|
|
||||||
|
# pip currently requires ssl support, so we ensure we handle
|
||||||
|
# it being missing (http://bugs.python.org/issue19744)
|
||||||
|
ensurepip_no_ssl = test.support.import_fresh_module("ensurepip",
|
||||||
|
blocked=["ssl"])
|
||||||
|
try:
|
||||||
|
import ssl
|
||||||
|
except ImportError:
|
||||||
|
def requires_usable_pip(f):
|
||||||
|
deco = unittest.skip(ensurepip._MISSING_SSL_MESSAGE)
|
||||||
|
return deco(f)
|
||||||
|
else:
|
||||||
|
def requires_usable_pip(f):
|
||||||
|
return f
|
||||||
|
|
||||||
class TestEnsurePipVersion(unittest.TestCase):
|
class TestEnsurePipVersion(unittest.TestCase):
|
||||||
|
|
||||||
def test_returns_version(self):
|
def test_returns_version(self):
|
||||||
|
@ -28,8 +42,10 @@ class EnsurepipMixin:
|
||||||
patched_os.path = os.path
|
patched_os.path = os.path
|
||||||
self.os_environ = patched_os.environ = os.environ.copy()
|
self.os_environ = patched_os.environ = os.environ.copy()
|
||||||
|
|
||||||
|
|
||||||
class TestBootstrap(EnsurepipMixin, unittest.TestCase):
|
class TestBootstrap(EnsurepipMixin, unittest.TestCase):
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_basic_bootstrapping(self):
|
def test_basic_bootstrapping(self):
|
||||||
ensurepip.bootstrap()
|
ensurepip.bootstrap()
|
||||||
|
|
||||||
|
@ -44,6 +60,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
|
||||||
additional_paths = self.run_pip.call_args[0][1]
|
additional_paths = self.run_pip.call_args[0][1]
|
||||||
self.assertEqual(len(additional_paths), 2)
|
self.assertEqual(len(additional_paths), 2)
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_bootstrapping_with_root(self):
|
def test_bootstrapping_with_root(self):
|
||||||
ensurepip.bootstrap(root="/foo/bar/")
|
ensurepip.bootstrap(root="/foo/bar/")
|
||||||
|
|
||||||
|
@ -56,6 +73,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
|
||||||
unittest.mock.ANY,
|
unittest.mock.ANY,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_bootstrapping_with_user(self):
|
def test_bootstrapping_with_user(self):
|
||||||
ensurepip.bootstrap(user=True)
|
ensurepip.bootstrap(user=True)
|
||||||
|
|
||||||
|
@ -67,6 +85,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
|
||||||
unittest.mock.ANY,
|
unittest.mock.ANY,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_bootstrapping_with_upgrade(self):
|
def test_bootstrapping_with_upgrade(self):
|
||||||
ensurepip.bootstrap(upgrade=True)
|
ensurepip.bootstrap(upgrade=True)
|
||||||
|
|
||||||
|
@ -78,6 +97,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
|
||||||
unittest.mock.ANY,
|
unittest.mock.ANY,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_bootstrapping_with_verbosity_1(self):
|
def test_bootstrapping_with_verbosity_1(self):
|
||||||
ensurepip.bootstrap(verbosity=1)
|
ensurepip.bootstrap(verbosity=1)
|
||||||
|
|
||||||
|
@ -89,6 +109,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
|
||||||
unittest.mock.ANY,
|
unittest.mock.ANY,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_bootstrapping_with_verbosity_2(self):
|
def test_bootstrapping_with_verbosity_2(self):
|
||||||
ensurepip.bootstrap(verbosity=2)
|
ensurepip.bootstrap(verbosity=2)
|
||||||
|
|
||||||
|
@ -100,6 +121,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
|
||||||
unittest.mock.ANY,
|
unittest.mock.ANY,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_bootstrapping_with_verbosity_3(self):
|
def test_bootstrapping_with_verbosity_3(self):
|
||||||
ensurepip.bootstrap(verbosity=3)
|
ensurepip.bootstrap(verbosity=3)
|
||||||
|
|
||||||
|
@ -111,14 +133,17 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
|
||||||
unittest.mock.ANY,
|
unittest.mock.ANY,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_bootstrapping_with_regular_install(self):
|
def test_bootstrapping_with_regular_install(self):
|
||||||
ensurepip.bootstrap()
|
ensurepip.bootstrap()
|
||||||
self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "install")
|
self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "install")
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_bootstrapping_with_alt_install(self):
|
def test_bootstrapping_with_alt_install(self):
|
||||||
ensurepip.bootstrap(altinstall=True)
|
ensurepip.bootstrap(altinstall=True)
|
||||||
self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall")
|
self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall")
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_bootstrapping_with_default_pip(self):
|
def test_bootstrapping_with_default_pip(self):
|
||||||
ensurepip.bootstrap(default_pip=True)
|
ensurepip.bootstrap(default_pip=True)
|
||||||
self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ)
|
self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ)
|
||||||
|
@ -128,6 +153,7 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
|
||||||
ensurepip.bootstrap(altinstall=True, default_pip=True)
|
ensurepip.bootstrap(altinstall=True, default_pip=True)
|
||||||
self.run_pip.assert_not_called()
|
self.run_pip.assert_not_called()
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_pip_environment_variables_removed(self):
|
def test_pip_environment_variables_removed(self):
|
||||||
# ensurepip deliberately ignores all pip environment variables
|
# ensurepip deliberately ignores all pip environment variables
|
||||||
# See http://bugs.python.org/issue19734 for details
|
# See http://bugs.python.org/issue19734 for details
|
||||||
|
@ -169,6 +195,7 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
|
||||||
self.run_pip.assert_not_called()
|
self.run_pip.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_uninstall(self):
|
def test_uninstall(self):
|
||||||
with fake_pip():
|
with fake_pip():
|
||||||
ensurepip._uninstall_helper()
|
ensurepip._uninstall_helper()
|
||||||
|
@ -177,6 +204,7 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
|
||||||
["uninstall", "-y", "pip", "setuptools"]
|
["uninstall", "-y", "pip", "setuptools"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_uninstall_with_verbosity_1(self):
|
def test_uninstall_with_verbosity_1(self):
|
||||||
with fake_pip():
|
with fake_pip():
|
||||||
ensurepip._uninstall_helper(verbosity=1)
|
ensurepip._uninstall_helper(verbosity=1)
|
||||||
|
@ -185,6 +213,7 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
|
||||||
["uninstall", "-y", "-v", "pip", "setuptools"]
|
["uninstall", "-y", "-v", "pip", "setuptools"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_uninstall_with_verbosity_2(self):
|
def test_uninstall_with_verbosity_2(self):
|
||||||
with fake_pip():
|
with fake_pip():
|
||||||
ensurepip._uninstall_helper(verbosity=2)
|
ensurepip._uninstall_helper(verbosity=2)
|
||||||
|
@ -193,6 +222,7 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
|
||||||
["uninstall", "-y", "-vv", "pip", "setuptools"]
|
["uninstall", "-y", "-vv", "pip", "setuptools"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_uninstall_with_verbosity_3(self):
|
def test_uninstall_with_verbosity_3(self):
|
||||||
with fake_pip():
|
with fake_pip():
|
||||||
ensurepip._uninstall_helper(verbosity=3)
|
ensurepip._uninstall_helper(verbosity=3)
|
||||||
|
@ -201,6 +231,7 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
|
||||||
["uninstall", "-y", "-vvv", "pip", "setuptools"]
|
["uninstall", "-y", "-vvv", "pip", "setuptools"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_pip_environment_variables_removed(self):
|
def test_pip_environment_variables_removed(self):
|
||||||
# ensurepip deliberately ignores all pip environment variables
|
# ensurepip deliberately ignores all pip environment variables
|
||||||
# See http://bugs.python.org/issue19734 for details
|
# See http://bugs.python.org/issue19734 for details
|
||||||
|
@ -210,6 +241,30 @@ class TestUninstall(EnsurepipMixin, unittest.TestCase):
|
||||||
self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
|
self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
|
||||||
|
|
||||||
|
|
||||||
|
class TestMissingSSL(EnsurepipMixin, unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
sys.modules["ensurepip"] = ensurepip_no_ssl
|
||||||
|
@self.addCleanup
|
||||||
|
def restore_module():
|
||||||
|
sys.modules["ensurepip"] = ensurepip
|
||||||
|
super().setUp()
|
||||||
|
|
||||||
|
def test_bootstrap_requires_ssl(self):
|
||||||
|
self.os_environ["PIP_THIS_SHOULD_STAY"] = "test fodder"
|
||||||
|
with self.assertRaisesRegex(RuntimeError, "requires SSL/TLS"):
|
||||||
|
ensurepip_no_ssl.bootstrap()
|
||||||
|
self.run_pip.assert_not_called()
|
||||||
|
self.assertIn("PIP_THIS_SHOULD_STAY", self.os_environ)
|
||||||
|
|
||||||
|
def test_uninstall_requires_ssl(self):
|
||||||
|
self.os_environ["PIP_THIS_SHOULD_STAY"] = "test fodder"
|
||||||
|
with self.assertRaisesRegex(RuntimeError, "requires SSL/TLS"):
|
||||||
|
with fake_pip():
|
||||||
|
ensurepip_no_ssl._uninstall_helper()
|
||||||
|
self.run_pip.assert_not_called()
|
||||||
|
self.assertIn("PIP_THIS_SHOULD_STAY", self.os_environ)
|
||||||
|
|
||||||
# Basic testing of the main functions and their argument parsing
|
# Basic testing of the main functions and their argument parsing
|
||||||
|
|
||||||
EXPECTED_VERSION_OUTPUT = "pip " + ensurepip._PIP_VERSION
|
EXPECTED_VERSION_OUTPUT = "pip " + ensurepip._PIP_VERSION
|
||||||
|
@ -224,6 +279,7 @@ class TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase):
|
||||||
self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
|
self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
|
||||||
self.run_pip.assert_not_called()
|
self.run_pip.assert_not_called()
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_basic_bootstrapping(self):
|
def test_basic_bootstrapping(self):
|
||||||
ensurepip._main([])
|
ensurepip._main([])
|
||||||
|
|
||||||
|
@ -248,6 +304,7 @@ class TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase):
|
||||||
self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
|
self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
|
||||||
self.run_pip.assert_not_called()
|
self.run_pip.assert_not_called()
|
||||||
|
|
||||||
|
@requires_usable_pip
|
||||||
def test_basic_uninstall(self):
|
def test_basic_uninstall(self):
|
||||||
with fake_pip():
|
with fake_pip():
|
||||||
ensurepip._uninstall._main([])
|
ensurepip._uninstall._main([])
|
||||||
|
|
|
@ -17,6 +17,9 @@ from test.support import (captured_stdout, captured_stderr, run_unittest,
|
||||||
import textwrap
|
import textwrap
|
||||||
import unittest
|
import unittest
|
||||||
import venv
|
import venv
|
||||||
|
|
||||||
|
# pip currently requires ssl support, so we ensure we handle
|
||||||
|
# it being missing (http://bugs.python.org/issue19744)
|
||||||
try:
|
try:
|
||||||
import ssl
|
import ssl
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -285,8 +288,8 @@ class EnsurePipTest(BaseTest):
|
||||||
self.run_with_capture(venv.create, self.env_dir, with_pip=False)
|
self.run_with_capture(venv.create, self.env_dir, with_pip=False)
|
||||||
self.assert_pip_not_installed()
|
self.assert_pip_not_installed()
|
||||||
|
|
||||||
# Temporary skip for http://bugs.python.org/issue19744
|
# Requesting pip fails without SSL (http://bugs.python.org/issue19744)
|
||||||
@unittest.skipIf(ssl is None, 'pip needs SSL support')
|
@unittest.skipIf(ssl is None, ensurepip._MISSING_SSL_MESSAGE)
|
||||||
def test_with_pip(self):
|
def test_with_pip(self):
|
||||||
shutil.rmtree(self.env_dir)
|
shutil.rmtree(self.env_dir)
|
||||||
with EnvironmentVarGuard() as envvars:
|
with EnvironmentVarGuard() as envvars:
|
||||||
|
|
|
@ -44,6 +44,10 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #19744: ensurepip now provides a better error message when Python is
|
||||||
|
built without SSL/TLS support (pip currently requires that support to run,
|
||||||
|
even if only operating with local wheel files)
|
||||||
|
|
||||||
- Issue #19734: ensurepip now ignores all pip environment variables to avoid
|
- Issue #19734: ensurepip now ignores all pip environment variables to avoid
|
||||||
odd behaviour based on user configuration settings
|
odd behaviour based on user configuration settings
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue