FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.12] gh-122903: Honor directories in zipfile.Path.glob. (GH-122908) by jaraco · Pull Request #122927 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
19 changes: 19 additions & 0 deletions Lib/test/test_zipfile/_path/test_path.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
5 changes: 4 additions & 1 deletion Lib/zipfile/_path/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand Down
13 changes: 13 additions & 0 deletions Lib/zipfile/_path/glob.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``zipfile.Path.glob`` now correctly matches directories instead of
silently omitting them.

Back | FazBrowse Home | New Git URL