gh-117686: Improve the performance of ntpath.expanduser() (#117690)

Refactor out _get_bothseps() call from the loop.
This commit is contained in:
Nice Zombies 2024-04-10 10:28:48 +02:00 committed by GitHub
parent 0d42ac9474
commit f90ff03672
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 2 deletions

View File

@ -368,13 +368,15 @@ def expanduser(path):
If user or $HOME is unknown, do nothing.""" If user or $HOME is unknown, do nothing."""
path = os.fspath(path) path = os.fspath(path)
if isinstance(path, bytes): if isinstance(path, bytes):
seps = b'\\/'
tilde = b'~' tilde = b'~'
else: else:
seps = '\\/'
tilde = '~' tilde = '~'
if not path.startswith(tilde): if not path.startswith(tilde):
return path return path
i, n = 1, len(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 i += 1
if 'USERPROFILE' in os.environ: if 'USERPROFILE' in os.environ:

View File

@ -4,7 +4,7 @@
.. release date: 2024-04-09 .. release date: 2024-04-09
.. section: Core and Builtins .. section: Core and Builtins
Improve performance of :func:`os.path.join`. Improve performance of :func:`os.path.join` and :func:`os.path.expanduser`.
.. ..