Skip to content

Commit

Permalink
Merge pull request #1503 from pallets/except-chain
Browse files Browse the repository at this point in the history
use exception chaining
  • Loading branch information
davidism authored Oct 4, 2021
2 parents 03146f5 + a99f8c1 commit 362cdcf
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/jinja2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/jinja2/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ def wrap(
)
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:
Expand Down
20 changes: 10 additions & 10 deletions src/jinja2/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ def up_to_date() -> bool:
# 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
Expand Down Expand Up @@ -476,8 +476,8 @@ def get_loader(self, template: str) -> t.Tuple[BaseLoader, str]:
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(
Expand All @@ -486,10 +486,10 @@ def get_source(
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(
Expand All @@ -501,10 +501,10 @@ def load(
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 = []
Expand Down Expand Up @@ -627,8 +627,8 @@ def load(
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.
Expand Down
32 changes: 16 additions & 16 deletions src/jinja2/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
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):
Expand All @@ -531,8 +531,8 @@ def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -779,8 +779,8 @@ def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:

try:
return func(*args, **kwargs)
except Exception:
raise Impossible()
except Exception as e:
raise Impossible() from e


class Filter(_FilterTestCommon):
Expand Down Expand Up @@ -847,8 +847,8 @@ def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
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):
Expand All @@ -869,8 +869,8 @@ def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:

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):
Expand Down Expand Up @@ -929,8 +929,8 @@ def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
return False

value = new_value
except Exception:
raise Impossible()
except Exception as e:
raise Impossible() from e

return result

Expand Down
2 changes: 1 addition & 1 deletion src/jinja2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ def __getattribute__(self, name: str) -> t.Any:
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
Expand Down

0 comments on commit 362cdcf

Please sign in to comment.