diff options
author | Raymond Hettinger <python@rcn.com> | 2004-05-19 08:20:33 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-05-19 08:20:33 (GMT) |
commit | 354433a59dd1701985ec8463ced25a967cade3c1 (patch) | |
tree | 9e342cc53199814f81b25daabcdb85da1851bd50 /Lib/compiler/symbols.py | |
parent | 285cfccecba911531b37b8a7dde45bf07d3cf9b0 (diff) | |
download | cpython-354433a59dd1701985ec8463ced25a967cade3c1.zip cpython-354433a59dd1701985ec8463ced25a967cade3c1.tar.gz cpython-354433a59dd1701985ec8463ced25a967cade3c1.tar.bz2 |
SF patch #872326: Generator expression implementation
(Code contributed by Jiwon Seo.)
The documentation portion of the patch is being re-worked and will be
checked-in soon. Likewise, PEP 289 will be updated to reflect Guido's
rationale for the design decisions on binding behavior (as described in
in his patch comments and in discussions on python-dev).
The test file, test_genexps.py, is written in doctest format and is
meant to exercise all aspects of the the patch. Further additions are
welcome from everyone. Please stress test this new feature as much as
possible before the alpha release.
Diffstat (limited to 'Lib/compiler/symbols.py')
-rw-r--r-- | Lib/compiler/symbols.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/compiler/symbols.py b/Lib/compiler/symbols.py index fa668f1..6843e19 100644 --- a/Lib/compiler/symbols.py +++ b/Lib/compiler/symbols.py @@ -179,6 +179,21 @@ class ModuleScope(Scope): class FunctionScope(Scope): pass +class GenExprScope(Scope): + __super_init = Scope.__init__ + + __counter = 1 + + def __init__(self, module, klass=None): + i = self.__counter + self.__counter += 1 + self.__super_init("generator expression<%d>"%i, module, klass) + self.add_param('[outmost-iterable]') + + def get_names(self): + keys = Scope.get_names() + return keys + class LambdaScope(FunctionScope): __super_init = Scope.__init__ @@ -220,6 +235,32 @@ class SymbolVisitor: self.visit(node.code, scope) self.handle_free_vars(scope, parent) + def visitGenExpr(self, node, parent): + scope = GenExprScope(self.module, self.klass); + if parent.nested or isinstance(parent, FunctionScope) \ + or isinstance(parent, GenExprScope): + scope.nested = 1 + + self.scopes[node] = scope + self.visit(node.code, scope) + + self.handle_free_vars(scope, parent) + + def visitGenExprInner(self, node, scope): + for genfor in node.quals: + self.visit(genfor, scope) + + self.visit(node.expr, scope) + + def visitGenExprFor(self, node, scope): + self.visit(node.assign, scope, 1) + self.visit(node.iter, scope) + for if_ in node.ifs: + self.visit(if_, scope) + + def visitGenExprIf(self, node, scope): + self.visit(node.test, scope) + def visitLambda(self, node, parent, assign=0): # Lambda is an expression, so it could appear in an expression # context where assign is passed. The transformer should catch |