From 142d2bc3f11e8ad362e2e8b63c86445643a217be Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 18 Feb 2013 12:20:44 +0200 Subject: Fix posixpath.realpath() for multiple pardirs (fixes issue #6975). --- Lib/posixpath.py | 6 ++++-- Lib/test/test_posixpath.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 5b2513a..d65dc75 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -382,9 +382,11 @@ def _joinrealpath(path, rest, seen): if name == pardir: # parent dir if path: - path = dirname(path) + path, name = split(path) + if name == pardir: + path = join(path, pardir, pardir) else: - path = name + path = pardir continue newpath = join(path, name) if not islink(newpath): diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 23def06..7443e30 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -214,6 +214,16 @@ class PosixPathTest(unittest.TestCase): self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz") self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar") + def test_realpath_curdir(self): + self.assertEqual(realpath('.'), os.getcwd()) + self.assertEqual(realpath('./.'), os.getcwd()) + self.assertEqual(realpath('/'.join(['.'] * 100)), os.getcwd()) + + def test_realpath_pardir(self): + self.assertEqual(realpath('..'), dirname(os.getcwd())) + self.assertEqual(realpath('../..'), dirname(dirname(os.getcwd()))) + self.assertEqual(realpath('/'.join(['..'] * 100)), '/') + if hasattr(os, "symlink"): def test_realpath_basic(self): # Basic operation. -- cgit v0.12