[New-bugs-announce] [issue32731] getpass.getuser() raises an unspecified exceptions (ImportError, OSError, etc)

Gregory P. Smith report at bugs.python.org
Wed Jan 31 13:02:32 EST 2018


New submission from Gregory P. Smith <greg at krypto.org>:

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.

----------
components: Library (Lib)
messages: 311349
nosy: gregory.p.smith
priority: normal
severity: normal
stage: needs patch
status: open
title: getpass.getuser() raises an unspecified exceptions (ImportError, OSError, etc)
type: behavior
versions: Python 2.7, Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32731>
_______________________________________


More information about the New-bugs-announce mailing list