# HG changeset patch # User paulb@localhost.localdomain # Date 1165105517 -3600 # Node ID 7aa4a0baa3aae255fa708eb7685b03c6a0f0d791 # Parent 8097612ea925a9f6686187eed30ba17dae36fc21 Removed the Viewer class and associated functions and exceptions. Provided empty argument list defaults for InvokeFunction. Added nicer viewer annotations. Fixed write attribute access types. diff -r 8097612ea925 -r 7aa4a0baa3aa simplified.py --- a/simplified.py Sun Dec 03 00:51:30 2006 +0100 +++ b/simplified.py Sun Dec 03 01:25:17 2006 +0100 @@ -338,9 +338,11 @@ "A function or method invocation." def __init__(self, *args, **kw): + self.args = [] + self.star = None + self.dstar = None Invoke.__init__(self, *args, **kw) - if hasattr(self, "args"): - self.set_args(self.args) + self.set_args(self.args) self.share_locals = 0 def set_args(self, args): diff -r 8097612ea925 -r 7aa4a0baa3aa simplify.py --- a/simplify.py Sun Dec 03 00:51:30 2006 +0100 +++ b/simplify.py Sun Dec 03 01:25:17 2006 +0100 @@ -223,9 +223,9 @@ if not in_sequence: expr = LoadTemp() else: - expr = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next"), star=None, dstar=None, args=[]) + expr = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next")) result = Assign(asslist, 1) - store = StoreTemp(expr=InvokeFunction(expr=LoadAttr(name="__iter__", expr=expr), star=None, dstar=None, args=[])) + store = StoreTemp(expr=InvokeFunction(expr=LoadAttr(name="__iter__", expr=expr))) release = ReleaseTemp() result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] return result @@ -236,7 +236,7 @@ if not in_sequence: return LoadTemp() else: - return InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next"), star=None, dstar=None, args=[]) + return InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next")) def visitAssName(self, assname, in_sequence=0): expr = self._visitAssNameOrAttr(assname, in_sequence) @@ -650,7 +650,7 @@ body=[ Assign( code=[ - StoreTemp(expr=InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next"), args=[], star=None, dstar=None)), + StoreTemp(expr=InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="next"))), self.dispatch(for_.assign), ReleaseTemp() ]) @@ -684,22 +684,26 @@ # Obtain an iterator for the sequence involved. # Then, make an invocation of the subprogram. - result = Assign(for_, 1) - result.code = [ - StoreTemp( - expr=InvokeFunction( - expr=LoadAttr( - name="__iter__", - expr=self.dispatch(for_.list) - ), - args=[], - star=None, - dstar=None - ) - ), - InvokeBlock(expr=LoadRef(ref=subprogram)), - ReleaseTemp() - ] + result = Assign(for_, 1, + code=[ + StoreTemp( + expr=InvokeFunction( + expr=LoadAttr( + name="__iter__", + expr=self.dispatch(for_.list) + ) + ) + ), + InvokeBlock(expr=LoadRef(ref=subprogram)), + ReleaseTemp() + ] + ) + + # Make nice annotations for the viewer. + + for_._iter_call = result.code[0].expr + for_._next_call = subprogram.code[0].body[0].code[0].expr + return result def visitFrom(self, from_): @@ -747,14 +751,14 @@ if has_star: star = ( function.argnames[npositional], - InvokeFunction(expr=LoadName(name="list"), args=[], star=None, dstar=None) + InvokeFunction(expr=LoadName(name="list")) ) else: star = None if has_dstar: dstar = ( function.argnames[npositional + has_star], - InvokeFunction(expr=LoadName(name="dict"), args=[], star=None, dstar=None) + InvokeFunction(expr=LoadName(name="dict")) ) else: dstar = None @@ -853,9 +857,7 @@ expr=self.dispatch(compare), name="__bool__" ), - args=[], - star=None, - dstar=None) + ) ) test.body = self.dispatch(stmt) nodes.append(test) @@ -921,9 +923,6 @@ expr=expr, name="__bool__" ), - args=[], - star=None, - dstar=None ) if not_ is not None: result = Not(not_, 1, expr=invocation) @@ -968,7 +967,7 @@ if node is not last: nodes.append(StoreTemp(expr=expr)) - invocation = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="__bool__"), args=[], star=None, dstar=None) + invocation = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="__bool__")) test = Conditional(test=invocation, body=[ReturnFromBlock(expr=LoadTemp())]) nodes.append(test) @@ -1328,9 +1327,6 @@ expr=LoadAttr( expr=self.dispatch(while_.test), name="__bool__"), - args=[], - star=None, - dstar=None ), body=self.dispatch(while_.body) + [ InvokeBlock( @@ -1358,6 +1354,11 @@ result = InvokeBlock(while_, 1, expr=LoadRef(ref=subprogram) ) + + # Make nice annotations for the viewer. + + while_._test_call = subprogram.code[0].test + return result # Convenience methods. @@ -1412,8 +1413,16 @@ self.current_subprograms.pop() self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram - result = InvokeBlock(produces_result=1) - result.expr = LoadRef(ref=subprogram) + result = InvokeBlock( + produces_result=1, + expr=LoadRef(ref=subprogram) + ) + + # Make nice annotations for the viewer. + + binary._left_call = subprogram.code[0].body[0].expr + binary._right_call = subprogram.code[0].handler[0].body[0].expr + return result def _visitBuiltin(self, builtin, name): @@ -1421,16 +1430,19 @@ return result def _visitUnary(self, unary, name): - return InvokeFunction(unary, 1, + result = InvokeFunction(unary, 1, expr=LoadAttr( expr=self.dispatch(unary.expr), name=name - ), - args=[], - star=None, - dstar=None + ) ) + # Make nice annotations for the viewer. + + unary._unary_call = result + + return result + # Convenience functions. def simplify(filename, builtins=0): diff -r 8097612ea925 -r 7aa4a0baa3aa test.py --- a/test.py Sun Dec 03 00:51:30 2006 +0100 +++ b/test.py Sun Dec 03 01:25:17 2006 +0100 @@ -14,10 +14,7 @@ fixnames.fix(module, builtins) if "-a" in sys.argv: - try: - aa([module], builtins) - except AnnotationError, exc: - viewer.report(exc) + aa([module], builtins) if "-d" in sys.argv: viewer.makedocs(module, builtins) diff -r 8097612ea925 -r 7aa4a0baa3aa viewer.py --- a/viewer.py Sun Dec 03 00:51:30 2006 +0100 +++ b/viewer.py Sun Dec 03 01:25:17 2006 +0100 @@ -27,75 +27,8 @@ import os import textwrap -# Exceptions. - -class ViewerError(SimplifiedError): - - "An error in viewing." - - pass - # Classes. -class Viewer(ASTVisitor): - - """ - A viewing visitor for AST nodes. - """ - - def __init__(self, stream): - ASTVisitor.__init__(self) - self.cached_files = {} - self.printed_lines = {} - self.visitor = self - self.stream = stream - - def process(self, module): - self.dispatch(module) - - def dispatch(self, node): - self.dispatch_only(node) - ASTVisitor.dispatch(self, node) - - def dispatch_only(self, node, every_time=0): - self.print_line(getattr(node, "filename", None), getattr(node, "lineno", None), every_time) - - def print_line(self, filename, lineno, every_time): - last_printed = self.printed_lines.get(filename, 0) - if lineno > last_printed or every_time: - self.stream.write(self.get_line(filename, lineno)) - self.printed_lines[filename] = lineno - - def get_line(self, filename, lineno): - if filename is None or lineno is None: - return "" - - if self.cached_files.has_key(filename): - lines = self.cached_files[filename] - else: - f = open(filename) - try: - self.cached_files[filename] = lines = f.readlines() - finally: - f.close() - - try: - return lines[lineno - 1] - except IndexError: - return "" - - def report(self, exc): - self.stream.write("Exception was:\n\n" + str(exc.exc) + "\n\n") - self.stream.write("Nodes:\n\n") - for node in exc.nodes: - self.stream.write(repr(node) + "\n") - if node is not None and hasattr(node, "original"): - self.dispatch_only(node.original, every_time=1) - if hasattr(exc.nodes[0], "original"): - self.stream.write("\nOriginal node was:\n\n" + repr(exc.nodes[0].original) + "\n") - self.stream.write("\nSimplified node was:\n\n") - exc.nodes[0].pprint(stream=self.stream) - # HTML-related output production. html_header = """ @@ -213,16 +146,6 @@ self.dispatch(module) self.stream.write(html_footer) - def dispatch(self, node): - ASTVisitor.dispatch(self, node) - #try: - # ASTVisitor.dispatch(self, node) - #except ViewerError, exc: - # exc.add(node) - # raise - #except Exception, exc: - # raise ViewerError(exc, node) - def visitModule(self, node): self.default(node) @@ -304,14 +227,14 @@ self.stream.write("\n") self._keyword("for") self._popup_start() - self._invocations(node._node.code[1].expr.ref.code[0].body[0].code[0].expr) # Link to next call in subprogram. + self._invocations(node._next_call) self._popup_end() self.stream.write("\n") self.dispatch(node.assign) self.stream.write("\n") self._keyword("in") self._popup_start() - self._invocations(node._node.code[0].expr) # Link to __iter__ call. + self._invocations(node._iter_call) self._popup_end() self.stream.write("\n") self.dispatch(node.list) @@ -505,7 +428,7 @@ self.stream.write("\n") self._keyword("while") self._popup_start() - self._invocations(node._node.expr.ref.code[0].test) + self._invocations(node._test_call) self._popup_end() self.stream.write("\n") self.dispatch(node.test) @@ -533,8 +456,8 @@ self.stream.write(symbol) self._popup_start() self.stream.write("
\n") - self._invocations_list(node._node.body[0].expr) # NOTE: See visitAdd in simplify. - self._invocations_list(node._node.handler[0].body[0].expr) # NOTE: See visitAdd in simplify. + self._invocations_list(node._left_call) + self._invocations_list(node._right_call) self.stream.write("
\n") self._popup_end() self.stream.write("\n") @@ -547,7 +470,7 @@ self.stream.write(symbol) self._popup_start() self.stream.write("
\n") - self._invocations_list(node._node) # NOTE: See _visitUnary in simplify. + self._invocations_list(node._unary_call) self.stream.write("
\n") self._popup_end() self.stream.write("\n") @@ -874,14 +797,22 @@ if node.types: self._types_list(node.types) else: - self.stream.write("
\n") - self.stream.write("no types\n") - self.stream.write("
\n") + self._no_types() + elif hasattr(node, "writes"): + if node.writes: + self._types_list(flatten(node.writes.values())) + else: + self._no_types() else: self.stream.write("
\n") self.stream.write("unvisited\n") self.stream.write("
\n") + def _no_types(self): + self.stream.write("
\n") + self.stream.write("no types\n") + self.stream.write("
\n") + def _types_list(self, types, style_class="types"): self.stream.write("
\n" % style_class) for type in types: @@ -922,14 +853,6 @@ # Convenience functions. -def view(module, stream=None): - viewer = Viewer(stream or sys.stdout) - viewer.process(module.original) - -def report(exc): - viewer = Viewer(stream or sys.stdout) - viewer.report(exc) - def browse(module, stream=None): browser = Browser(stream or sys.stdout) browser.process(module.original) @@ -943,13 +866,10 @@ stream.close() def makedocs(module, builtins): - try: - dirname = "%s-docs" % module.name - if not os.path.exists(dirname): - os.mkdir(dirname) - makedoc(module, os.path.join(dirname, "%s%shtml" % (module.name, os.path.extsep))) - makedoc(builtins, os.path.join(dirname, "%s%shtml" % (builtins.name, os.path.extsep))) - except ViewerError, exc: - raise + dirname = "%s-docs" % module.name + if not os.path.exists(dirname): + os.mkdir(dirname) + makedoc(module, os.path.join(dirname, "%s%shtml" % (module.name, os.path.extsep))) + makedoc(builtins, os.path.join(dirname, "%s%shtml" % (builtins.name, os.path.extsep))) # vim: tabstop=4 expandtab shiftwidth=4