summaryrefslogtreecommitdiff
path: root/tests/run/with_gil_automatic.pyx
blob: 425dbbce76cfb89645b3acd31d96044de26fd4f1 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# mode: run
# tag: nogil
# cython: language_level=2

cimport cython


#### print

@cython.test_assert_path_exists(
    "//GILStatNode",
    "//GILStatNode//GILStatNode",
    "//GILStatNode//GILStatNode//PrintStatNode",
)
def test_print_in_nogil_section(x):
    """
    >>> test_print_in_nogil_section(123)
    --123--
    """
    with nogil:
        print f"--{x}--"


@cython.test_assert_path_exists(
    "//GILStatNode",
    "//GILStatNode//PrintStatNode",
)
@cython.test_fail_if_path_exists(
    "//GILStatNode//GILStatNode",
)
cpdef int test_print_in_nogil_func(x) nogil except -1:
    """
    >>> _ = test_print_in_nogil_func(123)
    --123--
    """
    print f"--{x}--"


#### raise

@cython.test_assert_path_exists(
    "//GILStatNode",
    "//GILStatNode//GILStatNode",
    "//GILStatNode//GILStatNode//RaiseStatNode",
)
def test_raise_in_nogil_section(x):
    """
    >>> try: test_raise_in_nogil_section(123)
    ... except ValueError as exc: print(exc)
    ... else: print("NOT RAISED !")
    --123--
    """
    with nogil:
        raise ValueError(f"--{x}--")


@cython.test_assert_path_exists(
    "//GILStatNode",
    "//GILStatNode//RaiseStatNode",
)
@cython.test_fail_if_path_exists(
    "//GILStatNode//GILStatNode",
)
cpdef int test_raise_in_nogil_func(x) nogil except -1:
    """
    >>> test_raise_in_nogil_func(123)
    Traceback (most recent call last):
    ValueError: --123--
    """
    raise ValueError(f"--{x}--")


#### assert

@cython.test_assert_path_exists(
    "//GILStatNode",
    "//GILStatNode//AssertStatNode",
    "//GILStatNode//AssertStatNode//GILStatNode",
    "//GILStatNode//AssertStatNode//GILStatNode//RaiseStatNode",
)
def assert_in_nogil_section(int x):
    """
    >>> assert_in_nogil_section(123)
    >>> assert_in_nogil_section(0)
    Traceback (most recent call last):
    AssertionError
    """
    with nogil:
        assert x


@cython.test_assert_path_exists(
    "//GILStatNode",
    "//GILStatNode//AssertStatNode",
    "//GILStatNode//AssertStatNode//GILStatNode",
    "//GILStatNode//AssertStatNode//GILStatNode//RaiseStatNode",
)
def assert_in_nogil_section_ustring(int x):
    """
    >>> assert_in_nogil_section_string(123)
    >>> assert_in_nogil_section_string(0)
    Traceback (most recent call last):
    AssertionError: failed!
    """
    with nogil:
        assert x, u"failed!"


@cython.test_assert_path_exists(
    "//GILStatNode",
    "//GILStatNode//AssertStatNode",
    "//GILStatNode//AssertStatNode//GILStatNode",
    "//GILStatNode//AssertStatNode//GILStatNode//RaiseStatNode",
)
def assert_in_nogil_section_string(int x):
    """
    >>> assert_in_nogil_section_string(123)
    >>> assert_in_nogil_section_string(0)
    Traceback (most recent call last):
    AssertionError: failed!
    """
    with nogil:
        assert x, "failed!"


@cython.test_assert_path_exists(
    "//AssertStatNode",
    "//AssertStatNode//GILStatNode",
    "//AssertStatNode//GILStatNode//RaiseStatNode",
)
cpdef int assert_in_nogil_func(int x) nogil except -1:
    """
    >>> _ = assert_in_nogil_func(123)
    >>> assert_in_nogil_func(0)
    Traceback (most recent call last):
    AssertionError: failed!
    """
    assert x, "failed!"