# HG changeset patch # User paulb@localhost.localdomain # Date 1182556633 -7200 # Node ID 6e7b6fcd63025ed3f1939cc629a8ada4a162bb2e # Parent 016ca6f457d52247791fb091758933358bda01c7 Introduced a separate fix_structures method to the instance fixing class. Made new dictionaries when fixing references to instances via accesses/writes. Added recursion detection around getting distinct instances from classes. diff -r 016ca6f457d5 -r 6e7b6fcd6302 simplify/fixinstances.py --- a/simplify/fixinstances.py Sat Jun 23 01:54:26 2007 +0200 +++ b/simplify/fixinstances.py Sat Jun 23 01:57:13 2007 +0200 @@ -74,15 +74,7 @@ self.current_subprograms = [] self.module = module - - # Visit structures and instances. - - for structure in self.module.simplifier.structures: - for instance in structure.get_instances(): - for name, attrs in instance.namespace.items(): - instance.namespace[name] = self._replace(attrs) - - self.process_node(self.module) + self.process_node(module) # Then, process all functions and methods. @@ -95,6 +87,19 @@ for specialised in subprogram.active(): self.subprograms.append(self.process_node(specialised)) + def process_structures(self, module): + + "Process the structures of the given 'module'." + + self.module = module + + # Visit structures and instances. + + for structure in self.module.simplifier.structures: + for instance in structure.get_instances(): + for name, attrs in instance.namespace.items(): + instance.namespace[name] = self._replace(attrs) + def process_node(self, node): """ @@ -139,9 +144,10 @@ for name in ("accesses", "writes", "paramtypes"): if hasattr(node, name): d = getattr(node, name) + new_d = {} for expr, attrs in d.items(): - del d[expr] - d[self._get_replacement(expr)] = self._replace(attrs, name) + new_d[self._get_replacement(expr)] = self._replace(attrs, name) + setattr(node, name, new_d) # Visit program nodes. @@ -216,9 +222,16 @@ # Convenience functions. +def fix_structures(module): + + "Fix the structures in the given 'module'." + + fixer = Fixer() + fixer.process_structures(module) + def fix(module): - "Fix the instances in the given 'module'." + "Fix the structure references in the given 'module'." fixer = Fixer() fixer.process(module) diff -r 016ca6f457d5 -r 6e7b6fcd6302 simplify/simplified/data.py --- a/simplify/simplified/data.py Sat Jun 23 01:54:26 2007 +0200 +++ b/simplify/simplified/data.py Sat Jun 23 01:57:13 2007 +0200 @@ -24,6 +24,10 @@ from simplify.simplified.utils import Structure, WithName, name from simplify.simplified.program import Subprogram +# Housekeeping. + +current_structures = set() + # Special non-program nodes. class _Class(Structure, WithName): @@ -37,8 +41,6 @@ Structure.__init__(self, *args, **kw) WithName.__init__(self) - self.cache = None - def full_name(self): return "class %s" % self._full_name @@ -83,8 +85,8 @@ attribute types are distinct. """ - if self.cache is not None: - return self.cache + in_current_structure = (self in current_structures) + current_structures.add(self) instances = {} names_found = [] @@ -97,6 +99,11 @@ for instance_name in instance_names: instance = names_to_instances[instance_name] + + if in_current_structure: + instances[instance] = instance + continue + names = instance.namespace.names try: i = names_found.index(names) @@ -106,7 +113,8 @@ instances_found.append(instance) instances[instance] = instance - self.cache = instances + current_structures.remove(self) + return instances class SingleInstanceClass(_Class): diff -r 016ca6f457d5 -r 6e7b6fcd6302 test.py --- a/test.py Sat Jun 23 01:54:26 2007 +0200 +++ b/test.py Sat Jun 23 01:57:13 2007 +0200 @@ -27,6 +27,8 @@ raise else: if "-i" in sys.argv: + simplify.fixinstances.fix_structures(module) + simplify.fixinstances.fix_structures(builtins) simplify.fixinstances.fix(module) simplify.fixinstances.fix(builtins) if "-d" in sys.argv: