]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #5117: Case normalization was needed on ntpath.relpath(). And
authorHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Mon, 18 Oct 2010 12:13:18 +0000 (12:13 +0000)
committerHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Mon, 18 Oct 2010 12:13:18 +0000 (12:13 +0000)
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 d2410f66d0b979a00b2849c0ffe010391aa84c14..579673d34e8d1a7857ad4c2b00f914e26d786164 100644 (file)
@@ -616,7 +616,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)
@@ -626,7 +626,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 2d68b7322bd2d58340360b6b333019ef4ca22bc3..c9829e18d04c4a64664487fecd8da110497788db 100644 (file)
@@ -447,8 +447,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 5609aee5dbf4b560b745721eb488226aebb0df53..237aec01f49417f3f04eb30a2ff3fb5abef8c9d0 100644 (file)
@@ -238,6 +238,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_sameopenfile(self):
         with TemporaryFile() as tf1, TemporaryFile() as tf2:
index fd71ac9a50ecb686c4c1d22a962fba184a482c6e..8cac0df779149e06827c5673b38be04fe5783ae9 100644 (file)
@@ -394,6 +394,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
 
@@ -412,6 +421,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 15c0bb346a6d42134326a6ce75604b812bc003b9..768702c164b26c0f6f7b294673dec4b42e10cb67 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #5117: Case normalization was needed on ntpath.relpath(). And
+  fixed root directory issue on posixpath.relpath(). (Ported working fixes
+  from ntpath)
+
 - Issue #1343: xml.sax.saxutils.XMLGenerator now has an option
   short_empty_elements to direct it to use self-closing tags when appropriate.