]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
move closure out of loop for bugbear B023
authorDavid Lord <davidism@gmail.com>
Mon, 4 Jul 2022 14:26:15 +0000 (07:26 -0700)
committerDavid Lord <davidism@gmail.com>
Mon, 4 Jul 2022 14:26:15 +0000 (07:26 -0700)
src/jinja2/loaders.py

index d2f98093cde425fad2c4bbf2a07e383fce5e4a38..caebf80b23855b5633a321e9d905ee04f704fd4c 100644 (file)
@@ -15,7 +15,6 @@ from types import ModuleType
 
 from .exceptions import TemplateNotFound
 from .utils import internalcode
-from .utils import open_if_exists
 
 if t.TYPE_CHECKING:
     from .environment import Environment
@@ -193,29 +192,30 @@ class FileSystemLoader(BaseLoader):
         self, environment: "Environment", template: str
     ) -> t.Tuple[str, str, t.Callable[[], bool]]:
         pieces = split_template_path(template)
+
         for searchpath in self.searchpath:
             # Use posixpath even on Windows to avoid "drive:" or UNC
             # segments breaking out of the search directory.
             filename = posixpath.join(searchpath, *pieces)
-            f = open_if_exists(filename)
-            if f is None:
-                continue
-            try:
-                contents = f.read().decode(self.encoding)
-            finally:
-                f.close()
 
-            mtime = os.path.getmtime(filename)
+            if os.path.isfile(filename):
+                break
+        else:
+            raise TemplateNotFound(template)
+
+        with open(filename, encoding=self.encoding) as f:
+            contents = f.read()
 
-            def uptodate() -> bool:
-                try:
-                    return os.path.getmtime(filename) == mtime
-                except OSError:
-                    return False
+        mtime = os.path.getmtime(filename)
 
-            # Use normpath to convert Windows altsep to sep.
-            return contents, os.path.normpath(filename), uptodate
-        raise TemplateNotFound(template)
+        def uptodate() -> bool:
+            try:
+                return os.path.getmtime(filename) == mtime
+            except OSError:
+                return False
+
+        # Use normpath to convert Windows altsep to sep.
+        return contents, os.path.normpath(filename), uptodate
 
     def list_templates(self) -> t.List[str]:
         found = set()