From 9ee94d139197c0df8f4e096957576d124ad31c8e Mon Sep 17 00:00:00 2001 From: Nice Zombies Date: Sun, 14 Apr 2024 23:04:14 +0200 Subject: gh-117636: Remove redundant type check in `os.path.join()` (#117638) --- Lib/ntpath.py | 2 -- Lib/posixpath.py | 6 ++---- Lib/test/test_posixpath.py | 2 ++ .../2024-04-08-14-33-38.gh-issue-117636.exnRKd.rst | 1 + 4 files changed, 5 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-04-08-14-33-38.gh-issue-117636.exnRKd.rst diff --git a/Lib/ntpath.py b/Lib/ntpath.py index f5d1a21..aba18bf 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -108,8 +108,6 @@ def join(path, *paths): seps = '\\/' colon_seps = ':\\/' try: - if not paths: - path[:0] + sep #23780: Ensure compatible data type even if p is null. result_drive, result_root, result_path = splitroot(path) for p in paths: p_drive, p_root, p_path = splitroot(p) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 8fd49cd..dd29fbb 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -77,13 +77,11 @@ def join(a, *p): sep = _get_sep(a) path = a try: - if not p: - path[:0] + sep #23780: Ensure compatible data type even if p is null. for b in p: b = os.fspath(b) - if b.startswith(sep): + if b.startswith(sep) or not path: path = b - elif not path or path.endswith(sep): + elif path.endswith(sep): path += b else: path += sep + b diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 248fe2c..7c122e6 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -56,6 +56,8 @@ class PosixPathTest(unittest.TestCase): self.assertEqual(fn(b"/foo", b"bar", b"baz"), b"/foo/bar/baz") self.assertEqual(fn(b"/foo/", b"bar/", b"baz/"), b"/foo/bar/baz/") + self.assertEqual(fn("a", ""), "a/") + self.assertEqual(fn("a", "", ""), "a/") self.assertEqual(fn("a", "b"), "a/b") self.assertEqual(fn("a", "b/"), "a/b/") self.assertEqual(fn("a/", "b"), "a/b") diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-08-14-33-38.gh-issue-117636.exnRKd.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-08-14-33-38.gh-issue-117636.exnRKd.rst new file mode 100644 index 0000000..7d7cb50 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-08-14-33-38.gh-issue-117636.exnRKd.rst @@ -0,0 +1 @@ +Speedup :func:`os.path.join`. -- cgit v0.12