Index: Lib/ntpath.py =================================================================== --- Lib/ntpath.py (revision 69123) +++ Lib/ntpath.py (working copy) @@ -468,13 +468,20 @@ supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and sys.getwindowsversion()[3] >= 2) +# _abspath_split('d:\\') should return ['d:'] instead of ['d:', ''] +def _abspath_split(path): + path_list = abspath(path).split(sep) + if path_list and not path_list[-1]: + del path_list[-1] + return path_list + def relpath(path, start=curdir): """Return a relative version of a path""" if not path: raise ValueError("no path specified") - start_list = abspath(start).split(sep) - path_list = abspath(path).split(sep) + start_list = _abspath_split(start) + path_list = _abspath_split(path) if start_list[0].lower() != path_list[0].lower(): unc_path, rest = splitunc(path) unc_start, rest = splitunc(start) Index: Lib/posixpath.py =================================================================== --- Lib/posixpath.py (revision 69123) +++ Lib/posixpath.py (working copy) @@ -386,14 +386,21 @@ supports_unicode_filenames = False +# _abspath_split('/') should return [''] instead of ['', ''] +def _abspath_split(path): + path_list = abspath(path).split(sep) + if path_list and not path_list[-1]: + del path_list[-1] + return path_list + def relpath(path, start=curdir): """Return a relative version of a path""" if not path: raise ValueError("no path specified") - start_list = abspath(start).split(sep) - path_list = abspath(path).split(sep) + start_list = _abspath_split(start) + path_list = _abspath_split(path) # Work out how much of the filepath is shared by start and path. i = len(commonprefix([start_list, path_list])) Index: Lib/test/test_ntpath.py =================================================================== --- Lib/test/test_ntpath.py (revision 69123) +++ Lib/test/test_ntpath.py (working copy) @@ -177,8 +177,11 @@ 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("d:/a", "d:/")', 'a') + tester('ntpath.relpath("d:/a", "d:/b")', '..\\a') + tester('ntpath.relpath("d:/a", "d:/a/b")', '..') + tester('ntpath.relpath("d:/a/b", "d:/a")', 'b') - def test_main(): test_support.run_unittest(TestNtpath) Index: Lib/test/test_posixpath.py =================================================================== --- Lib/test/test_posixpath.py (revision 69123) +++ Lib/test/test_posixpath.py (working copy) @@ -502,6 +502,10 @@ 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("/a", "/"), "a") + self.assertEqual(posixpath.relpath("/a", "/b"), "../a") + self.assertEqual(posixpath.relpath("/a", "/a/b"), "..") + self.assertEqual(posixpath.relpath("/a/b", "/a"), "b") finally: os.getcwd = real_getcwd