From 23a2fef3919d773041f0c06e5f1a85471132098c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Mon, 3 Aug 2020 10:41:03 +0200 Subject: [PATCH] When one keyring attempt fails, don't bother with more This makes https://github.com/pypa/pip/issues/8090 much less painful. --- news/8090.bugfix | 3 +++ src/pip/_internal/network/auth.py | 2 ++ tests/unit/test_networking_auth.py | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 news/8090.bugfix diff --git a/news/8090.bugfix b/news/8090.bugfix new file mode 100644 index 0000000..e9f2b7c --- /dev/null +++ b/news/8090.bugfix @@ -0,0 +1,3 @@ +Only attempt to use the keyring once and if it fails, don't try again. +This prevents spamming users with several keyring unlock prompts when they +cannot unlock or don't want to do so. diff --git a/src/pip/_internal/network/auth.py b/src/pip/_internal/network/auth.py index 1e1da54..c101f93 100644 --- a/src/pip/_internal/network/auth.py +++ b/src/pip/_internal/network/auth.py @@ -45,6 +45,7 @@ except Exception as exc: def get_keyring_auth(url, username): """Return the tuple auth for a given url from keyring.""" + global keyring if not url or not keyring: return None @@ -70,6 +71,7 @@ def get_keyring_auth(url, username): logger.warning( "Keyring is skipped due to an exception: %s", str(exc), ) + keyring = None class MultiDomainBasicAuth(AuthBase): diff --git a/tests/unit/test_networking_auth.py b/tests/unit/test_networking_auth.py index 0f0b679..0fc5799 100644 --- a/tests/unit/test_networking_auth.py +++ b/tests/unit/test_networking_auth.py @@ -222,3 +222,29 @@ def test_keyring_get_credential(monkeypatch, url, expect): assert auth._get_new_credentials( url, allow_netrc=False, allow_keyring=True ) == expect + + +class KeyringModuleBroken(object): + """Represents the current supported API of keyring, but broken""" + + def __init__(self): + self._call_count = 0 + + def get_credential(self, system, username): + self._call_count += 1 + raise Exception("This keyring is broken!") + + +def test_broken_keyring_disables_keyring(monkeypatch): + keyring_broken = KeyringModuleBroken() + monkeypatch.setattr(pip._internal.network.auth, 'keyring', keyring_broken) + + auth = MultiDomainBasicAuth(index_urls=["http://example.com/"]) + + assert keyring_broken._call_count == 0 + for i in range(5): + url = "http://example.com/path" + str(i) + assert auth._get_new_credentials( + url, allow_netrc=False, allow_keyring=True + ) == (None, None) + assert keyring_broken._call_count == 1 -- 2.26.2