from .exceptions import TemplateNotFound
from .utils import internalcode
-from .utils import open_if_exists
if t.TYPE_CHECKING:
from .environment import Environment
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()