summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pietri <antoine.pietri1@gmail.com>2017-06-07 17:18:56 (GMT)
committerSteve Dower <steve.dower@microsoft.com>2017-06-07 17:18:56 (GMT)
commitceabf9acf03f9bbe660d856bff90ecab475ab543 (patch)
treeed32702b01d5fa6247a70cb89e7e842157a3976d /Lib
parent09b6c0c71ea944f7e8b46998f3ebaf5b9fbe15f6 (diff)
downloadcpython-ceabf9acf03f9bbe660d856bff90ecab475ab543.zip
cpython-ceabf9acf03f9bbe660d856bff90ecab475ab543.tar.gz
cpython-ceabf9acf03f9bbe660d856bff90ecab475ab543.tar.bz2
bpo-30177: pathlib: include the full path in resolve(strict=False) (#1893) (#1985)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/pathlib.py19
-rw-r--r--Lib/test/test_pathlib.py21
2 files changed, 20 insertions, 20 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 70f5cba..48b566d 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -187,19 +187,18 @@ class _WindowsFlavour(_Flavour):
if strict:
return self._ext_to_normal(_getfinalpathname(s))
else:
+ tail_parts = [] # End of the path after the first one not found
while True:
try:
s = self._ext_to_normal(_getfinalpathname(s))
except FileNotFoundError:
previous_s = s
- s = os.path.dirname(s)
+ s, tail = os.path.split(s)
+ tail_parts.append(tail)
if previous_s == s:
return path
else:
- if previous_s is None:
- return s
- else:
- return s + os.path.sep + os.path.basename(previous_s)
+ return os.path.join(s, *reversed(tail_parts))
# Means fallback on absolute
return None
@@ -330,12 +329,10 @@ class _PosixFlavour(_Flavour):
try:
target = accessor.readlink(newpath)
except OSError as e:
- if e.errno != EINVAL:
- if strict:
- raise
- else:
- return newpath
- # Not a symlink
+ if e.errno != EINVAL and strict:
+ raise
+ # Not a symlink, or non-strict mode. We just leave the path
+ # untouched.
path = newpath
else:
seen[newpath] = None # not resolved symlink
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 846f721..6502413 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -1507,10 +1507,10 @@ class _BasePathTest(object):
os.path.join(BASE, 'foo'))
p = P(BASE, 'foo', 'in', 'spam')
self.assertEqual(str(p.resolve(strict=False)),
- os.path.join(BASE, 'foo'))
+ os.path.join(BASE, 'foo', 'in', 'spam'))
p = P(BASE, '..', 'foo', 'in', 'spam')
self.assertEqual(str(p.resolve(strict=False)),
- os.path.abspath(os.path.join('foo')))
+ os.path.abspath(os.path.join('foo', 'in', 'spam')))
# These are all relative symlinks
p = P(BASE, 'dirB', 'fileB')
self._check_resolve_relative(p, p)
@@ -1522,16 +1522,18 @@ class _BasePathTest(object):
self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB'))
# Non-strict
p = P(BASE, 'dirA', 'linkC', 'fileB', 'foo', 'in', 'spam')
- self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB', 'foo'), False)
+ self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB', 'foo', 'in',
+ 'spam'), False)
p = P(BASE, 'dirA', 'linkC', '..', 'foo', 'in', 'spam')
if os.name == 'nt':
# In Windows, if linkY points to dirB, 'dirA\linkY\..'
# resolves to 'dirA' without resolving linkY first.
- self._check_resolve_relative(p, P(BASE, 'dirA', 'foo'), False)
+ self._check_resolve_relative(p, P(BASE, 'dirA', 'foo', 'in',
+ 'spam'), False)
else:
# In Posix, if linkY points to dirB, 'dirA/linkY/..'
# resolves to 'dirB/..' first before resolving to parent of dirB.
- self._check_resolve_relative(p, P(BASE, 'foo'), False)
+ self._check_resolve_relative(p, P(BASE, 'foo', 'in', 'spam'), False)
# Now create absolute symlinks
d = tempfile.mkdtemp(suffix='-dirD')
self.addCleanup(support.rmtree, d)
@@ -1541,16 +1543,17 @@ class _BasePathTest(object):
self._check_resolve_absolute(p, P(BASE, 'dirB', 'fileB'))
# Non-strict
p = P(BASE, 'dirA', 'linkX', 'linkY', 'foo', 'in', 'spam')
- self._check_resolve_relative(p, P(BASE, 'dirB', 'foo'), False)
+ self._check_resolve_relative(p, P(BASE, 'dirB', 'foo', 'in', 'spam'),
+ False)
p = P(BASE, 'dirA', 'linkX', 'linkY', '..', 'foo', 'in', 'spam')
if os.name == 'nt':
# In Windows, if linkY points to dirB, 'dirA\linkY\..'
# resolves to 'dirA' without resolving linkY first.
- self._check_resolve_relative(p, P(d, 'foo'), False)
+ self._check_resolve_relative(p, P(d, 'foo', 'in', 'spam'), False)
else:
# In Posix, if linkY points to dirB, 'dirA/linkY/..'
# resolves to 'dirB/..' first before resolving to parent of dirB.
- self._check_resolve_relative(p, P(BASE, 'foo'), False)
+ self._check_resolve_relative(p, P(BASE, 'foo', 'in', 'spam'), False)
@with_symlinks
def test_resolve_dot(self):
@@ -1564,7 +1567,7 @@ class _BasePathTest(object):
r = q / '3' / '4'
self.assertRaises(FileNotFoundError, r.resolve, strict=True)
# Non-strict
- self.assertEqual(r.resolve(strict=False), p / '3')
+ self.assertEqual(r.resolve(strict=False), p / '3' / '4')
def test_with(self):
p = self.cls(BASE)