summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Rittau <srittau@rittau.biz>2024-02-18 08:24:58 (GMT)
committerGitHub <noreply@github.com>2024-02-18 08:24:58 (GMT)
commit371c9708863c23ddc716085198ab07fa49968166 (patch)
tree305ca06fc4fa5d87a8ff08e2a01dc3eba0a2d802
parentf9154f8f237e31e7c30f8698f980bee5e494f1e0 (diff)
downloadcpython-371c9708863c23ddc716085198ab07fa49968166.zip
cpython-371c9708863c23ddc716085198ab07fa49968166.tar.gz
cpython-371c9708863c23ddc716085198ab07fa49968166.tar.bz2
gh-114709: Fix exceptions raised by posixpath.commonpath (#114710)
Fix the exceptions raised by posixpath.commonpath Raise ValueError, not IndexError when passed an empty iterable. Raise TypeError, not ValueError when passed None.
-rw-r--r--Doc/library/os.path.rst4
-rw-r--r--Lib/posixpath.py3
-rw-r--r--Lib/test/test_posixpath.py2
-rw-r--r--Misc/NEWS.d/next/Library/2024-01-29-13-46-41.gh-issue-114709.SQ998l.rst5
4 files changed, 11 insertions, 3 deletions
diff --git a/Doc/library/os.path.rst b/Doc/library/os.path.rst
index 34bc76b..16e654f 100644
--- a/Doc/library/os.path.rst
+++ b/Doc/library/os.path.rst
@@ -79,7 +79,7 @@ the :mod:`glob` module.)
.. function:: commonpath(paths)
- Return the longest common sub-path of each pathname in the sequence
+ Return the longest common sub-path of each pathname in the iterable
*paths*. Raise :exc:`ValueError` if *paths* contain both absolute
and relative pathnames, the *paths* are on the different drives or
if *paths* is empty. Unlike :func:`commonprefix`, this returns a
@@ -90,7 +90,7 @@ the :mod:`glob` module.)
.. versionadded:: 3.5
.. versionchanged:: 3.6
- Accepts a sequence of :term:`path-like objects <path-like object>`.
+ Accepts an iterable of :term:`path-like objects <path-like object>`.
.. function:: commonprefix(list)
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index e4f155e..33943b4 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -546,10 +546,11 @@ def relpath(path, start=None):
def commonpath(paths):
"""Given a sequence 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')
- paths = tuple(map(os.fspath, paths))
if isinstance(paths[0], bytes):
sep = b'/'
curdir = b'.'
diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py
index 86ce1b1..cbb7c4c 100644
--- a/Lib/test/test_posixpath.py
+++ b/Lib/test/test_posixpath.py
@@ -703,7 +703,9 @@ class PosixPathTest(unittest.TestCase):
self.assertRaises(exc, posixpath.commonpath,
[os.fsencode(p) for p in paths])
+ self.assertRaises(TypeError, posixpath.commonpath, None)
self.assertRaises(ValueError, posixpath.commonpath, [])
+ self.assertRaises(ValueError, posixpath.commonpath, iter([]))
check_error(ValueError, ['/usr', 'usr'])
check_error(ValueError, ['usr', '/usr'])
diff --git a/Misc/NEWS.d/next/Library/2024-01-29-13-46-41.gh-issue-114709.SQ998l.rst b/Misc/NEWS.d/next/Library/2024-01-29-13-46-41.gh-issue-114709.SQ998l.rst
new file mode 100644
index 0000000..ca0d790
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-01-29-13-46-41.gh-issue-114709.SQ998l.rst
@@ -0,0 +1,5 @@
+:func:`posixpath.commonpath()` now raises a :exc:`ValueError` exception when
+passed an empty iterable. Previously, :exc:`IndexError` was raised.
+
+:func:`posixpath.commonpath()` now raises a :exc:`TypeError` exception when
+passed ``None``. Previously, :exc:`ValueError` was raised.