File: cpp_exception_ptr.pyx

package info (click to toggle)
cython 3.1.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,932 kB
  • sloc: python: 92,172; ansic: 19,275; cpp: 1,407; xml: 1,031; javascript: 511; makefile: 373; sh: 223; sed: 11
file content (53 lines) | stat: -rw-r--r-- 1,281 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# mode: run
# tag: cpp, cpp11

from libcpp.exception cimport (
    exception_ptr,
    exception_ptr_error_handler,
    make_exception_ptr,
    rethrow_exception,
    wrapped_exception_ptr_from_exception,
)

cdef extern from *:
    """
    void raise_runtime_error() {
        throw std::runtime_error("hello");
    }
    """
    void raise_runtime_error() except +exception_ptr_error_handler

cdef extern from "<stdexcept>" namespace "std" nogil:
    cdef cppclass runtime_error:
        runtime_error(const char* s)

def test_custom_error_handler():
    """
    >>> test_custom_error_handler()
    """
    cdef exception_ptr eptr
    try:
        raise_runtime_error()
    except Exception as e:
        eptr = wrapped_exception_ptr_from_exception(e)
        assert eptr
        try:
            rethrow_exception(eptr)
        except RuntimeError as re:  # normal Cython error conversion
            assert re.args[0] == "hello"
        else:
            assert False
    else:
        assert False

def test_make_directly():
    """
    >>> test_make_directly()
    """
    eptr = make_exception_ptr(runtime_error("I'm an error"))
    try:
        rethrow_exception(eptr)
    except RuntimeError as re:
        assert re.args[0] == "I'm an error"
    else:
        assert False