Skip to content

Commit

Permalink
pythongh-117686: Improve the performance of ntpath.expanduser() (pyth…
Browse files Browse the repository at this point in the history
…on#117690)

Refactor out _get_bothseps() call from the loop.
  • Loading branch information
nineteendo authored Apr 10, 2024
1 parent 0d42ac9 commit f90ff03
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Misc/NEWS.d/3.13.0a6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

..
Expand Down

0 comments on commit f90ff03

Please sign in to comment.