]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
skip template with same name as directory 1081/head
authorDavid Lord <davidism@gmail.com>
Sun, 13 Oct 2019 13:40:54 +0000 (06:40 -0700)
committerDavid Lord <davidism@gmail.com>
Sun, 13 Oct 2019 13:42:44 +0000 (06:42 -0700)
When using multiple paths with FileSystemLoader, a template with the
same name as a directory will not prevent loading a template in the
directory.

CHANGES.rst
jinja2/utils.py
tests/res/templates2/foo [new file with mode: 0644]
tests/test_loader.py

index 89844b238c7a3a7b8a0187713314b1d474cfa09e..305eb963a7f0460bf84c1b1ee17b4a6d0eb4eae1 100644 (file)
@@ -1,7 +1,7 @@
 .. currentmodule:: jinja2
 
-Version 2.11
-------------
+Version 2.11.0
+--------------
 
 Unreleased
 
@@ -37,6 +37,8 @@ Unreleased
 -   The environment's ``finalize`` function is only applied to the
     output of expressions (constant or not), not static template data.
     :issue:`63`
+-   When providing multiple paths to ``FileSystemLoader``, a template
+    can have the same name as a directory. :issue:`821`
 
 
 Version 2.10.3
index 9fb2b353f52bcd9c3656fe2afd758c637da4bc07..a6f4dca0b817488189187f469dc1cec381119bc6 100644 (file)
@@ -8,9 +8,9 @@
     :copyright: (c) 2017 by the Jinja Team.
     :license: BSD, see LICENSE for more details.
 """
+import os
 import re
 import json
-import errno
 import warnings
 from collections import deque
 from threading import Lock
@@ -147,13 +147,12 @@ def import_string(import_name, silent=False):
 
 def open_if_exists(filename, mode='rb'):
     """Returns a file descriptor for the filename if that file exists,
-    otherwise `None`.
+    otherwise ``None``.
     """
-    try:
-        return open(filename, mode)
-    except IOError as e:
-        if e.errno not in (errno.ENOENT, errno.EISDIR, errno.EINVAL):
-            raise
+    if not os.path.isfile(filename):
+        return None
+
+    return open(filename, mode)
 
 
 def object_type_repr(obj):
diff --git a/tests/res/templates2/foo b/tests/res/templates2/foo
new file mode 100644 (file)
index 0000000..1c4ad3e
--- /dev/null
@@ -0,0 +1,2 @@
+Looks like the start of templates/foo/test.html
+Tested by test_filesystem_loader_overlapping_names
index 7e12628b938ea8a207b7f0066e37a53babb0ae37..cb30ebe571952eec183d9cce3722af5d9deb5b11 100644 (file)
@@ -44,6 +44,17 @@ class TestLoaders(object):
         assert tmpl.render().strip() == 'FOO'
         pytest.raises(TemplateNotFound, env.get_template, 'missing.html')
 
+    def test_filesystem_loader_overlapping_names(self, filesystem_loader):
+        res = os.path.dirname(filesystem_loader.searchpath[0])
+        t2_dir = os.path.join(res, "templates2")
+        # Make "foo" show up before "foo/test.html".
+        filesystem_loader.searchpath.insert(0, t2_dir)
+        e = Environment(loader=filesystem_loader)
+        e.get_template("foo")
+        # This would raise NotADirectoryError if "t2/foo" wasn't skipped.
+        e.get_template("foo/test.html")
+
+
     def test_choice_loader(self, choice_loader):
         env = Environment(loader=choice_loader)
         tmpl = env.get_template('justdict.html')