# HG changeset patch # User Paul Boddie # Date 1276376970 -7200 # Node ID 334af78f7931b7fcd05d714d00f44999d7e43325 # Parent 3851a63280ff19aaabfd2cca1fd58ae13478aa45 Fixed inspection of global usage after declarations in locals. Added list comprehension inspection to avoid scope errors. diff -r 3851a63280ff -r 334af78f7931 micropython/inspect.py --- a/micropython/inspect.py Sat Jun 12 21:39:53 2010 +0200 +++ b/micropython/inspect.py Sat Jun 12 23:09:30 2010 +0200 @@ -280,12 +280,15 @@ # Or store locally. - elif self.namespaces[-1].used_in_scope(name, "global"): - raise InspectError("Name %r already used as global." % name) - elif self.namespaces[-1].used_in_scope(name, "builtins"): - raise InspectError("Name %r already used as a built-in." % name) else: - self.namespaces[-1].set(name, obj, not self.in_loop) + locals = self.namespaces[-1] + + if locals.used_in_scope(name, "global") and not name in locals.globals: + raise InspectError("Name %r already used as global." % name) + elif locals.used_in_scope(name, "builtins"): + raise InspectError("Name %r already used as a built-in." % name) + else: + locals.set(name, obj, not self.in_loop) def store_lambda(self, obj): @@ -724,8 +727,8 @@ in_loop = self.in_loop self.in_loop = 1 + self.dispatch(node.list) self.dispatch(node.assign) - self.dispatch(node.list) # Enter the loop. # Propagate attribute usage to branches. @@ -884,9 +887,37 @@ visitList = OP - visitListComp = OP + def visitListComp(self, node): + for qual in node.quals: + self.dispatch(qual) + self.dispatch(node.expr) + + def visitListCompFor(self, node): + self.new_branchpoint() + + # Declare names which will be used by generated code. + + self.use_name("__iter__", node.list) + self.use_name("next") - visitListCompFor = NOP + in_loop = self.in_loop + self.in_loop = 1 + self.dispatch(node.list) + self.dispatch(node.assign) + + # Enter the loop. + # Propagate attribute usage to branches. + + self.new_branch() + + for if_ in node.ifs: + self.dispatch(if_) + + self.shelve_branch() + self.in_loop = in_loop + + self.merge_branches() + return None visitListCompIf = NOP