From 3cc39b16751c20e1578c788cca82f3c6730ea0db Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sat, 25 Nov 2023 20:29:57 +0000 Subject: [PATCH 1/3] gh-112405: Optimise `pathlib.Path.relative_to` --- Lib/pathlib.py | 3 ++- .../Library/2023-11-25-20-29-28.gh-issue-112405.cOtzxC.rst | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2023-11-25-20-29-28.gh-issue-112405.cOtzxC.rst diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 0e01099d490a7e..81f75cd47ed087 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -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: @@ -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: 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 index 00000000000000..acdd957d599bc5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-11-25-20-29-28.gh-issue-112405.cOtzxC.rst @@ -0,0 +1 @@ +Optimise :meth:`pathlib.Path.relative_to`. Patch by Alex Waygood. From ba3bf0c2e9276ba6088a8df088dcfc550a25e633 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 26 Nov 2023 09:51:04 +0000 Subject: [PATCH 2/3] Concede to the Yanks Co-authored-by: Itamar Oren --- .../next/Library/2023-11-25-20-29-28.gh-issue-112405.cOtzxC.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index acdd957d599bc5..9a6bd6ff5911c0 100644 --- 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 @@ -1 +1 @@ -Optimise :meth:`pathlib.Path.relative_to`. Patch by Alex Waygood. +Optimize :meth:`pathlib.Path.relative_to`. Patch by Alex Waygood. From c57d5679494800912d66b13121341880b43c310b Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 26 Nov 2023 15:39:31 +0000 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2023-11-25-20-29-28.gh-issue-112405.cOtzxC.rst Co-authored-by: Barney Gale --- .../next/Library/2023-11-25-20-29-28.gh-issue-112405.cOtzxC.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 9a6bd6ff5911c0..f6f1bee2a0c38f 100644 --- 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 @@ -1 +1 @@ -Optimize :meth:`pathlib.Path.relative_to`. Patch by Alex Waygood. +Optimize :meth:`pathlib.PurePath.relative_to`. Patch by Alex Waygood.