Title: netrc does not work if $HOME is not set
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.7, Python 3.6, Python 3.5, Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Dimitri Merejkowsky, berker.peksag, haypo, terry.reedy, xiang.zhang
Priority: normal Keywords: patch

Created on 2016-10-01 15:35 by Dimitri Merejkowsky, last changed 2017-02-15 20:32 by python-dev.

File name Uploaded Description Edit
netrc-use-expanduser.patch Dimitri Merejkowsky, 2016-10-01 15:35 review
netrc-use-expanduser-with-test.patch Dimitri Merejkowsky, 2016-10-02 14:50 review
Pull Requests
URL Status Linked Edit
PR 123 open python-dev, 2017-02-15 20:32
Messages (7)
msg277824 - (view) Author: Dimitri Merejkowsky (Dimitri Merejkowsky) * Date: 2016-10-01 15:35
If $HOME is not set, netrc will raise an exception.

Attached patch fixes the problem by using `os.path.expanduser` instead
msg277847 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-10-02 04:00
Thanks for the patch, Dimitri. I think this is a reasonable improvement. However, since we are changing the behavior of the netrc() class, I'm not sure this can be considered as a bug fix.

In any case, 3.3 and 3.4 are in security-fix-only mode so I'm going to remove them from the versions field.

We need two things from you to move this forward:

1. A test. It should go in Lib/test/ You can use

       env = support.EnvironmentVarGuard()

   to test the new behavior.

2. A CLA form. You can sign it online at
msg277894 - (view) Author: Dimitri Merejkowsky (Dimitri Merejkowsky) * Date: 2016-10-02 14:50
> However, since we are changing the behavior of the netrc() class, I'm not sure this can be considered as a bug fix.

I was not sure either. The patch does change behavior in subtle ways...

I've added a new patch, and sent the CLA form.

Thanks for your time!
msg278276 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-10-07 22:07
On Windows, HOME is both split into two variables and replaced by USERPROFILE.
C:\Users\Terry>set HOME

C:\Users\Terry>set USERPROFILE

So if it make sense to run this on Windows*, I would call it a bug.
* I am guessing that Windows' FTP clients do not use .netrc.

The doc says "the file .netrc in the user’s home directory will be read." without qualification by "if $HOME is set".  I don't remember how os.path.expanduser might otherwise find the home directory on unix and don't know what unix users might reasonbly expect.
msg278438 - (view) Author: Dimitri Merejkowsky (Dimitri Merejkowsky) * Date: 2016-10-10 18:09
> if it make sense to run this on Windows

I found this issue while running cross-platform code. I needed to store some credentials, did not mind having them in plain-text and I thought .netrc was a good place for this. (did not need to re-invent the wheel ...)

>  don't know what unix users might reasonbly expect.

Well, I guess as a bonus of this patch we could add a link to the `os.path.expanduser` section[1] in `netrc` documentation.

The behavior when $HOME is not set is properly documented there.

msg278447 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-10-10 23:33
FWIW, some history: The $HOME lookup was in Guido's original 1998-12-22 patch:
        if not file:
            file = os.path.join(os.environ['HOME'], ".netrc")
The lookup was wrapped in try-except 4 years later to give a 'consistent error message'.

Module ntpath dates back to 1994.  Expanduser only used HOMEDRIVE and HOMEPATH.  Guido added support for HOME in 1997.  Support for USERPROFILE was added a decade later.

Since the module is NOT marked 'Unix-only', I think it a bug to not use os.path.expanduser.  Larry, Ned, do either of you have an opinion on whether the change should be made in current versions or only 3.7?
msg278932 - (view) Author: Dimitri Merejkowsky (Dimitri Merejkowsky) * Date: 2016-10-18 19:58
During review SilentGhost suggested that maybe a test was not essential.

In any case, I think patching documentation about the new behavior won't hurt. 

Do you want me to do that?
Date User Action Args
2017-02-15 20:32:48python-devsetpull_requests: + pull_request86
2016-11-28 05:08:39xiang.zhangsetnosy: + xiang.zhang
2016-10-18 21:26:53hayposetnosy: + haypo
2016-10-18 19:58:51Dimitri Merejkowskysetmessages: + msg278932
2016-10-10 23:33:19terry.reedysetmessages: + msg278447
2016-10-10 18:09:55Dimitri Merejkowskysetmessages: + msg278438
2016-10-07 22:07:01terry.reedysetnosy: + terry.reedy
messages: + msg278276
2016-10-02 14:50:53Dimitri Merejkowskysetfiles: + netrc-use-expanduser-with-test.patch

messages: + msg277894
2016-10-02 04:00:57berker.peksagsetversions: - Python 3.3, Python 3.4
nosy: + berker.peksag

messages: + msg277847

stage: patch review
2016-10-01 15:35:54Dimitri Merejkowskycreate