Skip to content

Commit e7e6e4b

Browse files
authored
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.
1 parent 6d5b6e7 commit e7e6e4b

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Lib/pathlib.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,12 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
633633
for step, path in enumerate([other] + list(other.parents)):
634634
if self.is_relative_to(path):
635635
break
636+
elif not walk_up:
637+
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
638+
elif path.name == '..':
639+
raise ValueError(f"'..' segment in {str(other)!r} cannot be walked")
636640
else:
637641
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
638-
if step and not walk_up:
639-
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
640642
parts = ['..'] * step + self._tail[len(path._tail):]
641643
return self.with_segments(*parts)
642644

Lib/test/test_pathlib.py

+12
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,14 @@ def test_relative_to_common(self):
693693
self.assertRaises(ValueError, p.relative_to, P('a/b/c'))
694694
self.assertRaises(ValueError, p.relative_to, P('a/c'))
695695
self.assertRaises(ValueError, p.relative_to, P('/a'))
696+
self.assertRaises(ValueError, p.relative_to, P("../a"))
697+
self.assertRaises(ValueError, p.relative_to, P("a/.."))
698+
self.assertRaises(ValueError, p.relative_to, P("/a/.."))
696699
self.assertRaises(ValueError, p.relative_to, P('/'), walk_up=True)
697700
self.assertRaises(ValueError, p.relative_to, P('/a'), walk_up=True)
701+
self.assertRaises(ValueError, p.relative_to, P("../a"), walk_up=True)
702+
self.assertRaises(ValueError, p.relative_to, P("a/.."), walk_up=True)
703+
self.assertRaises(ValueError, p.relative_to, P("/a/.."), walk_up=True)
698704
p = P('/a/b')
699705
self.assertEqual(p.relative_to(P('/')), P('a/b'))
700706
self.assertEqual(p.relative_to('/'), P('a/b'))
@@ -723,8 +729,14 @@ def test_relative_to_common(self):
723729
self.assertRaises(ValueError, p.relative_to, P())
724730
self.assertRaises(ValueError, p.relative_to, '')
725731
self.assertRaises(ValueError, p.relative_to, P('a'))
732+
self.assertRaises(ValueError, p.relative_to, P("../a"))
733+
self.assertRaises(ValueError, p.relative_to, P("a/.."))
734+
self.assertRaises(ValueError, p.relative_to, P("/a/.."))
726735
self.assertRaises(ValueError, p.relative_to, P(''), walk_up=True)
727736
self.assertRaises(ValueError, p.relative_to, P('a'), walk_up=True)
737+
self.assertRaises(ValueError, p.relative_to, P("../a"), walk_up=True)
738+
self.assertRaises(ValueError, p.relative_to, P("a/.."), walk_up=True)
739+
self.assertRaises(ValueError, p.relative_to, P("/a/.."), walk_up=True)
728740

729741
def test_is_relative_to_common(self):
730742
P = self.cls
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix invalid result from :meth:`PurePath.relative_to` method when attempting to walk
2+
a "``..``" segment in *other* with *walk_up* enabled. A :exc:`ValueError` exception
3+
is now raised in this case.

0 commit comments

Comments
 (0)