Skip to content

Commit

Permalink
fix test_package_zip_list on 3.13 (#1979)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed May 13, 2024
2 parents e82013c + 679af7f commit 8a8e2bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
32 changes: 26 additions & 6 deletions src/jinja2/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = (
Expand All @@ -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, "/"))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def test_package_zip_source(package_zip_loader, template, expect):


@pytest.mark.xfail(
sys.implementation.name == "pypy" or sys.version_info > (3, 13),
sys.implementation.name == "pypy",
reason="zipimporter doesn't have a '_files' attribute",
raises=TypeError,
)
Expand Down

0 comments on commit 8a8e2bc

Please sign in to comment.