add changelog and cleanup for pep 451 support
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent a524e77 · Browse files · 3 files·+15 added−20 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+15 added−20 removed
- tests:+7 added−14 removed
- other:+8 added−6 removed
Provenance
- Commit
c067578- Tree
3faf870- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
a524e77
3 files changed+15 added−20 removed
modified:src/jinja2/loaders.py+6 added-6 removed
253 unmodified lines254 # Make sure the package exists. This also makes namespace255 # packages work, otherwise get_loader returns None.256 import_module(package_name)257 spec = importlib.util.find_spec(package_name)258- self._loader = spec.loader259-258+ self._loader = loader = spec.loader259 self._archive = None260 self._template_root = None261+262- if isinstance(spec.loader, zipimport.zipimporter):263- self._archive = spec.loader.archive262+ 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 "packages" and multiple for268- # namespace packages267+ # 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):289 unmodified linesmodified:tests/test_loader.py+7 added-14 removed
350 unmodified lines351 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(tmp_path):356- package_name = "_my_pep451_pkg"357- pkg = tmp_path.joinpath(package_name)358- pkg.mkdir()359- pkg.joinpath("__init__.py").touch()360- templates = pkg.joinpath("templates")361- templates.mkdir()362- templates.joinpath("foo.html").write_text("hello world")363-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):366- if name != package_name:358+ if name != "res":359 return None368- path = [str(tmp_path)]369- spec = importlib.machinery.PathFinder.find_spec(name, path=path)360+361+ spec = importlib.machinery.PathFinder.find_spec(name)362 return importlib.util.spec_from_file_location(363 name,364 spec.origin,365 loader=self,7 unmodified lines373 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())387- package_loader = PackageLoader(package_name)388- assert package_loader.list_templates() == ["foo.html"]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