Fixed posixpath.normpath() to respect two leading slashes, but
turn three or more into a single slash. (This is in sync with POSIX susv2 according to Fredrik.)
This commit is contained in:
parent
fde66e1bcc
commit
bf222c9f12
|
@ -343,21 +343,26 @@ def normpath(path):
|
||||||
"""Normalize path, eliminating double slashes, etc."""
|
"""Normalize path, eliminating double slashes, etc."""
|
||||||
if path == '':
|
if path == '':
|
||||||
return '.'
|
return '.'
|
||||||
initial_slash = (path[0] == '/')
|
initial_slashes = path.startswith('/')
|
||||||
|
# POSIX allows one or two initial slashes, but treats three or more
|
||||||
|
# as single slash.
|
||||||
|
if (initial_slashes and
|
||||||
|
path.startswith('//') and not path.startswith('///')):
|
||||||
|
initial_slashes = 2
|
||||||
comps = path.split('/')
|
comps = path.split('/')
|
||||||
new_comps = []
|
new_comps = []
|
||||||
for comp in comps:
|
for comp in comps:
|
||||||
if comp in ('', '.'):
|
if comp in ('', '.'):
|
||||||
continue
|
continue
|
||||||
if (comp != '..' or (not initial_slash and not new_comps) or
|
if (comp != '..' or (not initial_slashes and not new_comps) or
|
||||||
(new_comps and new_comps[-1] == '..')):
|
(new_comps and new_comps[-1] == '..')):
|
||||||
new_comps.append(comp)
|
new_comps.append(comp)
|
||||||
elif new_comps:
|
elif new_comps:
|
||||||
new_comps.pop()
|
new_comps.pop()
|
||||||
comps = new_comps
|
comps = new_comps
|
||||||
path = '/'.join(comps)
|
path = '/'.join(comps)
|
||||||
if initial_slash:
|
if initial_slashes:
|
||||||
path = '/' + path
|
path = '/'*initial_slashes + path
|
||||||
return path or '.'
|
return path or '.'
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue