diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 39fc975..1c232c9 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -552,10 +552,7 @@ class InteractiveShell(SingletonConfigurable, Magic): def init_pushd_popd_magic(self): # for pushd/popd management - try: - self.home_dir = get_home_dir() - except HomeDirError, msg: - fatal(msg) + self.home_dir = get_home_dir() self.dir_stack = [] @@ -1751,12 +1748,10 @@ class InteractiveShell(SingletonConfigurable, Magic): # Or if libedit is used, load editrc. inputrc_name = os.environ.get('INPUTRC') if inputrc_name is None: - home_dir = get_home_dir() - if home_dir is not None: - inputrc_name = '.inputrc' - if readline.uses_libedit: - inputrc_name = '.editrc' - inputrc_name = os.path.join(home_dir, inputrc_name) + inputrc_name = '.inputrc' + if readline.uses_libedit: + inputrc_name = '.editrc' + inputrc_name = os.path.join(self.home_dir, inputrc_name) if os.path.isfile(inputrc_name): try: readline.read_init_file(inputrc_name) diff --git a/IPython/utils/path.py b/IPython/utils/path.py index 5e224bb..cf59b9a 100644 --- a/IPython/utils/path.py +++ b/IPython/utils/path.py @@ -167,15 +167,24 @@ class HomeDirError(Exception): pass -def get_home_dir(): - """Return the closest possible equivalent to a 'home' directory. +def get_home_dir(require_writable=False): + """Return the 'home' directory, as a unicode string. * First, check for frozen env in case of py2exe - * Otherwise, defer to os.path.expanduser('~'), ensuring unicode + * Otherwise, defer to os.path.expanduser('~') See stdlib docs for how this is determined. - $HOME is first priority on *ALL* platforms. + + Parameters + ---------- + + require_writable : bool [default: False] + if True: + guarantees the return value is a writable directory, otherwise + raises HomeDirError + if False: + The path is resolved, but it is not guaranteed to exist or be writable. """ # first, check py2exe distribution root directory for _ipython. @@ -192,10 +201,10 @@ def get_home_dir(): return py3compat.cast_unicode(root, fs_encoding) homedir = os.path.expanduser('~') - if _writable_dir(homedir): + if (not require_writable) or _writable_dir(homedir): return py3compat.cast_unicode(homedir, fs_encoding) else: - raise HomeDirError('%s not a writable dir, set $HOME env to override' % homedir) + raise HomeDirError('%s is not a writable dir, set $HOME env to override' % homedir) def get_xdg_dir(): """Return the XDG_CONFIG_HOME, if it is defined and exists, else None. @@ -207,7 +216,7 @@ def get_xdg_dir(): if os.name == 'posix': # Linux, Unix, AIX, OS X - # use ~/.config if not set OR empty + # use ~/.config if empty OR not set xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') if xdg and _writable_dir(xdg): return py3compat.cast_unicode(xdg, fs_encoding) @@ -231,6 +240,7 @@ def get_ipython_dir(): home_dir = get_home_dir() xdg_dir = get_xdg_dir() + # import pdb; pdb.set_trace() # dbg ipdir = env.get('IPYTHON_DIR', env.get('IPYTHONDIR', None)) if ipdir is None: diff --git a/IPython/utils/tests/test_path.py b/IPython/utils/tests/test_path.py index e9429d2..93aa4dc 100644 --- a/IPython/utils/tests/test_path.py +++ b/IPython/utils/tests/test_path.py @@ -141,7 +141,7 @@ def test_get_home_dir_2(): #fake filename for IPython.__init__ IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() - home_dir = path.get_home_dir() + home_dir = path.get_home_dir(True) nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower()) @@ -149,7 +149,7 @@ def test_get_home_dir_2(): def test_get_home_dir_3(): """get_home_dir() uses $HOME if set""" env["HOME"] = HOME_TEST_DIR - home_dir = path.get_home_dir() + home_dir = path.get_home_dir(True) nt.assert_equal(home_dir, env["HOME"]) @@ -159,14 +159,14 @@ def test_get_home_dir_4(): if 'HOME' in env: del env['HOME'] # this should still succeed, but we don't know what the answer should be - home = path.get_home_dir() + home = path.get_home_dir(True) nt.assert_true(path._writable_dir(home)) @with_environment def test_get_home_dir_5(): """raise HomeDirError if $HOME is specified, but not a writable dir""" env['HOME'] = abspath(HOME_TEST_DIR+'garbage') - nt.assert_raises(path.HomeDirError, path.get_home_dir) + nt.assert_raises(path.HomeDirError, path.get_home_dir, True) @with_environment def test_get_ipython_dir_1():