From 733e56ef9656dd79055acc2a3cecaf6054a45b6c Mon Sep 17 00:00:00 2001 From: Nice Zombies Date: Sun, 7 Apr 2024 11:00:08 +0200 Subject: [PATCH] gh-117584: Raise TypeError for non-paths in posixpath.relpath() (GH-117585) --- Lib/posixpath.py | 2 +- Lib/test/test_posixpath.py | 1 + .../2024-04-06-16-42-34.gh-issue-117584.hqk9Hn.rst | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-04-06-16-42-34.gh-issue-117584.hqk9Hn.rst 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()`.