mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-117335: Handle non-iterables for ntpath.commonpath
(GH-117336)
This commit is contained in:
parent
18cf239e39
commit
14f1ca7d53
3 changed files with 10 additions and 7 deletions
|
@ -831,23 +831,22 @@ def relpath(path, start=None):
|
||||||
raise
|
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
|
# The function is case-insensitive and 'separator-insensitive', i.e. if the
|
||||||
# only difference between two paths is the use of '\' versus '/' as separator,
|
# only difference between two paths is the use of '\' versus '/' as separator,
|
||||||
# they are deemed to be equal.
|
# they are deemed to be equal.
|
||||||
#
|
#
|
||||||
# However, the returned path will have the standard '\' separator (even if the
|
# 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
|
# 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.
|
# stripped from the returned path.
|
||||||
|
|
||||||
def commonpath(paths):
|
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."""
|
||||||
|
|
||||||
if not paths:
|
|
||||||
raise ValueError('commonpath() arg is an empty sequence')
|
|
||||||
|
|
||||||
paths = tuple(map(os.fspath, paths))
|
paths = tuple(map(os.fspath, paths))
|
||||||
|
if not paths:
|
||||||
|
raise ValueError('commonpath() arg is an empty iterable')
|
||||||
|
|
||||||
if isinstance(paths[0], bytes):
|
if isinstance(paths[0], bytes):
|
||||||
sep = b'\\'
|
sep = b'\\'
|
||||||
altsep = b'/'
|
altsep = b'/'
|
||||||
|
|
|
@ -871,11 +871,14 @@ class TestNtpath(NtpathTestCase):
|
||||||
self.assertRaises(exc, ntpath.commonpath,
|
self.assertRaises(exc, ntpath.commonpath,
|
||||||
[os.fsencode(p) for p in paths])
|
[os.fsencode(p) for p in paths])
|
||||||
|
|
||||||
|
self.assertRaises(TypeError, ntpath.commonpath, None)
|
||||||
self.assertRaises(ValueError, ntpath.commonpath, [])
|
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', 'Program Files'])
|
||||||
check_error(ValueError, ['C:\\Program Files', 'C:Program Files'])
|
check_error(ValueError, ['C:\\Program Files', 'C:Program Files'])
|
||||||
check_error(ValueError, ['\\Program Files', 'Program Files'])
|
check_error(ValueError, ['\\Program Files', 'Program Files'])
|
||||||
check_error(ValueError, ['Program Files', 'C:\\Program Files'])
|
check_error(ValueError, ['Program Files', 'C:\\Program Files'])
|
||||||
|
|
||||||
check(['C:\\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'], 'C:\\Program Files')
|
||||||
check(['C:\\Program Files\\', 'C:\\Program Files'],
|
check(['C:\\Program Files\\', 'C:\\Program Files'],
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Raise TypeError for non-sequences for :func:`ntpath.commonpath`.
|
Loading…
Add table
Add a link
Reference in a new issue