Mailing List Archive

gh-117686: Improve the performance of ntpath.expanduser() (#117690)
https://github.com/python/cpython/commit/f90ff0367271ea474b4ce3c8e2643cb51d188c18
commit: f90ff0367271ea474b4ce3c8e2643cb51d188c18
branch: main
author: Nice Zombies <nineteendo19d0@gmail.com>
committer: erlend-aasland <erlend.aasland@protonmail.com>
date: 2024-04-10T10:28:48+02:00
summary:

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

Refactor out _get_bothseps() call from the loop.

files:
M Lib/ntpath.py
M Misc/NEWS.d/3.13.0a6.rst

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:
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`.

..


_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-leave@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: list-python-checkins@lists.gossamer-threads.com