# HG changeset patch # User Paul Boddie # Date 1232231117 -3600 # Node ID 9abe641af4e62799415a7828bb544e4e5e1c83c0 # Parent d213a26b1846790bd50dc66510f161691b15bff2 Make unused name and object removal more conservative, avoiding issues of module/class attributes and non-class/function objects being removed and then being requested in code generation. Ensure the registration of constant-related objects in order to prevent their removal. diff -r d213a26b1846 -r 9abe641af4e6 micropython/inspect.py --- a/micropython/inspect.py Sat Jan 17 22:33:19 2009 +0100 +++ b/micropython/inspect.py Sat Jan 17 23:25:17 2009 +0100 @@ -146,17 +146,12 @@ if self.should_optimise_unused_objects(): - # Remove unused names. - - if name not in self.importer.names_used: - del self[name] - # Remove entries for unreferenced objects. # This, due to the nature of the referenced attribute, assumes # that only explicitly mentioned classes and functions are # employed in the final program. - elif isinstance(value, Attr): + if isinstance(value, Attr): attr_value = value.value # Only remove entries for classes and functions, not methods. @@ -195,16 +190,15 @@ if isinstance(obj, Class): for name, attr in obj.class_attributes().items(): - if name not in self.importer.names_used: - del obj[name] + # Methods can only be deleted if they are the only + # assigned object to the class and are unreferenced. - # Methods can only be deleted if they are the only - # assigned object to the class and are unreferenced. + if name not in self.importer.names_used and \ + attr.assignments == 1 and isinstance(attr.value, Function) and \ + attr.value.is_method() and not attr.value.referenced: - if attr.assignments == 1 and isinstance(attr.value, Function) and \ - attr.value.is_method() and not attr.value.referenced: - - self.all_objects.remove(attr.value) + self.all_objects.remove(attr.value) + del obj[name] def finalise(self): @@ -529,6 +523,16 @@ return self.OP(node) def visitConst(self, node): + + # Accounting. + + name = node.value.__class__.__name__ + if self.builtins is not None and self.builtins.has_key(name): + attr = self.builtins[name] + attr.set_referenced() + + # Register the constant, if necessary, returning the resulting object. + return self.importer.make_constant(node.value) visitContinue = NOP diff -r d213a26b1846 -r 9abe641af4e6 tests/attributes.py --- a/tests/attributes.py Sat Jan 17 22:33:19 2009 +0100 +++ b/tests/attributes.py Sat Jan 17 23:25:17 2009 +0100 @@ -2,6 +2,7 @@ class C: clsattr = 123 + clsattr2 = 456 def __init__(self, value): self.instattr = value