[Python-checkins] cpython: Issue #19744: improve ensurepip error when ssl is missing

nick.coghlan python-checkins at python.org
Mon Dec 23 14:07:21 CET 2013


http://hg.python.org/cpython/rev/f670d8db8ef3
changeset: 88149:f670d8db8ef3
user: Nick Coghlan <ncoghlan at gmail.com>
date: Mon Dec 23 23:07:07 2013 +1000
summary:
 Issue #19744: improve ensurepip error when ssl is missing
files:
 Lib/ensurepip/__init__.py | 15 ++++++
 Lib/test/test_ensurepip.py | 57 ++++++++++++++++++++++++++
 Lib/test/test_venv.py | 7 ++-
 Misc/NEWS | 4 +
 4 files changed, 81 insertions(+), 2 deletions(-)
diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py
--- a/Lib/ensurepip/__init__.py
+++ b/Lib/ensurepip/__init__.py
@@ -14,6 +14,19 @@
 
 _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 = [
 ("setuptools", _SETUPTOOLS_VERSION),
 ("pip", _PIP_VERSION),
@@ -57,6 +70,7 @@
 if altinstall and default_pip:
 raise ValueError("Cannot use altinstall and default_pip together")
 
+ _require_ssl_for_pip()
 _clear_pip_environment_variables()
 
 # By default, installing pip and setuptools installs all of the
@@ -121,6 +135,7 @@
 "({!r} installed, {!r} bundled)")
 raise RuntimeError(msg.format(pip.__version__, _PIP_VERSION))
 
+ _require_ssl_for_pip()
 _clear_pip_environment_variables()
 
 # Construct the arguments to be passed to the pip command
diff --git a/Lib/test/test_ensurepip.py b/Lib/test/test_ensurepip.py
--- a/Lib/test/test_ensurepip.py
+++ b/Lib/test/test_ensurepip.py
@@ -9,6 +9,20 @@
 import ensurepip
 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):
 
 def test_returns_version(self):
@@ -28,8 +42,10 @@
 patched_os.path = os.path
 self.os_environ = patched_os.environ = os.environ.copy()
 
+
 class TestBootstrap(EnsurepipMixin, unittest.TestCase):
 
+ @requires_usable_pip
 def test_basic_bootstrapping(self):
 ensurepip.bootstrap()
 
@@ -44,6 +60,7 @@
 additional_paths = self.run_pip.call_args[0][1]
 self.assertEqual(len(additional_paths), 2)
 
+ @requires_usable_pip
 def test_bootstrapping_with_root(self):
 ensurepip.bootstrap(root="/foo/bar/")
 
@@ -56,6 +73,7 @@
 unittest.mock.ANY,
 )
 
+ @requires_usable_pip
 def test_bootstrapping_with_user(self):
 ensurepip.bootstrap(user=True)
 
@@ -67,6 +85,7 @@
 unittest.mock.ANY,
 )
 
+ @requires_usable_pip
 def test_bootstrapping_with_upgrade(self):
 ensurepip.bootstrap(upgrade=True)
 
@@ -78,6 +97,7 @@
 unittest.mock.ANY,
 )
 
+ @requires_usable_pip
 def test_bootstrapping_with_verbosity_1(self):
 ensurepip.bootstrap(verbosity=1)
 
@@ -89,6 +109,7 @@
 unittest.mock.ANY,
 )
 
+ @requires_usable_pip
 def test_bootstrapping_with_verbosity_2(self):
 ensurepip.bootstrap(verbosity=2)
 
@@ -100,6 +121,7 @@
 unittest.mock.ANY,
 )
 
+ @requires_usable_pip
 def test_bootstrapping_with_verbosity_3(self):
 ensurepip.bootstrap(verbosity=3)
 
@@ -111,14 +133,17 @@
 unittest.mock.ANY,
 )
 
+ @requires_usable_pip
 def test_bootstrapping_with_regular_install(self):
 ensurepip.bootstrap()
 self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "install")
 
+ @requires_usable_pip
 def test_bootstrapping_with_alt_install(self):
 ensurepip.bootstrap(altinstall=True)
 self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall")
 
+ @requires_usable_pip
 def test_bootstrapping_with_default_pip(self):
 ensurepip.bootstrap(default_pip=True)
 self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ)
@@ -128,6 +153,7 @@
 ensurepip.bootstrap(altinstall=True, default_pip=True)
 self.run_pip.assert_not_called()
 
+ @requires_usable_pip
 def test_pip_environment_variables_removed(self):
 # ensurepip deliberately ignores all pip environment variables
 # See http://bugs.python.org/issue19734 for details
@@ -169,6 +195,7 @@
 self.run_pip.assert_not_called()
 
 
+ @requires_usable_pip
 def test_uninstall(self):
 with fake_pip():
 ensurepip._uninstall_helper()
@@ -177,6 +204,7 @@
 ["uninstall", "-y", "pip", "setuptools"]
 )
 
+ @requires_usable_pip
 def test_uninstall_with_verbosity_1(self):
 with fake_pip():
 ensurepip._uninstall_helper(verbosity=1)
@@ -185,6 +213,7 @@
 ["uninstall", "-y", "-v", "pip", "setuptools"]
 )
 
+ @requires_usable_pip
 def test_uninstall_with_verbosity_2(self):
 with fake_pip():
 ensurepip._uninstall_helper(verbosity=2)
@@ -193,6 +222,7 @@
 ["uninstall", "-y", "-vv", "pip", "setuptools"]
 )
 
+ @requires_usable_pip
 def test_uninstall_with_verbosity_3(self):
 with fake_pip():
 ensurepip._uninstall_helper(verbosity=3)
@@ -201,6 +231,7 @@
 ["uninstall", "-y", "-vvv", "pip", "setuptools"]
 )
 
+ @requires_usable_pip
 def test_pip_environment_variables_removed(self):
 # ensurepip deliberately ignores all pip environment variables
 # See http://bugs.python.org/issue19734 for details
@@ -210,6 +241,30 @@
 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
 
 EXPECTED_VERSION_OUTPUT = "pip " + ensurepip._PIP_VERSION
@@ -224,6 +279,7 @@
 self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
 self.run_pip.assert_not_called()
 
+ @requires_usable_pip
 def test_basic_bootstrapping(self):
 ensurepip._main([])
 
@@ -248,6 +304,7 @@
 self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
 self.run_pip.assert_not_called()
 
+ @requires_usable_pip
 def test_basic_uninstall(self):
 with fake_pip():
 ensurepip._uninstall._main([])
diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py
--- a/Lib/test/test_venv.py
+++ b/Lib/test/test_venv.py
@@ -17,6 +17,9 @@
 import textwrap
 import unittest
 import venv
+
+# pip currently requires ssl support, so we ensure we handle
+# it being missing (http://bugs.python.org/issue19744)
 try:
 import ssl
 except ImportError:
@@ -285,8 +288,8 @@
 self.run_with_capture(venv.create, self.env_dir, with_pip=False)
 self.assert_pip_not_installed()
 
- # Temporary skip for http://bugs.python.org/issue19744
- @unittest.skipIf(ssl is None, 'pip needs SSL support')
+ # Requesting pip fails without SSL (http://bugs.python.org/issue19744)
+ @unittest.skipIf(ssl is None, ensurepip._MISSING_SSL_MESSAGE)
 def test_with_pip(self):
 shutil.rmtree(self.env_dir)
 with EnvironmentVarGuard() as envvars:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -44,6 +44,10 @@
 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
 odd behaviour based on user configuration settings
 
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /