Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 4647 - Python os.getlogin() doesn't work
Summary: Python os.getlogin() doesn't work
Status: RESOLVED INVALID
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Development (show other bugs)
Hardware: x86 Linux
: High major (vote)
Assignee: Jon Nelson (RETIRED)
URL:
Whiteboard:
Keywords:
: 20037 (view as bug list)
Depends on:
Blocks:
 
Reported: 2002-07-07 12:13 UTC by Robert Blyler
Modified: 2003-05-21 03:59 UTC (History)
2 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
output of strace -o (output,25.93 KB, text/plain)
2002-07-08 18:42 UTC, Jon Nelson (RETIRED)
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Robert Blyler 2002-07-07 12:13:14 UTC
Python error on standard library:

Traceback (most recent call last):
  File "./trilliji.py", line 25, in ?
    main()
  File "./trilliji.py", line 16, in main
    Connections = TriCon()
  File "./clsTriCon.py", line 16, in __init__
    self.login_user=os.getlogin()
OSError: [Errno 2] No such file or directory

Worked in 1.1a
Comment 1 Jon Nelson (RETIRED) 2002-07-08 07:32:35 UTC
python -c 'import os;print os.getlogin()' works for me on (all tested versions)
of python, and I tried several across several platforms.

The os.getlogin() function is a thin wrapper around the C function getlogin(3).
What you are likely seeing is a problem with utmp. Do this:

strace -o output python2.2 -c 'import os;print os.getlogin()'

and attach the file "output" (or just paste it in here).
Comment 2 Jon Nelson (RETIRED) 2002-07-08 18:42:06 UTC
The following was sent to me directly:


bash-2.05a# strace -o output python2.2 -c 'import os;print
os.getlogin()'
Traceback (most recent call last):
  File "<string>", line 1, in ?
OSError: [Errno 2] No such file or directory


Comment 3 Jon Nelson (RETIRED) 2002-07-08 18:42:22 UTC
Created attachment 2049 [details]
output of strace -o
Comment 4 Jon Nelson (RETIRED) 2002-07-08 22:56:43 UTC
Well.
SOme investigation has lead me to believe that your utmp file is corrupt, *and*
that the error returned by Python/getlogin (not sure which) is also wrong, or at
least, misleading.

Thus, please two things for me:

1. move the utmp file out of the way, and 
2. run 'ltrace -o foo python -c "import os;print os.getlogin()"

and attach to this bug the foo file, please (content-type text/plain,
*uncompressed* please), if it still fails.

Thanks!

Comment 5 Robert Blyler 2002-07-11 18:33:57 UTC
Ok, I upgraded my system to current, moved utmp to a different location, and
created a new one with touch utmp. I still have the same problem.
Attached ltrace as you suggested. The bug submission system will not allow me to
attach the ltrace output. It says it is too large for a non-patch submission.
Comment 6 Robert Blyler 2002-07-14 10:12:22 UTC
Did a fresh install from scratch of gentoo 1.2 this weekend. After install the
results are the same as previously. The bug is repeatable here.
Comment 7 Arun Thomas (RETIRED) gentoo-dev 2002-07-18 20:47:34 UTC
I have the same problem. It only occurs when I run python -c 'import os;print
os.getlogin()' in gnome-terminal/multi-gnome-terminal/rxvt/Eterm. When I run it
in an xterm or on the virtual console, it works perfectly. 

Out of curiosity, I tried calling getlogin() from a C program running in the
gnome-terminal, etc. It returns a null pointer. There is an errno of 2 (No such
file or directory) associated with the getlogin() call, just as the Python
interpreter reports. That's a strange error... When executing in the virtual
console or xterm, the C program returns the userid as it should.

Maybe it's a terminal emulation issue. I wonder why it works in the xterm...I'm
not sure if this is a bug or not. It may jsimply be a getlogin() limitation. Let
me know if I can help. I'm a wannabe gentoo developer, so I could use the
experience.

Workaround:

This works in gnome-terminal, etc. as well as VC/xterm:

import os, pwd
print pwd.getpwuid(os.geteuid())[0]
Comment 8 Jon Nelson (RETIRED) 2002-07-18 21:40:24 UTC
Arun - thanks for the great debugging job!

I've come to the conclusion that isn't is *not* a bug in python, as the exact
same problem is experienced by the most minimal C program.

More likely, this is a problem with some Xterm-like programs (gnome-terminal,
the infamous rxvt, and most likely others) that don't update the utmp file.

Using the code Arun provided is almost certainly a better solution, and more
portable anyway.

Comment 9 Robert Blyler 2002-07-19 16:24:51 UTC
Thanks to everyone!

This strikes me as a problem that should be passed up to the python developers.
The reason being that if a more robust method of determining the user id exists
then the developers would be interested implementing it. It most certainly does
not work as expected. 

Please let me know if the gentoo developers want to escalate it to the python
folks, otherwise I'll do it myself. This feature was added to python2 to solve
portability issues, since the old way of doing it was:

python -c "import os; print os.getenv('USER')"

Which was not necessarily portable.

Comment 10 Robert Blyler 2002-07-21 12:31:11 UTC
Submitted to python bug tracking Request ID: 584566
Comment 11 Philippe Lafoucrière 2003-05-21 03:59:46 UTC
*** Bug 20037 has been marked as a duplicate of this bug. ***