diff options
author | Clemens <chofreither@gmail.com> | 2020-07-07 21:23:47 +0200 |
---|---|---|
committer | Stefan Behnel <stefan_ml@behnel.de> | 2020-07-07 21:24:50 +0200 |
commit | 34ab658bb5ce7c5e854a19a654d9bee9970962cf (patch) | |
tree | 1537e5869e31ea8228465a95246557ed86604ac1 | |
parent | 1910ad2bf27a28e1cb976f8f6a6558f25344e629 (diff) | |
download | cython-34ab658bb5ce7c5e854a19a654d9bee9970962cf.tar.gz |
Always consider 0-sized arrays as C- and F-contiguous (GH-3728)
Fixes https://github.com/cython/cython/issues/2093
-rw-r--r-- | Cython/Utility/MemoryView_C.c | 22 | ||||
-rw-r--r-- | tests/memoryview/relaxed_strides.pyx | 22 |
2 files changed, 35 insertions, 9 deletions
diff --git a/Cython/Utility/MemoryView_C.c b/Cython/Utility/MemoryView_C.c index 9f4828d1f..0a5d8ee2c 100644 --- a/Cython/Utility/MemoryView_C.c +++ b/Cython/Utility/MemoryView_C.c @@ -347,18 +347,22 @@ static int __Pyx_ValidateAndInit_memviewslice( } /* Check axes */ - for (i = 0; i < ndim; i++) { - spec = axes_specs[i]; - if (unlikely(!__pyx_check_strides(buf, i, ndim, spec))) - goto fail; - if (unlikely(!__pyx_check_suboffsets(buf, i, ndim, spec))) + if (buf->len > 0) { + // 0-sized arrays do not undergo these checks since their strides are + // irrelevant and they are always both C- and F-contiguous. + for (i = 0; i < ndim; i++) { + spec = axes_specs[i]; + if (unlikely(!__pyx_check_strides(buf, i, ndim, spec))) + goto fail; + if (unlikely(!__pyx_check_suboffsets(buf, i, ndim, spec))) + goto fail; + } + + /* Check contiguity */ + if (unlikely(buf->strides && !__pyx_verify_contig(buf, ndim, c_or_f_flag))) goto fail; } - /* Check contiguity */ - if (unlikely(buf->strides && !__pyx_verify_contig(buf, ndim, c_or_f_flag))) - goto fail; - /* Initialize */ if (unlikely(__Pyx_init_memviewslice(memview, ndim, memviewslice, new_memview != NULL) == -1)) { diff --git a/tests/memoryview/relaxed_strides.pyx b/tests/memoryview/relaxed_strides.pyx index 3e4ba1543..07e6164dd 100644 --- a/tests/memoryview/relaxed_strides.pyx +++ b/tests/memoryview/relaxed_strides.pyx @@ -62,3 +62,25 @@ def test_zero_sized(array): """ cdef double[::1] a = array return a + +def test_zero_sized_multidim_ccontig(array): + """ + >>> contig = np.ascontiguousarray(np.zeros((4,4,4))[::2, 2:2, ::2]) + >>> _ = test_zero_sized_multidim_ccontig(contig) + + >>> a = np.zeros((4,4,4))[::2, 2:2, ::2] + >>> if NUMPY_HAS_RELAXED_STRIDES: _ = test_zero_sized_multidim_ccontig(a) + """ + cdef double[:, :, ::1] a = array + return a + +def test_zero_sized_multidim_fcontig(array): + """ + >>> contig = np.ascontiguousarray(np.zeros((4,4,4))[::2, 2:2, ::2]) + >>> _ = test_zero_sized_multidim_fcontig(contig) + + >>> a = np.zeros((4,4,4))[::2, 2:2, ::2] + >>> if NUMPY_HAS_RELAXED_STRIDES: _ = test_zero_sized_multidim_fcontig(a) + """ + cdef double[::1, :, :] a = array + return a |