@@ -357,14 +357,30 @@ def test_get_user_uid_not_found():
357357 assert get_user () is None
358358
359359
360+ def test_get_user_handles_oserror (monkeypatch : MonkeyPatch ) -> None :
361+ """Test that get_user() handles getpass.getuser() raising OSError.
362+
363+ Python 3.13+ consistently raises OSError when no username can be
364+ determined from the environment (for example on Windows with no username
365+ environment variables set).
366+ """
367+
368+ def raise_oserror () -> str :
369+ raise OSError ("No username set in the environment" )
370+
371+ monkeypatch .setattr ("getpass.getuser" , raise_oserror )
372+
373+ assert get_user () is None
374+
375+
360376@pytest .mark .skipif (not sys .platform .startswith ("win" ), reason = "win only" )
361- def test_get_user (monkeypatch ) :
362- """Test that get_user() function works even if environment variables
363- required by getpass module are missing from the environment on Windows
364- (#1010 ).
377+ def test_get_user (monkeypatch : MonkeyPatch ) -> None :
378+ """Test that get_user() works even if the environment variables
379+ required by getpass are missing from the environment on Windows (#1010,
380+ #13835 ).
365381 """
366- monkeypatch . delenv ( " USER" , raising = False )
367- monkeypatch .delenv ("USERNAME" , raising = False )
382+ for envvar in ( "LOGNAME" , " USER" , "LNAME" , "USERNAME" ):
383+ monkeypatch .delenv (envvar , raising = False )
368384 assert get_user () is None
369385
370386
0 commit comments