Commit 1f95bca1 authored by Lisandro Dalcin's avatar Lisandro Dalcin

Python 3 except clauses (ticket #519)

parent cd8f7608
...@@ -1334,6 +1334,14 @@ def p_except_clause(s): ...@@ -1334,6 +1334,14 @@ def p_except_clause(s):
if s.sy == ',': if s.sy == ',':
s.next() s.next()
exc_value = p_simple_expr(s) 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) body = p_suite(s)
return Nodes.ExceptClauseNode(pos, return Nodes.ExceptClauseNode(pos,
pattern = exc_type, target = exc_value, body = body) pattern = exc_type, target = exc_value, body = body)
......
...@@ -43,3 +43,44 @@ def f(a, b, c, x): ...@@ -43,3 +43,44 @@ def f(a, b, c, x):
i = 1 i = 1
except: except:
raise 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
__doc__ = u""" __doc__ = u"""
>>> a >>> a
2 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 a = 0
...@@ -13,3 +25,71 @@ except KeyError: ...@@ -13,3 +25,71 @@ except KeyError:
a = 2 a = 2
except: except:
a = 3 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment