os.getlogin() Error

Cameron Simpson cs at zip.com.au
Thu May 4 23:36:48 EDT 2017


On 04May2017 20:42, Wildman <best_lay at yahoo.com> wrote:
>On Fri, 05 May 2017 09:00:58 +1000, Cameron Simpson wrote:
>> On 04May2017 15:03, Wildman <best_lay at yahoo.com> wrote:
>>>The program installs using the Debian package system (.deb) and an
>>>entry is created in the Applications Menu.  The strange thing is
>>>that the crash only occurs when the program is run from the menu.
>>>If I open a terminal and run the program from there, the program
>>>runs fine.
>>
>> And this supports that.
>>
>> getlogin is not magic, and can be overused. The Python docs say "Return the
>> name of the user logged in on the controlling terminal of the process." Clearly
>> that will fail.
>>
>> When you start from a terminal, your command will have that as its controlling
>> terminal unless it has gone out of its way not to. When you start from a menu,
>> usually that menu system will not be associated with a terminal. In this case
>> you need to fall back on other methods of figuring out "who is logged in".
>
>What I don't understand is why the program will run from the menu
>on some Linux distros and not others.  I might need to take a
>closer look at the structure of the .desktop file used to launch
>my program.  Thanks.

I presume it will come down (in this instance) to the controlling tty. Put some 
testing code into something you can invoke from a menu. Try to open '/dev/tty' 
and see what error results. See if stdin, stdout and stderr are attached to 
terminals (or even open). You can os.fstat(sys.stdin.fileno()) and so forth.

And direct stderr to a logfile so you get to see any stack traces; you can 
write other info there, or dup stdout to the same logfile.

You can "tail -f" the logfile from a terminal to see your test results easily.

It may also depend on how X11 is started. On some distros you may have a GUI 
login, and your X11 session may have no controlling terminal.

On my systems I run text consoles and use "startx" to kick things off. I'd 
expect my window manager etc to have a controlling tty, the console. Unless 
something lets go of it deliberately before kicking off my desktop.

So you're right, different distros and setups may well present somewhat 
different inherited environments to whatever presents your menus.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list