# HG changeset patch # User paulb@localhost.localdomain # Date 1185733989 -7200 # Node ID 76ab0f064cc3dfdd3a7f9fb7fbc7afb7295514ac # Parent 410f07366c9b6162a606e817d57704820e98f5d6 Tidied up invocations, making InvokeFunction, InvokeRef and Subprogram have explicit initialisation parameters which help to document the range of attributes available on those nodes. diff -r 410f07366c9b -r 76ab0f064cc3 simplify/ast.py --- a/simplify/ast.py Sun Jul 29 01:53:25 2007 +0200 +++ b/simplify/ast.py Sun Jul 29 20:33:09 2007 +0200 @@ -228,9 +228,7 @@ Raise(assert_, expr=InvokeFunction(assert_, expr=LoadName(name="AssertionError"), - args=fail_args, - star=None, - dstar=None + args=fail_args ) ) ] @@ -327,8 +325,6 @@ expr=InvokeFunction( # referenced below augassign, args=[expr], - star=None, - dstar=None, expr=LoadAttr( expr=self.dispatch(augassign.node), name=self.augassign_methods[augassign.op] @@ -359,7 +355,7 @@ StoreTemp( expr=InvokeFunction( # referenced below augassign, - args=[expr], star=None, dstar=None, + args=[expr], expr=LoadAttr( expr=LoadAttr(augassign.node, 1, expr=LoadTemp(index="expr"), @@ -404,7 +400,7 @@ StoreTemp( expr=InvokeFunction( # referenced below augassign, - args=[expr], star=None, dstar=None, + args=[expr], expr=LoadAttr( expr=self._visitSlice( augassign.node, @@ -446,7 +442,7 @@ StoreTemp( expr=InvokeFunction( # referenced below augassign, - args=[expr], star=None, dstar=None, + args=[expr], expr=LoadAttr( expr=self._visitSubscript( augassign.node, @@ -572,7 +568,7 @@ return result def visitCallFunc(self, callfunc): - result = InvokeFunction(callfunc, 1, star=None, dstar=None, args=self.dispatches(callfunc.args)) + result = InvokeFunction(callfunc, 1, args=self.dispatches(callfunc.args)) if callfunc.star_args is not None: result.star = self.dispatch(callfunc.star_args) if callfunc.dstar_args is not None: @@ -692,9 +688,7 @@ invocation = InvokeFunction( new_op, 1, expr=LoadName(name="__is__"), - args=[previous, expr], - star=None, - dstar=None) + args=[previous, expr]) elif op_name == "is not": invocation = Not( @@ -702,9 +696,7 @@ expr=InvokeFunction( new_op, expr=LoadName(name="__is__"), - args=[previous, expr], - star=None, - dstar=None) + args=[previous, expr]) ) elif op_name == "in": @@ -714,9 +706,7 @@ expr=previous, name="__contains__" ), - args=[expr], - star=None, - dstar=None) + args=[expr]) elif op_name == "not in": invocation = Not( @@ -727,9 +717,7 @@ expr=previous, name="__contains__" ), - args=[expr], - star=None, - dstar=None) + args=[expr]) ) else: @@ -878,9 +866,8 @@ test=InvokeFunction( node, expr=LoadName(name="isinstance"), - args=[LoadExc(), LoadName(name="StopIteration")], - star=None, - dstar=None), + args=[LoadExc(), LoadName(name="StopIteration")] + ), body=else_stmt, else_=[ Raise( @@ -1022,7 +1009,7 @@ """ subprogram = Subprogram(function, name=function.name, module=self.module, structures=self.current_structures[:], - internal=0, returns_value=1, star=None, dstar=None, is_method=self.within_class, original_def=function) + internal=0, returns_value=1, star=None, dstar=None, is_method=self.within_class) # Make nice annotations for the viewer. @@ -1138,7 +1125,7 @@ # Make a subprogram for the function and record it outside the main # tree. - subprogram = Subprogram(lambda_, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=lambda_) + subprogram = Subprogram(lambda_, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None) # Make nice annotations for the viewer. @@ -1160,7 +1147,7 @@ # Make a subprogram for the list construction and record it outside the # main tree. - subprogram = Subprogram(list, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=list) + subprogram = Subprogram(list, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None) self.current_subprograms.append(subprogram) # Make nice annotations for the viewer. @@ -1174,9 +1161,7 @@ expr=LoadName( name="list" ), - args=[], - star=None, - dstar=None + args=[] ) ) ] @@ -1189,9 +1174,7 @@ expr=LoadTemp(), name="append" ), - args=[self.dispatch(node)], - star=None, - dstar=None + args=[self.dispatch(node)] ) ) @@ -1217,7 +1200,7 @@ # Make a subprogram for the list comprehension and record it outside the # main tree. - subprogram = Subprogram(listcomp, name=None, module=self.module, internal=1, returns_value=1, star=None, dstar=None, original_def=listcomp) + subprogram = Subprogram(listcomp, name=None, module=self.module, internal=1, returns_value=1, star=None, dstar=None) self.current_subprograms.append(subprogram) # Make nice annotations for the viewer. @@ -1233,9 +1216,7 @@ index="listcomp", expr=InvokeFunction( expr=LoadName(name="list"), - args=[], - star=None, - dstar=None + args=[] ) ) ] + self._visitListCompFor(listcomp, listcomp.quals) + [ @@ -1299,9 +1280,7 @@ expr=LoadTemp(index="listcomp"), name="append" ), - args=[self.dispatch(node.expr)], - star=None, - dstar=None + args=[self.dispatch(node.expr)] ) ] @@ -1442,9 +1421,7 @@ InvokeFunction( print_, expr=LoadTemp(index="print"), - args=[self.dispatch(node)], - star=None, - dstar=None + args=[self.dispatch(node)] ) ) @@ -1461,9 +1438,7 @@ InvokeFunction( printnl, expr=LoadTemp(index="print"), - args=[self.dispatch(compiler.ast.Const("\n"))], - star=None, - dstar=None + args=[self.dispatch(compiler.ast.Const("\n"))] ) ) return result @@ -1476,9 +1451,7 @@ result.expr = InvokeFunction( raise_, expr=self.dispatch(raise_.expr1), - args=[self.dispatch(raise_.expr2)], - star=None, - dstar=None + args=[self.dispatch(raise_.expr2)] ) if raise_.expr3 is not None: result.traceback = self.dispatch(raise_.expr3) @@ -1499,8 +1472,6 @@ expr=expr, name="__setslice__" ), - star=None, - dstar=None, args=[lower, upper, value] ) elif flags == "OP_APPLY": @@ -1510,8 +1481,6 @@ expr=expr, name="__getslice__" ), - star=None, - dstar=None, args=[lower, upper] ) elif flags == "OP_DELETE": @@ -1521,8 +1490,6 @@ expr=expr, name="__delslice__" ), - star=None, - dstar=None, args=[lower, upper] ) else: @@ -1537,9 +1504,7 @@ def visitSliceobj(self, sliceobj): return InvokeFunction(sliceobj, 1, expr=LoadName(name="slice"), - args=self.dispatches(sliceobj.nodes), - star=None, - dstar=None + args=self.dispatches(sliceobj.nodes) ) def visitStmt(self, stmt): @@ -1555,8 +1520,6 @@ expr=expr, name="__setitem__" ), - star=None, - dstar=None, args=[subs, value] ) elif flags == "OP_APPLY": @@ -1566,8 +1529,6 @@ expr=expr, name="__getitem__" ), - star=None, - dstar=None, args=[subs] ) elif flags == "OP_DELETE": @@ -1577,8 +1538,6 @@ expr=expr, name="__delitem__" ), - star=None, - dstar=None, args=[subs] ) else: @@ -1592,9 +1551,7 @@ else: return InvokeFunction(node, 1, expr=LoadName(name="tuple"), - args=self.dispatches(subs), - star=None, - dstar=None + args=self.dispatches(subs) ) def visitSubscript(self, subscript, element=None): @@ -1826,9 +1783,8 @@ name=left_name), args=[ LoadTemp(index="right") - ], - star=None, - dstar=None) + ] + ) ), Conditional( isolate_test=1, @@ -1845,9 +1801,8 @@ name=right_name), args=[ LoadTemp(index="left") - ], - star=None, - dstar=None) + ] + ) ), Conditional( isolate_test=1, @@ -1927,9 +1882,8 @@ expr=InvokeFunction( binary, expr=LoadAttr(expr=LoadTemp(index="left"), name=left_name), - args=[LoadTemp(index="right")], - star=None, - dstar=None) + args=[LoadTemp(index="right")] + ) ) ], else_=[], @@ -1942,9 +1896,8 @@ expr=InvokeFunction( binary, expr=LoadAttr(expr=LoadTemp(index="right"), name=right_name), - args=[LoadTemp(index="left")], - star=None, - dstar=None) + args=[LoadTemp(index="left")] + ) ) ], else_=[] diff -r 410f07366c9b -r 76ab0f064cc3 simplify/simplified/program.py --- a/simplify/simplified/program.py Sun Jul 29 01:53:25 2007 +0200 +++ b/simplify/simplified/program.py Sun Jul 29 20:33:09 2007 +0200 @@ -366,11 +366,21 @@ "A function or method invocation." - def __init__(self, *args, **kw): - self.args = [] - self.star = None - self.dstar = None - Invoke.__init__(self, *args, **kw) + def __init__(self, original=None, defining=0, expr=None, args=None, star=None, dstar=None, **kw): + + """ + Initialise the invocation with the following optional parameters: + + * The 'original' AST node represented by this invocation. + * Whether this invocation is 'defining' (false by default). + * The 'expr' or expression indicating the invoked subprogram. + * The 'args' or arguments to be supplied, yielding the 'pos_args' and + 'kw_args' attributes on this object. + * The 'star' argument containing additional unlabelled arguments. + * The 'dstar' argument containing keyword arguments. + """ + + Invoke.__init__(self, original, defining, expr=expr, args=(args or []), star=star, dstar=dstar, **kw) self.set_args(self.args) self.share_locals = 0 @@ -397,9 +407,21 @@ "A block or loop invocation." - def __init__(self, *args, **kw): - self.share_locals = 1 - Invoke.__init__(self, *args, **kw) + def __init__(self, original=None, defining=0, ref=None, produces_result=1, share_locals=1, **kw): + + """ + Initialise the invocation with the following optional parameters: + + * The 'original' AST node represented by this invocation. + * Whether this invocation is 'defining' (false by default). + * The 'ref' indicating the subprogram to be invoked. + * Whether a result is produced as indicated by 'produces_result' (true + by default). + * Whether the subprogram shares the locals of the caller as indicated + by 'share_locals' (true by default). + """ + + Invoke.__init__(self, original, defining, ref=ref, produces_result=produces_result, share_locals=share_locals, **kw) # Program structure nodes. @@ -414,23 +436,37 @@ "A subprogram: functions, methods and loops." - def __init__(self, *args, **kw): - Node.__init__(self, *args, **kw) + def __init__(self, original=None, defining=0, name=None, module=None, structure=None, + structures=None, internal=0, returns_value=1, params=None, star=None, + dstar=None, **kw): + + """ + Initialise the subprogram with the following optional parameters: + + * The 'original' AST node represented by this subprogram. + * Whether this subprogram is 'defining' or not (false by default). + * The 'name' of this subprogram which may be None. + * The 'module' in which this subprogram is found. + * The 'structure' initialised by this subprogram. + * The 'structures' within which this subprogram resides. + * The 'internal' status of this subprogram (false by default), which + if true typically means that a loop or operation is being + represented. + * Whether a value is returned, as specified by 'returns_value' (true + by default). + * The 'params' (a parameter list which is empty by default). + * The 'star' parameter which collects excess positional arguments. + * The 'dstar' parameter which collects unmatched keyword arguments. + """ + + Node.__init__(self, original, defining, name=name, module=module, structure=structure, + structures=structures, internal=internal, returns_value=returns_value, + params=(params or []), star=star, dstar=dstar, **kw) + WithName.__init__(self) self.raises = set() self.returns = set() self.return_locals = set() self.namespace = Namespace() # NOTE: Temporary. - # NOTE: Logic similar to that below is used in simplify.fixinstances to - # NOTE: identify equivalent attributes which are subprograms. - - """ - def __eq__(self, other): - return self is other or \ - not getattr(self, "internal", 0) and not getattr(other, "internal", 0) and \ - getattr(self, "copy_of", self) is getattr(other, "copy_of", other) and \ - getattr(self, "paramtypes", None) == getattr(other, "paramtypes", None) - """ - # vim: tabstop=4 expandtab shiftwidth=4