From b98a404c739d9434cab6a948239710c4c3758b6c Mon Sep 17 00:00:00 2001 From: nineteendo Date: Mon, 8 Apr 2024 17:33:32 +0200 Subject: [PATCH 1/7] Use set comprehension for `posixpath.commonpath()` --- Lib/posixpath.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index b7fbdff20cac99..16b8d9091c5a84 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -558,10 +558,8 @@ def commonpath(paths): try: split_paths = [path.split(sep) for path in paths] - try: - isabs, = set(p[:1] == sep for p in paths) - except ValueError: - raise ValueError("Can't mix absolute and relative paths") from None + if len({p.startswith(sep) for p in paths}) != 1: + raise ValueError("Can't mix absolute and relative paths") split_paths = [[c for c in s if c and c != curdir] for s in split_paths] s1 = min(split_paths) @@ -572,7 +570,7 @@ def commonpath(paths): common = s1[:i] break - prefix = sep if isabs else sep[:0] + prefix = sep if paths[0].startswith(sep) else sep[:0] return prefix + sep.join(common) except (TypeError, AttributeError): genericpath._check_arg_types('commonpath', *paths) From a9a716129bd764018364a812221a02c9f7e8a1f0 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 8 Apr 2024 19:30:39 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst new file mode 100644 index 00000000000000..9e8cf0d6f6aa6f --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst @@ -0,0 +1 @@ +Speedup :func:`os.path.commonpath()` by up to 9% on Unix. From a9bf9b6d3b851de1da3f46800dc2906a72f5300f Mon Sep 17 00:00:00 2001 From: Nineteendo Date: Tue, 9 Apr 2024 08:02:50 +0200 Subject: [PATCH 3/7] replace startswith with slice comparison --- Lib/posixpath.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 16b8d9091c5a84..000fe70e41807a 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -558,7 +558,7 @@ def commonpath(paths): try: split_paths = [path.split(sep) for path in paths] - if len({p.startswith(sep) for p in paths}) != 1: + if len({p[:1] == sep for p in paths}) != 1: raise ValueError("Can't mix absolute and relative paths") split_paths = [[c for c in s if c and c != curdir] for s in split_paths] @@ -570,7 +570,7 @@ def commonpath(paths): common = s1[:i] break - prefix = sep if paths[0].startswith(sep) else sep[:0] + prefix = sep if paths[0][:1] == sep else sep[:0] return prefix + sep.join(common) except (TypeError, AttributeError): genericpath._check_arg_types('commonpath', *paths) From 4597fbf8113711d6c28282207a3f185d9d34d568 Mon Sep 17 00:00:00 2001 From: nineteendo Date: Tue, 9 Apr 2024 08:34:36 +0200 Subject: [PATCH 4/7] Revert "replace startswith with slice comparison" This reverts commit a9bf9b6d3b851de1da3f46800dc2906a72f5300f. --- Lib/posixpath.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 000fe70e41807a..16b8d9091c5a84 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -558,7 +558,7 @@ def commonpath(paths): try: split_paths = [path.split(sep) for path in paths] - if len({p[:1] == sep for p in paths}) != 1: + if len({p.startswith(sep) for p in paths}) != 1: raise ValueError("Can't mix absolute and relative paths") split_paths = [[c for c in s if c and c != curdir] for s in split_paths] @@ -570,7 +570,7 @@ def commonpath(paths): common = s1[:i] break - prefix = sep if paths[0][:1] == sep else sep[:0] + prefix = sep if paths[0].startswith(sep) else sep[:0] return prefix + sep.join(common) except (TypeError, AttributeError): genericpath._check_arg_types('commonpath', *paths) From d98a69a54877e9301cfdac23034de20080c55bca Mon Sep 17 00:00:00 2001 From: Nice Zombies Date: Tue, 9 Apr 2024 21:57:42 +0200 Subject: [PATCH 5/7] Update 2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst --- .../2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst index 9e8cf0d6f6aa6f..c5a087f9f1566d 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst @@ -1 +1 @@ -Speedup :func:`os.path.commonpath()` by up to 9% on Unix. +Speedup :func:`os.path.commonpath()` on Unix. From 1f12063747b6d97b941617ac3b7e79f044bcb63a Mon Sep 17 00:00:00 2001 From: Nice Zombies Date: Thu, 11 Apr 2024 11:36:55 +0200 Subject: [PATCH 6/7] Update 2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst --- .../2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst index c5a087f9f1566d..e313c133b72173 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-08-19-30-38.gh-issue-117641.oaBGSJ.rst @@ -1 +1 @@ -Speedup :func:`os.path.commonpath()` on Unix. +Speedup :func:`os.path.commonpath` on Unix. From 98674d592b8bd762178e532f7547aa1e4c5dae79 Mon Sep 17 00:00:00 2001 From: Nineteendo Date: Tue, 16 Apr 2024 09:25:28 +0200 Subject: [PATCH 7/7] Revert unpacking --- Lib/posixpath.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 16b8d9091c5a84..91c0797d41a5df 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -558,8 +558,10 @@ def commonpath(paths): try: split_paths = [path.split(sep) for path in paths] - if len({p.startswith(sep) for p in paths}) != 1: - raise ValueError("Can't mix absolute and relative paths") + try: + isabs, = {p.startswith(sep) for p in paths} + except ValueError: + raise ValueError("Can't mix absolute and relative paths") from None split_paths = [[c for c in s if c and c != curdir] for s in split_paths] s1 = min(split_paths) @@ -570,7 +572,7 @@ def commonpath(paths): common = s1[:i] break - prefix = sep if paths[0].startswith(sep) else sep[:0] + prefix = sep if isabs else sep[:0] return prefix + sep.join(common) except (TypeError, AttributeError): genericpath._check_arg_types('commonpath', *paths)