1# mode: run
2# tag: cpp, werror
3
4cdef int raise_py_error() except *:
5    raise TypeError("custom")
6
7cdef extern from "cpp_exceptions_helper.h":
8    cdef int raise_int_raw "raise_int"(bint fire) except +
9    cdef int raise_int_value "raise_int"(bint fire) except +ValueError
10    cdef int raise_int_custom "raise_int"(bint fire) except +raise_py_error
11
12    cdef int raise_index_raw "raise_index"(bint fire) except +
13    cdef int raise_index_value "raise_index"(bint fire) except +ValueError
14    cdef int raise_index_custom "raise_index"(bint fire) except +raise_py_error
15
16    cdef void raise_domain_error() except +
17    cdef void raise_ios_failure() except +
18    cdef void raise_memory() except +
19    cdef void raise_overflow() except +
20    cdef void raise_range_error() except +
21    cdef void raise_typeerror() except +
22    cdef void raise_underflow() except +
23
24    cdef raise_or_throw(bint py) except +
25    cdef int raise_or_throw_int(bint py) except +*
26
27    cdef cppclass Foo:
28        int bar_raw "bar"(bint fire) except +
29        int bar_value "bar"(bint fire) except +ValueError
30        int bar_custom "bar"(bint fire) except +raise_py_error
31
32
33def test_domain_error():
34    """
35    >>> test_domain_error()
36    Traceback (most recent call last):
37    ...
38    ValueError: domain_error
39    """
40    raise_domain_error()
41
42def test_ios_failure():
43    """
44    >>> try: test_ios_failure()
45    ... except (IOError, OSError): pass
46    """
47    raise_ios_failure()
48
49def test_memory():
50    """
51    >>> test_memory()
52    Traceback (most recent call last):
53    ...
54    MemoryError
55    """
56    # Re-raise the exception without a description string because we can't
57    # rely on the implementation-defined value of what() in the doctest.
58    try:
59        raise_memory()
60    except MemoryError:
61        raise MemoryError
62
63def test_overflow():
64    """
65    >>> test_overflow()
66    Traceback (most recent call last):
67    ...
68    OverflowError: overflow_error
69    """
70    raise_overflow()
71
72def test_range_error():
73    """
74    >>> test_range_error()
75    Traceback (most recent call last):
76    ...
77    ArithmeticError: range_error
78    """
79    raise_range_error()
80
81def test_typeerror():
82    """
83    >>> test_typeerror()
84    Traceback (most recent call last):
85    ...
86    TypeError
87    """
88    # Re-raise the exception without a description string because we can't
89    # rely on the implementation-defined value of what() in the doctest.
90    try:
91        raise_typeerror()
92    except TypeError:
93        raise TypeError
94
95def test_underflow():
96    """
97    >>> test_underflow()
98    Traceback (most recent call last):
99    ...
100    ArithmeticError: underflow_error
101    """
102    raise_underflow()
103
104def test_func_that_can_raise_or_throw(bint py):
105    """
106    >>> test_func_that_can_raise_or_throw(0)
107    Traceback (most recent call last):
108    ...
109    RuntimeError: oopsie
110    >>> test_func_that_can_raise_or_throw(1)
111    Traceback (most recent call last):
112    ...
113    ValueError: oopsie
114    """
115    raise_or_throw(py)
116
117def test_func_that_can_raise_or_throw_c_return(bint py):
118    """
119    >>> test_func_that_can_raise_or_throw_c_return(0)
120    Traceback (most recent call last):
121    ...
122    RuntimeError: oopsie
123    >>> test_func_that_can_raise_or_throw_c_return(1)
124    Traceback (most recent call last):
125    ...
126    ValueError: oopsie
127    """
128    raise_or_throw_int(py)
129
130def test_int_raw(bint fire):
131    """
132    >>> test_int_raw(False)
133    >>> test_int_raw(True)
134    Traceback (most recent call last):
135    ...
136    RuntimeError: Unknown exception
137    """
138    raise_int_raw(fire)
139
140def test_int_value(bint fire):
141    """
142    >>> test_int_value(False)
143    >>> test_int_value(True)
144    Traceback (most recent call last):
145    ...
146    ValueError
147    """
148    raise_int_value(fire)
149
150def test_int_custom(bint fire):
151    """
152    >>> test_int_custom(False)
153    >>> test_int_custom(True)
154    Traceback (most recent call last):
155    ...
156    TypeError: custom
157    """
158    raise_int_custom(fire)
159
160def test_index_raw(bint fire):
161    """
162    >>> test_index_raw(False)
163    >>> test_index_raw(True)
164    Traceback (most recent call last):
165    ...
166    IndexError: c++ error
167    """
168    raise_index_raw(fire)
169
170def test_index_value(bint fire):
171    """
172    >>> test_index_value(False)
173    >>> test_index_value(True)
174    Traceback (most recent call last):
175    ...
176    ValueError: c++ error
177    """
178    raise_index_value(fire)
179
180def test_index_custom(bint fire):
181    """
182    >>> test_index_custom(False)
183    >>> test_index_custom(True)
184    Traceback (most recent call last):
185    ...
186    TypeError: custom
187    """
188    raise_index_custom(fire)
189
190def test_cppclass_method_raw(bint fire):
191    """
192    >>> test_cppclass_method_raw(False)
193    >>> test_cppclass_method_raw(True)
194    Traceback (most recent call last):
195    ...
196    RuntimeError: Unknown exception
197    """
198    foo = new Foo()
199    try:
200        foo.bar_raw(fire)
201    finally:
202        del foo
203
204def test_cppclass_method_value(bint fire):
205    """
206    >>> test_cppclass_method_value(False)
207    >>> test_cppclass_method_value(True)
208    Traceback (most recent call last):
209    ...
210    ValueError
211    """
212    foo = new Foo()
213    try:
214        foo.bar_value(fire)
215    finally:
216        del foo
217
218def test_cppclass_method_custom(bint fire):
219    """
220    >>> test_cppclass_method_custom(False)
221    >>> test_cppclass_method_custom(True)
222    Traceback (most recent call last):
223    ...
224    TypeError: custom
225    """
226    foo = new Foo()
227    try:
228        foo.bar_custom(fire)
229    finally:
230        del foo
231