diff -r d5536c06a082 Lib/ntpath.py --- a/Lib/ntpath.py Fri Jul 05 01:40:52 2013 +0200 +++ b/Lib/ntpath.py Sun Jul 07 00:03:45 2013 +0200 @@ -620,6 +620,11 @@ start_list = [x for x in start_rest.split(sep) if x] path_list = [x for x in path_rest.split(sep) if x] # Work out how much of the filepath is shared by start and path. + + if start_list and "." in str(start_list[-1]): + # A file (not directory) is given as an argument for 'start'. + start_list = start_list[:-1] + i = 0 for e1, e2 in zip(start_list, path_list): if normcase(e1) != normcase(e2): diff -r d5536c06a082 Lib/posixpath.py --- a/Lib/posixpath.py Fri Jul 05 01:40:52 2013 +0200 +++ b/Lib/posixpath.py Sun Jul 07 00:03:45 2013 +0200 @@ -442,6 +442,10 @@ 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 and "." in str(start_list[-1]): + # A file (not directory) is given as an argument for 'start'. + start_list = start_list[:-1] + # Work out how much of the filepath is shared by start and path. i = len(commonprefix([start_list, path_list])) diff -r d5536c06a082 Lib/test/test_ntpath.py --- a/Lib/test/test_ntpath.py Fri Jul 05 01:40:52 2013 +0200 +++ b/Lib/test/test_ntpath.py Sun Jul 07 00:03:45 2013 +0200 @@ -242,6 +242,7 @@ tester('ntpath.relpath("/a", "/a")', '.') tester('ntpath.relpath("/a/b", "/a/b")', '.') tester('ntpath.relpath("c:/foo", "C:/FOO")', '.') + tester('ntpath.relpath("bar/bat", "foo.ext")', 'bar\\bat') def test_sameopenfile(self): with TemporaryFile() as tf1, TemporaryFile() as tf2: diff -r d5536c06a082 Lib/test/test_posixpath.py --- a/Lib/test/test_posixpath.py Fri Jul 05 01:40:52 2013 +0200 +++ b/Lib/test/test_posixpath.py Sun Jul 07 00:03:45 2013 +0200 @@ -505,6 +505,7 @@ self.assertEqual(posixpath.relpath("/", "/"), '.') self.assertEqual(posixpath.relpath("/a", "/a"), '.') self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.') + self.assertEqual(posixpath.relpath("foo/bar", "bat.ext"), "foo/bar") finally: os.getcwd = real_getcwd @@ -532,6 +533,8 @@ 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.assertEqual(posixpath.relpath(b"foo/bar", b"bat.ext"), b"foo/bar") + self.assertRaises(TypeError, posixpath.relpath, b"bytes", "str") self.assertRaises(TypeError, posixpath.relpath, "str", b"bytes")