mirror of
https://github.com/python/cpython.git
synced 2025-09-27 18:59:43 +00:00
gh-105002: [pathlib] Fix relative_to with walk_up=True using ".." (#107014)
It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown.
This commit is contained in:
parent
6d5b6e71c8
commit
e7e6e4b035
3 changed files with 19 additions and 2 deletions
|
@ -633,10 +633,12 @@ class PurePath:
|
||||||
for step, path in enumerate([other] + list(other.parents)):
|
for step, path in enumerate([other] + list(other.parents)):
|
||||||
if self.is_relative_to(path):
|
if self.is_relative_to(path):
|
||||||
break
|
break
|
||||||
|
elif not walk_up:
|
||||||
|
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
|
||||||
|
elif path.name == '..':
|
||||||
|
raise ValueError(f"'..' segment in {str(other)!r} cannot be walked")
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
|
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
|
||||||
if step and not walk_up:
|
|
||||||
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
|
|
||||||
parts = ['..'] * step + self._tail[len(path._tail):]
|
parts = ['..'] * step + self._tail[len(path._tail):]
|
||||||
return self.with_segments(*parts)
|
return self.with_segments(*parts)
|
||||||
|
|
||||||
|
|
|
@ -693,8 +693,14 @@ class PurePathTest(unittest.TestCase):
|
||||||
self.assertRaises(ValueError, p.relative_to, P('a/b/c'))
|
self.assertRaises(ValueError, p.relative_to, P('a/b/c'))
|
||||||
self.assertRaises(ValueError, p.relative_to, P('a/c'))
|
self.assertRaises(ValueError, p.relative_to, P('a/c'))
|
||||||
self.assertRaises(ValueError, p.relative_to, P('/a'))
|
self.assertRaises(ValueError, p.relative_to, P('/a'))
|
||||||
|
self.assertRaises(ValueError, p.relative_to, P("../a"))
|
||||||
|
self.assertRaises(ValueError, p.relative_to, P("a/.."))
|
||||||
|
self.assertRaises(ValueError, p.relative_to, P("/a/.."))
|
||||||
self.assertRaises(ValueError, p.relative_to, P('/'), walk_up=True)
|
self.assertRaises(ValueError, p.relative_to, P('/'), walk_up=True)
|
||||||
self.assertRaises(ValueError, p.relative_to, P('/a'), walk_up=True)
|
self.assertRaises(ValueError, p.relative_to, P('/a'), walk_up=True)
|
||||||
|
self.assertRaises(ValueError, p.relative_to, P("../a"), walk_up=True)
|
||||||
|
self.assertRaises(ValueError, p.relative_to, P("a/.."), walk_up=True)
|
||||||
|
self.assertRaises(ValueError, p.relative_to, P("/a/.."), walk_up=True)
|
||||||
p = P('/a/b')
|
p = P('/a/b')
|
||||||
self.assertEqual(p.relative_to(P('/')), P('a/b'))
|
self.assertEqual(p.relative_to(P('/')), P('a/b'))
|
||||||
self.assertEqual(p.relative_to('/'), P('a/b'))
|
self.assertEqual(p.relative_to('/'), P('a/b'))
|
||||||
|
@ -723,8 +729,14 @@ class PurePathTest(unittest.TestCase):
|
||||||
self.assertRaises(ValueError, p.relative_to, P())
|
self.assertRaises(ValueError, p.relative_to, P())
|
||||||
self.assertRaises(ValueError, p.relative_to, '')
|
self.assertRaises(ValueError, p.relative_to, '')
|
||||||
self.assertRaises(ValueError, p.relative_to, P('a'))
|
self.assertRaises(ValueError, p.relative_to, P('a'))
|
||||||
|
self.assertRaises(ValueError, p.relative_to, P("../a"))
|
||||||
|
self.assertRaises(ValueError, p.relative_to, P("a/.."))
|
||||||
|
self.assertRaises(ValueError, p.relative_to, P("/a/.."))
|
||||||
self.assertRaises(ValueError, p.relative_to, P(''), walk_up=True)
|
self.assertRaises(ValueError, p.relative_to, P(''), walk_up=True)
|
||||||
self.assertRaises(ValueError, p.relative_to, P('a'), walk_up=True)
|
self.assertRaises(ValueError, p.relative_to, P('a'), walk_up=True)
|
||||||
|
self.assertRaises(ValueError, p.relative_to, P("../a"), walk_up=True)
|
||||||
|
self.assertRaises(ValueError, p.relative_to, P("a/.."), walk_up=True)
|
||||||
|
self.assertRaises(ValueError, p.relative_to, P("/a/.."), walk_up=True)
|
||||||
|
|
||||||
def test_is_relative_to_common(self):
|
def test_is_relative_to_common(self):
|
||||||
P = self.cls
|
P = self.cls
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Fix invalid result from :meth:`PurePath.relative_to` method when attempting to walk
|
||||||
|
a "``..``" segment in *other* with *walk_up* enabled. A :exc:`ValueError` exception
|
||||||
|
is now raised in this case.
|
Loading…
Add table
Add a link
Reference in a new issue