diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2024-08-12 00:33:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-12 00:33:33 (GMT) |
commit | 6aa35f3002dda25858d47e702e750e2871e42a7c (patch) | |
tree | a5acf584667ac9996681ded1ab591dbd5efbc692 /Lib/zipfile | |
parent | 9cd03263100ddb1657826cc4a71470786cab3932 (diff) | |
download | cpython-6aa35f3002dda25858d47e702e750e2871e42a7c.zip cpython-6aa35f3002dda25858d47e702e750e2871e42a7c.tar.gz cpython-6aa35f3002dda25858d47e702e750e2871e42a7c.tar.bz2 |
gh-122903: Honor directories in zipfile.Path.glob. (#122908)
Diffstat (limited to 'Lib/zipfile')
-rw-r--r-- | Lib/zipfile/_path/__init__.py | 8 | ||||
-rw-r--r-- | Lib/zipfile/_path/glob.py | 10 |
2 files changed, 14 insertions, 4 deletions
diff --git a/Lib/zipfile/_path/__init__.py b/Lib/zipfile/_path/__init__.py index 0f18147..3c01659 100644 --- a/Lib/zipfile/_path/__init__.py +++ b/Lib/zipfile/_path/__init__.py @@ -250,7 +250,10 @@ def _extract_text_encoding(encoding=None, *args, **kwargs): class Path: """ - A pathlib-compatible interface for zip files. + A :class:`importlib.resources.abc.Traversable` interface for zip files. + + Implements many of the features users enjoy from + :class:`pathlib.Path`. Consider a zip file with this structure:: @@ -466,8 +469,7 @@ class Path: prefix = re.escape(self.at) tr = Translator(seps='/') matches = re.compile(prefix + tr.translate(pattern)).fullmatch - names = (data.filename for data in self.root.filelist) - return map(self._next, filter(matches, names)) + return map(self._next, filter(matches, self.root.namelist())) def rglob(self, pattern): return self.glob(f'**/{pattern}') diff --git a/Lib/zipfile/_path/glob.py b/Lib/zipfile/_path/glob.py index 69c41d7..4320f1c 100644 --- a/Lib/zipfile/_path/glob.py +++ b/Lib/zipfile/_path/glob.py @@ -28,7 +28,7 @@ class Translator: """ Given a glob pattern, produce a regex that matches it. """ - return self.extend(self.translate_core(pattern)) + return self.extend(self.match_dirs(self.translate_core(pattern))) def extend(self, pattern): r""" @@ -41,6 +41,14 @@ class Translator: """ return rf'(?s:{pattern})\Z' + def match_dirs(self, pattern): + """ + Ensure that zipfile.Path directory names are matched. + + zipfile.Path directory names always end in a slash. + """ + return rf'{pattern}[/]?' + def translate_core(self, pattern): r""" Given a glob pattern, produce a regex that matches it. |