]> granicus.if.org Git - python/commitdiff
Issue #5117: Fixed root directory related issue on posixpath.relpath() and
authorHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Mon, 18 Oct 2010 13:55:29 +0000 (13:55 +0000)
committerHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Mon, 18 Oct 2010 13:55:29 +0000 (13:55 +0000)
ntpath.relpath().

Lib/ntpath.py
Lib/posixpath.py
Lib/test/test_ntpath.py
Lib/test/test_posixpath.py
Misc/NEWS

index 11a0a3efcbdb4d5c3aa44bd88088a1469c1a69af..091a242485ff1ce79fccf190d8acf25fc6da125b 100644 (file)
@@ -488,8 +488,8 @@ def relpath(path, start=curdir):
 
     if not path:
         raise ValueError("no path specified")
-    start_list = abspath(start).split(sep)
-    path_list = abspath(path).split(sep)
+    start_list = [x for x in abspath(start).split(sep) if x]
+    path_list = [x for x in abspath(path).split(sep) if x]
     if start_list[0].lower() != path_list[0].lower():
         unc_path, rest = splitunc(path)
         unc_start, rest = splitunc(start)
index 81f25b86e030ecfbc64c41a67494da20997e0155..aae38d5abf3bc7e5a81270eca8067b3cca05ddef 100644 (file)
@@ -403,8 +403,8 @@ def relpath(path, start=curdir):
     if not path:
         raise ValueError("no path specified")
 
-    start_list = abspath(start).split(sep)
-    path_list = abspath(path).split(sep)
+    start_list = [x for x in abspath(start).split(sep) if x]
+    path_list = [x for x in abspath(path).split(sep) if x]
 
     # Work out how much of the filepath is shared by start and path.
     i = len(commonprefix([start_list, path_list]))
index 73645f19bf3c77968144414ec5d924c0f4743ca4..e1852c0eb7fdd2c234ff2fa6dfbf6ad822863860 100644 (file)
@@ -178,6 +178,16 @@ class TestNtpath(unittest.TestCase):
         tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
         tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
         tester('ntpath.relpath("a", "a")', '.')
+        tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
+        tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
+        tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
+        tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
+        tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
+        tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
+        tester('ntpath.relpath("/", "/")', '.')
+        tester('ntpath.relpath("/a", "/a")', '.')
+        tester('ntpath.relpath("/a/b", "/a/b")', '.')
+        tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
 
 
 class NtCommonTest(test_genericpath.CommonTest):
index 2288c48f47d6b21d4aa3002812c83c1b6a672b99..8bb78d66e2fbff69839547904b1a18dbac71a310 100644 (file)
@@ -322,6 +322,15 @@ class PosixPathTest(unittest.TestCase):
             self.assertEqual(posixpath.relpath("a/b", "../c"), "../"+curdir+"/a/b")
             self.assertEqual(posixpath.relpath("a", "b/c"), "../../a")
             self.assertEqual(posixpath.relpath("a", "a"), ".")
+            self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat')
+            self.assertEqual(posixpath.relpath("/foo/bar/bat", "/foo/bar"), 'bat')
+            self.assertEqual(posixpath.relpath("/foo/bar/bat", "/"), 'foo/bar/bat')
+            self.assertEqual(posixpath.relpath("/", "/foo/bar/bat"), '../../..')
+            self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x"), '../foo/bar/bat')
+            self.assertEqual(posixpath.relpath("/x", "/foo/bar/bat"), '../../../x')
+            self.assertEqual(posixpath.relpath("/", "/"), '.')
+            self.assertEqual(posixpath.relpath("/a", "/a"), '.')
+            self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.')
         finally:
             os.getcwd = real_getcwd
 
index b597bd57839beabcdc29bef59e58a21be58426e1..3ff641c6c92f539828cec9d55b003cc3dab297aa 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -60,6 +60,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #5117: Fixed root directory related issue on posixpath.relpath() and
+  ntpath.relpath().
+
 - Issue 9409: Fix the regex to match all kind of filenames, for interactive
   debugging in doctests.