Skip to content
Myr

get_netrc_auth: handle os.path.expanduser failure

Before adoption: this commit came in with the history the project adopted, so no landing records it.

os.path.expanduser can raise a KeyError when $HOME is not set and the POSIX getpwuid() call fails, which can happen when running under a UID which is not in /etc/passwd or when the password file cannot be read. The upstream bug report http://bugs.python.org/issue20164 is unlikely to be backported to Python 2.x even if fixed so this change handles KeyError by skipping netrc authentication. Closes #1846

authored by Chris Adams · committed by Chris Adams · parent 99ebac7 · Browse files · 1 file+10 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+10 added−2 removed

  • other:+10 added−2 removed

1 file+10 added−2 removed

modified:requests/utils.py+10 added-2 removed
62 unmodified lines
63 if hasattr(o, 'getvalue'):64 # e.g. BytesIO, cStringIO.StringI65 return len(o.getvalue())666768def get_netrc_auth(url):69 """Returns the Requests tuple auth for a given url from netrc."""7071 try:72 from netrc import netrc, NetrcParseError7373 locations = (os.path.expanduser('~/{0}'.format(f)) for f in NETRC_FILES)74 netrc_path = None7576 for loc in locations:76 for f in NETRC_FILES:77 try:78 loc = os.path.expanduser('~/{0}'.format(f))79 except KeyError:80 # os.path.expanduser can fail when $HOME is undefined and81 # getpwuid fails. See http://bugs.python.org/issue20164 &82 # https://github.com/kennethreitz/requests/issues/184683 return8485 if os.path.exists(loc) and not netrc_path:86 netrc_path = loc8788 # Abort early if there isn't one.
563 unmodified lines