Merge pull request #1503 from pallets/except-chain
Before adoption: this commit came in with the history the project adopted, so no landing records it.
use exception chaining
authored by David Lord · committed by GitHub · parents 03146f5, a99f8c1 · 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
Provenance
- Commit
362cdcf- Tree
92659b8- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
03146f5- Parent 2
a99f8c1
5 files changed+32 added−32 removed
modified:src/jinja2/filters.py+4 added-4 removed
1348 unmodified lines1349 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 e1355 1356 1357 @pass_environment1358 def do_attr(331 unmodified lines1690 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 None1695 1696 def func(item: t.Any) -> t.Any:1697 return context.environment.call_filter(1698 name, item, args, kwargs, context=context12 unmodified lines1711 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 None1716 1717 transfunc = make_attrgetter(context.environment, attr)1718 off = 11719 else:105 unmodified linesmodified:src/jinja2/lexer.py+1 added-1 removed
653 unmodified lines654 .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 versions207 unmodified linesmodified:src/jinja2/loaders.py+10 added-10 removed
334 unmodified lines335 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 e341 342 # 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 lines475 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, name482 483 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 e493 494 @internalcode495 def load(496 self,3 unmodified lines500 ) -> "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 e508 509 def list_templates(self) -> t.List[str]:510 result = []511 for prefix, loader in self.mapping.items():114 unmodified lines626 627 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 e632 633 # 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 linesmodified:src/jinja2/nodes.py+16 added-16 removed
505 unmodified lines506 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 e512 513 514 class UnaryExpr(Expr):515 """Baseclass for all unary expressions."""14 unmodified lines530 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 e536 537 538 class Name(Expr):539 """Looks up a name or stores a value in a name.182 unmodified lines722 723 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 e728 729 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 e734 735 return args, kwargs736 737 40 unmodified lines778 args.insert(0, eval_ctx.environment)779 780 try:781 return func(*args, **kwargs)782- except Exception:783- raise Impossible()782+ except Exception as e:783+ raise Impossible() from e784 785 786 class Filter(_FilterTestCommon):787 """Apply a filter to an expression. ``name`` is the name of the58 unmodified lines846 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 e852 853 854 class Getattr(Expr):855 """Get an attribute or item from an expression that is a ascii-only12 unmodified lines868 eval_ctx = get_eval_context(self, eval_ctx)869 870 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 e874 875 876 class Slice(Expr):877 """Represents a slice object. This must only be used as argument for50 unmodified lines928 if not result:929 return False930 931 value = new_value932- except Exception:933- raise Impossible()932+ except Exception as e:933+ raise Impossible() from e934 935 return result936 937 268 unmodified linesmodified:src/jinja2/utils.py+1 added-1 removed
822 unmodified lines823 return object.__getattribute__(self, name)824 try:825 return self.__attrs[name]826 except KeyError:827- raise AttributeError(name)827+ raise AttributeError(name) from None828 829 def __setitem__(self, name: str, value: t.Any) -> None:830 self.__attrs[name] = value831 23 unmodified lines