Skip to content

Commit

Permalink
pythongh-105002: [pathlib] Fix relative_to with walk_up=True using ".."
Browse files Browse the repository at this point in the history
  • Loading branch information
kukovecz committed Jul 22, 2023
1 parent 0ba07b2 commit ee2700e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,8 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
for step, path in enumerate([other] + list(other.parents)):
if self.is_relative_to(path):
break
elif path.name == '..':
raise ValueError(f"'..' segment in {str(other)!r} cannot be walked")
else:
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
if step and not walk_up:
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,12 @@ def test_relative_to_common(self):
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'))
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('/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')
self.assertEqual(p.relative_to(P('/')), P('a/b'))
self.assertEqual(p.relative_to('/'), P('a/b'))
Expand Down Expand Up @@ -723,8 +727,12 @@ def test_relative_to_common(self):
self.assertRaises(ValueError, p.relative_to, P())
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(''), 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):
P = self.cls
Expand Down

0 comments on commit ee2700e

Please sign in to comment.