Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-112405: Optimise pathlib.Path.relative_to
  • Loading branch information
AlexWaygood committed Nov 25, 2023
commit 3cc39b16751c20e1578c788cca82f3c6730ea0db
3 changes: 2 additions & 1 deletion Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import warnings
from _collections_abc import Sequence
from errno import ENOENT, ENOTDIR, EBADF, ELOOP, EINVAL
from itertools import chain
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO

try:
Expand Down Expand Up @@ -445,7 +446,7 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
other = self.with_segments(other, *_deprecated)
elif not isinstance(other, PurePath):
other = self.with_segments(other)
for step, path in enumerate([other] + list(other.parents)):
for step, path in enumerate(chain([other], other.parents)):
if path == self or path in self.parents:
break
elif not walk_up:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimise :meth:`pathlib.Path.relative_to`. Patch by Alex Waygood.
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated