bug #990669: os.path.realpath() will resolve symlinks before normalizing the

path, as normalizing the path may alter the meaning of the path if it contains
symlinks.

Also add tests for infinite symlink loops and parent symlinks that need to be
resolved.
This commit is contained in:
Johannes Gijsbers 2004-08-14 15:01:53 +00:00
parent f9a098efe1
commit 4ec40648a5
3 changed files with 109 additions and 10 deletions

View file

@ -400,9 +400,11 @@ def abspath(path):
def realpath(filename):
"""Return the canonical path of the specified filename, eliminating any
symbolic links encountered in the path."""
filename = abspath(filename)
bits = ['/'] + filename.split('/')[1:]
if isabs(filename):
bits = ['/'] + filename.split('/')[1:]
else:
bits = filename.split('/')
for i in range(2, len(bits)+1):
component = join(*bits[0:i])
# Resolve symbolic links.
@ -410,13 +412,13 @@ symbolic links encountered in the path."""
resolved = _resolve_link(component)
if resolved is None:
# Infinite loop -- return original component + rest of the path
return join(*([component] + bits[i:]))
return abspath(join(*([component] + bits[i:])))
else:
newpath = join(*([resolved] + bits[i:]))
return realpath(newpath)
return filename
return realpath(newpath)
return abspath(filename)
def _resolve_link(path):
"""Internal helper function. Takes a path and follows symlinks