Merge pull request #1079 from ThiefMaster/cond-expr-undefined
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Use separate undefined object for else-less conditional expressions
authored by David Lord · committed by GitHub · parents 464e493, 4d331a0 · Browse files · 5 files·+13 added−3 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+13 added−3 removed
- tests:+1 added−0 removed
- other:+12 added−3 removed
Provenance
- Commit
8146e45- Tree
0209e7b- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
464e493- Parent 2
4d331a0
5 files changed+13 added−3 removed
modified:docs/templates.rst+2 added-1 removed
1353 unmodified lines1354 The general syntax is ``<do something> if <something is true> else <do1355 something else>``.1356 1357 The `else` part is optional. If not provided, the else block implicitly1358-evaluates into an undefined object:1358+evaluates into an :class:`Undefined` object (regardless of what ``undefined``1359+in the environment is set to):1360 1361 .. sourcecode:: jinja1362 1363 {{ "[{}]".format(page.title) if page.title }}399 unmodified linesmodified:jinja2/compiler.py+4 added-1 removed
607 unmodified lines608 through a dead branch.609 """610 self.writeline('resolve = context.resolve_or_missing')611 self.writeline('undefined = environment.undefined')612+ # always use the standard Undefined class for the implicit else of613+ # conditional expressions614+ self.writeline('cond_expr_undefined = Undefined')615 self.writeline('if 0: yield None')616 617 def push_parameter_definitions(self, frame):618 """Pushes all parameter targets from the given frame into a local1006 unmodified lines1625 def visit_CondExpr(self, node, frame):1626 def write_expr2():1627 if node.expr2 is not None:1628 return self.visit(node.expr2, frame)1626- self.write('undefined(%r)' % ('the inline if-'1629+ self.write('cond_expr_undefined(%r)' % ('the inline if-'1630 'expression on %s evaluated to false and '1631 'no else section was defined.' % self.position(node)))1632 1633 self.write('(')100 unmodified linesmodified:jinja2/runtime.py+1 added-1 removed
25 unmodified lines26 # these variables are exported to the template runtime27 __all__ = ['LoopContext', 'TemplateReference', 'Macro', 'Markup',28 'TemplateRuntimeError', 'missing', 'concat', 'escape',29 'markup_join', 'unicode_join', 'to_string', 'identity',30- 'TemplateNotFound', 'Namespace']30+ 'TemplateNotFound', 'Namespace', 'Undefined']31 32 #: the name of the function that is used to convert something into33 #: a string. We can just use the text type here.34 to_string = text_type803 unmodified linesmodified:tests/test_api.py+1 added
365 unmodified lines366 assert env.from_string('{{ missing|default("default", true) }}')\367 .render() == 'default'368 with pytest.raises(AttributeError):369 getattr(StrictUndefined, '__slots__')370+ assert env.from_string('{{ "foo" if false }}').render() == ''371 372 def test_indexing_gives_undefined(self):373 t = Template("{{ var[42].foo }}")374 pytest.raises(UndefinedError, t.render, var=0)41 unmodified linesmodified:CHANGES.rst+5 added
37 unmodified lines38 output of expressions (constant or not), not static template data.39 :issue:`63`40 - When providing multiple paths to ``FileSystemLoader``, a template41 can have the same name as a directory. :issue:`821`42+- Always return a standard :class:`Undefined` when omitting the else clause43+ in a ``{{ 'foo' if bar }}`` expression, regardless of what is specified as44+ the environment's ``undefined`` class, as omitting the else clause is a45+ valid shortcut and not actually an undefined variable that shauld cause46+ an error (in case of :class:`StrictUndefined`). :issue:`710`, :pr:`1079`47 48 49 Version 2.10.350 --------------629 unmodified lines