summaryrefslogtreecommitdiff
blob: b68091e5397a9a110fca616d09c385b33fb5f098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
From 679af7f816ced8941ed5cf9b151a0cac543d0336 Mon Sep 17 00:00:00 2001
From: Thomas Grainger <tagrain@gmail.com>
Date: Mon, 13 May 2024 18:02:35 +0100
Subject: [PATCH] fix test_package_zip_list on 3.13

---
 src/jinja2/loaders.py | 32 ++++++++++++++++++++++++++------
 tests/test_loader.py  |  2 +-
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/src/jinja2/loaders.py b/src/jinja2/loaders.py
index 9eaf647ba..8c2c86cd0 100644
--- a/src/jinja2/loaders.py
+++ b/src/jinja2/loaders.py
@@ -238,6 +238,30 @@ def list_templates(self) -> t.List[str]:
         return sorted(found)
 
 
+if sys.version_info >= (3, 13):
+
+    def _get_zipimporter_files(z: t.Any) -> t.Dict[str, object]:
+        try:
+            get_files = z._get_files
+        except AttributeError as e:
+            raise TypeError(
+                "This zip import does not have the required"
+                " metadata to list templates."
+            ) from e
+        return get_files()
+else:
+
+    def _get_zipimporter_files(z: t.Any) -> t.Dict[str, object]:
+        try:
+            files = z._files
+        except AttributeError as e:
+            raise TypeError(
+                "This zip import does not have the required"
+                " metadata to list templates."
+            ) from e
+        return files  # type: ignore[no-any-return]
+
+
 class PackageLoader(BaseLoader):
     """Load templates from a directory in a Python package.
 
@@ -382,11 +406,7 @@ def list_templates(self) -> t.List[str]:
                     for name in filenames
                 )
         else:
-            if not hasattr(self._loader, "_files"):
-                raise TypeError(
-                    "This zip import does not have the required"
-                    " metadata to list templates."
-                )
+            files = _get_zipimporter_files(self._loader)
 
             # Package is a zip file.
             prefix = (
@@ -395,7 +415,7 @@ def list_templates(self) -> t.List[str]:
             )
             offset = len(prefix)
 
-            for name in self._loader._files.keys():
+            for name in files:
                 # Find names under the templates directory that aren't directories.
                 if name.startswith(prefix) and name[-1] != os.path.sep:
                     results.append(name[offset:].replace(os.path.sep, "/"))