Skip to content

Commit

Permalink
Small tweaks to relative_to()
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Oct 28, 2023
1 parent 6e93540 commit 4dfbfac
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,15 +652,12 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
other = self.with_segments(other)
for step, path in enumerate([other] + list(other.parents)):
if path == self or path in self.parents:
break
return self.with_segments(*['..'] * step, *self._tail[len(path._tail):])
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:
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
parts = ['..'] * step + self._tail[len(path._tail):]
return self.with_segments(*parts)
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")

def is_relative_to(self, other, /, *_deprecated):
"""Return True if the path is relative to another path or False.
Expand Down

0 comments on commit 4dfbfac

Please sign in to comment.