]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Make ntpath compress multiple slashes between drive letter and the rest of the
authorBrett Cannon <bcannon@gmail.com>
Sat, 10 Jul 2004 20:42:22 +0000 (20:42 +0000)
committerBrett Cannon <bcannon@gmail.com>
Sat, 10 Jul 2004 20:42:22 +0000 (20:42 +0000)
path.  Also clarifies UNC handling and adds appropriate tests.

Applies patch #988607 to fix bug #980327.  Thanks Paul Moore.

Lib/ntpath.py
Lib/test/test_ntpath.py
Misc/NEWS

index 549c35ea696b9ff426f2ab1782c629be891dc2b8..1f355ecd978f0113f97b591cf5dbd8a2d9e40f2d 100644 (file)
@@ -440,9 +440,25 @@ def normpath(path):
     """Normalize path, eliminating double slashes, etc."""
     path = path.replace("/", "\\")
     prefix, path = splitdrive(path)
-    while path[:1] == "\\":
-        prefix = prefix + "\\"
-        path = path[1:]
+    # We need to be careful here. If the prefix is empty, and the path starts
+    # with a backslash, it could either be an absolute path on the current
+    # drive (\dir1\dir2\file) or a UNC filename (\\server\mount\dir1\file). It
+    # is therefore imperative NOT to collapse multiple backslashes blindly in
+    # that case.
+    # The code below preserves multiple backslashes when there is no drive
+    # letter. This means that the invalid filename \\\a\b is preserved
+    # unchanged, where a\\\b is normalised to a\b. It's not clear that there
+    # is any better behaviour for such edge cases.
+    if prefix == '':
+        # No drive letter - preserve initial backslashes
+        while path[:1] == "\\":
+            prefix = prefix + "\\"
+            path = path[1:]
+    else:
+        # We have a drive letter - collapse initial backslashes
+        if path.startswith("\\"):
+            prefix = prefix + "\\"
+            path = path.lstrip("\\")
     comps = path.split("\\")
     i = 0
     while i < len(comps):
index fdb431aa6413cd003bbc23e4b70dffef6b72bd66..139aa1f28e06160dc72752c605872d2f37696dda 100644 (file)
@@ -99,12 +99,9 @@ tester("ntpath.normpath('C:A//B')", r'C:A\B')
 tester("ntpath.normpath('D:A/./B')", r'D:A\B')
 tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
 
-# Next 3 seem dubious, and especially the 3rd, but normpath is possibly
-# trying to leave UNC paths alone without actually knowing anything about
-# them.
-tester("ntpath.normpath('C:///A//B')", r'C:\\\A\B')
-tester("ntpath.normpath('D:///A/./B')", r'D:\\\A\B')
-tester("ntpath.normpath('e:///A/foo/../B')", r'e:\\\A\B')
+tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
+tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
+tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
 
 tester("ntpath.normpath('..')", r'..')
 tester("ntpath.normpath('.')", r'.')
@@ -115,6 +112,8 @@ tester("ntpath.normpath('/../.././..')", '\\')
 tester("ntpath.normpath('c:/../../..')", 'c:\\')
 tester("ntpath.normpath('../.././..')", r'..\..\..')
 tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
+tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
+tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
 
 # ntpath.abspath() can only be used on a system with the "nt" module
 # (reasonably), so we protect this test with "import nt".  This allows
index 2a16fcc58c309064e0f67e2aac9700ecd803185a..a09e255cd0af9b0213463b527dab4f2833b8386a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,10 @@ Extension modules
 Library
 -------
 
+- Bug #980327: ntpath not handles compressing erroneous slashes between the
+  drive letter and the rest of the path.  Also clearly handles UNC addresses now
+  as well.  Thanks Paul Moore.
+
 - bug #679953: zipfile.py should now work for files over 2 GB.  The packed data
   for file sizes (compressed and uncompressed) was being stored as signed
   instead of unsigned.