Skip to content

Commit 13e9330

Browse files
committed
pythongh-101360: Fix anchor matching in pathlib.PureWindowsPath.match()
Use `fnmatch` to match path and pattern anchors, just as we do for other path parts. This allows patterns such as `'*:/Users/*'` to be matched.
1 parent 498598e commit 13e9330

3 files changed

Lines changed: 9 additions & 8 deletions

File tree

‎Lib/pathlib.py‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -657,15 +657,10 @@ def match(self, path_pattern):
657657
drv, root, pat_parts = self._parse_parts((path_pattern,))
658658
if not pat_parts:
659659
raise ValueError("empty pattern")
660-
elif drv and drv != self._flavour.normcase(self._drv):
661-
return False
662-
elif root and root != self._root:
663-
return False
664660
parts = self._parts_normcase
665661
if drv or root:
666662
if len(pat_parts) != len(parts):
667663
return False
668-
pat_parts = pat_parts[1:]
669664
elif len(pat_parts) > len(parts):
670665
return False
671666
for part, pat in zip(reversed(parts), reversed(pat_parts)):

‎Lib/test/test_pathlib.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,7 @@ def test_as_uri(self):
889889
def test_match_common(self):
890890
P = self.cls
891891
# Absolute patterns.
892-
self.assertTrue(P('c:/b.py').match('/*.py'))
893-
self.assertTrue(P('c:/b.py').match('c:*.py'))
892+
self.assertTrue(P('c:/b.py').match('*:/*.py'))
894893
self.assertTrue(P('c:/b.py').match('c:/*.py'))
895894
self.assertFalse(P('d:/b.py').match('c:/*.py')) # wrong drive
896895
self.assertFalse(P('b.py').match('/*.py'))
@@ -901,14 +900,18 @@ def test_match_common(self):
901900
self.assertFalse(P('/b.py').match('c:*.py'))
902901
self.assertFalse(P('/b.py').match('c:/*.py'))
903902
# UNC patterns.
904-
self.assertTrue(P('//some/share/a.py').match('/*.py'))
903+
self.assertTrue(P('//some/share/a.py').match('//*/*/*.py'))
905904
self.assertTrue(P('//some/share/a.py').match('//some/share/*.py'))
906905
self.assertFalse(P('//other/share/a.py').match('//some/share/*.py'))
907906
self.assertFalse(P('//some/share/a/b.py').match('//some/share/*.py'))
908907
# Case-insensitivity.
909908
self.assertTrue(P('B.py').match('b.PY'))
910909
self.assertTrue(P('c:/a/B.Py').match('C:/A/*.pY'))
911910
self.assertTrue(P('//Some/Share/B.Py').match('//somE/sharE/*.pY'))
911+
# Path anchor doesn't match pattern anchor
912+
self.assertFalse(P('c:/b.py').match('/*.py')) # 'c:/' vs '/'
913+
self.assertFalse(P('c:/b.py').match('c:*.py')) # 'c:/' vs 'c:'
914+
self.assertFalse(P('//some/share/a.py').match('/*.py')) # '//some/share/' vs '/'
912915

913916
def test_ordering_common(self):
914917
# Case-insensitivity.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix anchor matching in :meth:`pathlib.PureWindowsPath.match`. Path and
2+
pattern anchors are now matched with :mod:`fnmatch`, just like other path
3+
parts. This allows patterns such as `"*:/Users/*"` to be matched.

0 commit comments

Comments
 (0)