Skip to content

Commit a99f8c1

Browse files
committed
use exception chaining
fixes flake8-bugbear B904
1 parent 03146f5 commit a99f8c1

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

src/jinja2/filters.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,8 +1350,8 @@ def do_reverse(value: t.Union[str, t.Iterable[V]]) -> t.Union[str, t.Iterable[V]
13501350
rv = list(value)
13511351
rv.reverse()
13521352
return rv
1353-
except TypeError:
1354-
raise FilterArgumentError("argument must be iterable")
1353+
except TypeError as e:
1354+
raise FilterArgumentError("argument must be iterable") from e
13551355

13561356

13571357
@pass_environment
@@ -1691,7 +1691,7 @@ def prepare_map(
16911691
name = args[0]
16921692
args = args[1:]
16931693
except LookupError:
1694-
raise FilterArgumentError("map requires a filter argument")
1694+
raise FilterArgumentError("map requires a filter argument") from None
16951695

16961696
def func(item: t.Any) -> t.Any:
16971697
return context.environment.call_filter(
@@ -1712,7 +1712,7 @@ def prepare_select_or_reject(
17121712
try:
17131713
attr = args[0]
17141714
except LookupError:
1715-
raise FilterArgumentError("Missing parameter for attribute name")
1715+
raise FilterArgumentError("Missing parameter for attribute name") from None
17161716

17171717
transfunc = make_attrgetter(context.environment, attr)
17181718
off = 1

src/jinja2/lexer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ def wrap(
655655
)
656656
except Exception as e:
657657
msg = str(e).split(":")[-1].strip()
658-
raise TemplateSyntaxError(msg, lineno, name, filename)
658+
raise TemplateSyntaxError(msg, lineno, name, filename) from e
659659
elif token == TOKEN_INTEGER:
660660
value = int(value_str.replace("_", ""), 0)
661661
elif token == TOKEN_FLOAT:

src/jinja2/loaders.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ def up_to_date() -> bool:
336336
# Package is a zip file.
337337
try:
338338
source = self._loader.get_data(p) # type: ignore
339-
except OSError:
340-
raise TemplateNotFound(template)
339+
except OSError as e:
340+
raise TemplateNotFound(template) from e
341341

342342
# Could use the zip's mtime for all template mtimes, but
343343
# would need to safely reload the module if it's out of
@@ -476,8 +476,8 @@ def get_loader(self, template: str) -> t.Tuple[BaseLoader, str]:
476476
try:
477477
prefix, name = template.split(self.delimiter, 1)
478478
loader = self.mapping[prefix]
479-
except (ValueError, KeyError):
480-
raise TemplateNotFound(template)
479+
except (ValueError, KeyError) as e:
480+
raise TemplateNotFound(template) from e
481481
return loader, name
482482

483483
def get_source(
@@ -486,10 +486,10 @@ def get_source(
486486
loader, name = self.get_loader(template)
487487
try:
488488
return loader.get_source(environment, name)
489-
except TemplateNotFound:
489+
except TemplateNotFound as e:
490490
# re-raise the exception with the correct filename here.
491491
# (the one that includes the prefix)
492-
raise TemplateNotFound(template)
492+
raise TemplateNotFound(template) from e
493493

494494
@internalcode
495495
def load(
@@ -501,10 +501,10 @@ def load(
501501
loader, local_name = self.get_loader(name)
502502
try:
503503
return loader.load(environment, local_name, globals)
504-
except TemplateNotFound:
504+
except TemplateNotFound as e:
505505
# re-raise the exception with the correct filename here.
506506
# (the one that includes the prefix)
507-
raise TemplateNotFound(name)
507+
raise TemplateNotFound(name) from e
508508

509509
def list_templates(self) -> t.List[str]:
510510
result = []
@@ -627,8 +627,8 @@ def load(
627627
if mod is None:
628628
try:
629629
mod = __import__(module, None, None, ["root"])
630-
except ImportError:
631-
raise TemplateNotFound(name)
630+
except ImportError as e:
631+
raise TemplateNotFound(name) from e
632632

633633
# remove the entry from sys.modules, we only want the attribute
634634
# on the module object we have stored on the loader.

src/jinja2/nodes.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,8 @@ def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
507507
f = _binop_to_func[self.operator]
508508
try:
509509
return f(self.left.as_const(eval_ctx), self.right.as_const(eval_ctx))
510-
except Exception:
511-
raise Impossible()
510+
except Exception as e:
511+
raise Impossible() from e
512512

513513

514514
class UnaryExpr(Expr):
@@ -531,8 +531,8 @@ def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
531531
f = _uaop_to_func[self.operator]
532532
try:
533533
return f(self.node.as_const(eval_ctx))
534-
except Exception:
535-
raise Impossible()
534+
except Exception as e:
535+
raise Impossible() from e
536536

537537

538538
class Name(Expr):
@@ -723,14 +723,14 @@ def args_as_const(
723723
if node.dyn_args is not None:
724724
try:
725725
args.extend(node.dyn_args.as_const(eval_ctx))
726-
except Exception:
727-
raise Impossible()
726+
except Exception as e:
727+
raise Impossible() from e
728728

729729
if node.dyn_kwargs is not None:
730730
try:
731731
kwargs.update(node.dyn_kwargs.as_const(eval_ctx))
732-
except Exception:
733-
raise Impossible()
732+
except Exception as e:
733+
raise Impossible() from e
734734

735735
return args, kwargs
736736

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

780780
try:
781781
return func(*args, **kwargs)
782-
except Exception:
783-
raise Impossible()
782+
except Exception as e:
783+
raise Impossible() from e
784784

785785

786786
class Filter(_FilterTestCommon):
@@ -847,8 +847,8 @@ def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
847847
return eval_ctx.environment.getitem(
848848
self.node.as_const(eval_ctx), self.arg.as_const(eval_ctx)
849849
)
850-
except Exception:
851-
raise Impossible()
850+
except Exception as e:
851+
raise Impossible() from e
852852

853853

854854
class Getattr(Expr):
@@ -869,8 +869,8 @@ def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
869869

870870
try:
871871
return eval_ctx.environment.getattr(self.node.as_const(eval_ctx), self.attr)
872-
except Exception:
873-
raise Impossible()
872+
except Exception as e:
873+
raise Impossible() from e
874874

875875

876876
class Slice(Expr):
@@ -929,8 +929,8 @@ def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
929929
return False
930930

931931
value = new_value
932-
except Exception:
933-
raise Impossible()
932+
except Exception as e:
933+
raise Impossible() from e
934934

935935
return result
936936

src/jinja2/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ def __getattribute__(self, name: str) -> t.Any:
824824
try:
825825
return self.__attrs[name]
826826
except KeyError:
827-
raise AttributeError(name)
827+
raise AttributeError(name) from None
828828

829829
def __setitem__(self, name: str, value: t.Any) -> None:
830830
self.__attrs[name] = value

0 commit comments

Comments
 (0)