diff options
author | Armin Ronacher <armin.ronacher@active-4.com> | 2017-01-06 12:53:31 +0100 |
---|---|---|
committer | Armin Ronacher <armin.ronacher@active-4.com> | 2017-01-06 12:53:31 +0100 |
commit | d1a32e7502ebd0a0605dba19a49df4cddec601e1 (patch) | |
tree | d1bb769e8e808256a1dfa448e08a12e9d71fab45 | |
parent | a4dde4b0650e28bbdd7b212f4e03a991dbaff919 (diff) | |
download | jinja2-feature/no-locals.tar.gz |
Better error messages for some undefines. This fixes #575feature/no-locals
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | jinja2/environment.py | 14 |
2 files changed, 14 insertions, 2 deletions
@@ -18,6 +18,8 @@ Version 2.9 - Restored behavior of Cycler for Python 3 users. - Subtraction now follows the same behavior as other operators on undefined values. +- `map` and friends will now give better error messages if you forgot to + quote the parameter. Version 2.8.2 ------------- diff --git a/jinja2/environment.py b/jinja2/environment.py index cfa7ff1..0b5f957 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -88,6 +88,16 @@ def load_extensions(environment, extensions): return result +def fail_for_missing_callable(string, name): + msg = string % name + if isinstance(name, Undefined): + try: + name._fail_with_undefined_error() + except Exception as e: + msg = '%s (%s; did you forget to quote the callable name?)' % (msg, e) + raise TemplateRuntimeError(msg) + + def _environment_sanity_check(environment): """Perform a sanity check on the environment.""" assert issubclass(environment.undefined, Undefined), 'undefined must ' \ @@ -439,7 +449,7 @@ class Environment(object): """ func = self.filters.get(name) if func is None: - raise TemplateRuntimeError('no filter named %r' % name) + fail_for_missing_callable('no filter named %r', name) args = [value] + list(args or ()) if getattr(func, 'contextfilter', False): if context is None: @@ -464,7 +474,7 @@ class Environment(object): """ func = self.tests.get(name) if func is None: - raise TemplateRuntimeError('no test named %r' % name) + fail_for_missing_callable('no test named %r', name) return func(value, *(args or ()), **(kwargs or {})) @internalcode |