# HG changeset patch # User Paul Boddie # Date 1152449136 -7200 # Node ID f5043236bf21819d399a8b654d4452189ca300bb # Parent 922988fe36321bcff8c940a493fc984d60a02ed8 Added Raise, fixed TryExcept for empty handler specifications. diff -r 922988fe3632 -r f5043236bf21 simplified.py --- a/simplified.py Sun Jul 09 14:25:16 2006 +0200 +++ b/simplified.py Sun Jul 09 14:45:36 2006 +0200 @@ -112,6 +112,7 @@ class Conditional(Node): "A conditional node consisting of a test and outcomes." class Try(Node): "A try...except...else...finally grouping node." class Except(Node): "An exception handler node." +class Raise(Node): "An exception raising node." class And(Node): "A conjunction of expressions." class Or(Node): "A disjunction of expressions." class Not(Node): "A negation of an expression." diff -r 922988fe3632 -r f5043236bf21 simplify.py --- a/simplify.py Sun Jul 09 14:25:16 2006 +0200 +++ b/simplify.py Sun Jul 09 14:45:36 2006 +0200 @@ -30,17 +30,16 @@ """ A simplifying visitor for AST nodes. - Covered: AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break, + Covered: And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, Break, CallFunc, Class, Const, Continue, Dict, Discard, For, From, Function, Getattr, Global, If, Import, Invert, Keyword, Lambda, - List, Module, Name, Pass, Return, Stmt, TryExcept, TryFinally, - Tuple, While, UnaryAdd, UnarySub. + List, Module, Name, Not, Or, Pass, Raise, Return, Stmt, TryExcept, + TryFinally, Tuple, While, UnaryAdd, UnarySub. - Missing: Add, And, Assert, Backquote, Bitand, Bitor, Bitxor, Compare, - Decorators, Div, Ellipsis, Exec, FloorDiv, LeftShift, - ListComp, ListCompFor, ListCompIf, Mod, Mul, Not, Or, Power, Print, - Printnl, Raise, RightShift, Slice, Sliceobj, Sub, Subscript, - Yield. + Missing: Add, Assert, Backquote, Bitand, Bitor, Bitxor, Compare, Decorators, + Div, Ellipsis, Exec, FloorDiv, LeftShift, ListComp, ListCompFor, + ListCompIf, Mod, Mul, Power, Print, Printnl, RightShift, + Slice, Sliceobj, Sub, Subscript, Yield. """ def __init__(self): @@ -141,6 +140,14 @@ result.expr = LoadRef(ref=self.current_subprograms[-1]) return result + def visitRaise(self, raise_): + result = Raise(raise_, expr=self.dispatch(raise_.expr1), traceback=None) + if raise_.expr2 is not None: + result.args = [self.dispatch(raise_.expr2)] + if raise_.expr3 is not None: + result.traceback = self.dispatch(raise_.expr3) + return result + def visitIf(self, if_): result = If(if_, else_=[]) tests = [] @@ -450,8 +457,13 @@ handlers = [] for spec, assign, stmt in tryexcept.handlers: get_exc = Assign() - get_exc.code = [StoreTemp(expr=LoadExc()), self.dispatch(assign), ReleaseTemp()] - handler = Except(spec=self.dispatch(spec)) + get_exc.code = [StoreTemp(expr=LoadExc())] + if assign is not None: + get_exc.code.append(self.dispatch(assign)) + get_exc.code.append(ReleaseTemp()) + handler = Except() + if spec is not None: + handler.spec = self.dispatch(spec) handler.code = [get_exc] + self.dispatch(stmt) handlers.append(handler) result.handlers = handlers diff -r 922988fe3632 -r f5043236bf21 tests/raise.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/raise.py Sun Jul 09 14:45:36 2006 +0200 @@ -0,0 +1,12 @@ +try: + raise Test, x +except: + pass +try: + raise Test(x) +except: + pass +try: + raise Test, x, tb +except: + pass