From 418d585febd280e779274f313f910613ac1a1c30 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 26 Nov 2023 15:56:03 +0000 Subject: gh-112405: Optimise `pathlib.Path.relative_to` (#112406) --- Lib/pathlib.py | 3 ++- .../NEWS.d/next/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 0e01099..81f75cd 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -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 index 0000000..f6f1bee --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-11-25-20-29-28.gh-issue-112405.cOtzxC.rst @@ -0,0 +1 @@ +Optimize :meth:`pathlib.PurePath.relative_to`. Patch by Alex Waygood. -- cgit v0.12