PackageLoader works with single module file
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 46f3a68 · Browse files · 3 files·+36 added−4 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+36 added−4 removed
- tests:+22 added−0 removed
- other:+14 added−4 removed
Provenance
- Commit
eec0db8- Tree
2e561e3- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
46f3a68
3 files changed+36 added−4 removed
modified:src/jinja2/loaders.py+12 added-4 removed
295 unmodified lines296 if isinstance(loader, zipimport.zipimporter):297 self._archive = loader.archive298 pkgdir = next(iter(spec.submodule_search_locations)) # type: ignore299 template_root = os.path.join(pkgdir, package_path)300- elif spec.submodule_search_locations:301- # This will be one element for regular packages and multiple302- # for namespace packages.303- for root in spec.submodule_search_locations:300+ else:301+ roots: t.List[str] = []302+303+ # One element for regular packages, multiple for namespace304+ # packages, or None for single module file.305+ if spec.submodule_search_locations:306+ roots.extend(spec.submodule_search_locations)307+ # A single module file, use the parent directory instead.308+ elif spec.origin is not None:309+ roots.append(os.path.dirname(spec.origin))310+311+ for root in roots:312 root = os.path.join(root, package_path)313 314 if os.path.isdir(root):315 template_root = root337 unmodified linesmodified:tests/test_loader.py+22 added
312 unmodified lines313 assert "test.html" in templates314 315 316 @pytest.fixture()317+def package_file_loader(monkeypatch):318+ monkeypatch.syspath_prepend(Path(__file__).parent / "res")319+ return PackageLoader("__init__")320+321+322+@pytest.mark.parametrize(323+ ("template", "expect"), [("foo/test.html", "FOO"), ("test.html", "BAR")]324+)325+def test_package_file_source(package_file_loader, template, expect):326+ source, name, up_to_date = package_file_loader.get_source(None, template)327+ assert source.rstrip() == expect328+ assert name.endswith(os.path.join(*split_template_path(template)))329+ assert up_to_date()330+331+332+def test_package_file_list(package_file_loader):333+ templates = package_file_loader.list_templates()334+ assert "foo/test.html" in templates335+ assert "test.html" in templates336+337+338+@pytest.fixture()339 def package_zip_loader(monkeypatch):340 package_zip = (Path(__file__) / ".." / "res" / "package.zip").resolve()341 monkeypatch.syspath_prepend(package_zip)342 return PackageLoader("t_pack")61 unmodified linesmodified:CHANGES.rst+2 added
11 unmodified lines12 - Improve async performance by avoiding checks for common types.13 :issue:`1514`14 - Revert change to ``hash(Node)`` behavior. Nodes are hashed by id15 again :issue:`1521`16+- ``PackageLoader`` works when the package is a single module file.17+ :issue:`1512`18 19 20 Version 3.0.221 -------------894 unmodified lines