1.1 --- a/micropython/inspect.py Mon Jun 01 23:06:55 2009 +0200
1.2 +++ b/micropython/inspect.py Tue Jun 02 20:17:59 2009 +0200
1.3 @@ -128,11 +128,24 @@
1.4 "Process the given 'module'."
1.5
1.6 self.astnode = self.module = module
1.7 +
1.8 + # First, visit module-level code, recording global names.
1.9 +
1.10 processed = self.dispatch(module)
1.11
1.12 + # Then, for each function, detect and record globals declared in those
1.13 + # functions.
1.14 +
1.15 + for node, namespaces in self.functions:
1.16 + self.process_globals(node)
1.17 +
1.18 + # Then, visit each function, recording other names.
1.19 +
1.20 for node, namespaces in self.functions:
1.21 self._visitFunctionBody(node, namespaces)
1.22
1.23 + # Add references to other modules declared using the __all__ global.
1.24 +
1.25 if self.has_key("__all__"):
1.26 all = self["__all__"]
1.27 if isinstance(all, compiler.ast.List):
1.28 @@ -141,6 +154,21 @@
1.29
1.30 return processed
1.31
1.32 + def process_globals(self, node):
1.33 +
1.34 + """
1.35 + Within the given 'node', process global declarations, adjusting the
1.36 + module namespace.
1.37 + """
1.38 +
1.39 + for n in node.getChildNodes():
1.40 + if isinstance(n, compiler.ast.Global):
1.41 + for name in n.names:
1.42 + if not self.has_key(name):
1.43 + self[name] = None
1.44 + else:
1.45 + self.process_globals(n)
1.46 +
1.47 def vacuum(self):
1.48
1.49 """
1.50 @@ -170,11 +198,6 @@
1.51 else:
1.52 del self[name]
1.53
1.54 - # Complain about globals not initialised at the module level.
1.55 -
1.56 - if isinstance(value, Global):
1.57 - print "Warning: global %r in module %r not initialised at the module level." % (name, self.name)
1.58 -
1.59 # Remove unreferenced objects.
1.60
1.61 if self.should_optimise_unused_objects():
1.62 @@ -639,10 +662,7 @@
1.63 if not ns.make_global(name):
1.64 raise InspectError(ns.full_name(), node, "Name %r is global and local in %r" % (name, ns.full_name()))
1.65
1.66 - # Record a global entry for the name in the module.
1.67 -
1.68 - if not self.has_key(name):
1.69 - self[name] = Global()
1.70 + # The name is recorded in an earlier process.
1.71
1.72 def visitIf(self, node):
1.73 for test, body in node.tests:
1.74 @@ -786,13 +806,4 @@
1.75
1.76 visitYield = NOP
1.77
1.78 -class Global:
1.79 -
1.80 - """
1.81 - A reference to an object assigned to a global from outside the module
1.82 - top-level.
1.83 - """
1.84 -
1.85 - pass
1.86 -
1.87 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/tests/global_forward_declaration.py Tue Jun 02 20:17:59 2009 +0200
2.3 @@ -0,0 +1,11 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +def f(x):
2.7 + return g(x)
2.8 +
2.9 +def g(x):
2.10 + return x
2.11 +
2.12 +result_13579 = f(13579)
2.13 +
2.14 +# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/tests/global_implicit.py Tue Jun 02 20:17:59 2009 +0200
3.3 @@ -0,0 +1,13 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +def f(x):
3.7 + global y
3.8 + y = x
3.9 +
3.10 +def g():
3.11 + return y
3.12 +
3.13 +f(24680)
3.14 +result_24680 = g()
3.15 +
3.16 +# vim: tabstop=4 expandtab shiftwidth=4