From 679af7f816ced8941ed5cf9b151a0cac543d0336 Mon Sep 17 00:00:00 2001 From: Thomas Grainger 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, "/"))