Skip to content

Commit

Permalink
normpath on final template filename
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Mar 25, 2022
1 parent 155e51d commit 7e30d9d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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
11 changes: 8 additions & 3 deletions src/jinja2/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import importlib.util
import os
import pathlib
import posixpath
import sys
import typing as t
Expand Down Expand Up @@ -213,7 +214,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 +332,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
9 changes: 9 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,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

0 comments on commit 7e30d9d

Please sign in to comment.