summaryrefslogtreecommitdiffstats
path: root/Lib/ntpath.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/ntpath.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/ntpath.py')
-rw-r--r--Lib/ntpath.py19
1 files changed, 10 insertions, 9 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index e810b65..b833e0b 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -805,6 +805,9 @@ supports_unicode_filenames = True
def relpath(path, start=None):
"""Return a relative version of a path"""
path = os.fspath(path)
+ if not path:
+ raise ValueError("no path specified")
+
if isinstance(path, bytes):
sep = b'\\'
curdir = b'.'
@@ -816,22 +819,20 @@ def relpath(path, start=None):
if start is None:
start = curdir
+ else:
+ start = os.fspath(start)
- if not path:
- raise ValueError("no path specified")
-
- start = os.fspath(start)
try:
- start_abs = abspath(normpath(start))
- path_abs = abspath(normpath(path))
+ start_abs = abspath(start)
+ path_abs = abspath(path)
start_drive, _, start_rest = splitroot(start_abs)
path_drive, _, path_rest = splitroot(path_abs)
if normcase(start_drive) != normcase(path_drive):
raise ValueError("path is on mount %r, start on mount %r" % (
path_drive, start_drive))
- start_list = [x for x in start_rest.split(sep) if x]
- path_list = [x for x in path_rest.split(sep) if x]
+ start_list = start_rest.split(sep) if start_rest else []
+ path_list = path_rest.split(sep) if path_rest else []
# Work out how much of the filepath is shared by start and path.
i = 0
for e1, e2 in zip(start_list, path_list):
@@ -842,7 +843,7 @@ def relpath(path, start=None):
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, ValueError, AttributeError, BytesWarning, DeprecationWarning):
genericpath._check_arg_types('relpath', path, start)
raise