diff options
author | Guido van Rossum <guido@python.org> | 1999-04-06 19:32:19 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1999-04-06 19:32:19 (GMT) |
commit | f3c695c4675e61d5c303c96db699184253810306 (patch) | |
tree | 0895e627025bc3d437dac785d19e544256c9f483 /Lib/ntpath.py | |
parent | 8137680704e1d2b81a0b7fef84c5e954369b6231 (diff) | |
download | cpython-f3c695c4675e61d5c303c96db699184253810306.zip cpython-f3c695c4675e61d5c303c96db699184253810306.tar.gz cpython-f3c695c4675e61d5c303c96db699184253810306.tar.bz2 |
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()).
Diffstat (limited to 'Lib/ntpath.py')
-rw-r--r-- | Lib/ntpath.py | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py index a74cce3..94c349c 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -49,15 +49,27 @@ def join(a, *p): # Split a path in a drive specification (a drive letter followed by a -# colon, or a UNC resource) and the path specification. +# colon) and the path specification. # It is always true that drivespec + pathspec == p def splitdrive(p): - """Split a pathname into drive and path specifiers. - - Return a 2-tuple (drive, path); either part may be empty. - This recognizes UNC paths (e.g. '\\\\host\\mountpoint\\dir\\file')""" + """Split a pathname into drive and path specifiers. Returns a 2-tuple +"(drive,path)"; either part may be empty""" if p[1:2] == ':': return p[0:2], p[2:] + return '', p + + +# Parse UNC paths +def splitunc(p): + """Split a pathname into UNC mount point and relative path specifiers. + + Return a 2-tuple (unc, rest); either part may be empty. + If unc is not empty, it has the form '//host/mount' (or similar + using backslashes). unc+rest is always the input path. + Paths containing drive letters never have an UNC part. + """ + if p[1:2] == ':': + return '', p # Drive letter present firstTwo = p[0:2] if firstTwo == '//' or firstTwo == '\\\\': # is a UNC path: @@ -220,11 +232,14 @@ def isfile(path): return stat.S_ISREG(st[stat.ST_MODE]) -# Is a path a mount point? -# XXX This degenerates in: 'is this the root?' on DOS/Windows +# Is a path a mount point? Either a root (with or without drive letter) +# or an UNC path with at most a / or \ after the mount point. def ismount(path): """Test whether a path is a mount point (defined as root of drive)""" + unc, rest = splitunc(path) + if unc: + return rest in ("", "/", "\\") p = splitdrive(path)[1] return len(p)==1 and p[0] in '/\\' @@ -388,7 +403,10 @@ def abspath(path): """Return the absolute version of a path""" try: import win32api - return win32api.GetFullPathName(path) + try: + return win32api.GetFullPathName(path) + except win32api.error: + return path # Bad path - return unchanged. except ImportError: if not isabs(path): path = join(os.getcwd(), path) |