[3.12] gh-122903: Honor directories in zipfile.Path.glob. (GH-122908) (#122927)

(cherry picked from commit 6aa35f3002)
This commit is contained in:
Jason R. Coombs 2024-08-11 21:03:03 -04:00 committed by GitHub
parent dcc5182f27
commit f511a939b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 38 additions and 1 deletions

View file

@ -472,6 +472,18 @@ def test_glob_recursive(self, alpharep):
assert list(root.glob("**/*.txt")) == list(root.rglob("*.txt"))
@pass_alpharep
def test_glob_dirs(self, alpharep):
root = zipfile.Path(alpharep)
assert list(root.glob('b')) == [zipfile.Path(alpharep, "b/")]
assert list(root.glob('b*')) == [zipfile.Path(alpharep, "b/")]
@pass_alpharep
def test_glob_subdir(self, alpharep):
root = zipfile.Path(alpharep)
assert list(root.glob('g/h')) == [zipfile.Path(alpharep, "g/h/")]
assert list(root.glob('g*/h*')) == [zipfile.Path(alpharep, "g/h/")]
@pass_alpharep
def test_glob_subdirs(self, alpharep):
root = zipfile.Path(alpharep)
@ -594,3 +606,10 @@ def test_malformed_paths(self):
'two-slash.txt',
'parent.txt',
]
@pass_alpharep
def test_interface(self, alpharep):
from importlib.resources.abc import Traversable
zf = zipfile.Path(alpharep)
assert isinstance(zf, Traversable)

View file

@ -236,7 +236,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::

View file

@ -2,6 +2,19 @@
def translate(pattern):
return match_dirs(translate_core(pattern))
def match_dirs(pattern):
"""
Ensure that zipfile.Path directory names are matched.
zipfile.Path directory names always end in a slash.
"""
return rf'{pattern}[/]?'
def translate_core(pattern):
r"""
Given a glob pattern, produce a regex that matches it.

View file

@ -0,0 +1,2 @@
``zipfile.Path.glob`` now correctly matches directories instead of
silently omitting them.