aboutsummaryrefslogtreecommitdiffstats
path: root/mako
diff options
context:
space:
mode:
Diffstat (limited to 'mako')
-rw-r--r--mako/__init__.py4
-rw-r--r--mako/_ast_util.py2
-rw-r--r--mako/ast.py79
-rw-r--r--mako/cache.py2
-rw-r--r--mako/codegen.py6
-rw-r--r--mako/compat.py1
-rw-r--r--mako/exceptions.py2
-rw-r--r--mako/ext/autohandler.py2
-rw-r--r--mako/ext/babelplugin.py20
-rw-r--r--mako/ext/preprocessors.py2
-rw-r--r--mako/ext/pygmentplugin.py2
-rw-r--r--mako/ext/turbogears.py2
-rw-r--r--mako/filters.py2
-rw-r--r--mako/lexer.py2
-rw-r--r--mako/lookup.py2
-rw-r--r--mako/parsetree.py14
-rw-r--r--mako/pygen.py2
-rw-r--r--mako/pyparser.py23
-rw-r--r--mako/runtime.py2
-rw-r--r--mako/template.py2
-rw-r--r--mako/util.py2
21 files changed, 108 insertions, 67 deletions
diff --git a/mako/__init__.py b/mako/__init__.py
index cb59f3a..aa99505 100644
--- a/mako/__init__.py
+++ b/mako/__init__.py
@@ -1,9 +1,9 @@
# mako/__init__.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
-__version__ = '0.9.1'
+__version__ = '0.9.2'
diff --git a/mako/_ast_util.py b/mako/_ast_util.py
index 3b0bd21..788b86a 100644
--- a/mako/_ast_util.py
+++ b/mako/_ast_util.py
@@ -1,5 +1,5 @@
# mako/_ast_util.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/ast.py b/mako/ast.py
index f9ae3e1..8f711b4 100644
--- a/mako/ast.py
+++ b/mako/ast.py
@@ -1,5 +1,5 @@
# mako/ast.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
@@ -112,38 +112,65 @@ class FunctionDecl(object):
if not allow_kwargs and self.kwargs:
raise exceptions.CompileException(
"'**%s' keyword argument not allowed here" %
- self.argnames[-1], **exception_kwargs)
+ self.kwargnames[-1], **exception_kwargs)
- def get_argument_expressions(self, include_defaults=True):
- """return the argument declarations of this FunctionDecl as a printable
- list."""
+ def get_argument_expressions(self, as_call=False):
+ """Return the argument declarations of this FunctionDecl as a printable
+ list.
+
+ By default the return value is appropriate for writing in a ``def``;
+ set `as_call` to true to build arguments to be passed to the function
+ instead (assuming locals with the same names as the arguments exist).
+ """
namedecls = []
- defaults = [d for d in self.defaults]
- kwargs = self.kwargs
- varargs = self.varargs
- argnames = [f for f in self.argnames]
- argnames.reverse()
- for arg in argnames:
- default = None
- if kwargs:
- arg = "**" + arg_stringname(arg)
- kwargs = False
- elif varargs:
- arg = "*" + arg_stringname(arg)
- varargs = False
+
+ # Build in reverse order, since defaults and slurpy args come last
+ argnames = self.argnames[::-1]
+ kwargnames = self.kwargnames[::-1]
+ defaults = self.defaults[::-1]
+ kwdefaults = self.kwdefaults[::-1]
+
+ # Named arguments
+ if self.kwargs:
+ namedecls.append("**" + kwargnames.pop(0))
+
+ for name in kwargnames:
+ # Keyword-only arguments must always be used by name, so even if
+ # this is a call, print out `foo=foo`
+ if as_call:
+ namedecls.append("%s=%s" % (name, name))
+ elif kwdefaults:
+ default = kwdefaults.pop(0)
+ if default is None:
+ # The AST always gives kwargs a default, since you can do
+ # `def foo(*, a=1, b, c=3)`
+ namedecls.append(name)
+ else:
+ namedecls.append("%s=%s" % (
+ name, pyparser.ExpressionGenerator(default).value()))
else:
- default = len(defaults) and defaults.pop() or None
- if include_defaults and default:
- namedecls.insert(0, "%s=%s" %
- (arg,
- pyparser.ExpressionGenerator(default).value()
- )
- )
+ namedecls.append(name)
+
+ # Positional arguments
+ if self.varargs:
+ namedecls.append("*" + argnames.pop(0))
+
+ for name in argnames:
+ if as_call or not defaults:
+ namedecls.append(name)
else:
- namedecls.insert(0, arg)
+ default = defaults.pop(0)
+ namedecls.append("%s=%s" % (
+ name, pyparser.ExpressionGenerator(default).value()))
+
+ namedecls.reverse()
return namedecls
+ @property
+ def allargnames(self):
+ return tuple(self.argnames) + tuple(self.kwargnames)
+
class FunctionArgs(FunctionDecl):
"""the argument portion of a function declaration"""
diff --git a/mako/cache.py b/mako/cache.py
index 4433040..b7de6b5 100644
--- a/mako/cache.py
+++ b/mako/cache.py
@@ -1,5 +1,5 @@
# mako/cache.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/codegen.py b/mako/codegen.py
index 2779a6d..045d03c 100644
--- a/mako/codegen.py
+++ b/mako/codegen.py
@@ -1,5 +1,5 @@
# mako/codegen.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
@@ -543,7 +543,7 @@ class _GenerateRenderMethod(object):
"""write a locally-available callable referencing a top-level def"""
funcname = node.funcname
namedecls = node.get_argument_expressions()
- nameargs = node.get_argument_expressions(include_defaults=False)
+ nameargs = node.get_argument_expressions(as_call=True)
if not self.in_def and (
len(self.identifiers.locally_assigned) > 0 or
@@ -864,7 +864,7 @@ class _GenerateRenderMethod(object):
if node.is_anonymous:
self.printer.writeline("%s()" % node.funcname)
else:
- nameargs = node.get_argument_expressions(include_defaults=False)
+ nameargs = node.get_argument_expressions(as_call=True)
nameargs += ['**pageargs']
self.printer.writeline("if 'parent' not in context._data or "
"not hasattr(context._data['parent'], '%s'):"
diff --git a/mako/compat.py b/mako/compat.py
index 31da8bd..c5ef84b 100644
--- a/mako/compat.py
+++ b/mako/compat.py
@@ -3,6 +3,7 @@ import time
py3k = sys.version_info >= (3, 0)
py33 = sys.version_info >= (3, 3)
+py2k = sys.version_info < (3,)
py26 = sys.version_info >= (2, 6)
py25 = sys.version_info >= (2, 5)
jython = sys.platform.startswith('java')
diff --git a/mako/exceptions.py b/mako/exceptions.py
index dcf81a6..b8f97ee 100644
--- a/mako/exceptions.py
+++ b/mako/exceptions.py
@@ -1,5 +1,5 @@
# mako/exceptions.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/ext/autohandler.py b/mako/ext/autohandler.py
index d56cbc1..99f651a 100644
--- a/mako/ext/autohandler.py
+++ b/mako/ext/autohandler.py
@@ -1,5 +1,5 @@
# ext/autohandler.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/ext/babelplugin.py b/mako/ext/babelplugin.py
index ba244bd..2ff0781 100644
--- a/mako/ext/babelplugin.py
+++ b/mako/ext/babelplugin.py
@@ -1,5 +1,5 @@
# ext/babelplugin.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
@@ -80,41 +80,37 @@ def extract_nodes(nodes, keywords, comment_tags, options):
child_nodes = node.nodes
elif isinstance(node, parsetree.ControlLine):
if node.isend:
- translator_comments = []
in_translator_comments = False
continue
code = node.text
elif isinstance(node, parsetree.Code):
- # <% and <%! blocks would provide their own translator comments
- translator_comments = []
in_translator_comments = False
-
code = node.code.code
elif isinstance(node, parsetree.Expression):
code = node.code.code
else:
- translator_comments = []
- in_translator_comments = False
continue
# Comments don't apply unless they immediately preceed the message
if translator_comments and \
translator_comments[-1][0] < node.lineno - 1:
translator_comments = []
- else:
- translator_comments = \
- [comment[1] for comment in translator_comments]
+
+ translator_strings = [comment[1] for comment in translator_comments]
if isinstance(code, compat.text_type):
code = code.encode('ascii', 'backslashreplace')
+ used_translator_comments = False
code = compat.byte_buffer(code)
for lineno, funcname, messages, python_translator_comments \
in extract_python(code, keywords, comment_tags, options):
yield (node.lineno + (lineno - 1), funcname, messages,
- translator_comments + python_translator_comments)
+ translator_strings + python_translator_comments)
+ used_translator_comments = True
- translator_comments = []
+ if used_translator_comments:
+ translator_comments = []
in_translator_comments = False
if child_nodes:
diff --git a/mako/ext/preprocessors.py b/mako/ext/preprocessors.py
index 5a15ff3..94569c5 100644
--- a/mako/ext/preprocessors.py
+++ b/mako/ext/preprocessors.py
@@ -1,5 +1,5 @@
# ext/preprocessors.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/ext/pygmentplugin.py b/mako/ext/pygmentplugin.py
index 040a25c..7e3cb42 100644
--- a/mako/ext/pygmentplugin.py
+++ b/mako/ext/pygmentplugin.py
@@ -1,5 +1,5 @@
# ext/pygmentplugin.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/ext/turbogears.py b/mako/ext/turbogears.py
index a4179d0..ebbee50 100644
--- a/mako/ext/turbogears.py
+++ b/mako/ext/turbogears.py
@@ -1,5 +1,5 @@
# ext/turbogears.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/filters.py b/mako/filters.py
index 77aaf75..369cbc3 100644
--- a/mako/filters.py
+++ b/mako/filters.py
@@ -1,5 +1,5 @@
# mako/filters.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/lexer.py b/mako/lexer.py
index 42b9ecd..dca88e1 100644
--- a/mako/lexer.py
+++ b/mako/lexer.py
@@ -1,5 +1,5 @@
# mako/lexer.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/lookup.py b/mako/lookup.py
index 3edd101..3ac21f4 100644
--- a/mako/lookup.py
+++ b/mako/lookup.py
@@ -1,5 +1,5 @@
# mako/lookup.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/parsetree.py b/mako/parsetree.py
index 1c04c26..0612070 100644
--- a/mako/parsetree.py
+++ b/mako/parsetree.py
@@ -1,5 +1,5 @@
# mako/parsetree.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
@@ -437,7 +437,7 @@ class DefTag(Tag):
return self.function_decl.get_argument_expressions(**kw)
def declared_identifiers(self):
- return self.function_decl.argnames
+ return self.function_decl.allargnames
def undeclared_identifiers(self):
res = []
@@ -451,7 +451,7 @@ class DefTag(Tag):
).union(
self.expression_undeclared_identifiers
).difference(
- self.function_decl.argnames
+ self.function_decl.allargnames
)
class BlockTag(Tag):
@@ -502,7 +502,7 @@ class BlockTag(Tag):
return self.body_decl.get_argument_expressions(**kw)
def declared_identifiers(self):
- return self.body_decl.argnames
+ return self.body_decl.allargnames
def undeclared_identifiers(self):
return (self.filter_args.\
@@ -524,7 +524,7 @@ class CallTag(Tag):
**self.exception_kwargs)
def declared_identifiers(self):
- return self.code.declared_identifiers.union(self.body_decl.argnames)
+ return self.code.declared_identifiers.union(self.body_decl.allargnames)
def undeclared_identifiers(self):
return self.code.undeclared_identifiers.\
@@ -554,7 +554,7 @@ class CallNamespaceTag(Tag):
**self.exception_kwargs)
def declared_identifiers(self):
- return self.code.declared_identifiers.union(self.body_decl.argnames)
+ return self.code.declared_identifiers.union(self.body_decl.allargnames)
def undeclared_identifiers(self):
return self.code.undeclared_identifiers.\
@@ -589,6 +589,6 @@ class PageTag(Tag):
**self.exception_kwargs)
def declared_identifiers(self):
- return self.body_decl.argnames
+ return self.body_decl.allargnames
diff --git a/mako/pygen.py b/mako/pygen.py
index ed7ccc7..cba9464 100644
--- a/mako/pygen.py
+++ b/mako/pygen.py
@@ -1,5 +1,5 @@
# mako/pygen.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/pyparser.py b/mako/pyparser.py
index 75301cc..aa2d882 100644
--- a/mako/pyparser.py
+++ b/mako/pyparser.py
@@ -1,5 +1,5 @@
# mako/pyparser.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
@@ -214,13 +214,25 @@ if _ast:
def visit_FunctionDef(self, node):
self.listener.funcname = node.name
+
argnames = [arg_id(arg) for arg in node.args.args]
if node.args.vararg:
argnames.append(arg_stringname(node.args.vararg))
+
+ if compat.py2k:
+ # kw-only args don't exist in Python 2
+ kwargnames = []
+ else:
+ kwargnames = [arg_id(arg) for arg in node.args.kwonlyargs]
if node.args.kwarg:
- argnames.append(arg_stringname(node.args.kwarg))
+ kwargnames.append(arg_stringname(node.args.kwarg))
self.listener.argnames = argnames
self.listener.defaults = node.args.defaults # ast
+ self.listener.kwargnames = kwargnames
+ if compat.py2k:
+ self.listener.kwdefaults = []
+ else:
+ self.listener.kwdefaults = node.args.kw_defaults
self.listener.varargs = node.args.vararg
self.listener.kwargs = node.args.kwarg
@@ -367,8 +379,13 @@ else:
def visitFunction(self, node, *args):
self.listener.funcname = node.name
- self.listener.argnames = node.argnames
+ self.listener.argnames = list(node.argnames)
+ if node.kwargs:
+ self.listener.kwargnames = [self.listener.argnames.pop()]
+ else:
+ self.listener.kwargnames = []
self.listener.defaults = node.defaults
+ self.listener.kwdefaults = []
self.listener.varargs = node.varargs
self.listener.kwargs = node.kwargs
diff --git a/mako/runtime.py b/mako/runtime.py
index f94c109..5a3489b 100644
--- a/mako/runtime.py
+++ b/mako/runtime.py
@@ -1,5 +1,5 @@
# mako/runtime.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/template.py b/mako/template.py
index 3a7b7f0..00783b7 100644
--- a/mako/template.py
+++ b/mako/template.py
@@ -1,5 +1,5 @@
# mako/template.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
diff --git a/mako/util.py b/mako/util.py
index 0a9e14e..0108109 100644
--- a/mako/util.py
+++ b/mako/util.py
@@ -1,5 +1,5 @@
# mako/util.py
-# Copyright (C) 2006-2013 the Mako authors and contributors <see AUTHORS file>
+# Copyright (C) 2006-2014 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php