Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/890.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve logging when keyring fails.
33 changes: 31 additions & 2 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import getpass
import logging
import re

import pytest

Expand Down Expand Up @@ -152,14 +153,42 @@ def test_get_username_runtime_error_suppressed(
entered_username, keyring_no_backends_get_credential, caplog, config
):
assert auth.Resolver(config, auth.CredentialInput()).username == "entered user"
assert caplog.messages == ["fail!"]
assert re.search(
r"Error from keyring.+Traceback.+RuntimeError: fail!", caplog.text, re.DOTALL
)


def test_get_password_runtime_error_suppressed(
entered_password, keyring_no_backends, caplog, config
):
assert auth.Resolver(config, auth.CredentialInput("user")).password == "entered pw"
assert caplog.messages == ["fail!"]
assert re.search(
r"Error from keyring.+Traceback.+RuntimeError: fail!", caplog.text, re.DOTALL
)


@pytest.mark.parametrize(
"get_credential",
[
auth.Resolver.get_username_from_keyring,
auth.Resolver.get_password_from_keyring,
],
Comment thread
bhrutledge marked this conversation as resolved.
Outdated
)
def test_log_exception_on_keyring_failure(get_credential, config, monkeypatch, caplog):
class FailKeyring:
@staticmethod
def get_credential(system, username):
# Simulate the error from https://github.com/pypa/twine/issues/889
environ = {}
environ["HOME"]
Comment thread
bhrutledge marked this conversation as resolved.
Outdated

monkeypatch.setattr(auth, "keyring", FailKeyring())

assert not get_credential(auth.Resolver(config, auth.CredentialInput()))

assert re.search(
r"Error from keyring.+Traceback.+KeyError: 'HOME'", caplog.text, re.DOTALL
)
Comment thread
bhrutledge marked this conversation as resolved.


def test_get_username_return_none(entered_username, monkeypatch, config):
Expand Down
4 changes: 2 additions & 2 deletions twine/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def get_username_from_keyring(self) -> Optional[str]:
# To support keyring prior to 15.2
pass
except Exception as exc:
logger.warning(str(exc))
logger.warning("Error from keyring", exc_info=exc)
return None

def get_password_from_keyring(self) -> Optional[str]:
Expand All @@ -73,7 +73,7 @@ def get_password_from_keyring(self) -> Optional[str]:
logger.info("Querying keyring for password")
return cast(str, keyring.get_password(system, username))
except Exception as exc:
logger.warning(str(exc))
logger.warning("Error from keyring", exc_info=exc)
return None

def username_from_keyring_or_prompt(self) -> str:
Expand Down