# HG changeset patch # User paulb@localhost.localdomain # Date 1169766320 -3600 # Node ID bcff64bc6ffaba6da00be14f5154a61ca5748175 # Parent 7ea581069c2e337ecea0070f7ff099bf93676657 Consolidated original node information in the original attribute, adding it for all invocations so that instance lookup may still work. Renamed _new_instance to new_instance, removing the old new_instance method. Increased the recursion limit. diff -r 7ea581069c2e -r bcff64bc6ffa annotate.py --- a/annotate.py Thu Jan 25 01:03:33 2007 +0100 +++ b/annotate.py Fri Jan 26 00:05:20 2007 +0100 @@ -451,7 +451,7 @@ # Instantiate the class. - instance = self.new_instance(invoke, "new", attr.type.full_name(), attr.type) + instance = self.new_instance(invoke, attr.type) # For instantiations, switch the context. @@ -869,22 +869,9 @@ # Utility methods. def get_builtin_instances(self, node, name): - return [Attribute(None, self._new_instance(node, attr.type)) for attr in self.builtins.namespace[name]] - - def new_instance(self, node, reason, target, type): - - "Create, on the given 'node', a new instance with the given 'type'." + return [Attribute(None, self.new_instance(node, attr.type)) for attr in self.builtins.namespace[name]] - if not hasattr(node, "instances"): - node.instances = {} - - if not node.instances.has_key((reason, target, type)): - instance = self._new_instance(node, type) - node.instances[(reason, target, type)] = instance - - return node.instances[(reason, target, type)] - - def _new_instance(self, node, type): + def new_instance(self, node, type): "For the given 'node', obtain an instance from the given 'type'." @@ -1153,6 +1140,7 @@ code=[ StoreTemp( expr=InvokeFunction( + invocation.original, expr=LoadAttr( expr=LoadRef( ref=self.builtins @@ -1170,6 +1158,7 @@ for arg in star_args: code.append( InvokeFunction( + invocation.original, expr=LoadAttr( expr=LoadTemp(), name="append" @@ -1185,6 +1174,7 @@ ] invocation.stars[subprogram.full_name()] = InvokeBlock( + invocation.original, produces_result=1, expr=LoadRef( ref=Subprogram( @@ -1214,6 +1204,7 @@ code=[ StoreTemp( expr=InvokeFunction( + invocation.original, expr=LoadAttr( expr=LoadRef( ref=self.builtins @@ -1252,6 +1243,7 @@ ) ), InvokeFunction( + invocation.original, expr=LoadAttr( expr=LoadTemp(), name="__setitem__" @@ -1271,6 +1263,7 @@ ] invocation.dstars[subprogram.full_name()] = InvokeBlock( + invocation.original, produces_result=1, expr=LoadRef( ref=Subprogram( diff -r 7ea581069c2e -r bcff64bc6ffa simplified.py --- a/simplified.py Thu Jan 25 01:03:33 2007 +0100 +++ b/simplified.py Fri Jan 26 00:05:20 2007 +0100 @@ -151,7 +151,7 @@ choices Any choices which may be included in the final program. """ - common_attributes = "name", "index", "value", "nstype", "internal", "returns_value", "is_method", "ref", "module", "structures", "original_def" + common_attributes = "name", "index", "value", "nstype", "internal", "returns_value", "is_method", "ref", "module", "structures", "original" expression_attributes = "expr", "lvalue", "test", "star", "dstar" invocation_attributes = "params", # not "args" - see "pos_args", "kw_args" grouping_attributes = "code", "body", "else_", "handler", "finally_", "choices" @@ -285,20 +285,13 @@ # Node manipulation functions. - def copy(self, new_name=None, original_def=None): + def copy(self, new_name=None): """ Perform a deep copy of the node, optionally specifying a 'new_name', returning a new unannotated copy. - - The 'original_def' parameter is used to assign a particular AST node to - copied regions of the simplified node tree. """ - # Obtain an AST node to be assigned to the copied nodes. - - original_def = getattr(self, "original", None) or original_def or getattr(self, "original_def", None) - # Copy the common attributes of this node. common = {} @@ -309,13 +302,9 @@ if new_name is not None: common["name"] = new_name - if original_def is not None: - common["original_def"] = original_def - # Instantiate the copy, avoiding side-effects with original and defining. node = self.__class__(**common) - node.original = self.original node.defining = self.defining # Add links to copied nodes from original AST nodes. @@ -333,7 +322,7 @@ if n is None: n2 = n else: - n2 = n.copy(original_def=original_def) + n2 = n.copy() setattr(node, attr, n2) for attr in self.invocation_attributes: @@ -344,23 +333,23 @@ if n is None: l2.append((name, n)) else: - l2.append((name, n.copy(original_def=original_def))) + l2.append((name, n.copy())) setattr(node, attr, l2) for attr in self.grouping_attributes: if hasattr(self, attr): l = getattr(self, attr) - setattr(node, attr, [n.copy(original_def=original_def) for n in l]) + setattr(node, attr, [n.copy() for n in l]) # Arguments are usually processed further - "args" is useless. if hasattr(self, "pos_args"): - node.pos_args = [n.copy(original_def=original_def) for n in self.pos_args] + node.pos_args = [n.copy() for n in self.pos_args] if hasattr(self, "kw_args"): node.kw_args = {} for name, n in self.kw_args.items(): - node.kw_args[name] = n.copy(original_def=original_def) + node.kw_args[name] = n.copy() return node @@ -549,7 +538,7 @@ self.attributes_for_instances = {} def _get_key(self, node): - return getattr(node, "original_def", None) or getattr(node, "original", None) # self.module.original + return id(getattr(node, "original", None)) # self.module.original def has_instance(self, node): return self.instances.has_key(self._get_key(node)) diff -r 7ea581069c2e -r bcff64bc6ffa simplify.py --- a/simplify.py Thu Jan 25 01:03:33 2007 +0100 +++ b/simplify.py Fri Jan 26 00:05:20 2007 +0100 @@ -224,9 +224,9 @@ if not in_sequence: expr = LoadTemp() else: - expr = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next")) + expr = InvokeFunction(asslist, expr=LoadAttr(expr=LoadTemp(), name="next")) result = Assign(asslist, 1) - store = StoreTemp(expr=InvokeFunction(expr=LoadAttr(name="__iter__", expr=expr))) + store = StoreTemp(expr=InvokeFunction(asslist, expr=LoadAttr(name="__iter__", expr=expr))) release = ReleaseTemp() result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] return result @@ -237,7 +237,7 @@ if not in_sequence: return LoadTemp() else: - return InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next")) + return InvokeFunction(node, expr=LoadAttr(expr=LoadTemp(), name="next")) def visitAssName(self, assname, in_sequence=0): expr = self._visitAssNameOrAttr(assname, in_sequence) @@ -277,6 +277,7 @@ result.code = [ StoreTemp( expr=InvokeFunction( # referenced below + augassign, args=[expr], star=None, dstar=None, @@ -309,6 +310,7 @@ ), StoreTemp( expr=InvokeFunction( # referenced below + augassign, args=[expr], star=None, dstar=None, expr=LoadAttr( expr=LoadAttr(augassign.node, 1, @@ -353,6 +355,7 @@ ), StoreTemp( expr=InvokeFunction( # referenced below + augassign, args=[expr], star=None, dstar=None, expr=LoadAttr( expr=self._visitSlice( @@ -394,6 +397,7 @@ StoreTemp(index="subs", expr=self._visitSubscriptSubs(augassign.node, augassign.node.subs)), StoreTemp( expr=InvokeFunction( # referenced below + augassign, args=[expr], star=None, dstar=None, expr=LoadAttr( expr=self._visitSubscript( @@ -478,6 +482,7 @@ expr=LoadRef(ref=structure) ), InvokeBlock( + class_, share_locals=0, # override the local sharing usually in InvokeBlock expr=LoadRef(ref=subprogram) ) @@ -531,6 +536,7 @@ method_name = self.comparison_methods[op_name] if method_name: invocation = InvokeFunction( + compare, expr=LoadAttr( expr=previous, name=method_name), @@ -540,6 +546,7 @@ elif op_name == "is": invocation = InvokeFunction( + compare, expr=LoadName(name="__is__"), args=[previous, expr], star=None, @@ -548,6 +555,7 @@ elif op_name == "is not": invocation = Not( expr=InvokeFunction( + compare, expr=LoadName(name="__is__"), args=[previous, expr], star=None, @@ -618,7 +626,7 @@ result = InvokeFunction(dict, 1, expr=LoadName(name="dict"), star=None, dstar=None) args = [] for key, value in dict.items: - tuple = InvokeFunction(expr=LoadName(name="tuple"), star=None, dstar=None) + tuple = InvokeFunction(dict, expr=LoadName(name="tuple"), star=None, dstar=None) tuple.set_args([self.dispatch(key), self.dispatch(value)]) args.append(tuple) result.set_args(args) @@ -671,18 +679,20 @@ body=[ Assign( code=[ - StoreTemp(expr=InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next"))), + StoreTemp(expr=InvokeFunction(for_, expr=LoadAttr(expr=LoadTemp(), name="next"))), self.dispatch(for_.assign), ReleaseTemp() ]) ] + self.dispatch(for_.body) + [ InvokeBlock( + for_, expr=LoadRef(ref=subprogram) ) ], handler=[ Conditional( test=InvokeFunction( + for_, expr=LoadName(name="isinstance"), args=[LoadExc(), LoadName(name="StopIteration")], star=None, @@ -709,13 +719,14 @@ code=[ StoreTemp( expr=InvokeFunction( + for_, expr=LoadAttr( name="__iter__", expr=self.dispatch(for_.list) ) ) ), - InvokeBlock(expr=LoadRef(ref=subprogram)), + InvokeBlock(for_, expr=LoadRef(ref=subprogram)), ReleaseTemp() ] ) @@ -780,14 +791,14 @@ if has_star: star = ( function.argnames[npositional], - InvokeFunction(expr=LoadName(name="list")) + self.dispatch(compiler.ast.List([])) ) else: star = None if has_dstar: dstar = ( function.argnames[npositional + has_star], - InvokeFunction(expr=LoadName(name="dict")) + self.dispatch(compiler.ast.Dict([])) ) else: dstar = None @@ -887,6 +898,7 @@ test = Conditional(if_, first, test=InvokeFunction( + if_, expr=LoadAttr( expr=self.dispatch(compare), name="__bool__" @@ -959,6 +971,7 @@ def _visitNot(self, expr, not_=None): invocation = InvokeFunction( + not_, # NOTE: May need a real original node. expr=LoadAttr( expr=expr, name="__bool__" @@ -1007,7 +1020,7 @@ if node is not last: nodes.append(StoreTemp(expr=expr)) - invocation = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="__bool__")) + invocation = InvokeFunction(or_, expr=LoadAttr(expr=LoadTemp(), name="__bool__")) test = Conditional(test=invocation, body=[ReturnFromBlock(expr=LoadTemp())]) nodes.append(test) @@ -1079,6 +1092,7 @@ for node in print_.nodes: result.code.append( InvokeFunction( + print_, expr=LoadTemp(index="print"), args=[self.dispatch(node)], star=None, @@ -1097,6 +1111,7 @@ result.code.insert( len(result.code) - 1, InvokeFunction( + printnl, expr=LoadTemp(index="print"), args=[self.dispatch(compiler.ast.Const("\n"))], star=None, @@ -1111,6 +1126,7 @@ result.expr = self.dispatch(raise_.expr1) else: result.expr = InvokeFunction( + raise_, expr=self.dispatch(raise_.expr1), args=[self.dispatch(raise_.expr2)], star=None, @@ -1362,12 +1378,14 @@ test = Conditional( test=InvokeFunction( + while_, expr=LoadAttr( expr=self.dispatch(while_.test), name="__bool__"), ), body=self.dispatch(while_.body) + [ InvokeBlock( + while_, expr=LoadRef(ref=subprogram) ), ReturnFromBlock() @@ -1422,6 +1440,7 @@ body=[ ReturnFromBlock( expr=InvokeFunction( + binary, expr=LoadAttr(expr=self.dispatch(binary.left), name=left_name), args=[self.dispatch(binary.right)], star=None, @@ -1436,6 +1455,7 @@ body=[ ReturnFromBlock( expr=InvokeFunction( + binary, expr=LoadAttr(expr=self.dispatch(binary.right), name=right_name), args=[self.dispatch(binary.left)], star=None, @@ -1452,6 +1472,7 @@ self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram result = InvokeBlock( + binary, produces_result=1, expr=LoadRef(ref=subprogram) ) diff -r 7ea581069c2e -r bcff64bc6ffa test.py --- a/test.py Thu Jan 25 01:03:33 2007 +0100 +++ b/test.py Fri Jan 26 00:05:20 2007 +0100 @@ -2,6 +2,7 @@ if __name__ == "__main__": import sys, os + sys.setrecursionlimit(10000) import simplified