diff options
author | Ram Rachum <ram@rachum.com> | 2020-06-12 13:42:51 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-12-09 17:57:51 -0500 |
commit | 8c4ce94b7d075e8fe705bce9100d6b0bc3e816e6 (patch) | |
tree | 1de9fa09606e65064482fedc3daf7479bf69e669 /mako | |
parent | f1fb195706b02966412009f310cb1e0e126a97be (diff) | |
download | mako-8c4ce94b7d075e8fe705bce9100d6b0bc3e816e6.tar.gz |
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
Diffstat (limited to 'mako')
-rw-r--r-- | mako/lookup.py | 12 | ||||
-rw-r--r-- | mako/pyparser.py | 4 | ||||
-rw-r--r-- | mako/runtime.py | 6 |
3 files changed, 12 insertions, 10 deletions
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, "<unknown>", 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): |