From eab431727c76f58dae4809656297688f15059fc7 Mon Sep 17 00:00:00 2001 From: Ofek Danny Date: Mon, 27 Apr 2026 11:17:40 +0300 Subject: [PATCH 1/2] testing: add regression test for get_user() swallowing OSError (#13835) Python 3.13 changed getpass.getuser() to raise OSError (instead of ImportError or KeyError) when no username can be determined from the environment. The fix was already applied in #11875, but no test existed to prevent regressions. Add test_get_user_oserror() which patches getpass.getuser to raise OSError and asserts that get_user() returns None rather than propagating the exception. Also adds the corresponding changelog entry. Closes #13835 Signed-off-by: Ofek Danny --- changelog/13835.bugfix.rst | 1 + testing/test_tmpdir.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 changelog/13835.bugfix.rst diff --git a/changelog/13835.bugfix.rst b/changelog/13835.bugfix.rst new file mode 100644 index 00000000000..e088d07340d --- /dev/null +++ b/changelog/13835.bugfix.rst @@ -0,0 +1 @@ +:func:`get_user` now correctly returns ``None`` when :func:`getpass.getuser` raises :exc:`OSError`, which Python 3.13 introduced when no username environment variable is set (previously only :exc:`ImportError` and :exc:`KeyError` were caught). diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index 096527e6273..0c59d261090 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -368,6 +368,21 @@ def test_get_user(monkeypatch): assert get_user() is None +def test_get_user_oserror(monkeypatch): + """Test that get_user() returns None when getpass.getuser() raises OSError. + + Python 3.13 changed getpass.getuser() to raise OSError (instead of + ImportError or KeyError) when no username can be determined from the + environment. Regression test for #13835. + """ + + def raise_oserror(): + raise OSError("No username set in the environment") + + monkeypatch.setattr("getpass.getuser", raise_oserror) + assert get_user() is None + + class TestNumberedDir: PREFIX = "fun-" From 5d497c717a256da3a4872f0c1c495df643121376 Mon Sep 17 00:00:00 2001 From: Ofek Danny Date: Mon, 27 Apr 2026 11:36:41 +0300 Subject: [PATCH 2/2] changelog: fix RST cross-reference roles for non-public functions Signed-off-by: Ofek Danny --- changelog/13835.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/13835.bugfix.rst b/changelog/13835.bugfix.rst index e088d07340d..89a8d1b96e1 100644 --- a/changelog/13835.bugfix.rst +++ b/changelog/13835.bugfix.rst @@ -1 +1 @@ -:func:`get_user` now correctly returns ``None`` when :func:`getpass.getuser` raises :exc:`OSError`, which Python 3.13 introduced when no username environment variable is set (previously only :exc:`ImportError` and :exc:`KeyError` were caught). +``get_user()`` now correctly returns ``None`` when ``getpass.getuser()`` raises :exc:`OSError`, which Python 3.13 introduced when no username environment variable is set (previously only :exc:`ImportError` and :exc:`KeyError` were caught).