diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 0e8bb5ab10d916..b7fbdff20cac99 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -502,10 +502,10 @@ def realpath(filename, *, strict=False): def relpath(path, start=None): """Return a relative version of a path""" + path = os.fspath(path) if not path: raise ValueError("no path specified") - path = os.fspath(path) if isinstance(path, bytes): curdir = b'.' sep = b'/' diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 807f985f7f4df7..ff78410738022d 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -650,6 +650,7 @@ def test_relpath(self): (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar") try: curdir = os.path.split(os.getcwd())[-1] + self.assertRaises(TypeError, posixpath.relpath, None) self.assertRaises(ValueError, posixpath.relpath, "") self.assertEqual(posixpath.relpath("a"), "a") self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a") diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-06-16-42-34.gh-issue-117584.hqk9Hn.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-06-16-42-34.gh-issue-117584.hqk9Hn.rst new file mode 100644 index 00000000000000..fd6a6097a89154 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-06-16-42-34.gh-issue-117584.hqk9Hn.rst @@ -0,0 +1 @@ +Raise :exc:`TypeError` for non-paths in :func:`posixpath.relpath()`.