# HG changeset patch # User Paul Boddie # Date 1243966679 -7200 # Node ID 6ba10a65eddd00ed0a9c0e092c598638555079ff # Parent 2197662b8603c6567e9a3898719395d88e5403ae Introduced a separate globals processing phase, recording all declared global names before attempting to resolve other names. Removed the Global class and warning about globals not being declared at the module level. Added tests of globals. diff -r 2197662b8603 -r 6ba10a65eddd micropython/inspect.py --- a/micropython/inspect.py Mon Jun 01 23:06:55 2009 +0200 +++ b/micropython/inspect.py Tue Jun 02 20:17:59 2009 +0200 @@ -128,11 +128,24 @@ "Process the given 'module'." self.astnode = self.module = module + + # First, visit module-level code, recording global names. + processed = self.dispatch(module) + # Then, for each function, detect and record globals declared in those + # functions. + + for node, namespaces in self.functions: + self.process_globals(node) + + # Then, visit each function, recording other names. + for node, namespaces in self.functions: self._visitFunctionBody(node, namespaces) + # Add references to other modules declared using the __all__ global. + if self.has_key("__all__"): all = self["__all__"] if isinstance(all, compiler.ast.List): @@ -141,6 +154,21 @@ return processed + def process_globals(self, node): + + """ + Within the given 'node', process global declarations, adjusting the + module namespace. + """ + + for n in node.getChildNodes(): + if isinstance(n, compiler.ast.Global): + for name in n.names: + if not self.has_key(name): + self[name] = None + else: + self.process_globals(n) + def vacuum(self): """ @@ -170,11 +198,6 @@ else: del self[name] - # Complain about globals not initialised at the module level. - - if isinstance(value, Global): - print "Warning: global %r in module %r not initialised at the module level." % (name, self.name) - # Remove unreferenced objects. if self.should_optimise_unused_objects(): @@ -639,10 +662,7 @@ if not ns.make_global(name): raise InspectError(ns.full_name(), node, "Name %r is global and local in %r" % (name, ns.full_name())) - # Record a global entry for the name in the module. - - if not self.has_key(name): - self[name] = Global() + # The name is recorded in an earlier process. def visitIf(self, node): for test, body in node.tests: @@ -786,13 +806,4 @@ visitYield = NOP -class Global: - - """ - A reference to an object assigned to a global from outside the module - top-level. - """ - - pass - # vim: tabstop=4 expandtab shiftwidth=4 diff -r 2197662b8603 -r 6ba10a65eddd tests/global_forward_declaration.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/global_forward_declaration.py Tue Jun 02 20:17:59 2009 +0200 @@ -0,0 +1,11 @@ +#!/usr/bin/env python + +def f(x): + return g(x) + +def g(x): + return x + +result_13579 = f(13579) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 2197662b8603 -r 6ba10a65eddd tests/global_implicit.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/global_implicit.py Tue Jun 02 20:17:59 2009 +0200 @@ -0,0 +1,13 @@ +#!/usr/bin/env python + +def f(x): + global y + y = x + +def g(): + return y + +f(24680) +result_24680 = g() + +# vim: tabstop=4 expandtab shiftwidth=4