]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
normpath on final template filename 1638/head
authorDavid Lord <davidism@gmail.com>
Fri, 25 Mar 2022 22:10:18 +0000 (15:10 -0700)
committerDavid Lord <davidism@gmail.com>
Fri, 25 Mar 2022 22:23:00 +0000 (15:23 -0700)
CHANGES.rst
src/jinja2/loaders.py
tests/test_loader.py

index afc86e5e78e49c6be2d52113047420b8c3c8b851..4cc30f11f3336efe00735514ba624f4ce8ada42d 100644 (file)
@@ -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
 -------------
index e255cd41f285e2b04b279bddfb97b9c3199fc595..d2f98093cde425fad2c4bbf2a07e383fce5e4a38 100644 (file)
@@ -213,7 +213,8 @@ class FileSystemLoader(BaseLoader):
                 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]:
@@ -330,8 +331,11 @@ class PackageLoader(BaseLoader):
         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:
index 812358316af1bee3992ffeaa1744f18f1582ecb7..04c921d24745e63fc5019842004bbe7a9e7ad208 100644 (file)
@@ -3,7 +3,6 @@ import importlib.machinery
 import importlib.util
 import os
 import platform
-import posixpath
 import shutil
 import sys
 import tempfile
@@ -172,6 +171,15 @@ class TestFileSystemLoader:
         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
@@ -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()
 
 
@@ -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()
 
 
@@ -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