GH-73435: Implement recursive wildcards in pathlib.PurePath.match() by barneygale · Pull Request #101398 · python/cpython (original) (raw)

PurePath.match() now handles the ** wildcard as in Path.glob(), i.e. it matches any number of path segments.

We now compile a re.Pattern object for the entire pattern. This is made more difficult by fnmatch not treating directory separators as special when evaluating wildcards (*, ?, etc), and so we arrange the path parts onto separate lines in a string, and ensure we don't set re.DOTALL.

This improves performance of match() around 2x-3x times for simple patterns, and more for complex patterns:

$ ./python -m timeit
-s 'from pathlib import PureWindowsPath as P; path = P("C:/foo/bar.py"); pattern = P("c://.py")'
'path.match(pattern)' 50000 loops, best of 5: 8.13 usec per loop # before 1000000 loops, best of 5: 297 nsec per loop # after