Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

normpath on final template filename #1638

Merged
merged 1 commit into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Version 3.1.1

Unreleased

- The template filename on Windows uses the primary path separator.
:issue:`1637`


Version 3.1.0
-------------
Expand Down
10 changes: 7 additions & 3 deletions src/jinja2/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ def uptodate() -> bool:
except OSError:
return False

return contents, filename, uptodate
# Use normpath to convert Windows altsep to sep.
return contents, os.path.normpath(filename), uptodate
raise TemplateNotFound(template)

def list_templates(self) -> t.List[str]:
Expand Down Expand Up @@ -330,8 +331,11 @@ def get_source(
self, environment: "Environment", template: str
) -> t.Tuple[str, str, t.Optional[t.Callable[[], bool]]]:
# Use posixpath even on Windows to avoid "drive:" or UNC
# segments breaking out of the search directory.
p = posixpath.join(self._template_root, *split_template_path(template))
# segments breaking out of the search directory. Use normpath to
# convert Windows altsep to sep.
p = os.path.normpath(
posixpath.join(self._template_root, *split_template_path(template))
)
up_to_date: t.Optional[t.Callable[[], bool]]

if self._archive is None:
Expand Down
16 changes: 12 additions & 4 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import importlib.util
import os
import platform
import posixpath
import shutil
import sys
import tempfile
Expand Down Expand Up @@ -172,6 +171,15 @@ def test_uses_specified_encoding(self, encoding, expect):
t = e.get_template("mojibake.txt")
assert t.render() == expect

def test_filename_normpath(self):
"""Nested template names should only contain ``os.sep`` in the
loaded filename.
"""
loader = loaders.FileSystemLoader(self.searchpath)
e = Environment(loader=loader)
t = e.get_template("foo/test.html")
assert t.filename == str(self.searchpath / "foo" / "test.html")


class TestModuleLoader:
archive = None
Expand Down Expand Up @@ -304,7 +312,7 @@ def package_dir_loader(monkeypatch):
def test_package_dir_source(package_dir_loader, template, expect):
source, name, up_to_date = package_dir_loader.get_source(None, template)
assert source.rstrip() == expect
assert name.endswith(posixpath.join(*split_template_path(template)))
assert name.endswith(os.path.join(*split_template_path(template)))
assert up_to_date()


Expand All @@ -326,7 +334,7 @@ def package_file_loader(monkeypatch):
def test_package_file_source(package_file_loader, template, expect):
source, name, up_to_date = package_file_loader.get_source(None, template)
assert source.rstrip() == expect
assert name.endswith(posixpath.join(*split_template_path(template)))
assert name.endswith(os.path.join(*split_template_path(template)))
assert up_to_date()


Expand All @@ -349,7 +357,7 @@ def package_zip_loader(monkeypatch):
def test_package_zip_source(package_zip_loader, template, expect):
source, name, up_to_date = package_zip_loader.get_source(None, template)
assert source.rstrip() == expect
assert name.endswith(posixpath.join(*split_template_path(template)))
assert name.endswith(os.path.join(*split_template_path(template)))
assert up_to_date is None


Expand Down