summaryrefslogtreecommitdiffstats
path: root/Lib/ntpath.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-01-13 07:36:05 (GMT)
committerGitHub <noreply@github.com>2024-01-13 07:36:05 (GMT)
commite4ff131e01184b68d868cfd241a03f8b7d2e0ff9 (patch)
tree0292c431d2dd38f47a265d1d25d961ef86ec43b7 /Lib/ntpath.py
parentdac1da21218a406652b35919aa2118cc32d4c65a (diff)
downloadcpython-e4ff131e01184b68d868cfd241a03f8b7d2e0ff9.zip
cpython-e4ff131e01184b68d868cfd241a03f8b7d2e0ff9.tar.gz
cpython-e4ff131e01184b68d868cfd241a03f8b7d2e0ff9.tar.bz2
GH-44626, GH-105476: Fix `ntpath.isabs()` handling of part-absolute paths (#113829)
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>
Diffstat (limited to 'Lib/ntpath.py')
-rw-r--r--Lib/ntpath.py13
1 files changed, 3 insertions, 10 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index 3061a4a..aa0e018 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -77,12 +77,6 @@ except ImportError:
return s.replace('/', '\\').lower()
-# Return whether a path is absolute.
-# Trivial in Posix, harder on Windows.
-# For Windows it is absolute if it starts with a slash or backslash (current
-# volume), or if a pathname after the volume-letter-and-colon or UNC-resource
-# starts with a slash or backslash.
-
def isabs(s):
"""Test whether a path is absolute"""
s = os.fspath(s)
@@ -90,16 +84,15 @@ def isabs(s):
sep = b'\\'
altsep = b'/'
colon_sep = b':\\'
+ double_sep = b'\\\\'
else:
sep = '\\'
altsep = '/'
colon_sep = ':\\'
+ double_sep = '\\\\'
s = s[:3].replace(altsep, sep)
# Absolute: UNC, device, and paths with a drive and root.
- # LEGACY BUG: isabs("/x") should be false since the path has no drive.
- if s.startswith(sep) or s.startswith(colon_sep, 1):
- return True
- return False
+ return s.startswith(colon_sep, 1) or s.startswith(double_sep)
# Join two (or more) paths.