Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move 'import getpass' statement to try-clause #9871

Merged
merged 7 commits into from Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -256,6 +256,7 @@ Ondřej Súkup
Oscar Benjamin
Parth Patel
Patrick Hayes
Paul Müller
Pauli Virtanen
Pavel Karateev
Paweł Adamczak
Expand Down
1 change: 1 addition & 0 deletions changelog/9871.bugfix.rst
@@ -0,0 +1 @@
Handle ``ImportError`` for ``getpass`` import in ``_pytest.tempdir.get_user``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the changelog is part of our user-facing documentation, we should describe this in terms of user-visible symptoms which are visible from the public API. In this case, I think that means something like:

Fix a bizarre (and fortunately rare) bug where the `temp_path` fixture could raise
an internal error while attempting to get the current user's username.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done!

5 changes: 3 additions & 2 deletions src/_pytest/tmpdir.py
Expand Up @@ -158,9 +158,10 @@ def getbasetemp(self) -> Path:
def get_user() -> Optional[str]:
"""Return the current user name, or None if getuser() does not work
in the current environment (see #1010)."""
import getpass

try:
# In some exotic environments, getpass may not be importable.
import getpass

return getpass.getuser()
except (ImportError, KeyError):
return None
Expand Down