2.1 --- a/micropython/__init__.py Tue Jun 15 00:50:51 2010 +0200
2.2 +++ b/micropython/__init__.py Wed Jun 16 01:08:18 2010 +0200
2.3 @@ -370,6 +370,7 @@
2.4 self.name_references = {}
2.5 self.specific_name_references = {}
2.6 self.attribute_users_visited = set()
2.7 + self.attributes_to_visit = {}
2.8
2.9 # Status information.
2.10
2.11 @@ -484,22 +485,35 @@
2.12
2.13 self.attributes_used.add(objname + "." + name)
2.14
2.15 + def use_object(self, objname):
2.16 +
2.17 + "Indicate that the object with the given 'objname' is used."
2.18 +
2.19 + self.attributes_used.add(objname)
2.20 +
2.21 def collect_attributes(self, objtable):
2.22
2.23 "Collect attribute references for the entire program."
2.24
2.25 + # Include names which may not be explicitly used in programs.
2.26 + # NOTE: Potentially declare these when inspecting.
2.27 +
2.28 + for attrname in self.names_always_used:
2.29 + for objname in objtable.all_possible_objects([attrname]):
2.30 +
2.31 + # Record attributes of objects for potential visiting.
2.32 +
2.33 + self.add_attribute_to_visit(objname, attrname)
2.34 +
2.35 # Start with the "root" modules, finding referenced objects.
2.36
2.37 self._collect_attributes("__builtins__", objtable)
2.38 self._collect_attributes("__main__", objtable)
2.39
2.40 - # Include names which may not be explicitly used in programs.
2.41 - # NOTE: Potentially declare these when inspecting.
2.42 -
2.43 - for attrname in self.names_always_used:
2.44 - for objname in objtable.all_possible_objects([attrname]):
2.45 - self.use_attribute(objname, attrname)
2.46 - self._collect_attributes(objname + "." + attrname, objtable)
2.47 + def add_attribute_to_visit(self, objname, attrname):
2.48 + if not self.attributes_to_visit.has_key(objname):
2.49 + self.attributes_to_visit[objname] = set()
2.50 + self.attributes_to_visit[objname].add(attrname)
2.51
2.52 def _collect_attributes(self, from_name, objtable):
2.53
2.54 @@ -524,10 +538,22 @@
2.55 if not objnames:
2.56 print "Warning: usage in %r finds no object supporting any attributes %r" % (from_name, names)
2.57
2.58 + # For each suggested object, consider each attribute given by the
2.59 + # names.
2.60 +
2.61 for objname in objnames:
2.62 for name in names:
2.63 - self.use_attribute(objname, name)
2.64 - self._collect_attributes(objname + "." + name, objtable)
2.65 +
2.66 + # Visit attributes of objects known to be used.
2.67 +
2.68 + if objname in self.attributes_used:
2.69 + self.use_attribute(objname, name)
2.70 + self._collect_attributes(objname + "." + name, objtable)
2.71 +
2.72 + # Record attributes of other objects for potential visiting.
2.73 +
2.74 + else:
2.75 + self.add_attribute_to_visit(objname, name)
2.76
2.77 # Get specific name references and visit the referenced objects.
2.78
2.79 @@ -543,6 +569,17 @@
2.80 self.use_attribute(from_name, "__init__")
2.81 self._collect_attributes(from_name + ".__init__", objtable)
2.82
2.83 + # Visit attributes on objects not previously visited.
2.84 +
2.85 + attributes_to_visit = self.attributes_to_visit.get(from_name, [])
2.86 +
2.87 + if attributes_to_visit:
2.88 + del self.attributes_to_visit[from_name]
2.89 +
2.90 + for attrname in attributes_to_visit:
2.91 + self.use_attribute(from_name, attrname)
2.92 + self._collect_attributes(from_name + "." + attrname, objtable)
2.93 +
2.94 # Constant accounting.
2.95
2.96 def init_predefined_constants(self):
2.97 @@ -763,6 +800,7 @@
2.98
2.99 # Record the module.
2.100
2.101 + self.use_object(module.full_name())
2.102 #print "Loaded", module_name, "with namespace", module.namespace.keys()
2.103 return module
2.104
3.1 --- a/micropython/inspect.py Tue Jun 15 00:50:51 2010 +0200
3.2 +++ b/micropython/inspect.py Wed Jun 16 01:08:18 2010 +0200
3.3 @@ -701,6 +701,7 @@
3.4
3.5 self.use_name("__iter__", node.list)
3.6 self.use_name("next")
3.7 + self.use_name("StopIteration")
3.8
3.9 in_loop = self.in_loop
3.10 self.in_loop = 1