classification
Title: getpass.getuser() raises an unspecified exceptions (ImportError, OSError, etc)
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: fangyizhou, gregory.p.smith, jarondl, matrixise
Priority: normal Keywords: patch

Created on 2018-01-31 18:02 by gregory.p.smith, last changed 2018-05-14 21:52 by matrixise.

Pull Requests
URL Status Linked Edit
PR 5616 open fangyizhou, 2018-02-11 03:26
Messages (4)
msg311349 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2018-01-31 18:02
This has been true since Python 1.x.  getpass.getuser() is documented as "raising an exception" but doesn't specify what exception.

On Windows when the environment variables it looks at are not set, an ImportError (!) escapes due to "import pwd" failing.  It could just a easily allow an exception from the following pwd.getpwuid(os.getuid())[0] call to escape on systems when pwd exists.

None of these are actually helpful to the caller as they are left having to catch a bare Exception around a getpass.getuser() call to take another course of action.  I filed this as I just saw someone suggesting code that specifically catches ImportError on getpass.getuser().

It is too late to change the exception type coming out in 2.7 or 3.6, but cleaning up this underspecified behavior to be a meaningful exception for 3.7 seems reasonable.
msg311358 - (view) Author: Yaron de Leeuw (jarondl) * Date: 2018-01-31 18:50
What kind of exception did you have in mind? As the 'someone' you mentioned, I can contribute this.

btw, os.getuid() on windows raises an attribute error, so we need to check that too.
msg312079 - (view) Author: Fangyi Zhou (fangyizhou) * Date: 2018-02-12 20:56
Hi. I've updated the docs.

I'm wondering if the code needs to be patched as well. Essentially we should raise a unified Exception for unable to get the username I suppose?

Please let me know if you have any comments/suggestions on wording. Thanks.
msg316587 - (view) Author: Stéphane Wirtel (matrixise) * (Python triager) Date: 2018-05-14 21:52
Hi,

For the wording, I have no suggestions but seems to be fine.

For the rest, I think you have modified the documentation and it's fine and we don't talk about the code.

We could merge the PR for the documentation and maybe you could create a new PR for the code.
History
Date User Action Args
2018-05-14 21:52:23matrixisesetnosy: + matrixise
messages: + msg316587
2018-02-12 20:56:22fangyizhousetnosy: + fangyizhou
messages: + msg312079
2018-02-11 03:26:17fangyizhousetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request5426
2018-01-31 18:50:46jarondlsetmessages: + msg311358
2018-01-31 18:11:11jarondlsetnosy: + jarondl
2018-01-31 18:02:32gregory.p.smithcreate