1.1 --- a/micropython/inspect.py Tue Jul 03 23:52:27 2012 +0200
1.2 +++ b/micropython/inspect.py Wed Jul 04 00:27:29 2012 +0200
1.3 @@ -85,6 +85,16 @@
1.4 """
1.5 An inspected module, providing core details via the Module superclass, but
1.6 capable of being used as an AST visitor.
1.7 +
1.8 + A module can be inspected through the invocation of the following methods in
1.9 + order:
1.10 +
1.11 + 1. parse
1.12 + 2. process_functions
1.13 + 3. vacuum
1.14 + 4. finalise
1.15 +
1.16 + A module importer can be expected to perform these invocations.
1.17 """
1.18
1.19 def __init__(self, name, importer):
1.20 @@ -138,16 +148,14 @@
1.21
1.22 self.store("__name__", self._visitConst(self.full_name()))
1.23
1.24 - # First, visit module-level code, recording global names.
1.25 + # Detect and record globals declared in the module.
1.26 +
1.27 + self.process_globals(module)
1.28 +
1.29 + # Visit module-level code, also recording global names.
1.30
1.31 processed = self.dispatch(module)
1.32
1.33 - # Then, for each function, detect and record globals declared in those
1.34 - # functions.
1.35 -
1.36 - for node, namespaces in self.functions:
1.37 - self.process_globals(node)
1.38 -
1.39 self.finalise_attribute_usage()
1.40
1.41 # Add references to other modules declared using the __all__ global.
1.42 @@ -186,8 +194,12 @@
1.43 for n in node.getChildNodes():
1.44 if isinstance(n, compiler.ast.Global):
1.45 for name in n.names:
1.46 - if not self.has_key(name):
1.47 - self[name] = make_instance()
1.48 +
1.49 + # Each name may potentially be assigned many times.
1.50 + # NOTE: We don't try and find out the specifics at this
1.51 + # NOTE: point and just set an instance instead.
1.52 +
1.53 + self.set(name, make_instance(), False)
1.54 else:
1.55 self.process_globals(n)
1.56
1.57 @@ -1067,6 +1079,19 @@
1.58
1.59 # The name is recorded in an earlier process.
1.60
1.61 + # Since the presence of a global declaration may
1.62 + # indicate the modification of a name, and this may
1.63 + # happen during initialisation, the name is recorded
1.64 + # and any usage observations ultimately revoked.
1.65 +
1.66 + # NOTE: Actual modification could be detected by
1.67 + # NOTE: establishing a path from the module top-level to
1.68 + # NOTE: an actual assignment.
1.69 +
1.70 + if not self.modified_names.has_key(ns):
1.71 + self.modified_names[ns] = set()
1.72 + self.modified_names[ns].add(name)
1.73 +
1.74 def visitIf(self, node):
1.75 self.use_name("__bool__", node)
1.76 self.new_branchpoint()