Skip to content

Commit f511a93

Browse files
authored
[3.12] gh-122903: Honor directories in zipfile.Path.glob. (GH-122908) (#122927)
(cherry picked from commit 6aa35f3)
1 parent dcc5182 commit f511a93

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

Lib/test/test_zipfile/_path/test_path.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,18 @@ def test_glob_recursive(self, alpharep):
472472

473473
assert list(root.glob("**/*.txt")) == list(root.rglob("*.txt"))
474474

475+
@pass_alpharep
476+
def test_glob_dirs(self, alpharep):
477+
root = zipfile.Path(alpharep)
478+
assert list(root.glob('b')) == [zipfile.Path(alpharep, "b/")]
479+
assert list(root.glob('b*')) == [zipfile.Path(alpharep, "b/")]
480+
481+
@pass_alpharep
482+
def test_glob_subdir(self, alpharep):
483+
root = zipfile.Path(alpharep)
484+
assert list(root.glob('g/h')) == [zipfile.Path(alpharep, "g/h/")]
485+
assert list(root.glob('g*/h*')) == [zipfile.Path(alpharep, "g/h/")]
486+
475487
@pass_alpharep
476488
def test_glob_subdirs(self, alpharep):
477489
root = zipfile.Path(alpharep)
@@ -594,3 +606,10 @@ def test_malformed_paths(self):
594606
'two-slash.txt',
595607
'parent.txt',
596608
]
609+
610+
@pass_alpharep
611+
def test_interface(self, alpharep):
612+
from importlib.resources.abc import Traversable
613+
614+
zf = zipfile.Path(alpharep)
615+
assert isinstance(zf, Traversable)

Lib/zipfile/_path/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ def _extract_text_encoding(encoding=None, *args, **kwargs):
236236

237237
class Path:
238238
"""
239-
A pathlib-compatible interface for zip files.
239+
A :class:`importlib.resources.abc.Traversable` interface for zip files.
240+
241+
Implements many of the features users enjoy from
242+
:class:`pathlib.Path`.
240243
241244
Consider a zip file with this structure::
242245

Lib/zipfile/_path/glob.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33

44
def translate(pattern):
5+
return match_dirs(translate_core(pattern))
6+
7+
8+
def match_dirs(pattern):
9+
"""
10+
Ensure that zipfile.Path directory names are matched.
11+
12+
zipfile.Path directory names always end in a slash.
13+
"""
14+
return rf'{pattern}[/]?'
15+
16+
17+
def translate_core(pattern):
518
r"""
619
Given a glob pattern, produce a regex that matches it.
720
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``zipfile.Path.glob`` now correctly matches directories instead of
2+
silently omitting them.

0 commit comments

Comments
 (0)