Skip to content

Commit 134b7fa

Browse files
committed
When one keyring attempt fails, don't bother with more
This makes #8090 much less painful.
1 parent 89d8cba commit 134b7fa

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

news/8090.bugfix

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Only attempt to use the keyring once and if it fails, don't try again.
2+
This prevents spamming users with several keyring unlock prompts when they
3+
cannot unlock or don't want to do so.

src/pip/_internal/network/auth.py

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
def get_keyring_auth(url, username):
4545
# type: (str, str) -> Optional[AuthInfo]
4646
"""Return the tuple auth for a given url from keyring."""
47+
global keyring
4748
if not url or not keyring:
4849
return None
4950

@@ -69,6 +70,7 @@ def get_keyring_auth(url, username):
6970
logger.warning(
7071
"Keyring is skipped due to an exception: %s", str(exc),
7172
)
73+
keyring = None
7274
return None
7375

7476

tests/unit/test_network_auth.py

+25
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,28 @@ def test_keyring_get_credential(monkeypatch, url, expect):
242242
assert auth._get_new_credentials(
243243
url, allow_netrc=False, allow_keyring=True
244244
) == expect
245+
246+
247+
class KeyringModuleBroken(object):
248+
"""Represents the current supported API of keyring, but broken"""
249+
250+
def __init__(self):
251+
self._call_count = 0
252+
253+
def get_credential(self, system, username):
254+
self._call_count += 1
255+
raise Exception("This keyring is broken!")
256+
257+
258+
def test_broken_keyring_disables_keyring(monkeypatch):
259+
keyring_broken = KeyringModuleBroken()
260+
monkeypatch.setattr(pip._internal.network.auth, 'keyring', keyring_broken)
261+
262+
auth = MultiDomainBasicAuth(index_urls=["http://example.com/"])
263+
264+
assert keyring_broken._call_count == 0
265+
for _ in range(5):
266+
assert auth._get_new_credentials(
267+
"http://example.com/path1", allow_netrc=False, allow_keyring=True
268+
) == (None, None)
269+
assert keyring_broken._call_count == 1

0 commit comments

Comments
 (0)