Merge pull request #1169 from asottile/pep_451
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Use importlib machinery to fix PEP 451 import hooks
authored by David Lord · committed by GitHub · parents 0a37031, c067578 · Browse files · 3 files·+49 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+49 added−14 removed
- tests:+34 added−0 removed
- other:+15 added−14 removed
Provenance
- Commit
bff4893- Tree
3faf870- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
0a37031- Parent 2
c067578
3 files changed+49 added−14 removed
modified:src/jinja2/loaders.py+13 added-14 removed
1 """API and implementations for loading templates from different data2 sources.3 """4+import importlib.util5 import os5-import pkgutil6 import sys7 import weakref8+import zipimport9 from collections import abc10 from hashlib import sha111 from importlib import import_module12 from types import ModuleType240 unmodified lines253 254 # Make sure the package exists. This also makes namespace255 # packages work, otherwise get_loader returns None.256 import_module(package_name)256- self._loader = loader = pkgutil.get_loader(package_name)257-258- # Zip loader's archive attribute points at the zip.259- self._archive = getattr(loader, "archive", None)257+ spec = importlib.util.find_spec(package_name)258+ self._loader = loader = spec.loader259+ self._archive = None260 self._template_root = None261 262- if hasattr(loader, "get_filename"):263- # A standard directory package, or a zip package.264- self._template_root = os.path.join(265- os.path.dirname(loader.get_filename(package_name)), package_path266- )267- elif hasattr(loader, "_path"):268- # A namespace package, limited support. Find the first269- # contributor with the template directory.270- for root in loader._path:262+ if isinstance(loader, zipimport.zipimporter):263+ self._archive = loader.archive264+ pkgdir = next(iter(spec.submodule_search_locations))265+ self._template_root = os.path.join(pkgdir, package_path)266+ elif spec.submodule_search_locations:267+ # This will be one element for regular packages and multiple268+ # for namespace packages.269+ for root in spec.submodule_search_locations:270 root = os.path.join(root, package_path)271 272 if os.path.isdir(root):273 self._template_root = root288 unmodified linesmodified:tests/test_loader.py+34 added
1+import importlib.abc2+import importlib.machinery3+import importlib.util4 import os5 import platform6 import shutil7 import sys341 unmodified lines349 raises=TypeError,350 )351 def test_package_zip_list(package_zip_loader):352 assert package_zip_loader.list_templates() == ["foo/test.html", "test.html"]353+354+355+def test_pep_451_import_hook():356+ class ImportHook(importlib.abc.MetaPathFinder, importlib.abc.Loader):357+ def find_spec(self, name, path=None, target=None):358+ if name != "res":359+ return None360+361+ spec = importlib.machinery.PathFinder.find_spec(name)362+ return importlib.util.spec_from_file_location(363+ name,364+ spec.origin,365+ loader=self,366+ submodule_search_locations=spec.submodule_search_locations,367+ )368+369+ def create_module(self, spec):370+ return None # default behaviour is fine371+372+ def exec_module(self, module):373+ return None # we need this to satisfy the interface, it's wrong374+375+ # ensure we restore `sys.meta_path` after putting in our loader376+ before = sys.meta_path[:]377+378+ try:379+ sys.meta_path.insert(0, ImportHook())380+ package_loader = PackageLoader("res")381+ assert "test.html" in package_loader.list_templates()382+ finally:383+ sys.meta_path[:] = beforemodified:CHANGES.rst+2 added
7 unmodified lines8 - Drop support for Python 2.7 and 3.5.9 - Bump MarkupSafe dependency to >=1.1.10 - Bump Babel optional dependency to >=2.1.11 - Remove code that was marked deprecated.12+- Use :pep:`451` API to load templates with13+ :class:`~loaders.PackageLoader`. :issue:`1168`14 15 16 2.11.217 ------764 unmodified lines