Skip to content
Open
Show file tree
Hide file tree
Changes from all 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/13835.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The tmpdir user detection fallback is covered for Python 3.13+ environments where ``getpass.getuser()`` raises ``OSError``.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Best use RST roles that would link to the respective docs.

28 changes: 22 additions & 6 deletions testing/test_tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,30 @@ def test_get_user_uid_not_found():
assert get_user() is None


def test_get_user_handles_oserror(monkeypatch: MonkeyPatch) -> None:
Comment thread
webknjaz marked this conversation as resolved.
"""Test that get_user() handles getpass.getuser() raising OSError.

Python 3.13+ consistently raises OSError when no username can be
determined from the environment (for example on Windows with no username
environment variables set).
"""

def raise_oserror() -> str:
raise OSError("No username set in the environment")

monkeypatch.setattr("getpass.getuser", raise_oserror)

assert get_user() is None


@pytest.mark.skipif(not sys.platform.startswith("win"), reason="win only")
def test_get_user(monkeypatch):
"""Test that get_user() function works even if environment variables
required by getpass module are missing from the environment on Windows
(#1010).
def test_get_user(monkeypatch: MonkeyPatch) -> None:
"""Test that get_user() works even if the environment variables
required by getpass are missing from the environment on Windows (#1010,
#13835).
"""
monkeypatch.delenv("USER", raising=False)
monkeypatch.delenv("USERNAME", raising=False)
for envvar in ("LOGNAME", "USER", "LNAME", "USERNAME"):
monkeypatch.delenv(envvar, raising=False)
assert get_user() is None


Expand Down