Skip to content

Commit 14f1ca7

Browse files
authored
gh-117335: Handle non-iterables for ntpath.commonpath (GH-117336)
1 parent 18cf239 commit 14f1ca7

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

Lib/ntpath.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -831,23 +831,22 @@ def relpath(path, start=None):
831831
raise
832832

833833

834-
# Return the longest common sub-path of the sequence of paths given as input.
834+
# Return the longest common sub-path of the iterable of paths given as input.
835835
# The function is case-insensitive and 'separator-insensitive', i.e. if the
836836
# only difference between two paths is the use of '\' versus '/' as separator,
837837
# they are deemed to be equal.
838838
#
839839
# However, the returned path will have the standard '\' separator (even if the
840840
# given paths had the alternative '/' separator) and will have the case of the
841-
# first path given in the sequence. Additionally, any trailing separator is
841+
# first path given in the iterable. Additionally, any trailing separator is
842842
# stripped from the returned path.
843843

844844
def commonpath(paths):
845-
"""Given a sequence of path names, returns the longest common sub-path."""
846-
845+
"""Given an iterable of path names, returns the longest common sub-path."""
846+
paths = tuple(map(os.fspath, paths))
847847
if not paths:
848-
raise ValueError('commonpath() arg is an empty sequence')
848+
raise ValueError('commonpath() arg is an empty iterable')
849849

850-
paths = tuple(map(os.fspath, paths))
851850
if isinstance(paths[0], bytes):
852851
sep = b'\\'
853852
altsep = b'/'

Lib/test/test_ntpath.py

+3
Original file line numberDiff line numberDiff line change
@@ -871,11 +871,14 @@ def check_error(exc, paths):
871871
self.assertRaises(exc, ntpath.commonpath,
872872
[os.fsencode(p) for p in paths])
873873

874+
self.assertRaises(TypeError, ntpath.commonpath, None)
874875
self.assertRaises(ValueError, ntpath.commonpath, [])
876+
self.assertRaises(ValueError, ntpath.commonpath, iter([]))
875877
check_error(ValueError, ['C:\\Program Files', 'Program Files'])
876878
check_error(ValueError, ['C:\\Program Files', 'C:Program Files'])
877879
check_error(ValueError, ['\\Program Files', 'Program Files'])
878880
check_error(ValueError, ['Program Files', 'C:\\Program Files'])
881+
879882
check(['C:\\Program Files'], 'C:\\Program Files')
880883
check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files')
881884
check(['C:\\Program Files\\', 'C:\\Program Files'],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise TypeError for non-sequences for :func:`ntpath.commonpath`.

0 commit comments

Comments
 (0)