Merge pull request #1503 from pallets/except-chain

use exception chaining
This commit is contained in:
David Lord 2021-10-04 13:19:54 -07:00 committed by GitHub
commit 362cdcfd9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 32 deletions

View File

@ -1350,8 +1350,8 @@ def do_reverse(value: t.Union[str, t.Iterable[V]]) -> t.Union[str, t.Iterable[V]
rv = list(value)
rv.reverse()
return rv
except TypeError:
raise FilterArgumentError("argument must be iterable")
except TypeError as e:
raise FilterArgumentError("argument must be iterable") from e
@pass_environment
@ -1691,7 +1691,7 @@ def prepare_map(
name = args[0]
args = args[1:]
except LookupError:
raise FilterArgumentError("map requires a filter argument")
raise FilterArgumentError("map requires a filter argument") from None
def func(item: t.Any) -> t.Any:
return context.environment.call_filter(
@ -1712,7 +1712,7 @@ def prepare_select_or_reject(
try:
attr = args[0]
except LookupError:
raise FilterArgumentError("Missing parameter for attribute name")
raise FilterArgumentError("Missing parameter for attribute name") from None
transfunc = make_attrgetter(context.environment, attr)
off = 1

View File

@ -655,7 +655,7 @@ class Lexer:
)
except Exception as e:
msg = str(e).split(":")[-1].strip()
raise TemplateSyntaxError(msg, lineno, name, filename)
raise TemplateSyntaxError(msg, lineno, name, filename) from e
elif token == TOKEN_INTEGER:
value = int(value_str.replace("_", ""), 0)
elif token == TOKEN_FLOAT:

View File

@ -336,8 +336,8 @@ class PackageLoader(BaseLoader):
# Package is a zip file.
try:
source = self._loader.get_data(p) # type: ignore
except OSError:
raise TemplateNotFound(template)
except OSError as e:
raise TemplateNotFound(template) from e
# Could use the zip's mtime for all template mtimes, but
# would need to safely reload the module if it's out of
@ -476,8 +476,8 @@ class PrefixLoader(BaseLoader):
try:
prefix, name = template.split(self.delimiter, 1)
loader = self.mapping[prefix]
except (ValueError, KeyError):
raise TemplateNotFound(template)
except (ValueError, KeyError) as e:
raise TemplateNotFound(template) from e
return loader, name
def get_source(
@ -486,10 +486,10 @@ class PrefixLoader(BaseLoader):
loader, name = self.get_loader(template)
try:
return loader.get_source(environment, name)
except TemplateNotFound:
except TemplateNotFound as e:
# re-raise the exception with the correct filename here.
# (the one that includes the prefix)
raise TemplateNotFound(template)
raise TemplateNotFound(template) from e
@internalcode
def load(
@ -501,10 +501,10 @@ class PrefixLoader(BaseLoader):
loader, local_name = self.get_loader(name)
try:
return loader.load(environment, local_name, globals)
except TemplateNotFound:
except TemplateNotFound as e:
# re-raise the exception with the correct filename here.
# (the one that includes the prefix)
raise TemplateNotFound(name)
raise TemplateNotFound(name) from e
def list_templates(self) -> t.List[str]:
result = []
@ -627,8 +627,8 @@ class ModuleLoader(BaseLoader):
if mod is None:
try:
mod = __import__(module, None, None, ["root"])
except ImportError:
raise TemplateNotFound(name)
except ImportError as e:
raise TemplateNotFound(name) from e
# remove the entry from sys.modules, we only want the attribute
# on the module object we have stored on the loader.

View File

@ -507,8 +507,8 @@ class BinExpr(Expr):
f = _binop_to_func[self.operator]
try:
return f(self.left.as_const(eval_ctx), self.right.as_const(eval_ctx))
except Exception:
raise Impossible()
except Exception as e:
raise Impossible() from e
class UnaryExpr(Expr):
@ -531,8 +531,8 @@ class UnaryExpr(Expr):
f = _uaop_to_func[self.operator]
try:
return f(self.node.as_const(eval_ctx))
except Exception:
raise Impossible()
except Exception as e:
raise Impossible() from e
class Name(Expr):
@ -723,14 +723,14 @@ def args_as_const(
if node.dyn_args is not None:
try:
args.extend(node.dyn_args.as_const(eval_ctx))
except Exception:
raise Impossible()
except Exception as e:
raise Impossible() from e
if node.dyn_kwargs is not None:
try:
kwargs.update(node.dyn_kwargs.as_const(eval_ctx))
except Exception:
raise Impossible()
except Exception as e:
raise Impossible() from e
return args, kwargs
@ -779,8 +779,8 @@ class _FilterTestCommon(Expr):
try:
return func(*args, **kwargs)
except Exception:
raise Impossible()
except Exception as e:
raise Impossible() from e
class Filter(_FilterTestCommon):
@ -847,8 +847,8 @@ class Getitem(Expr):
return eval_ctx.environment.getitem(
self.node.as_const(eval_ctx), self.arg.as_const(eval_ctx)
)
except Exception:
raise Impossible()
except Exception as e:
raise Impossible() from e
class Getattr(Expr):
@ -869,8 +869,8 @@ class Getattr(Expr):
try:
return eval_ctx.environment.getattr(self.node.as_const(eval_ctx), self.attr)
except Exception:
raise Impossible()
except Exception as e:
raise Impossible() from e
class Slice(Expr):
@ -929,8 +929,8 @@ class Compare(Expr):
return False
value = new_value
except Exception:
raise Impossible()
except Exception as e:
raise Impossible() from e
return result

View File

@ -824,7 +824,7 @@ class Namespace:
try:
return self.__attrs[name]
except KeyError:
raise AttributeError(name)
raise AttributeError(name) from None
def __setitem__(self, name: str, value: t.Any) -> None:
self.__attrs[name] = value