Skip to content

Commit 9eefdcd

Browse files
committed
FIX: Handle missing paths better pre-3.6
1 parent 2328beb commit 9eefdcd

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

nipype/utils/filemanip.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,30 @@ def __init__(self, path):
6060
USING_PATHLIB2 = True
6161

6262

63+
def _resolve_with_filenotfound(path, **kwargs):
64+
""" Raise FileNotFoundError instead of OSError """
65+
try:
66+
return path.resolve(**kwargs)
67+
except OSError as e:
68+
if isinstance(e, FileNotFoundError):
69+
raise
70+
raise FileNotFoundError(str(path))
71+
72+
6373
def path_resolve(path, strict=False):
6474
try:
65-
return path.resolve(strict=strict)
75+
return _resolve_with_filenotfound(path, strict=strict)
6676
except TypeError: # PY35
67-
resolved = path.resolve()
68-
if strict and not resolved.exists():
69-
raise FileNotFoundError(resolved)
70-
return resolved
77+
pass
78+
79+
path = path.absolute()
80+
if strict or path.exists():
81+
return _resolve_with_filenotfound(path)
82+
83+
# This is a hacky shortcut, using path.absolute() unmodified
84+
# In cases where the existing part of the path contains a
85+
# symlink, different results will be produced
86+
return path
7187

7288

7389
def path_mkdir(path, mode=0o777, parents=False, exist_ok=False):

nipype/utils/tests/test_filemanip.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,6 @@ def test_path_strict_resolve(tmpdir):
580580
# Default strict=False should work out out of the box
581581
testfile = Path('somefile.txt')
582582
resolved = '%s/somefile.txt' % tmpdir
583-
assert str(testfile.resolve()) == resolved
584-
# path_resolve() is equivalent to Path.resolve()
585583
assert str(path_resolve(testfile)) == resolved
586584
# Strict keyword is always allowed
587585
assert str(path_resolve(testfile, strict=False)) == resolved

0 commit comments

Comments
 (0)