]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-117636: Remove redundant type check in `os.path.join()` (#117638)
authorNice Zombies <nineteendo19d0@gmail.com>
Sun, 14 Apr 2024 21:04:14 +0000 (23:04 +0200)
committerGitHub <noreply@github.com>
Sun, 14 Apr 2024 21:04:14 +0000 (14:04 -0700)
Lib/ntpath.py
Lib/posixpath.py
Lib/test/test_posixpath.py
Misc/NEWS.d/next/Core and Builtins/2024-04-08-14-33-38.gh-issue-117636.exnRKd.rst [new file with mode: 0644]

index f5d1a2195dd633d21c7a509fc313b91a3721eb9a..aba18bfe407abf931adbdc144f1c66eedbf1ae79 100644 (file)
@@ -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)
index 8fd49cdc35890854c8c7edc05d3f15e191726ed2..dd29fbb1614aa8007d3a6e37ed1d2d7c5eeb5e20 100644 (file)
@@ -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
index 248fe2cc5d5ca8dd3f49e77101269c5b0d8777d6..7c122e6204b1005ad3abb7f25557272d911845ec 100644 (file)
@@ -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 (file)
index 0000000..7d7cb50
--- /dev/null
@@ -0,0 +1 @@
+Speedup :func:`os.path.join`.