Skip to content
Myr

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

5 files+13 added−3 removed

modified:docs/templates.rst+2 added-1 removed
1353 unmodified lines
1354The general syntax is ``<do something> if <something is true> else <do1355something else>``.13561357The `else` part is optional. If not provided, the else block implicitly1358evaluates into an undefined object:1358evaluates into an :class:`Undefined` object (regardless of what ``undefined``1359in the environment is set to):13601361.. sourcecode:: jinja13621363 {{ "[{}]".format(page.title) if page.title }}
399 unmodified lines
modified:jinja2/compiler.py+4 added-1 removed
607 unmodified lines
608 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')616617 def push_parameter_definitions(self, frame):618 """Pushes all parameter targets from the given frame into a local
1006 unmodified lines
1625 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)))16321633 self.write('(')
100 unmodified lines
modified:jinja2/runtime.py+1 added-1 removed
25 unmodified lines
26# 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']3132#: the name of the function that is used to convert something into33#: a string. We can just use the text type here.34to_string = text_type
803 unmodified lines
modified:tests/test_api.py+1 added
365 unmodified lines
366 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() == ''371372 def test_indexing_gives_undefined(self):373 t = Template("{{ var[42].foo }}")374 pytest.raises(UndefinedError, t.render, var=0)
41 unmodified lines
modified:CHANGES.rst+5 added
37 unmodified lines
38 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`474849Version 2.10.350--------------
629 unmodified lines