Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f3c695c

Browse files
committed
Withdraw the UNC support from splitdrive(). Instead, a new function
splitunc() parses UNC paths. The contributor of the UNC parsing in splitdrive() doesn't like it, but I haven't heard a good reason to keep it, and it causes some problems. (I think there's a philosophical problem -- to me, the split*() functions are purely syntactical, and the fact that \\foo is not a valid path doesn't mean that it shouldn't be considered an absolute path.) Also (quite separately, but strangely related to the philosophical issue above) fix abspath() so that if win32api exists, it doesn't fail when the path doesn't actually exist -- if GetFullPathName() fails, fall back on the old strategy (join with getcwd() if neccessary, and then use normpath()).
1 parent 8137680 commit f3c695c

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

Lib/ntpath.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,27 @@ def join(a, *p):
4949

5050

5151
# Split a path in a drive specification (a drive letter followed by a
52-
# colon, or a UNC resource) and the path specification.
52+
# colon) and the path specification.
5353
# It is always true that drivespec + pathspec == p
5454
def splitdrive(p):
55-
"""Split a pathname into drive and path specifiers.
56-
57-
Return a 2-tuple (drive, path); either part may be empty.
58-
This recognizes UNC paths (e.g. '\\\\host\\mountpoint\\dir\\file')"""
55+
"""Split a pathname into drive and path specifiers. Returns a 2-tuple
56+
"(drive,path)"; either part may be empty"""
5957
if p[1:2] == ':':
6058
return p[0:2], p[2:]
59+
return '', p
60+
61+
62+
# Parse UNC paths
63+
def splitunc(p):
64+
"""Split a pathname into UNC mount point and relative path specifiers.
65+
66+
Return a 2-tuple (unc, rest); either part may be empty.
67+
If unc is not empty, it has the form '//host/mount' (or similar
68+
using backslashes). unc+rest is always the input path.
69+
Paths containing drive letters never have an UNC part.
70+
"""
71+
if p[1:2] == ':':
72+
return '', p # Drive letter present
6173
firstTwo = p[0:2]
6274
if firstTwo == '//' or firstTwo == '\\\\':
6375
# is a UNC path:
@@ -220,11 +232,14 @@ def isfile(path):
220232
return stat.S_ISREG(st[stat.ST_MODE])
221233

222234

223-
# Is a path a mount point?
224-
# XXX This degenerates in: 'is this the root?' on DOS/Windows
235+
# Is a path a mount point? Either a root (with or without drive letter)
236+
# or an UNC path with at most a / or \ after the mount point.
225237

226238
def ismount(path):
227239
"""Test whether a path is a mount point (defined as root of drive)"""
240+
unc, rest = splitunc(path)
241+
if unc:
242+
return rest in ("", "/", "\\")
228243
p = splitdrive(path)[1]
229244
return len(p)==1 and p[0] in '/\\'
230245

@@ -388,7 +403,10 @@ def abspath(path):
388403
"""Return the absolute version of a path"""
389404
try:
390405
import win32api
391-
return win32api.GetFullPathName(path)
406+
try:
407+
return win32api.GetFullPathName(path)
408+
except win32api.error:
409+
return path # Bad path - return unchanged.
392410
except ImportError:
393411
if not isabs(path):
394412
path = join(os.getcwd(), path)

0 commit comments

Comments
 (0)