Skip to content

Commit 518af64

Browse files
gh-85681: Make ntpath.abspath() platform independent (GH-155395)
On non-Windows platforms, ntpath.abspath() now strips trailing dots and spaces from the last path component, and a single trailing dot from other components (unless preceded by another dot), like the Windows path normalization does. So relpath() and other functions based on it no longer depend on the platform.
1 parent 45d64ef commit 518af64

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

‎Lib/ntpath.py‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,28 @@ def normpath(path):
516516
from nt import _getfullpathname
517517

518518
except ImportError: # not running on Windows - mock up something sensible
519+
def _strip_dots_and_spaces(path):
520+
# Imitate the trailing dot and space stripping performed by the
521+
# Windows path normalization. This is a best effort, not a
522+
# specification.
523+
if isinstance(path, bytes):
524+
sep = b'\\'
525+
dot = b'.'
526+
dots_and_spaces = b'. '
527+
else:
528+
sep = '\\'
529+
dot = '.'
530+
dots_and_spaces = '. '
531+
drive, root, tail = splitroot(path)
532+
if not tail:
533+
return path
534+
comps = tail.split(sep)
535+
for i, comp in enumerate(comps[:-1]):
536+
if len(comp) > 1 and comp.endswith(dot) and not comp.endswith(dot*2):
537+
comps[i] = comp[:-1]
538+
comps[-1] = comps[-1].rstrip(dots_and_spaces)
539+
return drive + root + sep.join(comps)
540+
519541
def abspath(path):
520542
"""Return the absolute version of a path."""
521543
path = os.fspath(path)
@@ -525,7 +547,7 @@ def abspath(path):
525547
else:
526548
cwd = os.getcwd()
527549
path = join(cwd, path)
528-
return normpath(path)
550+
return _strip_dots_and_spaces(normpath(path))
529551

530552
else: # use native Windows method on Windows
531553
def abspath(path):

‎Lib/test/test_ntpath.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,36 @@ def test_expanduser(self):
12351235

12361236

12371237

1238+
def test_abspath_dots_and_spaces(self):
1239+
# gh-85681: trailing dots and spaces are stripped from the last
1240+
# component, and a single trailing dot from other components.
1241+
# This is the observed Windows behavior, not a specification.
1242+
tester('ntpath.abspath("C:/spam. . .")', "C:\\spam")
1243+
tester('ntpath.abspath("C:/spam.")', "C:\\spam")
1244+
tester('ntpath.abspath("C:/spam ")', "C:\\spam")
1245+
tester('ntpath.abspath("C:/spam..")', "C:\\spam")
1246+
tester('ntpath.abspath("C:/.spam")', "C:\\.spam")
1247+
tester('ntpath.abspath("C:/spam./eggs")', "C:\\spam\\eggs")
1248+
tester('ntpath.abspath("C:/spam.b./eggs")', "C:\\spam.b\\eggs")
1249+
tester('ntpath.abspath("C:/spam ./eggs")', "C:\\spam \\eggs")
1250+
# A trailing dot preceded by a dot is not stripped.
1251+
tester('ntpath.abspath("C:/spam../eggs")', "C:\\spam..\\eggs")
1252+
tester('ntpath.abspath("C:/spam.../eggs")', "C:\\spam...\\eggs")
1253+
# Trailing dots and spaces are stripped even in extended paths.
1254+
tester('ntpath.abspath("\\\\?\\C:/spam. . .")', "\\\\?\\C:\\spam")
1255+
tester('ntpath.abspath("\\\\.\\C:/spam. . .")', "\\\\.\\C:\\spam")
1256+
tester('ntpath.abspath("//server/share/spam. ")',
1257+
"\\\\server\\share\\spam")
1258+
1259+
def test_relpath_dots_and_spaces(self):
1260+
# gh-85681: relpath() is based on abspath().
1261+
tester('ntpath.relpath("foo ", "foo")', ".")
1262+
tester('ntpath.relpath("foo.", "foo")', ".")
1263+
tester('ntpath.relpath("foo/bar ", "foo")', "bar")
1264+
tester('ntpath.relpath("foo/bar.", "foo")', "bar")
1265+
# "foo.." denotes "foo" as the last component, but not as other.
1266+
tester('ntpath.relpath("foo../bar", "foo..")', "..\\foo..\\bar")
1267+
12381268
@unittest.skipUnless(nt, "abspath requires 'nt' module")
12391269
def test_abspath(self):
12401270
tester('ntpath.abspath("C:\\")', "C:\\")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:func:`!ntpath.abspath` on non-Windows platforms now strips trailing dots and
2+
spaces like the Windows path normalization does. This makes
3+
:func:`!ntpath.relpath` and other functions based on it platform independent.

0 commit comments

Comments
 (0)