]> granicus.if.org Git - python/commitdiff
netrc will now raise a more predictable exception when $HOME is not set
authorPaul Prescod <prescod@prescod.net>
Mon, 18 Mar 2002 02:13:48 +0000 (02:13 +0000)
committerPaul Prescod <prescod@prescod.net>
Mon, 18 Mar 2002 02:13:48 +0000 (02:13 +0000)
(as it is often not on Windows). The code was always designed so that it
would raise an IOError if there was no .netrc. But if there was no $HOME
it would return a KeyError which would be somewhat unexpected for code
that didn't know the algorithm it used to find .netrc. The particular
code that triggered this problem for me was ftpmirror.py which handled
the IOError gracefully, but not the KeyError.

Lib/netrc.py

index 3f1c7c3c4c73c19bcfa2b3f37e4c437704f36103..b78d4d83e5bd7c386409de9cfcdf6c8bbec1b426 100644 (file)
@@ -22,7 +22,10 @@ class NetrcParseError(Exception):
 class netrc:
     def __init__(self, file=None):
         if not file:
-            file = os.path.join(os.environ['HOME'], ".netrc")
+            try:
+                file = os.path.join(os.environ['HOME'], ".netrc")
+            except KeyError:
+                raise IOError("Could not find .netrc: $HOME is not set")
         fp = open(file)
         self.hosts = {}
         self.macros = {}