From: Alex Waygood Date: Sun, 26 Nov 2023 15:56:03 +0000 (+0000) Subject: gh-112405: Optimise `pathlib.Path.relative_to` (#112406) X-Git-Tag: v3.13.0a3~638 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=418d585febd280e779274f313f910613ac1a1c30;p=thirdparty%2FPython%2Fcpython.git gh-112405: Optimise `pathlib.Path.relative_to` (#112406) --- diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 0e01099d490a..81f75cd47ed0 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 000000000000..f6f1bee2a0c3 --- /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.