diff -r 0acc5626a578 Lib/posixpath.py --- a/Lib/posixpath.py Tue Oct 23 22:50:11 2012 +0100 +++ b/Lib/posixpath.py Wed Oct 24 22:08:29 2012 +0300 @@ -394,49 +394,64 @@ if isinstance(filename, bytes): sep = b'/' empty = b'' + dot = b'.' + dotdot = b'..' else: sep = '/' empty = '' + dot = '.' + dotdot = '..' + + stack = [] + paths_seen = set() + cache = {} + if isabs(filename): - bits = [sep] + filename.split(sep)[1:] + rest = filename[1:] + path = sep else: - bits = [empty] + filename.split(sep) + rest = filename + path = empty - for i in range(2, len(bits)+1): - component = join(*bits[0:i]) - # Resolve symbolic links. - if islink(component): - resolved = _resolve_link(component) - if resolved is None: - # Infinite loop -- return original component + rest of the path - return abspath(join(*([component] + bits[i:]))) - else: - newpath = join(*([resolved] + bits[i:])) - return realpath(newpath) + while True: + while rest: + name, _, rest = rest.partition(sep) + if not name or name == dot: + continue + if name == dotdot: + if path: + path = dirname(path) + else: + path = name + continue + dir = path + path = join(path, name) + if path in cache: + path = cache[path] + continue + # Resolve symbolic links. + if islink(path): + if path in paths_seen: + path = join(path, rest, *[x[1] for x in reversed(stack)]) + return abspath(path) + resolved = os.readlink(path) + stack.append((path, rest)) + paths_seen.add(path) + if isabs(resolved): + rest = resolved[1:] + path = sep + else: + rest = resolved + path = dir + if not stack: + break + oldpath, rest = stack.pop() + cache[oldpath] = path + paths_seen.remove(oldpath) - return abspath(filename) + return abspath(path) -def _resolve_link(path): - """Internal helper function. Takes a path and follows symlinks - until we either arrive at something that isn't a symlink, or - encounter a path we've seen before (meaning that there's a loop). - """ - paths_seen = set() - while islink(path): - if path in paths_seen: - # Already seen this path, so we must have a symlink loop - return None - paths_seen.add(path) - # Resolve where the link points to - resolved = os.readlink(path) - if not isabs(resolved): - dir = dirname(path) - path = normpath(join(dir, resolved)) - else: - path = normpath(resolved) - return path - supports_unicode_filenames = (sys.platform == 'darwin') def relpath(path, start=None): diff -r 0acc5626a578 Lib/test/test_posixpath.py --- a/Lib/test/test_posixpath.py Tue Oct 23 22:50:11 2012 +0100 +++ b/Lib/test/test_posixpath.py Wed Oct 24 22:08:29 2012 +0300 @@ -377,6 +377,22 @@ self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1") self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2") + self.assertEqual(realpath(ABSTFN+"1/x"), ABSTFN+"1/x") + self.assertEqual(realpath(ABSTFN+"1/.."), dirname(ABSTFN)) + self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x") + os.symlink(ABSTFN+"x", ABSTFN+"y") + self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"), + ABSTFN + "y") + self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"), + ABSTFN + "1") + + os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a") + self.assertEqual(realpath(ABSTFN+"a"), ABSTFN+"a/b") + + os.symlink("../" + basename(dirname(ABSTFN)) + "/" + + basename(ABSTFN) + "c", ABSTFN+"c") + self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c") + # Test using relative path as well. os.chdir(dirname(ABSTFN)) self.assertEqual(realpath(basename(ABSTFN)), ABSTFN) @@ -385,6 +401,45 @@ support.unlink(ABSTFN) support.unlink(ABSTFN+"1") support.unlink(ABSTFN+"2") + support.unlink(ABSTFN+"y") + support.unlink(ABSTFN+"c") + + @unittest.skipUnless(hasattr(os, "symlink"), + "Missing symlink implementation") + @skip_if_ABSTFN_contains_backslash + def test_realpath_repeated_indirect_symlinks(self): + # Issue #6975. + try: + os.mkdir(ABSTFN) + os.symlink('../' + basename(ABSTFN), ABSTFN + '/self') + os.symlink('self/self/self', ABSTFN + '/link') + self.assertEqual(realpath(ABSTFN + '/link'), ABSTFN) + finally: + support.unlink(ABSTFN + '/self') + support.unlink(ABSTFN + '/link') + safe_rmdir(ABSTFN) + + @unittest.skipUnless(hasattr(os, "symlink"), + "Missing symlink implementation") + @skip_if_ABSTFN_contains_backslash + def test_realpath_deep_recursion(self): + depth = 10 + old_path = abspath('.') + try: + os.mkdir(ABSTFN) + for i in range(depth): + os.symlink('/'.join(['%d' % i] * 10), ABSTFN + '/%d' % (i + 1)) + os.symlink('.', ABSTFN + '/0') + self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN) + + # Test using relative path as well. + os.chdir(ABSTFN) + self.assertEqual(realpath('%d' % depth), ABSTFN) + finally: + os.chdir(old_path) + for i in range(depth + 1): + support.unlink(ABSTFN + '/%d' % i) + safe_rmdir(ABSTFN) @unittest.skipUnless(hasattr(os, "symlink"), "Missing symlink implementation")