# HG changeset patch # User paulb@localhost.localdomain # Date 1185744559 -7200 # Node ID 1b5d0e034a6e14333e868a2fc5b3d6a5f0f4d207 # Parent 76ab0f064cc3dfdd3a7f9fb7fbc7afb7295514ac Removed various getattr and hasattr tests, making use of attributes which should now always exist on invocation-related nodes. diff -r 76ab0f064cc3 -r 1b5d0e034a6e simplify/annotate.py --- a/simplify/annotate.py Sun Jul 29 20:33:09 2007 +0200 +++ b/simplify/annotate.py Sun Jul 29 23:29:19 2007 +0200 @@ -193,7 +193,7 @@ # Add namespace details to any structure involved. - if getattr(node, "structure", None) is not None: + if node.structure is not None: node.structure.namespace = Namespace() # Initialise bases where appropriate. @@ -285,9 +285,6 @@ system's annotation counter. """ - if not hasattr(node, "paramtypes"): - node.paramtypes = {} - for param, types in items: if not node.paramtypes.has_key(param): node.paramtypes[param] = set() @@ -444,7 +441,7 @@ and storing details on the node. """ - module = self.importer.load(import_.name, self.builtins, getattr(import_, "alias", None)) + module = self.importer.load(import_.name, self.builtins, import_.alias) if module is not None: self.namespace.set_types(set([module])) else: @@ -787,7 +784,7 @@ those raised in the namespace. """ - if getattr(raise_, "traceback", None) is not None: + if raise_.traceback is not None: self.dispatch(raise_.traceback) self.dispatch(raise_.expr) @@ -1021,14 +1018,14 @@ # Provide the correct namespace for the invocation. # This may be a "shared" namespace... - if getattr(invoke, "share_locals", 0): + if invoke.share_locals: namespace = Namespace() namespace.merge_namespace(self.namespace, everything=0) using_module_namespace = self.namespace is self.module.namespace # Or it may be a structure... - elif getattr(target, "structure", None): + elif target.structure: namespace = Namespace() using_module_namespace = 0 @@ -1044,7 +1041,7 @@ # NOTE: subprogram within itself. Do not define the name of the function # NOTE: within a method definition. - if getattr(target, "name", None) is not None and not getattr(target, "is_method", 0): + if target.name is not None and not target.is_method: namespace.store(target.name, set([Attribute(None, target)])) # Process the subprogram. @@ -1054,7 +1051,7 @@ # NOTE: Improve and verify this. # If the invocation returns a value, acquire the return types. - if getattr(target, "returns_value", 0): + if target.returns_value: self.namespace.set_types(target.returns) self.annotate(invoke) @@ -1063,7 +1060,7 @@ # logical expressions, the namespace can be modified whilst values are # returned as results. - if getattr(invoke, "share_locals", 0): + if invoke.share_locals: self.namespace.reset() # Merge the locals snapshots. @@ -1073,7 +1070,7 @@ # For blocks returning values (such as operations), do not merge # snapshots or results. - if getattr(target, "returns_value", 0): + if target.returns_value: self.namespace.merge_namespace(locals, everything=0, temp=0) # For blocks not returning values (such as loops), merge @@ -1085,8 +1082,6 @@ # Incorporate any raised exceptions. - if not hasattr(invoke, "raises"): - invoke.raises = set() invoke.raises.update(target.raises) self.namespace.raises.update(target.raises) diff -r 76ab0f064cc3 -r 1b5d0e034a6e simplify/simplified/program.py --- a/simplify/simplified/program.py Sun Jul 29 20:33:09 2007 +0200 +++ b/simplify/simplified/program.py Sun Jul 29 23:29:19 2007 +0200 @@ -326,7 +326,6 @@ class StoreAttr(Node): "Associate an object's attribute with a value." class ReleaseTemp(Node): "Release a temporary value." class Try(Node): "A try...except...else...finally grouping node." -class Raise(Node): "An exception raising node." class Not(Node): "A negation of an expression." class CheckType(Node): "Check a value's type from a list of choices." class Return(Node): "Return an evaluated expression." @@ -362,6 +361,25 @@ # Invocations involve some more work to process calculated attributes. +class Raise(Invoke): + + "An exception raising node which may behave like an invocation." + + def __init__(self, original=None, defining=0, expr=None, traceback=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 'traceback' associated with the raised exception. + """ + + Invoke.__init__(self, original, defining, expr=expr, traceback=traceback, **kw) + self.share_locals = 0 + self.raises = set() + class InvokeFunction(Invoke): "A function or method invocation." @@ -383,6 +401,7 @@ Invoke.__init__(self, original, defining, expr=expr, args=(args or []), star=star, dstar=dstar, **kw) self.set_args(self.args) self.share_locals = 0 + self.raises = set() def set_args(self, args): @@ -422,6 +441,7 @@ """ Invoke.__init__(self, original, defining, ref=ref, produces_result=produces_result, share_locals=share_locals, **kw) + self.raises = set() # Program structure nodes. @@ -429,6 +449,10 @@ "A Python module." + def __init__(self, *args, **kw): + Node.__init__(self, *args, **kw) + self.structure = None + def full_name(self): return "module %s" % self.name @@ -437,7 +461,7 @@ "A subprogram: functions, methods and loops." def __init__(self, original=None, defining=0, name=None, module=None, structure=None, - structures=None, internal=0, returns_value=1, params=None, star=None, + structures=None, internal=0, returns_value=1, is_method=0, params=None, star=None, dstar=None, **kw): """ @@ -454,6 +478,8 @@ represented. * Whether a value is returned, as specified by 'returns_value' (true by default). + * Whether the subprogram is a method, as specified by 'is_method' + (false 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. @@ -461,12 +487,13 @@ 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) + is_method=is_method, params=(params or []), star=star, dstar=dstar, **kw) WithName.__init__(self) self.raises = set() self.returns = set() self.return_locals = set() + self.paramtypes = {} self.namespace = Namespace() # NOTE: Temporary. # vim: tabstop=4 expandtab shiftwidth=4 diff -r 76ab0f064cc3 -r 1b5d0e034a6e simplify/viewer.py --- a/simplify/viewer.py Sun Jul 29 20:33:09 2007 +0200 +++ b/simplify/viewer.py Sun Jul 29 23:29:19 2007 +0200 @@ -1109,7 +1109,7 @@ param, default = params[i][n] else: param, default = params[i] - if hasattr(subprogram, "paramtypes"): + if subprogram.paramtypes: types.update(subprogram.paramtypes[param]) return self._types_container(types, "types") @@ -1207,7 +1207,7 @@ raises = set() for node in nodes: - if hasattr(node, "raises") and node.raises: + if node.raises: raises.update(node.raises) return self._types_container(raises, "raises")