diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-11-10 12:09:30 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-11-10 12:09:30 -0500 |
| commit | ca21a5353a4c88f3477a53f2f45cd9ca280b0479 (patch) | |
| tree | 45649d02b5b83ce56ff1a6ad702c955823ebbc6b /mako/pyparser.py | |
| parent | c8598ab628a0da0d55857196627753dad9849a39 (diff) | |
| download | external_python_mako-ca21a5353a4c88f3477a53f2f45cd9ca280b0479.tar.gz external_python_mako-ca21a5353a4c88f3477a53f2f45cd9ca280b0479.tar.bz2 external_python_mako-ca21a5353a4c88f3477a53f2f45cd9ca280b0479.zip | |
- The range of Python identifiers that
are considered "undefined", meaning they
are pulled from the context, has been
trimmed back to not include variables
declared inside of expressions (i.e. from
list comprehensions), as well as
in the argument list of lambdas. This
to better support the strict_undefined
feature.
Diffstat (limited to 'mako/pyparser.py')
| -rw-r--r-- | mako/pyparser.py | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/mako/pyparser.py b/mako/pyparser.py index b90278e..d011690 100644 --- a/mako/pyparser.py +++ b/mako/pyparser.py @@ -89,12 +89,19 @@ if _ast: for statement in node.body: self.visit(statement) + def visit_Lambda(self, node, *args): + self._visit_function(node, True) + def visit_FunctionDef(self, node): self._add_declared(node.name) + self._visit_function(node, False) + + def _visit_function(self, node, islambda): # push function state onto stack. dont log any # more identifiers as "declared" until outside of the function, # but keep logging identifiers as "undeclared". - # track argument names in each function header so they arent counted as "undeclared" + # track argument names in each function header + # so they arent counted as "undeclared" saved = {} inf = self.in_function self.in_function = True @@ -104,13 +111,16 @@ if _ast: saved[arg_id(arg)] = True else: self.local_ident_stack[arg_id(arg)] = True - for n in node.body: - self.visit(n) + if islambda: + self.visit(node.body) + else: + for n in node.body: + self.visit(n) self.in_function = inf for arg in node.args.args: if arg_id(arg) not in saved: del self.local_ident_stack[arg_id(arg)] - + def visit_For(self, node): # flip around visit self.visit(node.iter) @@ -138,7 +148,13 @@ if _ast: self._add_declared(name.asname) else: if name.name == '*': - raise exceptions.CompileException("'import *' is not supported, since all identifier names must be explicitly declared. Please use the form 'from <modulename> import <name1>, <name2>, ...' instead.", **self.exception_kwargs) + raise exceptions.CompileException( + "'import *' is not supported, since all " + "identifier names must be explicitly " + "declared. Please use the form 'from " + "<modulename> import <name1>, " + "<name2>, ...' instead.", + **self.exception_kwargs) self._add_declared(name.name) class FindTuple(_ast_util.NodeVisitor): @@ -197,12 +213,17 @@ else: self.visit(node.expr, *args) for n in node.nodes: self.visit(n, *args) + def visitLambda(self, node, *args): + self._visit_function(node, args) def visitFunction(self,node, *args): self._add_declared(node.name) + self._visit_function(node, args) + def _visit_function(self, node, args): # push function state onto stack. dont log any # more identifiers as "declared" until outside of the function, # but keep logging identifiers as "undeclared". - # track argument names in each function header so they arent counted as "undeclared" + # track argument names in each function header so + # they arent counted as "undeclared" saved = {} inf = self.in_function self.in_function = True @@ -217,6 +238,7 @@ else: for arg in node.argnames: if arg not in saved: del self.local_ident_stack[arg] + def visitFor(self, node, *args): # flip around visit self.visit(node.list, *args) |
