]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-122903: Honor directories in zipfile.Path.glob. (GH-122908) (#122927)
authorJason R. Coombs <jaraco@jaraco.com>
Mon, 12 Aug 2024 01:03:03 +0000 (21:03 -0400)
committerGitHub <noreply@github.com>
Mon, 12 Aug 2024 01:03:03 +0000 (01:03 +0000)
(cherry picked from commit 6aa35f3002dda25858d47e702e750e2871e42a7c)

Lib/test/test_zipfile/_path/test_path.py
Lib/zipfile/_path/__init__.py
Lib/zipfile/_path/glob.py
Misc/NEWS.d/next/Library/2024-08-11-14-23-07.gh-issue-122903.xktZta.rst [new file with mode: 0644]

index 90885dbbe39b92cbbaa3dff5d8b3cfdadff86659..f95b019f64a11b0645845ade0fb8653f4bcc10ea 100644 (file)
@@ -472,6 +472,18 @@ class TestPath(unittest.TestCase):
 
         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 @@ class TestPath(unittest.TestCase):
             '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)
index 42f9fded21198e77300213de5e82ab862f113070..7cfa8d220c39b56bbe10aece51042f4d6bff2efb 100644 (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::
 
index 4a2e665e27078aad32e2be2a43f02532cb99725d..d5213533ad2270214d8c39029f64bf88ab4560d5 100644 (file)
@@ -2,6 +2,19 @@ import re
 
 
 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.
 
diff --git a/Misc/NEWS.d/next/Library/2024-08-11-14-23-07.gh-issue-122903.xktZta.rst b/Misc/NEWS.d/next/Library/2024-08-11-14-23-07.gh-issue-122903.xktZta.rst
new file mode 100644 (file)
index 0000000..c2a1e64
--- /dev/null
@@ -0,0 +1,2 @@
+``zipfile.Path.glob`` now correctly matches directories instead of
+silently omitting them.