Skip to content
Myr

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

3 files+49 added−14 removed

modified:src/jinja2/loaders.py+13 added-14 removed
1"""API and implementations for loading templates from different data2sources.3"""4import importlib.util5import os5import pkgutil6import sys7import weakref8import zipimport9from collections import abc10from hashlib import sha111from importlib import import_module12from types import ModuleType
240 unmodified lines
253254 # 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)257258 # 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 = None261262 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)271272 if os.path.isdir(root):273 self._template_root = root
288 unmodified lines
modified:tests/test_loader.py+34 added
1import importlib.abc2import importlib.machinery3import importlib.util4import os5import platform6import shutil7import sys
341 unmodified lines
349 raises=TypeError,350)351def test_package_zip_list(package_zip_loader):352 assert package_zip_loader.list_templates() == ["foo/test.html", "test.html"]353354355def 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 None360361 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 )368369 def create_module(self, spec):370 return None # default behaviour is fine371372 def exec_module(self, module):373 return None # we need this to satisfy the interface, it's wrong374375 # ensure we restore `sys.meta_path` after putting in our loader376 before = sys.meta_path[:]377378 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[:] = before
modified:CHANGES.rst+2 added
7 unmodified lines
8- 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`1415162.11.217------
764 unmodified lines