From 8c4ce94b7d075e8fe705bce9100d6b0bc3e816e6 Mon Sep 17 00:00:00 2001 From: Ram Rachum Date: Fri, 12 Jun 2020 13:42:51 -0400 Subject: Fix exception causes in lookup.py Mako now performs exception chaining using ``raise from``, correctly identifying underlying exception conditions when it raises its own exceptions. Pull request courtesy Ram Rachum. Additionally includes cleanup of the test suite to include better exception fixtures. Closes: #319 Pull-request: https://github.com/sqlalchemy/mako/pull/319 Pull-request-sha: d06526ac3f80ca9d24cd8143d8afde254f80b094 Additionally: Fixes: #348 Change-Id: Ibb2864de822bf4b63adf22a6bb32cf0758d296bd --- mako/lookup.py | 12 ++++++------ mako/pyparser.py | 4 ++-- mako/runtime.py | 6 ++++-- 3 files changed, 12 insertions(+), 10 deletions(-) (limited to 'mako') diff --git a/mako/lookup.py b/mako/lookup.py index 17cef8e..7afe242 100644 --- a/mako/lookup.py +++ b/mako/lookup.py @@ -241,7 +241,7 @@ class TemplateLookup(TemplateCollection): return self._check(uri, self._collection[uri]) else: return self._collection[uri] - except KeyError: + except KeyError as e: u = re.sub(r"^\/+", "", uri) for dir_ in self.directories: # make sure the path seperators are posix - os.altsep is empty @@ -252,8 +252,8 @@ class TemplateLookup(TemplateCollection): return self._load(srcfile, uri) else: raise exceptions.TopLevelLookupException( - "Cant locate template for uri %r" % uri - ) + "Can't locate template for uri %r" % uri + ) from e def adjust_uri(self, uri, relativeto): """Adjust the given ``uri`` based on the given relative URI.""" @@ -337,11 +337,11 @@ class TemplateLookup(TemplateCollection): return template self._collection.pop(uri, None) return self._load(template.filename, uri) - except OSError: + except OSError as e: self._collection.pop(uri, None) raise exceptions.TemplateLookupException( - "Cant locate template for uri %r" % uri - ) + "Can't locate template for uri %r" % uri + ) from e def put_string(self, uri, text): """Place a new :class:`.Template` object into this diff --git a/mako/pyparser.py b/mako/pyparser.py index 2a1ba5f..5c55505 100644 --- a/mako/pyparser.py +++ b/mako/pyparser.py @@ -34,7 +34,7 @@ def parse(code, mode="exec", **exception_kwargs): try: return _ast_util.parse(code, "", mode) - except Exception: + except Exception as e: raise exceptions.SyntaxException( "(%s) %s (%r)" % ( @@ -43,7 +43,7 @@ def parse(code, mode="exec", **exception_kwargs): code[0:50], ), **exception_kwargs, - ) + ) from e class FindIdentifiers(_ast_util.NodeVisitor): diff --git a/mako/runtime.py b/mako/runtime.py index f5dbe1a..6d7fa68 100644 --- a/mako/runtime.py +++ b/mako/runtime.py @@ -835,8 +835,10 @@ def _lookup_template(context, uri, relativeto): uri = lookup.adjust_uri(uri, relativeto) try: return lookup.get_template(uri) - except exceptions.TopLevelLookupException: - raise exceptions.TemplateLookupException(str(compat.exception_as())) + except exceptions.TopLevelLookupException as e: + raise exceptions.TemplateLookupException( + str(compat.exception_as()) + ) from e def _populate_self_namespace(context, template, self_ns=None): -- cgit v1.2.1