diff options
author | Skip Montanaro <skip@pobox.com> | 2000-07-19 17:09:51 (GMT) |
---|---|---|
committer | Skip Montanaro <skip@pobox.com> | 2000-07-19 17:09:51 (GMT) |
commit | 018dfae246ba7dc173008722e6b3d1bd7197734c (patch) | |
tree | cf19cee4415d5d10984649cc615cf9ed9067982a /Lib/posixpath.py | |
parent | 7cb15245860998c4b952c52a0d439d9686ea39bc (diff) | |
download | cpython-018dfae246ba7dc173008722e6b3d1bd7197734c.zip cpython-018dfae246ba7dc173008722e6b3d1bd7197734c.tar.gz cpython-018dfae246ba7dc173008722e6b3d1bd7197734c.tar.bz2 |
added rewritten normpath from Moshe Zadka that does the right thing with
paths containing ..
Diffstat (limited to 'Lib/posixpath.py')
-rw-r--r-- | Lib/posixpath.py | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 2f7780c..d8da4ef 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -346,30 +346,25 @@ are left unchanged""" def normpath(path): """Normalize path, eliminating double slashes, etc.""" + if path == '': + return '.' import string - # Treat initial slashes specially - slashes = '' - while path[:1] == '/': - slashes = slashes + '/' - path = path[1:] - comps = string.splitfields(path, '/') - i = 0 - while i < len(comps): - if comps[i] == '.': - del comps[i] - while i < len(comps) and comps[i] == '': - del comps[i] - elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'): - del comps[i-1:i+1] - i = i-1 - elif comps[i] == '' and i > 0 and comps[i-1] <> '': - del comps[i] - else: - i = i+1 - # If the path is now empty, substitute '.' - if not comps and not slashes: - comps.append('.') - return slashes + string.joinfields(comps, '/') + initial_slash = (path[0] == '/') + comps = string.split(path, '/') + new_comps = [] + for comp in comps: + if comp in ('', '.'): + continue + if (comp != '..' or (not initial_slash and not new_comps) or + (new_comps and new_comps[-1] == '..')): + new_comps.append(comp) + elif new_comps: + new_comps.pop() + comps = new_comps + path = string.join(comps, '/') + if initial_slash: + path = '/' + path + return path or '.' def abspath(path): |