Skip to content

Commit

Permalink
Make last-ditch attempt to find $HOME when environment is broken.
Browse files Browse the repository at this point in the history
Backport of master 5a7eee9, thanks to gh user dmirota for report
and patch.
  • Loading branch information
fperez committed Mar 13, 2011
1 parent 0f1d8a1 commit 627f9d3
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions IPython/genutils.py
Expand Up @@ -936,10 +936,20 @@ def get_home_dir():
# in case a user stuck some string which does NOT resolve to a
# valid path, it's as good as if we hadn't foud it
raise KeyError
return homedir
return homedir.decode(sys.getfilesystemencoding())
except KeyError:
if os.name == 'posix':
raise HomeDirError,'undefined $HOME, IPython can not proceed.'
# Last-ditch attempt at finding a suitable $HOME, on systems where
# it may not be defined in the environment but the system shell
# still knows it - reported once as:
# https://github.com/ipython/ipython/issues/154
from subprocess import Popen, PIPE
homedir = Popen('echo $HOME', shell=True,
stdout=PIPE).communicate()[0].strip()
if homedir:
return homedir.decode(sys.getfilesystemencoding())
else:
raise HomeDirError('Undefined $HOME, IPython cannot proceed.')
elif os.name == 'nt':
# For some strange reason, win9x returns 'nt' for os.name.
try:
Expand All @@ -948,7 +958,7 @@ def get_home_dir():
homedir = os.path.join(env['USERPROFILE'])
if not isdir(homedir):
raise HomeDirError
return homedir
return homedir.decode(sys.getfilesystemencoding())
except KeyError:
try:
# Use the registry to get the 'My Documents' folder.
Expand All @@ -964,7 +974,7 @@ def get_home_dir():
'This is not a valid directory on your system.' %
homedir)
raise HomeDirError(e)
return homedir
return homedir.decode(sys.getfilesystemencoding())
except HomeDirError:
raise
except:
Expand Down

0 comments on commit 627f9d3

Please sign in to comment.