From 3f415ab94c9d906177ea7ac21332f7b073cc1f6c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 2 Oct 2025 15:17:26 +0200 Subject: [PATCH 1/3] gh-139322: Reenable test_os.test_getlogin() --- Lib/test/test_os/test_os.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index 623d05235835c2..9705219788e2c0 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -3197,9 +3197,6 @@ def test_spawnvpe_invalid_env(self): self._test_invalid_env(os.spawnvpe) -# The introduction of this TestCase caused at least two different errors on -# *nix buildbots. Temporarily skip this to let the buildbots move along. -@unittest.skip("Skip due to platform/environment differences on *NIX buildbots") @unittest.skipUnless(hasattr(os, 'getlogin'), "test needs os.getlogin") class LoginTests(unittest.TestCase): def test_getlogin(self): From ecd2fdcaee4e96169360586b28682439fd10715d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 2 Oct 2025 15:45:11 +0200 Subject: [PATCH 2/3] Fix getlogin() errno Skip test_getlogin() is getlogin() fails with ENOTTY. --- Lib/test/test_os/test_os.py | 8 +++++++- .../2025-10-02-15-45-08.gh-issue-139322.rouPGj.rst | 2 ++ Modules/posixmodule.c | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-10-02-15-45-08.gh-issue-139322.rouPGj.rst diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index 9705219788e2c0..a2d396a63d88c2 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -3200,7 +3200,13 @@ def test_spawnvpe_invalid_env(self): @unittest.skipUnless(hasattr(os, 'getlogin'), "test needs os.getlogin") class LoginTests(unittest.TestCase): def test_getlogin(self): - user_name = os.getlogin() + try: + user_name = os.getlogin() + except OSError as exc: + if exc.errno == errno.ENOTTY: + self.skipTest(str(exc)) + else: + raise self.assertNotEqual(len(user_name), 0) diff --git a/Misc/NEWS.d/next/Library/2025-10-02-15-45-08.gh-issue-139322.rouPGj.rst b/Misc/NEWS.d/next/Library/2025-10-02-15-45-08.gh-issue-139322.rouPGj.rst new file mode 100644 index 00000000000000..39cae22474c4db --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-02-15-45-08.gh-issue-139322.rouPGj.rst @@ -0,0 +1,2 @@ +Fix :func:`os.getlogin` error handling: fix the error number. Patch by +Victor Stinner. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index b7a0110226590e..f50167c223e2fc 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -9605,7 +9605,7 @@ os_getlogin_impl(PyObject *module) int err = getlogin_r(name, sizeof(name)); if (err) { int old_errno = errno; - errno = -err; + errno = err; posix_error(); errno = old_errno; } From cd42a962a95bac9210903b445782c9a481f22dea Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 2 Oct 2025 16:43:06 +0200 Subject: [PATCH 3/3] Skip also the test on ENXIO --- Lib/test/test_os/test_os.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index a2d396a63d88c2..535e5a2a651445 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -3203,7 +3203,7 @@ def test_getlogin(self): try: user_name = os.getlogin() except OSError as exc: - if exc.errno == errno.ENOTTY: + if exc.errno in (errno.ENOTTY, errno.ENXIO): self.skipTest(str(exc)) else: raise