diff --git a/AUTHORS b/AUTHORS index 69e71fa10d4..5ab1c2f7a03 100644 --- a/AUTHORS +++ b/AUTHORS @@ -256,6 +256,7 @@ Ondřej Súkup Oscar Benjamin Parth Patel Patrick Hayes +Paul Müller Pauli Virtanen Pavel Karateev Paweł Adamczak diff --git a/changelog/9871.bugfix.rst b/changelog/9871.bugfix.rst new file mode 100644 index 00000000000..ed787b3da34 --- /dev/null +++ b/changelog/9871.bugfix.rst @@ -0,0 +1,2 @@ +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. diff --git a/src/_pytest/tmpdir.py b/src/_pytest/tmpdir.py index f901fd5727c..12dc463a2a2 100644 --- a/src/_pytest/tmpdir.py +++ b/src/_pytest/tmpdir.py @@ -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