Skip to content

Commit d923e8d

Browse files
committed
Use expanduser() to find netrc file
Avoids an exception when $HOME is not set. This behavior matches the behavior in Python 3.7 See python/cpython#4537 close #2
1 parent 23b5cbb commit d923e8d

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
1.2.0 (unreleased)
5+
------------------
6+
7+
* Don't error if ``$HOME`` is not set `#2 <https://github.com/sloria/tinynetrc/issues/2>`_.
8+
49
1.1.0 (2017-11-04)
510
------------------
611

File renamed without changes.

tests/test_tinynetrc.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,22 @@
77

88
@pytest.fixture()
99
def netrc():
10-
return tinynetrc.Netrc(os.path.join(HERE, 'netrc_valid'))
10+
return tinynetrc.Netrc(os.path.join(HERE, '.netrc'))
11+
12+
13+
def test_file_not_found():
14+
with pytest.raises(IOError):
15+
tinynetrc.Netrc('notfound')
16+
17+
18+
def test_home_unset(monkeypatch):
19+
# Make "~" == the current directory
20+
monkeypatch.setattr(os.path, 'expanduser', lambda path: HERE)
21+
# Unset $HOME
22+
monkeypatch.delenv('HOME', raising=False)
23+
# No error
24+
result = tinynetrc.Netrc()
25+
assert 'mail.google.com' in result.hosts
1126

1227

1328
def test_hosts(netrc):

tinynetrc.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ class Netrc(MutableMapping):
3434

3535
def __init__(self, file=None):
3636
if file is None:
37-
try:
38-
file = os.path.join(os.environ['HOME'], ".netrc")
39-
except KeyError:
40-
raise OSError("Could not find .netrc: $HOME is not set")
37+
file = os.path.join(os.path.expanduser('~'), '.netrc')
4138
self.file = file
4239
self._netrc = netrc.netrc(file)
4340
self.machines = dictify_hosts(self._netrc.hosts)

0 commit comments

Comments
 (0)