Skip to content

Commit 2100e3e

Browse files
barneygalejonfoster
andcommitted
pythonGH-44626, pythonGH-105476: Fix ntpath.isabs() handling of part-absolute paths.
On Windows, `os.path.isabs()` now returns `False` when given a path that starts with exactly one (back)slash. This is more compatible with other functions in `os.path`, and with Microsoft's own documentation. Also adjust `pathlib.PureWindowsPath.is_absolute()` to call `ntpath.isabs()`, which corrects its handling of partial UNC/device paths like `//foo`. Co-authored-by: Jon Foster <jon@jon-foster.co.uk>
1 parent c6ca562 commit 2100e3e

File tree

7 files changed

+36
-19
lines changed

7 files changed

+36
-19
lines changed

Doc/library/os.path.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,16 @@ the :mod:`glob` module.)
239239
.. function:: isabs(path)
240240

241241
Return ``True`` if *path* is an absolute pathname. On Unix, that means it
242-
begins with a slash, on Windows that it begins with a (back)slash after chopping
243-
off a potential drive letter.
242+
begins with a slash, on Windows that it begins with two (back)slashes, or a
243+
drive letter, colon, and (back)slash together.
244244

245245
.. versionchanged:: 3.6
246246
Accepts a :term:`path-like object`.
247247

248+
.. versionchanged:: 3.13
249+
On Windows, returns ``False`` if the given path starts with exactly one
250+
(back)slash.
251+
248252

249253
.. function:: isfile(path)
250254

Doc/whatsnew/3.13.rst

+7
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,13 @@ os
307307
:c:func:`!posix_spawn_file_actions_addclosefrom_np`.
308308
(Contributed by Jakub Kulik in :gh:`113117`.)
309309

310+
os.path
311+
-------
312+
313+
* On Windows, :func:`os.path.isabs` no longer considers paths starting with
314+
exactly one (back)slash to be absolute.
315+
(Contributed by Barney Gale and Jon Foster in :gh:`44626`.)
316+
310317
pathlib
311318
-------
312319

Lib/ntpath.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -77,29 +77,22 @@ def normcase(s):
7777
return s.replace('/', '\\').lower()
7878

7979

80-
# Return whether a path is absolute.
81-
# Trivial in Posix, harder on Windows.
82-
# For Windows it is absolute if it starts with a slash or backslash (current
83-
# volume), or if a pathname after the volume-letter-and-colon or UNC-resource
84-
# starts with a slash or backslash.
85-
8680
def isabs(s):
8781
"""Test whether a path is absolute"""
8882
s = os.fspath(s)
8983
if isinstance(s, bytes):
9084
sep = b'\\'
9185
altsep = b'/'
9286
colon_sep = b':\\'
87+
double_sep = b'\\\\'
9388
else:
9489
sep = '\\'
9590
altsep = '/'
9691
colon_sep = ':\\'
92+
double_sep = '\\\\'
9793
s = s[:3].replace(altsep, sep)
9894
# Absolute: UNC, device, and paths with a drive and root.
99-
# LEGACY BUG: isabs("/x") should be false since the path has no drive.
100-
if s.startswith(sep) or s.startswith(colon_sep, 1):
101-
return True
102-
return False
95+
return s.startswith(colon_sep, 1) or s.startswith(double_sep)
10396

10497

10598
# Join two (or more) paths.

Lib/pathlib/_abc.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import functools
2-
import ntpath
32
import posixpath
43
import sys
54
from errno import ENOENT, ENOTDIR, EBADF, ELOOP, EINVAL
@@ -433,10 +432,7 @@ def parents(self):
433432
def is_absolute(self):
434433
"""True if the path is absolute (has both a root and, if applicable,
435434
a drive)."""
436-
if self.pathmod is ntpath:
437-
# ntpath.isabs() is defective - see GH-44626.
438-
return bool(self.drive and self.root)
439-
elif self.pathmod is posixpath:
435+
if self.pathmod is posixpath:
440436
# Optimization: work with raw paths on POSIX.
441437
for path in self._raw_paths:
442438
if path.startswith('/'):

Lib/test/test_ntpath.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,18 @@ def test_split(self):
227227
tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
228228

229229
def test_isabs(self):
230+
tester('ntpath.isabs("foo\\bar")', 0)
231+
tester('ntpath.isabs("foo/bar")', 0)
230232
tester('ntpath.isabs("c:\\")', 1)
233+
tester('ntpath.isabs("c:\\foo\\bar")', 1)
234+
tester('ntpath.isabs("c:/foo/bar")', 1)
231235
tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
232-
tester('ntpath.isabs("\\foo")', 1)
233-
tester('ntpath.isabs("\\foo\\bar")', 1)
236+
237+
# gh-44626: paths with only a drive or root are not absolute.
238+
tester('ntpath.isabs("\\foo\\bar")', 0)
239+
tester('ntpath.isabs("/foo/bar")', 0)
240+
tester('ntpath.isabs("c:foo\\bar")', 0)
241+
tester('ntpath.isabs("c:foo/bar")', 0)
234242

235243
# gh-96290: normal UNC paths and device paths without trailing backslashes
236244
tester('ntpath.isabs("\\\\conky\\mountpoint")', 1)

Lib/test/test_pathlib/test_pathlib.py

+4
Original file line numberDiff line numberDiff line change
@@ -970,10 +970,14 @@ def test_is_absolute(self):
970970
self.assertTrue(P('c:/a').is_absolute())
971971
self.assertTrue(P('c:/a/b/').is_absolute())
972972
# UNC paths are absolute by definition.
973+
self.assertTrue(P('//').is_absolute())
974+
self.assertTrue(P('//a').is_absolute())
973975
self.assertTrue(P('//a/b').is_absolute())
974976
self.assertTrue(P('//a/b/').is_absolute())
975977
self.assertTrue(P('//a/b/c').is_absolute())
976978
self.assertTrue(P('//a/b/c/d').is_absolute())
979+
self.assertTrue(P('//?/UNC/').is_absolute())
980+
self.assertTrue(P('//?/UNC/spam').is_absolute())
977981

978982
def test_join(self):
979983
P = self.cls
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix :func:`os.path.isabs` incorrectly returning ``True`` when given a path
2+
that starts with exactly one (back)slash on Windows.
3+
4+
Fix :meth:`pathlib.PureWindowsPath.is_absolute` incorrectly returning
5+
``False`` for some paths beginning with two (back)slashes.

0 commit comments

Comments
 (0)