# HG changeset patch # User paulb@localhost.localdomain # Date 1172185964 -3600 # Node ID b86339b061e0004d0cc6d96c76450794446ca864 # Parent 95ea3c071ed937bce07437353a2c019d45e28a75 Fixed lambda annotations. Fixed list comprehensions without if clauses. Added assert support and the AssertionError built-in type. Made the simplification process raise SimplifiedError instances when unsupported nodes are encountered. diff -r 95ea3c071ed9 -r b86339b061e0 lib/builtins.py --- a/lib/builtins.py Thu Feb 22 22:06:10 2007 +0100 +++ b/lib/builtins.py Fri Feb 23 00:12:44 2007 +0100 @@ -765,6 +765,9 @@ def __init__(self, *args): pass +class AssertionError(Exception): + __atomic__ = 1 + class AttributeError(Exception): __atomic__ = 1 diff -r 95ea3c071ed9 -r b86339b061e0 simplified.py --- a/simplified.py Thu Feb 22 22:06:10 2007 +0100 +++ b/simplified.py Fri Feb 23 00:12:44 2007 +0100 @@ -109,7 +109,7 @@ ASTVisitor.__init__(self) def default(self, node, *args): - raise ValueError, node.__class__ + raise SimplifiedError, (None, node) def dispatch(self, node, *args): return ASTVisitor.dispatch(self, node, *args) diff -r 95ea3c071ed9 -r b86339b061e0 simplify.py --- a/simplify.py Thu Feb 22 22:06:10 2007 +0100 +++ b/simplify.py Fri Feb 23 00:12:44 2007 +0100 @@ -47,15 +47,15 @@ """ A simplifying visitor for AST nodes. - Covered: Add, And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, - Break, CallFunc, Class, Compare, Const, Continue, Dict, Discard, - Div, FloorDiv, For, From, Function, Getattr, Global, If, Import, - Invert, Keyword, Lambda, List, ListComp, ListCompFor, ListCompIf, - Mod, Module, Mul, Name, Not, Or, Pass, Power, Print, Printnl, - Raise, Return, Slice, Sliceobj, Stmt, Sub, Subscript, TryExcept, - TryFinally, Tuple, While, UnaryAdd, UnarySub. + Covered: Add, And, Assert, AssAttr, AssList, AssName, AssTuple, Assign, + AugAssign, Break, CallFunc, Class, Compare, Const, Continue, Dict, + Discard, Div, FloorDiv, For, From, Function, Getattr, Global, If, + Import, Invert, Keyword, Lambda, List, ListComp, ListCompFor, + ListCompIf, Mod, Module, Mul, Name, Not, Or, Pass, Power, Print, + Printnl, Raise, Return, Slice, Sliceobj, Stmt, Sub, Subscript, + TryExcept, TryFinally, Tuple, While, UnaryAdd, UnarySub. - Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis, + Missing: Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis, Exec, LeftShift, RightShift, Yield. """ @@ -205,6 +205,32 @@ result.expr = LoadRef(ref=subprogram) return result + def visitAssert(self, assert_): + if assert_.fail: + fail_args = [self.dispatch(assert_.fail)] + else: + fail_args = [] + + result = Conditional(assert_, 1, + test=self.dispatch(assert_.test), + body=[], + else_=[ + Raise(assert_, + expr=InvokeFunction(assert_, + expr=LoadName(name="AssertionError"), + args=fail_args, + star=None, + dstar=None + ) + ) + ] + ) + + # Make nice annotations for the viewer. + + assert_._raises = result.else_[0].expr + return result + # Assignments. def visitAssAttr(self, assattr, in_sequence=0): @@ -991,7 +1017,7 @@ # Make nice annotations for the viewer. - function._subprogram = subprogram + lambda_._subprogram = subprogram # Process the lambda contents. @@ -1059,8 +1085,10 @@ body = self._visitListCompFor(node, quals[1:]) if qual.ifs: body = self._visitListCompIf(node, qual.ifs, body) + elif qual.ifs: + body = self._visitListCompIf(node, qual.ifs) else: - body = self._visitListCompIf(node, qual.ifs) + body = self._visitListCompBody(node) return [self._visitFor(qual, body)] def _visitListCompIf(self, node, ifs, expr=None): @@ -1068,17 +1096,7 @@ if len(ifs) > 1: body = self._visitListCompIf(node, ifs[1:], expr) elif expr is None: - body = [ - InvokeFunction( - expr=LoadAttr( - expr=LoadTemp(index="listcomp"), - name="append" - ), - args=[self.dispatch(node.expr)], - star=None, - dstar=None - ) - ] + body = self._visitListCompBody(node) else: body = expr return [ @@ -1095,6 +1113,19 @@ ) ] + def _visitListCompBody(self, node): + return [ + InvokeFunction( + expr=LoadAttr( + expr=LoadTemp(index="listcomp"), + name="append" + ), + args=[self.dispatch(node.expr)], + star=None, + dstar=None + ) + ] + def visitMod(self, mod): return self._visitBinary(mod, "__mod__", "__rmod__") diff -r 95ea3c071ed9 -r b86339b061e0 test.py --- a/test.py Thu Feb 22 22:06:10 2007 +0100 +++ b/test.py Fri Feb 23 00:12:44 2007 +0100 @@ -20,7 +20,7 @@ try: builtins = load(os.path.join("lib", "builtins.py")) module = load(sys.argv[1], builtins, None, importer) - except AnnotationError, exc: + except simplified.SimplifiedError, exc: raise else: if "-d" in sys.argv: diff -r 95ea3c071ed9 -r b86339b061e0 tests/assert.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/assert.py Fri Feb 23 00:12:44 2007 +0100 @@ -0,0 +1,3 @@ +x = 123 +assert x != 456 +assert x != 123, "Failed!" diff -r 95ea3c071ed9 -r b86339b061e0 viewer.py --- a/viewer.py Thu Feb 22 22:06:10 2007 +0100 +++ b/viewer.py Fri Feb 23 00:12:44 2007 +0100 @@ -91,7 +91,8 @@ .operator, .iterator, .call, - .returns + .returns, + .failure { position: relative; } @@ -103,7 +104,8 @@ .operator:hover > .popup, .iterator:hover > .popup, .call:hover > .popup, - .returns:hover > .popup + .returns:hover > .popup, + .failure:hover > .popup { display: block; } @@ -124,15 +126,15 @@ """ A browsing visitor for AST nodes. - Covered: Add, And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, - Break, CallFunc, Class, Compare, Const, Continue, Dict, Discard, - Div, FloorDiv, For, From, Function, Getattr, Global, If, Import, - Keyword, Lambda, List, ListComp, ListCompFor, ListCompIf, Mod, - Module, Mul, Name, Not, Or, Pass, Power, Print, Printnl, Raise, - Return, Slice, Sliceobj, Stmt, Sub, Subscript, TryExcept, + Covered: Add, And, Assert, AssAttr, AssList, AssName, AssTuple, Assign, + AugAssign, Break, CallFunc, Class, Compare, Const, Continue, Dict, + Discard, Div, FloorDiv, For, From, Function, Getattr, Global, If, + Import, Keyword, Lambda, List, ListComp, ListCompFor, ListCompIf, + Mod, Module, Mul, Name, Not, Or, Pass, Power, Print, Printnl, + Raise, Return, Slice, Sliceobj, Stmt, Sub, Subscript, TryExcept, TryFinally, Tuple, UnaryAdd, UnarySub, While. - Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis, + Missing: Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis, Exec, Invert, LeftShift, RightShift, Yield. """ @@ -151,6 +153,20 @@ # Statements. + def visitAssert(self, node): + self.stream.write("
\n") + self.stream.write("\n") + self._keyword("assert") + self._popup( + self._types(node._raises.active()) + ) + self.stream.write("\n") + self.dispatch(node.test) + if node.fail: + self.stream.write(", ") + self.dispatch(node.fail) + self.stream.write("
\n") + def visitAssign(self, node): self.stream.write("
\n") for lvalue in node.nodes: