Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
GH-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.
  • Loading branch information
barneygale committed Jul 2, 2023
commit 4cc577803cb35c04df85c41aeb6bf9a4e0fbf8dc
8 changes: 6 additions & 2 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.