]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112405: Optimise `pathlib.Path.relative_to` (#112406)
authorAlex Waygood <Alex.Waygood@Gmail.com>
Sun, 26 Nov 2023 15:56:03 +0000 (15:56 +0000)
committerGitHub <noreply@github.com>
Sun, 26 Nov 2023 15:56:03 +0000 (15:56 +0000)
Lib/pathlib.py
Misc/NEWS.d/next/Library/2023-11-25-20-29-28.gh-issue-112405.cOtzxC.rst [new file with mode: 0644]

index 0e01099d490a7e3a271dbe58ba27cd9c17d681c9..81f75cd47ed08708ab29282863178eb3a076d6e0 100644 (file)
@@ -14,6 +14,7 @@ import sys
 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:
@@ -445,7 +446,7 @@ class PurePath:
             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:
diff --git a/Misc/NEWS.d/next/Library/2023-11-25-20-29-28.gh-issue-112405.cOtzxC.rst b/Misc/NEWS.d/next/Library/2023-11-25-20-29-28.gh-issue-112405.cOtzxC.rst
new file mode 100644 (file)
index 0000000..f6f1bee
--- /dev/null
@@ -0,0 +1 @@
+Optimize :meth:`pathlib.PurePath.relative_to`. Patch by Alex Waygood.