-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getpass.getuser() raises an unspecified exceptions (ImportError, OSError, etc) #76912
Comments
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. |
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. |
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. |
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. |
* bpo-32731: Raise OSError from any failure in getpass.getuser() Previously, if the username was not set in certain environment variables, ImportError escaped on Windows systems, and it was possible for KeyError to escape on other systems if getpwuid() failed.
thanks! |
…ython#29739) * bpo-32731: Raise OSError from any failure in getpass.getuser() Previously, if the username was not set in certain environment variables, ImportError escaped on Windows systems, and it was possible for KeyError to escape on other systems if getpwuid() failed.
…ython#29739) * bpo-32731: Raise OSError from any failure in getpass.getuser() Previously, if the username was not set in certain environment variables, ImportError escaped on Windows systems, and it was possible for KeyError to escape on other systems if getpwuid() failed.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: