Skip to content

os.getlogin() fails in tests #1156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ml31415 opened this issue Oct 27, 2015 · 4 comments
Closed

os.getlogin() fails in tests #1156

ml31415 opened this issue Oct 27, 2015 · 4 comments

Comments

@ml31415
Copy link

ml31415 commented Oct 27, 2015

import os

def test_pytest():
    name = os.getlogin()
    assert name
    print name

Running this as a test script fails with:

========================================== test session starts ===========================================
platform linux2 -- Python 2.7.9, pytest-2.8.2, py-1.4.30, pluggy-0.3.1
rootdir: /home/michael/workspace, inifile: 
collected 1 items 

test_pytest.py F

================================================ FAILURES ================================================
______________________________________________ test_pytest _______________________________________________

    def test_pytest():
>       name = os.getlogin()
E       OSError: [Errno 25] Inappropriate ioctl for device

test_pytest.py:4: OSError
======================================== 1 failed in 0.00 seconds ========================================

Outside the test environment, within the same shell, this runs without error. Is this intended behaviour?

@The-Compiler
Copy link
Member

From the Python docs:

Return the name of the user logged in on the controlling terminal of the process. For most purposes, it is more useful to use the environment variable LOGNAME to find out who the user is, or pwd.getpwuid(os.getuid())[0] to get the login name of the process’s real user id.

This doesn't work with pytest because of its stdout capturing - it'll probably work when passing -s. I'm not sure if there's anything we can do here though...

@ml31415
Copy link
Author

ml31415 commented Oct 27, 2015

Ok, well it's not problem to work around this, it just struck me with surprise. I wonder what's going on there under the hood. If os.getlogin() relies on not catching stdout, something should be wrong there?

@The-Compiler
Copy link
Member

os.getlogin() invokes the C function getlogin(3), which then uses the TCGETS ioctl on stdin. From the getlogin(3) manpage:

Note that glibc does not follow the POSIX specification and uses stdin instead of /dev/tty. A bug. (Other recent systems, like SunOS 5.8 and HP-UX 11.11 and FreeBSD 4.8 all return the login name also when stdin is redirected.)

And that's basically what pytest does - it replaces stdin by something which is not a terminal. I'm guessing glibc keeps this bug either because it's hard to fix, or for backwards-compatibility.

You should probably avoid using os.getlogin() altogether - I don't really see a good reason to use it.

I guess this can be closed?

@RonnyPfannschmidt
Copy link
Member

Its a glibc bug, we aren't at fault

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants