Skip to content
Myr

Changed behavior of macro defaults to be frame bound

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

authored by Armin Ronacher · committed by Armin Ronacher · parent 5ab69f2 · Browse files · 4 files+27 added−24 removed
This commit

State

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

Diff stats

4 files+27 added−24 removed

  • tests:+5 added−7 removed
  • other:+22 added−17 removed

4 files+27 added−24 removed

modified:jinja2/compiler.py+20 added-10 removed
494 unmodified lines
495 frame.require_output_check = False496 frame.symbols.analyze_node(node)497 self.writeline('%s(%s):' % (self.func('macro'), ', '.join(args)), node)498 self.indent()499500 self.buffer(frame)501 self.enter_frame(frame)502503 for idx, arg in enumerate(node.args):504 self.writeline('if %s is missing:' % frame.symbols.ref(arg.name))505 self.indent()506 try:507 default = node.defaults[idx - len(node.args)]508 except IndexError:509 self.writeline('%s = undefined(%r, name=%r)' % (510 frame.symbols.ref(arg.name),511 'parameter %r was not provided' % arg.name,512 arg.name))513 else:514 self.writeline('%s = ' % frame.symbols.ref(arg.name))515 self.visit(default, frame)516 self.outdent()517518 self.blockvisit(node.body, frame)519 self.return_buffer_contents(frame)520 self.leave_frame(frame, with_python_scope=True)521 self.outdent()
5 unmodified lines
527 arg_tuple = ', '.join(repr(x.name) for x in macro_ref.node.args)528 name = getattr(macro_ref.node, 'name', None)529 if len(macro_ref.node.args) == 1:530 arg_tuple += ','514 self.write('Macro(environment, macro, %r, (%s), (' %515 (name, arg_tuple))516 for arg in macro_ref.node.defaults:517 self.visit(arg, frame)518 self.write(', ')519 self.write('), %r, %r, %r)' % (520 bool(macro_ref.accesses_kwargs),521 bool(macro_ref.accesses_varargs),522 bool(macro_ref.accesses_caller)523 ))531 self.write('Macro(environment, macro, %r, (%s), %r, %r, %r)' %532 (name, arg_tuple, macro_ref.accesses_kwargs,533 macro_ref.accesses_varargs, macro_ref.accesses_caller))534535 def position(self, node):536 """Return a human readable position for the node."""537 rv = 'line %d' % node.lineno
978 unmodified lines
modified:jinja2/runtime.py+2 added-7 removed
395 unmodified lines
396397class Macro(object):398 """Wraps a macro function."""399400 def __init__(self, environment, func, name, arguments, defaults,400 def __init__(self, environment, func, name, arguments,401 catch_kwargs, catch_varargs, caller):402 self._environment = environment403 self._func = func404 self._argument_count = len(arguments)405 self.name = name406 self.arguments = arguments407 self.defaults = defaults407 self.catch_kwargs = catch_kwargs408 self.catch_varargs = catch_varargs409 self.caller = caller410
10 unmodified lines
421 for idx, name in enumerate(self.arguments[len(arguments):]):422 try:423 value = kwargs.pop(name)424 except KeyError:426 try:427 value = self.defaults[idx - self._argument_count + off]428 except IndexError:429 value = self._environment.undefined(430 'parameter %r was not provided' % name, name=name)425 value = missing426 arguments.append(value)427428 # it's important that the order of these arguments does not change429 # if not also changed in the compiler's `function_scoping` method.
249 unmodified lines
modified:tests/test_core_tags.py-2 removed
306 unmodified lines
307 '{% macro foo(a, b) %}{% endmacro %}'308 '{% macro bar() %}{{ varargs }}{{ kwargs }}{% endmacro %}'309 '{% macro baz() %}{{ caller() }}{% endmacro %}')310 assert tmpl.module.foo.arguments == ('a', 'b')311 assert tmpl.module.foo.defaults == ()311 assert tmpl.module.foo.name == 'foo'312 assert not tmpl.module.foo.caller313 assert not tmpl.module.foo.catch_kwargs314 assert not tmpl.module.foo.catch_varargs315 assert tmpl.module.bar.arguments == ()317 assert tmpl.module.bar.defaults == ()316 assert not tmpl.module.bar.caller317 assert tmpl.module.bar.catch_kwargs318 assert tmpl.module.bar.catch_varargs319 assert tmpl.module.baz.caller
21 unmodified lines
modified:tests/test_regression.py+5 added-5 removed
320 unmodified lines
321 def test_callable_defaults(self):322 env = Environment()323 env.globals['get_int'] = lambda: 42324 t = env.from_string('''325 {% macro test(arg1=get_int()) %}326 {{ arg1 }}327 {% endmacro %}328 {{ test(1) }}|{{ test() }}325 {% macro test(a, b, c=get_int()) -%}326 {{ a + b + c }}327 {%- endmacro %}328 {{ test(1, 2) }}|{{ test(1, 2, 3) }}329 ''')330 assert t.render().strip() == '1|42'330 assert t.render().strip() == '45|6'