Commit c3cc3617 authored by Lars Buitinck's avatar Lars Buitinck

C++ exceptions: handle domain error

parent 6291a297
...@@ -8216,20 +8216,21 @@ cpp_exception_utility_code = UtilityCode( ...@@ -8216,20 +8216,21 @@ cpp_exception_utility_code = UtilityCode(
proto = """ proto = """
#ifndef __Pyx_CppExn2PyErr #ifndef __Pyx_CppExn2PyErr
static void __Pyx_CppExn2PyErr() { static void __Pyx_CppExn2PyErr() {
// Catch a handful of different errors here and turn them into the
// equivalent Python errors.
try { try {
if (PyErr_Occurred()) if (PyErr_Occurred())
; // let the latest Python exn pass through and ignore the current one ; // let the latest Python exn pass through and ignore the current one
else else
throw; throw;
} catch (const std::invalid_argument& exn) {
// Catch a handful of different errors here and turn them into the
// equivalent Python errors.
// Change invalid_argument to ValueError
PyErr_SetString(PyExc_ValueError, exn.what());
} catch (const std::bad_alloc& exn) { } catch (const std::bad_alloc& exn) {
PyErr_SetString(PyExc_MemoryError, exn.what()); PyErr_SetString(PyExc_MemoryError, exn.what());
} catch (const std::bad_cast& exn) { } catch (const std::bad_cast& exn) {
PyErr_SetString(PyExc_TypeError, exn.what()); PyErr_SetString(PyExc_TypeError, exn.what());
} catch (const std::domain_error& exn) {
PyErr_SetString(PyExc_ValueError, exn.what());
} catch (const std::invalid_argument& exn) {
PyErr_SetString(PyExc_ValueError, exn.what());
} catch (const std::ios_base::failure& exn) { } catch (const std::ios_base::failure& exn) {
// Unfortunately, in standard C++ we have no way of distinguishing EOF // Unfortunately, in standard C++ we have no way of distinguishing EOF
// from other errors here; be careful with the exception mask // from other errors here; be careful with the exception mask
......
...@@ -12,12 +12,13 @@ cdef extern from "cpp_exceptions_helper.h": ...@@ -12,12 +12,13 @@ cdef extern from "cpp_exceptions_helper.h":
cdef int raise_index_value "raise_index"(bint fire) except +ValueError cdef int raise_index_value "raise_index"(bint fire) except +ValueError
cdef int raise_index_custom "raise_index"(bint fire) except +raise_py_error cdef int raise_index_custom "raise_index"(bint fire) except +raise_py_error
cdef void raise_ios_failure "raise_ios_failure"() except + cdef void raise_domain_error() except +
cdef void raise_memory "raise_memory"() except + cdef void raise_ios_failure() except +
cdef void raise_overflow "raise_overflow"() except + cdef void raise_memory() except +
cdef void raise_range_error "raise_range_error"() except + cdef void raise_overflow() except +
cdef void raise_typeerror "raise_typeerror"() except + cdef void raise_range_error() except +
cdef void raise_underflow "raise_underflow"() except + cdef void raise_typeerror() except +
cdef void raise_underflow() except +
cdef cppclass Foo: cdef cppclass Foo:
int bar_raw "bar"(bint fire) except + int bar_raw "bar"(bint fire) except +
...@@ -25,6 +26,15 @@ cdef extern from "cpp_exceptions_helper.h": ...@@ -25,6 +26,15 @@ cdef extern from "cpp_exceptions_helper.h":
int bar_custom "bar"(bint fire) except +raise_py_error int bar_custom "bar"(bint fire) except +raise_py_error
def test_domain_error():
"""
>>> test_domain_error()
Traceback (most recent call last):
...
ValueError: domain_error
"""
raise_domain_error()
def test_ios_failure(): def test_ios_failure():
""" """
>>> test_ios_failure() >>> test_ios_failure()
......
...@@ -26,6 +26,10 @@ class Foo { ...@@ -26,6 +26,10 @@ class Foo {
} }
}; };
void raise_domain_error() {
throw std::domain_error("domain_error");
}
void raise_ios_failure() { void raise_ios_failure() {
throw std::ios_base::failure("iostream failure"); throw std::ios_base::failure("iostream failure");
} }
......
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