]> granicus.if.org Git - python/commitdiff
Merged revisions 85689 via svnmerge from
authorHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Mon, 18 Oct 2010 13:49:09 +0000 (13:49 +0000)
committerHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Mon, 18 Oct 2010 13:49:09 +0000 (13:49 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r85689 | hirokazu.yamamoto | 2010-10-18 21:13:18 +0900 | 3 lines

  Issue #5117: Case normalization was needed on ntpath.relpath(). And
  fixed root directory issue on posixpath.relpath(). (Ported working fixes
  from ntpath)
........

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

index dd7372fefa978c704576a83b4a4f23c647d9babd..9e4cff7a6c003a749f37b13ffd1f7795e56b44c1 100644 (file)
@@ -600,7 +600,7 @@ def relpath(path, start=curdir):
     path_abs = abspath(normpath(path))
     start_drive, start_rest = splitdrive(start_abs)
     path_drive, path_rest = splitdrive(path_abs)
-    if start_drive != path_drive:
+    if normcase(start_drive) != normcase(path_drive):
         error = "path is on mount '{0}', start on mount '{1}'".format(
             path_drive, start_drive)
         raise ValueError(error)
@@ -610,7 +610,7 @@ def relpath(path, start=curdir):
     # Work out how much of the filepath is shared by start and path.
     i = 0
     for e1, e2 in zip(start_list, path_list):
-        if e1 != e2:
+        if normcase(e1) != normcase(e2):
             break
         i += 1
 
index d317cebd9b7e1f9713cdfd1627a71c2f5599b0b5..64fe9dfc5dafc529db6dfab6309b9a848b48ee28 100644 (file)
@@ -444,8 +444,8 @@ def relpath(path, start=None):
     if start is None:
         start = curdir
 
-    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 8ff7075ae522b0771c97ba7ebef95d7c052b48d3..3d18527243a5c9223522566ba3580f42b29d652e 100644 (file)
@@ -236,6 +236,7 @@ class TestNtpath(unittest.TestCase):
         tester('ntpath.relpath("/", "/")', '.')
         tester('ntpath.relpath("/a", "/a")', '.')
         tester('ntpath.relpath("/a/b", "/a/b")', '.')
+        tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
 
 
 def test_main():
index 0efe3ff6569bd794cde07a67b68cc5ea985809de..6123ae25904d9ffb8a27dbe44fc84ef2b3020fe3 100644 (file)
@@ -602,6 +602,15 @@ class PosixPathTest(unittest.TestCase):
                              "../"+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
 
@@ -620,6 +629,15 @@ class PosixPathTest(unittest.TestCase):
                              b"../"+curdir+b"/a/b")
             self.assertEqual(posixpath.relpath(b"a", b"b/c"), b"../../a")
             self.assertEqual(posixpath.relpath(b"a", b"a"), b".")
+            self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x/y/z"), b'../../../foo/bar/bat')
+            self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/foo/bar"), b'bat')
+            self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/"), b'foo/bar/bat')
+            self.assertEqual(posixpath.relpath(b"/", b"/foo/bar/bat"), b'../../..')
+            self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x"), b'../foo/bar/bat')
+            self.assertEqual(posixpath.relpath(b"/x", b"/foo/bar/bat"), b'../../../x')
+            self.assertEqual(posixpath.relpath(b"/", b"/"), b'.')
+            self.assertEqual(posixpath.relpath(b"/a", b"/a"), b'.')
+            self.assertEqual(posixpath.relpath(b"/a/b", b"/a/b"), b'.')
 
             self.assertRaises(TypeError, posixpath.relpath, b"bytes", "str")
             self.assertRaises(TypeError, posixpath.relpath, "str", b"bytes")
index 95db8ed59fcba963af3c083f77bc3380d89ff284..dc852eefbbc75d0fc5e1a99ac130f3b787937f53 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -128,6 +128,10 @@ C-API
 Library
 -------
 
+- Issue #5117: Case normalization was needed on ntpath.relpath(). And
+  fixed root directory issue on posixpath.relpath(). (Ported working fixes
+  from ntpath)
+
 - Issue #10041: The signature of optional arguments in socket.makefile()
   didn't match that of io.open(), and they also didn't get forwarded
   properly to TextIOWrapper in text mode.  Patch by Kai Zhu.