Skip to content

Commit 4cc5778

Browse files
committedJul 2, 2023
pythonGH-106330: Fix matching of empty path in pathlib.PurePath.match()
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.
1 parent c2622a0 commit 4cc5778

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed
 

‎Lib/pathlib.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,12 @@ def _lines(self):
463463
try:
464464
return self._lines_cached
465465
except AttributeError:
466-
trans = _SWAP_SEP_AND_NEWLINE[self._flavour.sep]
467-
self._lines_cached = str(self).translate(trans)
466+
path_str = str(self)
467+
if path_str == '.':
468+
self._lines_cached = ''
469+
else:
470+
trans = _SWAP_SEP_AND_NEWLINE[self._flavour.sep]
471+
self._lines_cached = path_str.translate(trans)
468472
return self._lines_cached
469473

470474
def __eq__(self, other):

‎Lib/test/test_pathlib.py

+4
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ def test_match_common(self):
384384
self.assertTrue(P('A.py').match('a.PY', case_sensitive=False))
385385
self.assertFalse(P('c:/a/B.Py').match('C:/A/*.pY', case_sensitive=True))
386386
self.assertTrue(P('/a/b/c.py').match('/A/*/*.Py', case_sensitive=False))
387+
# Matching against empty path
388+
self.assertFalse(P().match('*'))
389+
self.assertTrue(P().match('**'))
390+
self.assertFalse(P().match('**/*'))
387391

388392
def test_ordering_common(self):
389393
# Ordering is tuple-alike.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix incorrect matching of empty paths in :meth:`pathlib.PurePath.match`.
2+
This bug was introduced in Python 3.12.0 beta 1.

0 commit comments

Comments
 (0)
Please sign in to comment.