1.1 --- a/micropython/inspect.py Sat Nov 08 23:11:45 2008 +0100
1.2 +++ b/micropython/inspect.py Mon Nov 10 21:38:58 2008 +0100
1.3 @@ -137,24 +137,27 @@
1.4
1.5 def vacuum(self):
1.6
1.7 - "Vacuum the module namespace, removing unloaded module references."
1.8 + """
1.9 + Vacuum the module namespace, removing unreferenced objects and unused
1.10 + names.
1.11 + """
1.12
1.13 for name, value in self.items():
1.14
1.15 - if isinstance(value, Attr):
1.16 - attr_value = value.value
1.17 + if self.should_optimise_unused_objects():
1.18
1.19 - # Remove non-loaded modules.
1.20 + # Remove unused names.
1.21
1.22 - if isinstance(attr_value, Module) and not attr_value.loaded:
1.23 + if name not in self.importer.names_used:
1.24 del self[name]
1.25
1.26 - # Remove unreferenced names.
1.27 + # Remove entries for unreferenced objects.
1.28 # This, due to the nature of the referenced attribute, assumes
1.29 # that only explicitly mentioned classes and functions are
1.30 # employed in the final program.
1.31
1.32 - elif self.should_optimise_unused_objects():
1.33 + elif isinstance(value, Attr):
1.34 + attr_value = value.value
1.35
1.36 # Only remove entries for classes and functions, not methods.
1.37
1.38 @@ -169,7 +172,7 @@
1.39
1.40 # Complain about globals not initialised at the module level.
1.41
1.42 - elif isinstance(value, Global):
1.43 + if isinstance(value, Global):
1.44 print "Warning: global %r in module %r not initialised at the module level." % (name, self.name)
1.45
1.46 # Remove unreferenced objects.
1.47 @@ -179,10 +182,38 @@
1.48 all_objects = list(self.all_objects)
1.49
1.50 for obj in all_objects:
1.51 +
1.52 + # Only remove entries for classes and functions, not methods.
1.53 +
1.54 if (isinstance(obj, Function) and not obj.is_method() or
1.55 isinstance(obj, Class)) and not obj.referenced:
1.56 +
1.57 self.all_objects.remove(obj)
1.58
1.59 + # Remove unused entries from classes plus associated methods.
1.60 +
1.61 + if isinstance(obj, Class):
1.62 + for name, attr in obj.class_attributes().items():
1.63 +
1.64 + if name not in self.importer.names_used:
1.65 + del obj[name]
1.66 +
1.67 + # Methods can only be deleted if they are the only
1.68 + # assigned object to the class and are unreferenced.
1.69 +
1.70 + if attr.assignments == 1 and isinstance(attr.value, Function) and \
1.71 + attr.value.is_method() and not attr.value.referenced:
1.72 +
1.73 + self.all_objects.remove(attr.value)
1.74 +
1.75 + def finalise(self):
1.76 +
1.77 + "Finalise the module."
1.78 +
1.79 + for obj in self.all_objects:
1.80 + if isinstance(obj, (Class, Function)):
1.81 + obj.finalise_attributes()
1.82 +
1.83 def add_object(self, obj, any_scope=0):
1.84
1.85 """