# HG changeset patch # User paulb@localhost.localdomain # Date 1182209830 -7200 # Node ID 1d95c953c1140c67f86c702e85f5b714a9cafcea # Parent 4baf44a943b12c6f57cfba06dc3ecd0587c43a0d Added a values method to the Namespace class. Changed instance fixing to replace Attribute objects, rather than to mutate them (which disrupts comparison operations). Added parameters and parameter types to instance fixing. Removed spurious namespace references in instance fixing. Fixed the Attribute __eq__ and __hash__ methods. Made distinct instances dictionaries contain identity mappings. Added builtins to the test program's instance fixing. diff -r 4baf44a943b1 -r 1d95c953c114 simplify/annotate.py --- a/simplify/annotate.py Mon Jun 18 01:47:51 2007 +0200 +++ b/simplify/annotate.py Tue Jun 19 01:37:10 2007 +0200 @@ -1439,6 +1439,9 @@ def keys(self): return self.names.keys() + def values(self): + return self.names.values() + def items(self): return self.names.items() diff -r 4baf44a943b1 -r 1d95c953c114 simplify/fixinstances.py --- a/simplify/fixinstances.py Mon Jun 18 01:47:51 2007 +0200 +++ b/simplify/fixinstances.py Tue Jun 19 01:37:10 2007 +0200 @@ -87,7 +87,14 @@ for specialised in subprogram.active(): self.subprograms.append(self.process_node(specialised)) - def process_node(self, node, namespace=None): + # Visit structures and instances. + + for structure in self.module.simplifier.structures: + for instance in structure.instances.values(): + for attrs in instance.namespace.values(): + self._replace(attrs) + + def process_node(self, node): """ Process a subprogram or module 'node', discovering from attributes on @@ -126,7 +133,7 @@ if hasattr(node, name): attrs = getattr(node, name) self._replace(attrs) - for name in ("accesses", "writes"): + for name in ("accesses", "writes", "paramtypes"): if hasattr(node, name): d = getattr(node, name) for expr, attrs in d.items(): @@ -144,21 +151,40 @@ for attr in ("body", "else_", "handler", "finally_", "code", "choices", "nodes"): if hasattr(node, attr): self.dispatches(getattr(node, attr)) + if hasattr(node, "params"): + for param, default in node.params: + self.dispatch(default) + for attr in ("star", "dstar"): + if getattr(node, attr, None): + param, default = getattr(node, attr) + self.dispatch(default) return node - def _replace(self, attrs, name=None): - to_replace = {} - for attr in attrs: + def _replace(self, items, name=None): + to_replace = set() + for item in items: if name == "accesses": - _attr, accessor = attr - value = _attr.type + attr, accessor = item + value = attr.type else: + attr = item value = attr.type - if isinstance(value, Instance) and not to_replace.has_key(value): + if isinstance(value, Instance): distinct_instances = value.get_class().get_distinct_instances() if distinct_instances.has_key(value): - attr.type = distinct_instances[value] + to_replace.add((item, distinct_instances[value])) + + for item, replacement in to_replace: + items.remove(item) + + for item, replacement in to_replace: + if name == "accesses": + attr, accessor = item + items.append((Attribute(attr.context, replacement), accessor)) + else: + attr = item + items.add(Attribute(attr.context, replacement)) def dispatch(self, node, *args): return Visitor.dispatch(self, node, *args) @@ -176,10 +202,7 @@ # The special case of internal subprogram invocation is addressed by # propagating namespace information to the subprogram and processing it. - if invoke.share_locals: - subprogram = self.process_node(invoke.ref, self.namespace) - else: - subprogram = self.process_node(invoke.ref) + subprogram = self.process_node(invoke.ref) if subprogram is not None: self.subprograms.append(subprogram) diff -r 4baf44a943b1 -r 1d95c953c114 simplify/simplified/data.py --- a/simplify/simplified/data.py Mon Jun 18 01:47:51 2007 +0200 +++ b/simplify/simplified/data.py Tue Jun 19 01:37:10 2007 +0200 @@ -99,6 +99,7 @@ except ValueError: names_found.append(names) instances_found.append(instance) + instances[instance] = instance return instances @@ -263,9 +264,9 @@ return "Attribute(%s, %s)" % (repr(self.context), repr(self.type)) def __eq__(self, other): - return hasattr(other, "type") and other.type == self.type or other == self.type + return hasattr(other, "type") and other.type == self.type def __hash__(self): - return id(self.type) + return hash(self.type) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 4baf44a943b1 -r 1d95c953c114 test.py --- a/test.py Mon Jun 18 01:47:51 2007 +0200 +++ b/test.py Tue Jun 19 01:37:10 2007 +0200 @@ -28,6 +28,7 @@ else: if "-i" in sys.argv: simplify.fixinstances.fix(module) + simplify.fixinstances.fix(builtins) if "-d" in sys.argv: simplify.viewer.makedocs(module, importer.modules.values(), builtins, distinct=("-i" in sys.argv))