[Python-checkins] GH-106330: Fix matching of empty path in `pathlib.PurePath.match()` (GH-106331)
barneygale
webhook-mailer at python.org
Mon Jul 3 16:29:48 EDT 2023
https://github.com/python/cpython/commit/b4efdf8cda8fbbd0ca53b457d5f6e46a59348caf
commit: b4efdf8cda8fbbd0ca53b457d5f6e46a59348caf
branch: main
author: Barney Gale <barney.gale at gmail.com>
committer: barneygale <barney.gale at gmail.com>
date: 2023-07-03T21:29:44+01:00
summary:
GH-106330: Fix matching of empty path in `pathlib.PurePath.match()` (GH-106331)
We match paths using the `_lines` attribute, which is derived from the
path's string representation. The bug arises because an empty path's string
representation is `'.'` (not `''`), which is matched by the `'*'` wildcard.
files:
A Misc/NEWS.d/next/Library/2023-07-02-10-56-41.gh-issue-106330.QSkIUH.rst
M Lib/pathlib.py
M Lib/test/test_pathlib.py
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index e15718dc98d67..f3813e0410990 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -463,8 +463,12 @@ def _lines(self):
try:
return self._lines_cached
except AttributeError:
- trans = _SWAP_SEP_AND_NEWLINE[self._flavour.sep]
- self._lines_cached = str(self).translate(trans)
+ path_str = str(self)
+ if path_str == '.':
+ self._lines_cached = ''
+ else:
+ trans = _SWAP_SEP_AND_NEWLINE[self._flavour.sep]
+ self._lines_cached = path_str.translate(trans)
return self._lines_cached
def __eq__(self, other):
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 464a835212d47..eb2b0cfb26e85 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -384,6 +384,10 @@ def test_match_common(self):
self.assertTrue(P('A.py').match('a.PY', case_sensitive=False))
self.assertFalse(P('c:/a/B.Py').match('C:/A/*.pY', case_sensitive=True))
self.assertTrue(P('/a/b/c.py').match('/A/*/*.Py', case_sensitive=False))
+ # Matching against empty path
+ self.assertFalse(P().match('*'))
+ self.assertTrue(P().match('**'))
+ self.assertFalse(P().match('**/*'))
def test_ordering_common(self):
# Ordering is tuple-alike.
diff --git a/Misc/NEWS.d/next/Library/2023-07-02-10-56-41.gh-issue-106330.QSkIUH.rst b/Misc/NEWS.d/next/Library/2023-07-02-10-56-41.gh-issue-106330.QSkIUH.rst
new file mode 100644
index 0000000000000..c1f55ab658b51
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-07-02-10-56-41.gh-issue-106330.QSkIUH.rst
@@ -0,0 +1,2 @@
+Fix incorrect matching of empty paths in :meth:`pathlib.PurePath.match`.
+This bug was introduced in Python 3.12.0 beta 1.
More information about the Python-checkins
mailing list