Skip to content
Myr

Disallow f(x, y=1, z) and similar nonsense

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Python rejects such function definitions because it doesn't make much sense to have arguments with no default values after arguments with default values. Jinja did allow them, but handled them improperly (associating the default value with the wrong argument). Due to how broken the current behavior is, it makes more sense to reject templates containing such defintions instead of trying to handle them properly. Both cases are going to break existing code containing such definitions, but besides the fact that possibly no such code exists it is better to fail with a clear error than to silently change the values of arguments. This fixes #364

authored by ThiefMaster · committed by ThiefMAster · parent c6c6ea1 · Browse files · 3 files+15 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+15 added−0 removed

  • other:+15 added−0 removed

3 files+15 added−0 removed

modified:jinja2/testsuite/core_tags.py+9 added
242 unmodified lines
243{% macro m(a, b, c='c', d='d') %}{{ a }}|{{ b }}|{{ c }}|{{ d }}{% endmacro %}244{{ m() }}|{{ m('a') }}|{{ m('a', 'b') }}|{{ m(1, 2, 3) }}''')245 assert tmpl.render() == '||c|d|a||c|d|a|b|c|d|1|2|3|d'246247 def test_arguments_defaults_nonsense(self):248 self.assert_raises(TemplateSyntaxError, self.env.from_string, '''\249{% macro m(a, b=1, c) %}a={{ a }}, b={{ b }}, c={{ c }}{% endmacro %}''')250251 def test_caller_defaults_nonsense(self):252 self.assert_raises(TemplateSyntaxError, self.env.from_string, '''\253{% macro a() %}{{ caller() }}{% endmacro %}254{% call(x, y=1, z) a() %}{% endcall %}''')255256 def test_varargs(self):257 tmpl = self.env.from_string('''\258{% macro test() %}{{ varargs|join('|') }}{% endmacro %}\259{{ test(1, 2, 3) }}''')
72 unmodified lines
modified:jinja2/parser.py+2 added
312 unmodified lines
313 arg = self.parse_assign_target(name_only=True)314 arg.set_ctx('param')315 if self.stream.skip_if('assign'):316 defaults.append(self.parse_expression())317 elif defaults:318 self.fail('non-default argument follows default argument')319 args.append(arg)320 self.stream.expect('rparen')321322 def parse_call_block(self):
577 unmodified lines
modified:CHANGES+4 added
19 unmodified lines
20 reloaded now.21- Implemented a block ``set`` tag.22- Default cache size was incrased to 400 from a low 50.23- Fixed ``is number`` test to accept long integers in all Python versions.24- Added a check for default arguments followed by non-default arguments. This25 change makes ``{% macro m(x, y=1, z) %}...{% endmacro %}`` a syntax error. The26 previous behavior for this code was broken anyway (resulting in the default27 value being applied to `y`).2829Version 2.7.330-------------31(bugfix release, released on June 6th 2014)
328 unmodified lines