Message11103

Author stefan.richthofer
Recipients jamesmudd, pguermo, stefan.richthofer, zyasoft
Date 2017-02-20.15:44:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1487605499.49.0.372469664834.issue2534@psf.upfronthosting.co.za>
In-reply-to
Content
I see this too, but it even fails in CPython for me:

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getlogin()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory
>>> 

Did you check if os.getlogin works in CPython?

Then I found the following: https://mail.python.org/pipermail/python-bugs-list/2002-July/012691.html

(Yes it is rather old, but it seems to cover the error I observe.)


So, given that os.getlogin is supposed to be a thin wrapper for https://linux.die.net/man/3/getlogin (?) Jython's behavior might actually be right if that call fails due to other reasons. Only the error message might better be more sophisticated.

That said, it would be trivial to build some fallbacks into PosixModule.getlogin, e.g.:

    public static PyObject getlogin() {
        String login = posix.getlogin();
        if (login == null) {
            login = System.getenv("LOGNAME");
        }
        if (login == null) {
            login = System.getenv("USER");
        }
        if (login == null) {
            login = System.getProperty("user.name");
        }
        return new PyString(login);
    }

originally:
    public static PyObject getlogin() {
        return new PyString(posix.getlogin());
    }
(posix is JNR's posix-wrapper)

However os.getlogin shall only work on terminal/live console if at all, which would not hold then any more. So code using this to detect console-mode would break (bad way for detection anyway?).
So maybe we should better keep it that way, like a thin wrapper and apply such fallbacks on Python-level, i.e. os.getenv('LOGNAME'), os.getenv('USER'), os.getenv('USERNAME') (windows)

I am actually undecided, because I favor portable wrappers and guess these fallbacks could prevent failures in naive real-world existing code.

Opinions?
History
Date User Action Args
2017-02-20 15:44:59stefan.richthofersetmessageid: <1487605499.49.0.372469664834.issue2534@psf.upfronthosting.co.za>
2017-02-20 15:44:59stefan.richthofersetrecipients: + stefan.richthofer, zyasoft, jamesmudd, pguermo
2017-02-20 15:44:59stefan.richthoferlinkissue2534 messages
2017-02-20 15:44:58stefan.richthofercreate