diff options
Diffstat (limited to 'tests/memoryview/memoryview_annotation_typing.py')
-rw-r--r-- | tests/memoryview/memoryview_annotation_typing.py | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/tests/memoryview/memoryview_annotation_typing.py b/tests/memoryview/memoryview_annotation_typing.py index 260e9ffeb..57299c835 100644 --- a/tests/memoryview/memoryview_annotation_typing.py +++ b/tests/memoryview/memoryview_annotation_typing.py @@ -1,7 +1,9 @@ # mode: run -# tag: pep484, numpy, pure3.0 +# tag: pep484, numpy, pure3.7 ##, warnings +from __future__ import annotations # object[:] cannot be evaluated + import cython import numpy @@ -52,3 +54,67 @@ def one_dim_nogil_cfunc(a: cython.double[:]): with cython.nogil: result = _one_dim_nogil_cfunc(a) return result + + +def generic_object_memoryview(a: object[:]): + """ + >>> a = numpy.ones((10,), dtype=object) + >>> generic_object_memoryview(a) + 10 + """ + sum = 0 + for ai in a: + sum += ai + if cython.compiled: + assert cython.typeof(a) == "object[:]", cython.typeof(a) + return sum + + +def generic_object_memoryview_contig(a: object[::1]): + """ + >>> a = numpy.ones((10,), dtype=object) + >>> generic_object_memoryview_contig(a) + 10 + """ + sum = 0 + for ai in a: + sum += ai + if cython.compiled: + assert cython.typeof(a) == "object[::1]", cython.typeof(a) + return sum + + +@cython.cclass +class C: + x: cython.int + + def __init__(self, value): + self.x = value + + +def ext_type_object_memoryview(a: C[:]): + """ + >>> a = numpy.array([C(i) for i in range(10)], dtype=object) + >>> ext_type_object_memoryview(a) + 45 + """ + sum = 0 + for ai in a: + sum += ai.x + if cython.compiled: + assert cython.typeof(a) == "C[:]", cython.typeof(a) + return sum + + +def ext_type_object_memoryview_contig(a: C[::1]): + """ + >>> a = numpy.array([C(i) for i in range(10)], dtype=object) + >>> ext_type_object_memoryview_contig(a) + 45 + """ + sum = 0 + for ai in a: + sum += ai.x + if cython.compiled: + assert cython.typeof(a) == "C[::1]", cython.typeof(a) + return sum |