Skip to content

Commit 418d585

Browse files
authored
gh-112405: Optimise pathlib.Path.relative_to (#112406)
1 parent 9fe6034 commit 418d585

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

Lib/pathlib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import warnings
1515
from _collections_abc import Sequence
1616
from errno import ENOENT, ENOTDIR, EBADF, ELOOP, EINVAL
17+
from itertools import chain
1718
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
1819

1920
try:
@@ -445,7 +446,7 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
445446
other = self.with_segments(other, *_deprecated)
446447
elif not isinstance(other, PurePath):
447448
other = self.with_segments(other)
448-
for step, path in enumerate([other] + list(other.parents)):
449+
for step, path in enumerate(chain([other], other.parents)):
449450
if path == self or path in self.parents:
450451
break
451452
elif not walk_up:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimize :meth:`pathlib.PurePath.relative_to`. Patch by Alex Waygood.

0 commit comments

Comments
 (0)