1# mode: run
2# tag: exceptions, tryfinally
3
4import sys
5IS_PY3 = sys.version_info[0] >= 3
6
7
8def test_finally_c():
9    """
10    >>> def test_finally_py():
11    ...     try:
12    ...         raise AttributeError()
13    ...     finally:
14    ...         raise KeyError()
15
16    >>> try:
17    ...     test_finally_py()
18    ... except KeyError:
19    ...     print(sys.exc_info()[0] is KeyError or sys.exc_info()[0])
20    ...     if IS_PY3:
21    ...         print(isinstance(sys.exc_info()[1].__context__, AttributeError)
22    ...               or sys.exc_info()[1].__context__)
23    ...     else:
24    ...         print(True)
25    True
26    True
27
28    >>> try:
29    ...     test_finally_c()
30    ... except KeyError:
31    ...     print(sys.exc_info()[0] is KeyError or sys.exc_info()[0])
32    ...     if IS_PY3:
33    ...         print(isinstance(sys.exc_info()[1].__context__, AttributeError)
34    ...               or sys.exc_info()[1].__context__)
35    ...     else:
36    ...         print(True)
37    True
38    True
39    """
40    try:
41        raise AttributeError()
42    finally:
43        raise KeyError()
44