8000 [3.12] gh-105002: [pathlib] Fix relative_to with walk_up=True using "… · python/cpython@4f6d7a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f6d7a5

Browse files
[3.12] gh-105002: [pathlib] Fix relative_to with walk_up=True using ".." (GH-107014) (#107315)
gh-105002: [pathlib] Fix relative_to with walk_up=True using ".." (GH-107014) It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown. (cherry picked from commit e7e6e4b) Co-authored-by: János Kukovecz <kukoveczjanos@gmail.com>
1 parent 58af565 commit 4f6d7a5

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Lib/pathlib.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,12 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
679679
for step, path in enumerate([other] + list(other.parents)):
680680
if self.is_relative_to(path):
681681
break
682+
elif not walk_up:
683+
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
684+
elif path.name == '..':
685+
raise ValueError(f"'..' segment in {str(other)!r} cannot be walked")
682686
else:
683687
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
684-
if step and not walk_up:
685-
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
686688
parts = ['..'] * step + self._tail[len(path._tail):]
687689
return self.with_segments(*parts)
688690

Lib/test/test_pathlib.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,14 @@ def test_relative_to_common(self):
626626
self.assertRaises(ValueError, p.relative_to, P('a/b/c'))
627627
self.assertRaises(ValueError, p.relative_to, P('a/c'))
628628
self.assertRaises(ValueError, p.relative_to, P('/a'))
629+
self.assertRaises(ValueError, p.relative_to, P("../a"))
630+
self.assertRaises(ValueError, p.relative_to, P("a/.."))
631+
self.assertRaises(ValueError, p.relative_to, P("/a/.."))
629632
self.assertRaises(ValueError, p.relative_to, P('/'), walk_up=True)
630633
self.assertRaises(ValueError, p.relative_to, P('/a'), walk_up=True)
634+
self.assertRaises(ValueError, p.relative_to, P("../a"), walk_up=True)
635+
self.assertRaises(ValueError, p.relative_to, P("a/.."), walk_up=True)
636+
self.assertRaises(ValueError, p.relative_to, P("/a/.."), walk_up=True)
631637
p = P('/a/b')
632638
self.assertEqual(p.relative_to(P('/')), P('a/b'))
633639
self.assertEqual(p.relative_to('/'), P('a/b'))
@@ -656,8 +662,14 @@ def test_relative_to_common(self):
656662
self.assertRaises(ValueError, p.relative_to, P())
657663
self.assertRaises(ValueError, p.relative_to, '')
658664
self.assertRaises(ValueError, p.relative_to, P('a'))
665+
self.assertRaises(ValueError, p.relative_to, P("../a"))
666+
self.assertRaises(ValueError, p.relative_to, P("a/.."))
667+
self.assertRaises(ValueError, p.relative_to, P("/a/.."))
659668
self.assertRaises(ValueError, p.relative_to, P(''), walk_up=True)
660669
self.assertRaises(ValueError, p.relative_to, P('a'), walk_up=True)
670+
self.assertRaises(ValueError, p.relative_to, P("../a"), walk_up=True)
671+
self.assertRaises(ValueError, p.relative_to, P("a/.."), walk_up=True)
672+
self.assertRaises(ValueError, p.relative_to, P("/a/.."), walk_up=True)
661673

662674
def test_is_relative_to_common(self):
663675
P = self.cls
Lines changed: 3 additions & 0 deletions
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)
0