4
from nogil_other cimport voidexceptnogil_in_other_pxd
7
from StringIO import StringIO
9
from io import StringIO
24
cdef void f(int x) nogil:
29
cdef int g(int x) nogil:
34
cdef void release_gil_in_nogil() nogil:
35
# This should generate valid code with/without the GIL
39
cpdef void release_gil_in_nogil2() nogil:
40
# This should generate valid code with/without the GIL
44
def test_release_gil_in_nogil():
46
>>> test_release_gil_in_nogil()
49
release_gil_in_nogil()
51
release_gil_in_nogil2()
52
release_gil_in_nogil()
53
release_gil_in_nogil2()
55
cdef void get_gil_in_nogil() nogil:
59
cpdef void get_gil_in_nogil2() nogil:
63
def test_get_gil_in_nogil():
65
>>> test_get_gil_in_nogil()
74
cdef int with_gil_func() except -1 with gil:
75
raise Exception("error!")
77
cdef int nogil_func() except -1 nogil:
80
def test_nogil_exception_propagation():
82
>>> test_nogil_exception_propagation()
83
Traceback (most recent call last):
91
cdef int write_unraisable() noexcept nogil:
98
>>> print(test_unraisable()) # doctest: +ELLIPSIS
100
Exception...ignored...
103
old_stderr = sys.stderr
104
stderr = sys.stderr = StringIO()
108
sys.stderr = old_stderr
109
return stderr.getvalue().strip()
112
cdef int initialize_array() nogil:
113
cdef int[4] a = [1, 2, 3, 4]
114
return a[0] + a[1] + a[2] + a[3]
116
cdef int copy_array() nogil:
119
return a[0] + a[1] + a[2] + a[3]
121
cdef double copy_array2() nogil:
122
cdef double[4] x = [1.0, 3.0, 5.0, 7.0]
125
return y[0] + y[1] + y[2] + y[3]
127
cdef double copy_array3() nogil:
128
cdef double[4] x = [2.0, 4.0, 6.0, 8.0]
131
return y[0] + y[1] + y[2] + y[3]
133
cdef void copy_array_exception(int n) nogil:
134
cdef double[5] a = [1,2,3,4,5]
138
def test_initalize_array():
140
>>> test_initalize_array()
143
return initialize_array()
145
def test_copy_array():
147
>>> test_copy_array()
152
def test_copy_array2():
154
>>> test_copy_array2()
159
def test_copy_array3():
161
>>> test_copy_array3()
166
def test_copy_array_exception(n):
168
>>> test_copy_array_exception(20)
169
Traceback (most recent call last):
171
ValueError: Assignment to slice of wrong length, expected 5, got 20
173
copy_array_exception(n)
175
def test_copy_array_exception_nogil(n):
177
>>> test_copy_array_exception_nogil(20)
178
Traceback (most recent call last):
180
ValueError: Assignment to slice of wrong length, expected 5, got 20
184
copy_array_exception(cn)
186
# Should still get a warning even though it's declared in a pxd
187
cdef void voidexceptnogil_in_pxd() nogil:
190
def test_performance_hint_nogil():
192
>>> test_performance_hint_nogil()
195
voidexceptnogil_in_pxd()
196
# The function call should generate a performance hint, but the definition should
197
# not (since it's in an external pxd we don't control)
198
voidexceptnogil_in_other_pxd()
201
cdef int f_in_pxd1() nogil except -1:
204
cdef int f_in_pxd2() nogil: # implicit except -1?
207
def test_declared_in_pxd():
209
>>> test_declared_in_pxd()
212
# no warnings here because we're in the same file as the declaration
217
# Note that we're only able to check the first line of the performance hint
218
_PERFORMANCE_HINTS = """
219
5:18: No exception value declared for 'f_in_pxd1' in pxd file.
220
6:18: No exception value declared for 'f_in_pxd2' in pxd file.
221
20:9: Exception check after calling 'f' will always require the GIL to be acquired.
222
24:0: Exception check on 'f' will always require the GIL to be acquired.
223
34:0: Exception check on 'release_gil_in_nogil' will always require the GIL to be acquired.
224
39:0: Exception check on 'release_gil_in_nogil2' will always require the GIL to be acquired.
225
49:28: Exception check after calling 'release_gil_in_nogil' will always require the GIL to be acquired.
226
51:29: Exception check after calling 'release_gil_in_nogil2' will always require the GIL to be acquired.
227
55:0: Exception check on 'get_gil_in_nogil' will always require the GIL to be acquired.
228
59:0: Exception check on 'get_gil_in_nogil2' will always require the GIL to be acquired.
229
68:24: Exception check after calling 'get_gil_in_nogil' will always require the GIL to be acquired.
230
70:25: Exception check after calling 'get_gil_in_nogil2' will always require the GIL to be acquired.
231
133:0: Exception check on 'copy_array_exception' will always require the GIL to be acquired.
232
184:28: Exception check after calling 'copy_array_exception' will always require the GIL to be acquired.
233
187:0: Exception check on 'voidexceptnogil_in_pxd' will always require the GIL to be acquired.
234
195:30: Exception check after calling 'voidexceptnogil_in_pxd' will always require the GIL to be acquired.
235
198:36: Exception check after calling 'voidexceptnogil_in_other_pxd' will always require the GIL to be acquired.