summaryrefslogtreecommitdiffstats
path: root/Lib/ntpath.py
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2004-07-10 20:42:22 (GMT)
committerBrett Cannon <bcannon@gmail.com>2004-07-10 20:42:22 (GMT)
commitbdc36273a2e1cd4a3985f44261e3c9df5c8a19cc (patch)
tree8a725fdf56a266d62d3b6c50b7f1d880165f6460 /Lib/ntpath.py
parent85064ffd760687c395f65f74c36d573d94b98436 (diff)
downloadcpython-bdc36273a2e1cd4a3985f44261e3c9df5c8a19cc.zip
cpython-bdc36273a2e1cd4a3985f44261e3c9df5c8a19cc.tar.gz
cpython-bdc36273a2e1cd4a3985f44261e3c9df5c8a19cc.tar.bz2
Make ntpath compress multiple slashes between drive letter and the rest of the
path. Also clarifies UNC handling and adds appropriate tests. Applies patch #988607 to fix bug #980327. Thanks Paul Moore.
Diffstat (limited to 'Lib/ntpath.py')
-rw-r--r--Lib/ntpath.py22
1 files changed, 19 insertions, 3 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index 549c35e..1f355ec 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -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):