summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorda-woods <dw-git@d-woods.co.uk>2023-03-25 12:17:18 +0000
committerGitHub <noreply@github.com>2023-03-25 12:17:18 +0000
commit2a0d703048db48b31cc7ed84d8fe6aff46c60469 (patch)
tree5c479070633848f8f0693ab7f820e10dc6fc2e6b /tests
parentb8b378712bc74627f266f3ba6c13751af8e1991f (diff)
downloadcython-2a0d703048db48b31cc7ed84d8fe6aff46c60469.tar.gz
Allow soft-complex->double coercion to run without gil (#5287)
It'll only need the GIL on failure (which it can get) and "power of" type maths is the soft of thing that people are likely already doing in nogil blocks
Diffstat (limited to 'tests')
-rw-r--r--tests/run/cpow.pyx48
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/run/cpow.pyx b/tests/run/cpow.pyx
index d2691ba6a..6f050337c 100644
--- a/tests/run/cpow.pyx
+++ b/tests/run/cpow.pyx
@@ -208,6 +208,54 @@ def pythagoras_with_typedef(double a, double b):
return result
+@cython.cpow(False)
+def power_coercion_in_nogil_1(double a, double b):
+ """
+ >>> power_coercion_in_nogil_1(2., 2.)
+ 4.0
+ >>> power_coercion_in_nogil_1(-1., 0.5)
+ Traceback (most recent call last):
+ ...
+ TypeError: Cannot convert 'complex' with non-zero imaginary component to 'double' (this most likely comes from the '**' operator; use 'cython.cpow(True)' to return 'nan' instead of a complex number).
+ """
+ cdef double c
+ with nogil:
+ c = a**b
+ return c
+
+
+cdef double nogil_fun(double x) nogil:
+ return x
+
+def power_coercion_in_nogil_2(double a, double b):
+ """
+ >>> power_coercion_in_nogil_2(2., 2.)
+ 4.0
+ >>> power_coercion_in_nogil_2(-1., 0.5)
+ Traceback (most recent call last):
+ ...
+ TypeError: Cannot convert 'complex' with non-zero imaginary component to 'double' (this most likely comes from the '**' operator; use 'cython.cpow(True)' to return 'nan' instead of a complex number).
+ """
+ c = a**b
+ with nogil:
+ d = nogil_fun(c)
+ return d
+
+
+def power_coercion_in_nogil_3(double a, double b, double c):
+ """
+ >>> power_coercion_in_nogil_3(2., 2., 1.0)
+ 0.25
+ >>> power_coercion_in_nogil_3(-1., 0.5, 1.0)
+ Traceback (most recent call last):
+ ...
+ TypeError: Cannot convert 'complex' with non-zero imaginary component to 'double' (this most likely comes from the '**' operator; use 'cython.cpow(True)' to return 'nan' instead of a complex number).
+ """
+ with nogil:
+ c /= a**b
+ return c
+
+
_WARNINGS = """
63:21: Treating '**' as if 'cython.cpow(True)' since it is directly assigned to a a non-complex C numeric type. This is likely to be fragile and we recommend setting 'cython.cpow' explicitly.
64:32: Treating '**' as if 'cython.cpow(True)' since it is directly assigned to a a non-complex C numeric type. This is likely to be fragile and we recommend setting 'cython.cpow' explicitly.