Skip to content
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

Closed
gpshead opened this issue Jan 31, 2018 · 5 comments · Fixed by #29739
Closed

getpass.getuser() raises an unspecified exceptions (ImportError, OSError, etc) #76912

gpshead opened this issue Jan 31, 2018 · 5 comments · Fixed by #29739
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@gpshead
Copy link
Member

gpshead commented Jan 31, 2018

BPO 32731
Nosy @gpshead, @matrixise, @jarondl, @fangyi-zhou, @jacobtylerwalls
PRs
  • bpo-32731: Specify Exception type of getpass.getuser #5616
  • gh-76912: Raise OSError from any failure in getpass.getuser() #29739
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2018-01-31.18:02:32.028>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'getpass.getuser() raises an unspecified exceptions (ImportError, OSError, etc)'
    updated_at = <Date 2021-11-24.03:33:25.025>
    user = 'https://github.com/gpshead'

    bugs.python.org fields:

    activity = <Date 2021-11-24.03:33:25.025>
    actor = 'jacobtylerwalls'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2018-01-31.18:02:32.028>
    creator = 'gregory.p.smith'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32731
    keywords = ['patch']
    message_count = 4.0
    messages = ['311349', '311358', '312079', '316587']
    nosy_count = 5.0
    nosy_names = ['gregory.p.smith', 'matrixise', 'jarondl', 'fangyizhou', 'jacobtylerwalls']
    pr_nums = ['5616', '29739']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32731'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @gpshead
    Copy link
    Member Author

    gpshead commented Jan 31, 2018

    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.

    @gpshead gpshead added 3.7 (EOL) end of life 3.8 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 31, 2018
    @jarondl
    Copy link
    Mannequin

    jarondl mannequin commented Jan 31, 2018

    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.

    @fangyi-zhou
    Copy link
    Mannequin

    fangyi-zhou mannequin commented Feb 12, 2018

    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.

    @matrixise
    Copy link
    Member

    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.

    @iritkatriel iritkatriel added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes and removed 3.7 (EOL) end of life 3.8 (EOL) end of life labels Nov 23, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @arhadthedev arhadthedev added 3.12 bugs and security fixes 3.13 bugs and security fixes and removed 3.10 only security fixes 3.9 only security fixes labels Nov 19, 2023
    gpshead pushed a commit that referenced this issue Nov 27, 2023
    * 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.
    @gpshead
    Copy link
    Member Author

    gpshead commented Nov 27, 2023

    thanks!

    @gpshead gpshead self-assigned this Nov 27, 2023
    aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
    …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.
    Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
    …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.
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    Successfully merging a pull request may close this issue.

    4 participants