summaryrefslogtreecommitdiffstats
path: root/Lib/posixpath.py
diff options
context:
space:
mode:
authorNice Zombies <nineteendo19d0@gmail.com>2024-05-01 21:44:55 (GMT)
committerGitHub <noreply@github.com>2024-05-01 21:44:55 (GMT)
commita7711a2a4e5cf16b34fc284085da724a8c2c06dd (patch)
tree218d2901538003aa825a74e289902df30da23306 /Lib/posixpath.py
parent424438b11ec90110054f720bfa6ea67d644cc2ec (diff)
downloadcpython-a7711a2a4e5cf16b34fc284085da724a8c2c06dd.zip
cpython-a7711a2a4e5cf16b34fc284085da724a8c2c06dd.tar.gz
cpython-a7711a2a4e5cf16b34fc284085da724a8c2c06dd.tar.bz2
gh-117607: Speedup os.path.relpath() (GH-117608)
Diffstat (limited to 'Lib/posixpath.py')
-rw-r--r--Lib/posixpath.py8
1 files changed, 5 insertions, 3 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index 56b7915..f189c33 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -532,15 +532,17 @@ def relpath(path, start=None):
start = os.fspath(start)
try:
- start_list = [x for x in abspath(start).split(sep) if x]
- path_list = [x for x in abspath(path).split(sep) if x]
+ start_tail = abspath(start).lstrip(sep)
+ path_tail = abspath(path).lstrip(sep)
+ start_list = start_tail.split(sep) if start_tail else []
+ path_list = path_tail.split(sep) if path_tail else []
# Work out how much of the filepath is shared by start and path.
i = len(commonprefix([start_list, path_list]))
rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
if not rel_list:
return curdir
- return join(*rel_list)
+ return sep.join(rel_list)
except (TypeError, AttributeError, BytesWarning, DeprecationWarning):
genericpath._check_arg_types('relpath', path, start)
raise