Skip to content
Myr

use exception chaining

Before adoption: this commit came in with the history the project adopted, so no landing records it.

fixes flake8-bugbear B904

authored by David Lord · committed by David Lord · parent 03146f5 · Browse files · 5 files+32 added−32 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+32 added−32 removed

  • other:+32 added−32 removed

5 files+32 added−32 removed

modified:src/jinja2/filters.py+4 added-4 removed
1348 unmodified lines
1349 try:1350 rv = list(value)1351 rv.reverse()1352 return rv1353 except TypeError:1354 raise FilterArgumentError("argument must be iterable")1353 except TypeError as e:1354 raise FilterArgumentError("argument must be iterable") from e135513561357@pass_environment1358def do_attr(
331 unmodified lines
1690 try:1691 name = args[0]1692 args = args[1:]1693 except LookupError:1694 raise FilterArgumentError("map requires a filter argument")1694 raise FilterArgumentError("map requires a filter argument") from None16951696 def func(item: t.Any) -> t.Any:1697 return context.environment.call_filter(1698 name, item, args, kwargs, context=context
12 unmodified lines
1711 if lookup_attr:1712 try:1713 attr = args[0]1714 except LookupError:1715 raise FilterArgumentError("Missing parameter for attribute name")1715 raise FilterArgumentError("Missing parameter for attribute name") from None17161717 transfunc = make_attrgetter(context.environment, attr)1718 off = 11719 else:
105 unmodified lines
modified:src/jinja2/lexer.py+1 added-1 removed
653 unmodified lines
654 .decode("unicode-escape")655 )656 except Exception as e:657 msg = str(e).split(":")[-1].strip()658 raise TemplateSyntaxError(msg, lineno, name, filename)658 raise TemplateSyntaxError(msg, lineno, name, filename) from e659 elif token == TOKEN_INTEGER:660 value = int(value_str.replace("_", ""), 0)661 elif token == TOKEN_FLOAT:662 # remove all "_" first to support more Python versions
207 unmodified lines
modified:src/jinja2/loaders.py+10 added-10 removed
334 unmodified lines
335 else:336 # Package is a zip file.337 try:338 source = self._loader.get_data(p) # type: ignore339 except OSError:340 raise TemplateNotFound(template)339 except OSError as e:340 raise TemplateNotFound(template) from e341342 # Could use the zip's mtime for all template mtimes, but343 # would need to safely reload the module if it's out of344 # date, so just report it as always current.
130 unmodified lines
475 def get_loader(self, template: str) -> t.Tuple[BaseLoader, str]:476 try:477 prefix, name = template.split(self.delimiter, 1)478 loader = self.mapping[prefix]479 except (ValueError, KeyError):480 raise TemplateNotFound(template)479 except (ValueError, KeyError) as e:480 raise TemplateNotFound(template) from e481 return loader, name482483 def get_source(484 self, environment: "Environment", template: str485 ) -> t.Tuple[str, t.Optional[str], t.Optional[t.Callable[[], bool]]]:486 loader, name = self.get_loader(template)487 try:488 return loader.get_source(environment, name)489 except TemplateNotFound:489 except TemplateNotFound as e:490 # re-raise the exception with the correct filename here.491 # (the one that includes the prefix)492 raise TemplateNotFound(template)492 raise TemplateNotFound(template) from e493494 @internalcode495 def load(496 self,
3 unmodified lines
500 ) -> "Template":501 loader, local_name = self.get_loader(name)502 try:503 return loader.load(environment, local_name, globals)504 except TemplateNotFound:504 except TemplateNotFound as e:505 # re-raise the exception with the correct filename here.506 # (the one that includes the prefix)507 raise TemplateNotFound(name)507 raise TemplateNotFound(name) from e508509 def list_templates(self) -> t.List[str]:510 result = []511 for prefix, loader in self.mapping.items():
114 unmodified lines
626627 if mod is None:628 try:629 mod = __import__(module, None, None, ["root"])630 except ImportError:631 raise TemplateNotFound(name)630 except ImportError as e:631 raise TemplateNotFound(name) from e632633 # remove the entry from sys.modules, we only want the attribute634 # on the module object we have stored on the loader.635 sys.modules.pop(module, None)
7 unmodified lines
modified:src/jinja2/nodes.py+16 added-16 removed
505 unmodified lines
506 raise Impossible()507 f = _binop_to_func[self.operator]508 try:509 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 e512513514class UnaryExpr(Expr):515 """Baseclass for all unary expressions."""
14 unmodified lines
530 raise Impossible()531 f = _uaop_to_func[self.operator]532 try:533 return f(self.node.as_const(eval_ctx))534 except Exception:535 raise Impossible()534 except Exception as e:535 raise Impossible() from e536537538class Name(Expr):539 """Looks up a name or stores a value in a name.
182 unmodified lines
722723 if node.dyn_args is not None:724 try:725 args.extend(node.dyn_args.as_const(eval_ctx))726 except Exception:727 raise Impossible()726 except Exception as e:727 raise Impossible() from e728729 if node.dyn_kwargs is not None:730 try:731 kwargs.update(node.dyn_kwargs.as_const(eval_ctx))732 except Exception:733 raise Impossible()732 except Exception as e:733 raise Impossible() from e734735 return args, kwargs736737
40 unmodified lines
778 args.insert(0, eval_ctx.environment)779780 try:781 return func(*args, **kwargs)782 except Exception:783 raise Impossible()782 except Exception as e:783 raise Impossible() from e784785786class Filter(_FilterTestCommon):787 """Apply a filter to an expression. ``name`` is the name of the
58 unmodified lines
846 try:847 return eval_ctx.environment.getitem(848 self.node.as_const(eval_ctx), self.arg.as_const(eval_ctx)849 )850 except Exception:851 raise Impossible()850 except Exception as e:851 raise Impossible() from e852853854class Getattr(Expr):855 """Get an attribute or item from an expression that is a ascii-only
12 unmodified lines
868 eval_ctx = get_eval_context(self, eval_ctx)869870 try:871 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 e874875876class Slice(Expr):877 """Represents a slice object. This must only be used as argument for
50 unmodified lines
928 if not result:929 return False930931 value = new_value932 except Exception:933 raise Impossible()932 except Exception as e:933 raise Impossible() from e934935 return result936937
268 unmodified lines
modified:src/jinja2/utils.py+1 added-1 removed
822 unmodified lines
823 return object.__getattribute__(self, name)824 try:825 return self.__attrs[name]826 except KeyError:827 raise AttributeError(name)827 raise AttributeError(name) from None828829 def __setitem__(self, name: str, value: t.Any) -> None:830 self.__attrs[name] = value831
23 unmodified lines