summaryrefslogtreecommitdiff
path: root/tests/run/nogil.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'tests/run/nogil.pyx')
-rw-r--r--tests/run/nogil.pyx121
1 files changed, 118 insertions, 3 deletions
diff --git a/tests/run/nogil.pyx b/tests/run/nogil.pyx
index c6f6d8b53..e0fe3f8d2 100644
--- a/tests/run/nogil.pyx
+++ b/tests/run/nogil.pyx
@@ -28,10 +28,50 @@ cdef int g(int x) nogil:
y = x + 42
return y
-cdef int with_gil_func() except 0 with gil:
+cdef void release_gil_in_nogil() nogil:
+ # This should generate valid code with/without the GIL
+ with nogil:
+ pass
+
+cpdef void release_gil_in_nogil2() nogil:
+ # This should generate valid code with/without the GIL
+ with nogil:
+ pass
+
+def test_release_gil_in_nogil():
+ """
+ >>> test_release_gil_in_nogil()
+ """
+ with nogil:
+ release_gil_in_nogil()
+ with nogil:
+ release_gil_in_nogil2()
+ release_gil_in_nogil()
+ release_gil_in_nogil2()
+
+cdef void get_gil_in_nogil() nogil:
+ with gil:
+ pass
+
+cpdef void get_gil_in_nogil2() nogil:
+ with gil:
+ pass
+
+def test_get_gil_in_nogil():
+ """
+ >>> test_get_gil_in_nogil()
+ """
+ with nogil:
+ get_gil_in_nogil()
+ with nogil:
+ get_gil_in_nogil2()
+ get_gil_in_nogil()
+ get_gil_in_nogil2()
+
+cdef int with_gil_func() except -1 with gil:
raise Exception("error!")
-cdef int nogil_func() nogil except 0:
+cdef int nogil_func() except -1 nogil:
with_gil_func()
def test_nogil_exception_propagation():
@@ -45,7 +85,7 @@ def test_nogil_exception_propagation():
nogil_func()
-cdef int write_unraisable() nogil:
+cdef int write_unraisable() noexcept nogil:
with gil:
raise ValueError()
@@ -64,3 +104,78 @@ def test_unraisable():
finally:
sys.stderr = old_stderr
return stderr.getvalue().strip()
+
+
+cdef int initialize_array() nogil:
+ cdef int[4] a = [1, 2, 3, 4]
+ return a[0] + a[1] + a[2] + a[3]
+
+cdef int copy_array() nogil:
+ cdef int[4] a
+ a[:] = [0, 1, 2, 3]
+ return a[0] + a[1] + a[2] + a[3]
+
+cdef double copy_array2() nogil:
+ cdef double[4] x = [1.0, 3.0, 5.0, 7.0]
+ cdef double[4] y
+ y[:] = x[:]
+ return y[0] + y[1] + y[2] + y[3]
+
+cdef double copy_array3() nogil:
+ cdef double[4] x = [2.0, 4.0, 6.0, 8.0]
+ cdef double[4] y
+ y = x
+ return y[0] + y[1] + y[2] + y[3]
+
+cdef void copy_array_exception(int n) nogil:
+ cdef double[5] a = [1,2,3,4,5]
+ cdef double[6] b
+ b[:n] = a
+
+def test_initalize_array():
+ """
+ >>> test_initalize_array()
+ 10
+ """
+ return initialize_array()
+
+def test_copy_array():
+ """
+ >>> test_copy_array()
+ 6
+ """
+ return copy_array()
+
+def test_copy_array2():
+ """
+ >>> test_copy_array2()
+ 16.0
+ """
+ return copy_array2()
+
+def test_copy_array3():
+ """
+ >>> test_copy_array3()
+ 20.0
+ """
+ return copy_array3()
+
+def test_copy_array_exception(n):
+ """
+ >>> test_copy_array_exception(20)
+ Traceback (most recent call last):
+ ...
+ ValueError: Assignment to slice of wrong length, expected 5, got 20
+ """
+ copy_array_exception(n)
+
+def test_copy_array_exception_nogil(n):
+ """
+ >>> test_copy_array_exception_nogil(20)
+ Traceback (most recent call last):
+ ...
+ ValueError: Assignment to slice of wrong length, expected 5, got 20
+ """
+ cdef int cn = n
+ with nogil:
+ copy_array_exception(cn)