diff --git a/CHANGES b/CHANGES index 02c1539b335652e89fec71e59a430925b5aad6f8..3af9dc1359e397861698f4ec839ef1c45c8d97cc 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,11 @@ +0.3.2 +- Calling a def from the top, via + template.get_def(...).render() now checks the + argument signature the same way as it did in + 0.2.5, so that TypeError is not raised. + reopen of [ticket:116] + + 0.3.1 - Fixed incorrect dir name in setup.py [ticket:129] diff --git a/mako/__init__.py b/mako/__init__.py index 73a2f331d8b6da51bc6db54bcbe1fc70ed51b9f2..7a2f176b16665013602930a9a74116be62b220aa 100644 --- a/mako/__init__.py +++ b/mako/__init__.py @@ -5,5 +5,5 @@ # the MIT License: http://www.opensource.org/licenses/mit-license.php -__version__ = '0.3.1' +__version__ = '0.3.2' diff --git a/mako/runtime.py b/mako/runtime.py index 8811fd6ca0abaf4ca55045bee3221c616e1ded87..863b4e75db3f3f293e3c78ecd4f2e42dd898b8eb 100644 --- a/mako/runtime.py +++ b/mako/runtime.py @@ -338,7 +338,7 @@ def _include_file(context, uri, calling_uri, **kwargs): template = _lookup_template(context, uri, calling_uri) (callable_, ctx) = _populate_self_namespace(context._clean_inheritance_tokens(), template) - callable_(ctx, **_kwargs_for_callable(callable_, context._orig, **kwargs)) + callable_(ctx, **_kwargs_for_include(callable_, context._orig, **kwargs)) def _inherit_from(context, uri, calling_uri): """called by the _inherit method in template modules to set up the inheritance chain at the start @@ -399,10 +399,25 @@ def _render(template, callable_, args, data, as_unicode=False): context = Context(buf, **data) context._outputting_as_unicode = as_unicode context._with_template = template - _render_context(template, callable_, context, *args, **data) + + _render_context(template, callable_, context, *args, **_kwargs_for_callable(callable_, data)) return context._pop_buffer().getvalue() -def _kwargs_for_callable(callable_, data, **kwargs): +def _kwargs_for_callable(callable_, data): + argspec = inspect.getargspec(callable_) + # for normal pages, **pageargs is usually present + if argspec[2]: + return data + + # for rendering defs from the top level, figure out the args + namedargs = argspec[0] + [v for v in argspec[1:3] if v is not None] + kwargs = {} + for arg in namedargs: + if arg != 'context' and arg in data and arg not in kwargs: + kwargs[arg] = data[arg] + return kwargs + +def _kwargs_for_include(callable_, data, **kwargs): argspec = inspect.getargspec(callable_) namedargs = argspec[0] + [v for v in argspec[1:3] if v is not None] for arg in namedargs: diff --git a/test/test_def.py b/test/test_def.py index 1f7a39aa8ee55f89fbb692d51788a1b82a72021f..fa5908e7adec3bc25b4f5506af4977eeb0c69dd6 100644 --- a/test/test_def.py +++ b/test/test_def.py @@ -82,6 +82,9 @@ class DefTest(TemplateTest): filters=flatten_result) self._do_test(template.get_def("body"), "this is the body", filters=flatten_result) + # test that args outside of the dict can be used + self._do_test(template.get_def("a"), "this is a", + filters=flatten_result, template_args={'q':5,'zq':'test'}) class ScopeTest(TemplateTest): """test scoping rules. The key is, enclosing scope always takes precedence over contextual scope."""