]> granicus.if.org Git - python/commitdiff
Fix posixpath.realpath() for multiple pardirs (fixes issue #6975).
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 18 Feb 2013 10:21:04 +0000 (12:21 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 18 Feb 2013 10:21:04 +0000 (12:21 +0200)
Lib/posixpath.py
Lib/test/test_posixpath.py

index 7e9dd8571d7864972bb44f9843707f847cb37b5e..bc76c90dde42857f2ffaea1ac65ad2ee66e038fa 100644 (file)
@@ -417,9 +417,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):
index 430a41cf58062db9a631fdf8ed2dc19e02beab68..80006d96f4dd62158723e2939f6cd9d04f4da31f 100644 (file)
@@ -338,6 +338,24 @@ class PosixPathTest(unittest.TestCase):
         self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"),
                          b"/foo/bar")
 
+    def test_realpath_curdir(self):
+        self.assertEqual(realpath('.'), os.getcwd())
+        self.assertEqual(realpath('./.'), os.getcwd())
+        self.assertEqual(realpath('/'.join(['.'] * 100)), os.getcwd())
+
+        self.assertEqual(realpath(b'.'), os.getcwdb())
+        self.assertEqual(realpath(b'./.'), os.getcwdb())
+        self.assertEqual(realpath(b'/'.join([b'.'] * 100)), os.getcwdb())
+
+    def test_realpath_pardir(self):
+        self.assertEqual(realpath('..'), dirname(os.getcwd()))
+        self.assertEqual(realpath('../..'), dirname(dirname(os.getcwd())))
+        self.assertEqual(realpath('/'.join(['..'] * 100)), '/')
+
+        self.assertEqual(realpath(b'..'), dirname(os.getcwdb()))
+        self.assertEqual(realpath(b'../..'), dirname(dirname(os.getcwdb())))
+        self.assertEqual(realpath(b'/'.join([b'..'] * 100)), b'/')
+
     @unittest.skipUnless(hasattr(os, "symlink"),
                          "Missing symlink implementation")
     @skip_if_ABSTFN_contains_backslash