]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-117335: Handle non-iterables for `ntpath.commonpath` (GH-117336)
authorNice Zombies <nineteendo19d0@gmail.com>
Thu, 28 Mar 2024 21:20:08 +0000 (22:20 +0100)
committerGitHub <noreply@github.com>
Thu, 28 Mar 2024 21:20:08 +0000 (21:20 +0000)
Lib/ntpath.py
Lib/test/test_ntpath.py
Misc/NEWS.d/next/Core and Builtins/2024-03-28-19-13-20.gh-issue-117335.d6uKJu.rst [new file with mode: 0644]

index f1c48ecd1e5e2a6d548cb9e2d9ff703a0749effb..ecfc7d48dbb1928eae159dcc47bdc8c5f2d99897 100644 (file)
@@ -831,23 +831,22 @@ def relpath(path, start=None):
         raise
 
 
-# Return the longest common sub-path of the sequence of paths given as input.
+# Return the longest common sub-path of the iterable of paths given as input.
 # The function is case-insensitive and 'separator-insensitive', i.e. if the
 # only difference between two paths is the use of '\' versus '/' as separator,
 # they are deemed to be equal.
 #
 # However, the returned path will have the standard '\' separator (even if the
 # given paths had the alternative '/' separator) and will have the case of the
-# first path given in the sequence. Additionally, any trailing separator is
+# first path given in the iterable. Additionally, any trailing separator is
 # stripped from the returned path.
 
 def commonpath(paths):
-    """Given a sequence of path names, returns the longest common sub-path."""
-
+    """Given an iterable of path names, returns the longest common sub-path."""
+    paths = tuple(map(os.fspath, paths))
     if not paths:
-        raise ValueError('commonpath() arg is an empty sequence')
+        raise ValueError('commonpath() arg is an empty iterable')
 
-    paths = tuple(map(os.fspath, paths))
     if isinstance(paths[0], bytes):
         sep = b'\\'
         altsep = b'/'
index 9cb03e3cd5de8d748f3e5ca5c08c39d939011152..c816f99e7e9f1b49a0c7dacc0c8ea9641e895659 100644 (file)
@@ -871,11 +871,14 @@ class TestNtpath(NtpathTestCase):
             self.assertRaises(exc, ntpath.commonpath,
                               [os.fsencode(p) for p in paths])
 
+        self.assertRaises(TypeError, ntpath.commonpath, None)
         self.assertRaises(ValueError, ntpath.commonpath, [])
+        self.assertRaises(ValueError, ntpath.commonpath, iter([]))
         check_error(ValueError, ['C:\\Program Files', 'Program Files'])
         check_error(ValueError, ['C:\\Program Files', 'C:Program Files'])
         check_error(ValueError, ['\\Program Files', 'Program Files'])
         check_error(ValueError, ['Program Files', 'C:\\Program Files'])
+
         check(['C:\\Program Files'], 'C:\\Program Files')
         check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files')
         check(['C:\\Program Files\\', 'C:\\Program Files'],
diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-03-28-19-13-20.gh-issue-117335.d6uKJu.rst b/Misc/NEWS.d/next/Core and Builtins/2024-03-28-19-13-20.gh-issue-117335.d6uKJu.rst
new file mode 100644 (file)
index 0000000..e419b2e
--- /dev/null
@@ -0,0 +1 @@
+Raise TypeError for non-sequences for :func:`ntpath.commonpath`.