From 3a7ead6bcf2facd28819fed69abe79cdf90f5e10 Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Wed, 20 Apr 2022 15:48:57 -0700 Subject: [PATCH] [7.1.x] fix: move 'import getpass' statement to try-clause --- AUTHORS | 1 + changelog/9871.bugfix.rst | 2 ++ src/_pytest/tmpdir.py | 5 +++-- 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 changelog/9871.bugfix.rst diff --git a/AUTHORS b/AUTHORS index afe02b08f95..c0d1236b8b1 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