Handle non-existant CWDs gracefully

This commit is contained in:
Martin Preisler 2013-02-19 21:11:28 +01:00 committed by Kim Silkebækken
parent e14f5e4208
commit ee51b8b76c

View File

@ -52,9 +52,17 @@ def cwd(dir_shorten_len=None, dir_limit_depth=None):
''' '''
import re import re
try: try:
cwd = os.getcwdu() try:
except AttributeError: cwd = os.getcwdu()
cwd = os.getcwd() except AttributeError:
cwd = os.getcwd()
except OSError as e:
if e.errno == 2:
# user most probably deleted the directory
# this happens when removing files from Mercurial repos for example
cwd = "[not found]"
else:
raise
home = os.environ.get('HOME') home = os.environ.get('HOME')
if home: if home:
cwd = re.sub('^' + re.escape(home), '~', cwd, 1) cwd = re.sub('^' + re.escape(home), '~', cwd, 1)