From 285bfa37da08bb0a1a572b672edb6a6f199aef04 Mon Sep 17 00:00:00 2001 From: nineteendo Date: Tue, 9 Apr 2024 18:26:41 +0200 Subject: [PATCH 1/5] Move `_get_bothseps()` outside loop condition --- Lib/ntpath.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/ntpath.py b/Lib/ntpath.py index da5231ff2c0931..f5d1a2195dd633 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -368,13 +368,15 @@ def expanduser(path): If user or $HOME is unknown, do nothing.""" path = os.fspath(path) if isinstance(path, bytes): + seps = b'\\/' tilde = b'~' else: + seps = '\\/' tilde = '~' if not path.startswith(tilde): return path i, n = 1, len(path) - while i < n and path[i] not in _get_bothseps(path): + while i < n and path[i] not in seps: i += 1 if 'USERPROFILE' in os.environ: From 22b450626c161de41cb3a3e6b3606af7b8a6f19e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 9 Apr 2024 18:13:08 +0000 Subject: [PATCH 2/5] =?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-09-18-13-08.gh-issue-117686.4oIvxF.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-04-09-18-13-08.gh-issue-117686.4oIvxF.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-09-18-13-08.gh-issue-117686.4oIvxF.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-09-18-13-08.gh-issue-117686.4oIvxF.rst new file mode 100644 index 00000000000000..af6ca366a6976c --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-09-18-13-08.gh-issue-117686.4oIvxF.rst @@ -0,0 +1 @@ +Speedup :func:`os.path.expanduser()` on Windows for usernames. From e76e9755f77c3024fc43e32dd71b8e8c6ba622c1 Mon Sep 17 00:00:00 2001 From: nineteendo Date: Tue, 9 Apr 2024 23:06:49 +0200 Subject: [PATCH 3/5] Rewrite loop --- Lib/ntpath.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Lib/ntpath.py b/Lib/ntpath.py index f5d1a2195dd633..72fbf13cd9d577 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -375,8 +375,11 @@ def expanduser(path): tilde = '~' if not path.startswith(tilde): return path - i, n = 1, len(path) - while i < n and path[i] not in seps: + i = 0 + for i, char in enumerate(path[1:], start=1): + if char in seps: + break + else: i += 1 if 'USERPROFILE' in os.environ: From 3f826ca460d06871516e8077d88acd86380beb07 Mon Sep 17 00:00:00 2001 From: nineteendo Date: Tue, 9 Apr 2024 23:28:54 +0200 Subject: [PATCH 4/5] Revert "Rewrite loop" This reverts commit e76e9755f77c3024fc43e32dd71b8e8c6ba622c1. --- Lib/ntpath.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 72fbf13cd9d577..f5d1a2195dd633 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -375,11 +375,8 @@ def expanduser(path): tilde = '~' if not path.startswith(tilde): return path - i = 0 - for i, char in enumerate(path[1:], start=1): - if char in seps: - break - else: + i, n = 1, len(path) + while i < n and path[i] not in seps: i += 1 if 'USERPROFILE' in os.environ: From 8ad5863c400eb38ab1e8853a449e5e93a4597a5f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 9 Apr 2024 23:35:01 +0200 Subject: [PATCH 5/5] Fixup NEWS entry --- Misc/NEWS.d/3.13.0a6.rst | 2 +- .../2024-04-09-18-13-08.gh-issue-117686.4oIvxF.rst | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-04-09-18-13-08.gh-issue-117686.4oIvxF.rst diff --git a/Misc/NEWS.d/3.13.0a6.rst b/Misc/NEWS.d/3.13.0a6.rst index 52735dba3578b5..06807b396ed5da 100644 --- a/Misc/NEWS.d/3.13.0a6.rst +++ b/Misc/NEWS.d/3.13.0a6.rst @@ -4,7 +4,7 @@ .. release date: 2024-04-09 .. section: Core and Builtins -Improve performance of :func:`os.path.join`. +Improve performance of :func:`os.path.join` and :func:`os.path.expanduser`. .. diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-09-18-13-08.gh-issue-117686.4oIvxF.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-09-18-13-08.gh-issue-117686.4oIvxF.rst deleted file mode 100644 index af6ca366a6976c..00000000000000 --- a/Misc/NEWS.d/next/Core and Builtins/2024-04-09-18-13-08.gh-issue-117686.4oIvxF.rst +++ /dev/null @@ -1 +0,0 @@ -Speedup :func:`os.path.expanduser()` on Windows for usernames.