Don't make cond_expr_undefined configurable
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Adrian Moennich · committed by Adrian Moennich · parent c4d3996 · Browse files · 6 files·+16 added−34 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+16 added−34 removed
- tests:+1 added−9 removed
- other:+15 added−25 removed
Provenance
- Commit
4d331a0- Tree
0209e7b- Author
Adrian Moennich <adrian@planetcoding.net>- Committer
Adrian Moennich <adrian@planetcoding.net>- Parent
c4d3996
6 files changed+16 added−34 removed
modified:docs/templates.rst+2 added-2 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 (using the environment's undefined object1359-specified in ``cond_expr_undefined``):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+3 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 expressions612- self.writeline('cond_expr_undefined = environment.cond_expr_undefined')614+ 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 local1115 unmodified linesmodified:jinja2/environment.py+4 added-17 removed
99 unmodified lines100 def _environment_sanity_check(environment):101 """Perform a sanity check on the environment."""102 assert issubclass(environment.undefined, Undefined), 'undefined must ' \103 'be a subclass of undefined because filters depend on it.'104- assert issubclass(environment.cond_expr_undefined, Undefined), \105- 'cond_expr_undefined must be a subclass of undefined because filters ' \106- 'depend on it.'104 assert environment.block_start_string != \105 environment.variable_start_string != \106 environment.comment_start_string, 'block, variable and comment ' \107 'start strings must be different'74 unmodified lines182 `undefined`183 :class:`Undefined` or a subclass of it that is used to represent184 undefined values in the template.185 189- `cond_expr_undefined`190- :class:`Undefined` or a subclass of it that is used to represent191- undefined values in the template for conditional expressions that192- have no ``else`` part. This is usually useful when using a more193- strict undefined class without wanting to break the short version194- of conditional expressions.195-186 `finalize`187 A callable that can be used to process the result of a variable188 expression before it is output. For example one can convert189 ``None`` implicitly into an empty string here.92 unmodified lines282 loader=None,283 cache_size=400,284 auto_reload=True,285 bytecode_cache=None,296- enable_async=False,297- cond_expr_undefined=Undefined):286+ enable_async=False):287 # !!Important notice!!288 # The constructor accepts quite a few arguments that should be289 # passed by keyword rather than position. However it's important to290 # not change the order of arguments because it's used at least19 unmodified lines310 self.keep_trailing_newline = keep_trailing_newline311 312 # runtime information313 self.undefined = undefined325- self.cond_expr_undefined = cond_expr_undefined314 self.optimized = optimized315 self.finalize = finalize316 self.autoescape = autoescape317 44 unmodified lines362 trim_blocks=missing, lstrip_blocks=missing,363 extensions=missing, optimized=missing,364 undefined=missing, finalize=missing, autoescape=missing,365 loader=missing, cache_size=missing, auto_reload=missing,378- bytecode_cache=missing, cond_expr_undefined=missing):366+ bytecode_cache=missing):367 """Create a new overlay environment that shares all the data with the368 current environment except for cache and the overridden attributes.369 Extensions cannot be removed for an overlayed environment. An overlayed370 environment automatically gets all the extensions of the environment it564 unmodified lines935 optimized=True,936 undefined=Undefined,937 finalize=None,938 autoescape=False,951- enable_async=False,952- cond_expr_undefined=Undefined):939+ enable_async=False):940 env = get_spontaneous_environment(941 block_start_string, block_end_string, variable_start_string,942 variable_end_string, comment_start_string, comment_end_string,943 line_statement_prefix, line_comment_prefix, trim_blocks,944 lstrip_blocks, newline_sequence, keep_trailing_newline,945 frozenset(extensions), optimized, undefined, finalize, autoescape,959- None, 0, False, None, enable_async, cond_expr_undefined)946+ None, 0, False, None, enable_async)947 return env.from_string(source, template_class=cls)948 949 @classmethod950 def from_code(cls, environment, code, globals, uptodate=None):328 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-9 removed
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 371- def test_cond_expr_undefined(self):372- env = Environment(cond_expr_undefined=StrictUndefined)373- env2 = Environment(undefined=StrictUndefined)374- pytest.raises(UndefinedError,375- env.from_string('{{ "foo" if false }}').render)376- assert env.from_string('{{ ("foo" if false) is defined }}').render() == 'False'377- assert env2.from_string('{{ "foo" if false }}').render() == ''378- assert env2.from_string('{{ ("foo" if false) is defined }}').render() == 'False'379-372 def test_indexing_gives_undefined(self):373 t = Template("{{ var[42].foo }}")374 pytest.raises(UndefinedError, t.render, var=0)375 40 unmodified linesmodified:CHANGES.rst+5 added-4 removed
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-- Make the undefined object returned by ``{{ 'foo' if bar }}`` configurable,43- and default to standard ``Undefined`` regardless of what is specified as44- the environment's ``undefined`` object as doing so is not compatible with45- ``StrictUndefined``. :issue:`710`, :pr:`1079`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