From: David Lord Date: Mon, 4 Jul 2022 14:26:15 +0000 (-0700) Subject: move closure out of loop for bugbear B023 X-Git-Tag: 3.1.3~38^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6089d142c125b6e147da316dbca0df3a0ad68206;p=thirdparty%2Fjinja.git move closure out of loop for bugbear B023 --- diff --git a/src/jinja2/loaders.py b/src/jinja2/loaders.py index d2f98093..caebf80b 100644 --- a/src/jinja2/loaders.py +++ b/src/jinja2/loaders.py @@ -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()