barneygale / pathlib-abc

Python base classes for rich path objects
Other
26 stars 1 forks source link

Pattern matching uses regex feature available only in Python 3.11+ #8

Closed barneygale closed 9 months ago

barneygale commented 9 months ago

Pathlib's implementation of matching/globbing uses possessive quantifiers, which are new in Python 3.11. This package should support 3.8+, so we need to find another solution.

barneygale commented 9 months ago

Fix via Alex Waygood:

diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index 43e2670c4d..98823b7274 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -64,7 +64,7 @@ def _compile_pattern(pat, sep, case_sensitive):
     regex = glob.translate(pat, recursive=True, include_hidden=True, seps=sep)
     # The string representation of an empty path is a single dot ('.'). Empty
     # paths shouldn't match wildcards, so we consume it with an atomic group.
-    regex = r'(\.\Z)?+' + regex
+    regex = r'(?=(?P<empty>\.\Z|))(?P=empty)' + regex
     return re.compile(regex, flags=flags).match