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

Skip to content

Commit 2fc0109

Browse files
committed
Rewrite of normpath() by Corran Webster, so trailing :s are removed
(except for : and volume:, where they are needed).
1 parent 2afffd4 commit 2fc0109

1 file changed

Lines changed: 23 additions & 32 deletions

File tree

Lib/macpath.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -179,39 +179,30 @@ def expanduser(path):
179179
norm_error = 'macpath.norm_error: path cannot be normalized'
180180

181181
def normpath(s):
182-
"""Normalize a pathname: get rid of '::' sequences by backing up,
183-
e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
184-
Raise the exception norm_error below if backing up is impossible,
185-
e.g., for '::foo'."""
186-
# XXX The Unix version doesn't raise an exception but simply
187-
# returns an unnormalized path. Should do so here too.
188-
189-
import string
190-
if ':' not in s:
191-
return ':' + s
192-
f = string.splitfields(s, ':')
193-
pre = []
194-
post = []
195-
if not f[0]:
196-
pre = f[:1]
197-
f = f[1:]
198-
if not f[len(f)-1]:
199-
post = f[-1:]
200-
f = f[:-1]
201-
res = []
202-
for seg in f:
203-
if seg:
204-
res.append(seg)
182+
"""Normalize a pathname. Will return the same result for
183+
equivalent paths."""
184+
185+
if ":" not in s:
186+
return ":"+s
187+
188+
comps = string.splitfields(s, ":")
189+
i = 1
190+
while i < len(comps)-1:
191+
if comps[i] == "" and comps[i-1] != "":
192+
if i > 1:
193+
del comps[i-1:i+1]
194+
i = i-1
195+
else:
196+
# best way to handle this is to raise an exception
197+
raise norm_error, 'Cannot use :: immedeately after volume name'
205198
else:
206-
if not res: raise norm_error, 'path starts with ::'
207-
del res[len(res)-1]
208-
if not (pre or res):
209-
raise norm_error, 'path starts with volume::'
210-
if pre: res = pre + res
211-
if post: res = res + post
212-
s = res[0]
213-
for seg in res[1:]:
214-
s = s + ':' + seg
199+
i = i + 1
200+
201+
s = string.join(comps, ":")
202+
203+
# remove trailing ":" except for ":" and "Volume:"
204+
if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s):
205+
s = s[:-1]
215206
return s
216207

217208

0 commit comments

Comments
 (0)