From: Lisandro Dalcin Date: Sat, 13 Mar 2010 22:39:43 +0000 (-0300) Subject: Python 3 except clauses (ticket #519) X-Git-Tag: 0.13.beta0~299 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1f95bca17679f6ac415d13a3fdf5f1de650c75bc;p=cython.git Python 3 except clauses (ticket #519) --- diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py index 61c68486..0443b118 100644 --- a/Cython/Compiler/Parsing.py +++ b/Cython/Compiler/Parsing.py @@ -1334,6 +1334,14 @@ def p_except_clause(s): if s.sy == ',': s.next() exc_value = p_simple_expr(s) + elif s.sy == 'IDENT' and s.systring == 'as': + ## XXX In Python 3, it should be: + ## s.next() + ## pos2 = s.position() + ## name = p_ident(s) + ## exc_value = ExprNodes.NameNode(pos2, name = name) + s.next() + exc_value = p_simple_expr(s) body = p_suite(s) return Nodes.ExceptClauseNode(pos, pattern = exc_type, target = exc_value, body = body) diff --git a/tests/compile/tryexcept.pyx b/tests/compile/tryexcept.pyx index 6847d2ba..7be361bb 100644 --- a/tests/compile/tryexcept.pyx +++ b/tests/compile/tryexcept.pyx @@ -43,3 +43,44 @@ def f(a, b, c, x): i = 1 except: raise + +def g(a, b, c, x): + cdef int i + a = b + c + + try: + i = 1 + raise x + i = 2 + except a: + i = 3 + + try: + i = 1 + except a: + i = 2 + except b: + i = 3 + + try: + i = 1 + except a as b: + i = 2 + + try: + i = 1 + except a: + i = 2 + except: + i = 3 + + try: + i = 1 + except (a, b) as c[42]: + i = 2 + except (b, a) as c.x: + i = 3 + except: + i = 4 + else: + i = 5 diff --git a/tests/run/moduletryexcept.pyx b/tests/run/moduletryexcept.pyx index e4759ba7..6566db6b 100644 --- a/tests/run/moduletryexcept.pyx +++ b/tests/run/moduletryexcept.pyx @@ -1,6 +1,18 @@ __doc__ = u""" >>> a 2 +>>> b +3 +>>> exc[0].__class__.__name__ +'AttributeError' +>>> exc[1].__class__.__name__ +'KeyError' +>>> exc[2].__class__.__name__ +'IndexError' +>>> exc[3].__class__.__name__ +'ValueError' +>>> exc[3] is val +True """ a = 0 @@ -13,3 +25,71 @@ except KeyError: a = 2 except: a = 3 + +b = 0 + +try: + raise IndexError +except AttributeError: + b = 1 +except KeyError: + b = 2 +except: + b = 3 + +exc = [None]*4 + +try: + raise AttributeError +except AttributeError as e: + exc[0] = e +except KeyError as e: + exc[0] = e +except IndexError as e: + exc[0] = e +except: + exc[0] = e + +try: + raise KeyError +except AttributeError as e: + exc[1] = e +except KeyError as e: + exc[1] = e +except IndexError as e: + exc[1] = e +except: + exc[1] = e + +try: + raise IndexError +except AttributeError as e: + exc[2] = e +except KeyError as e: + exc[2] = e +except IndexError as e: + exc[2] = e +except: + exc[2] = e + +val = None +try: + try: + try: + raise ValueError + except AttributeError as e: + exc[3] = e + except KeyError as e: + exc[3] = e + except IndexError as e: + exc[3] = e + except: + raise + except (AttributeError, + KeyError, + IndexError, + ValueError) as e: + val = e + raise e +except Exception as e: + exc[3] = e