Skip to content

False positives for warning about writing to broadcast array in cython code #13929

Closed
@mhvk

Description

@mhvk

In astropy, we're getting loads of DeprecationWarning about possibly writing to a broadcast array from cythonized code. They all seem false positives, and may rather reflect something in Cython which I do not understand. But it can be reproduced with this trivial code below. For our particular case, I think this could be solved if one ensured the warning flag was only set if there was actual memory overlap inside the broadcast array.

Sample file broadcast.pyx

import warnings
warnings.filterwarnings('error')

import numpy as np
cimport numpy as np


t, _ = np.broadcast_arrays(np.ones(10), np.array(1.))

# Real code also has the following to ensure that broadcast arrays are copied.
# But that does not make a difference for the error, since the strides of `t` are fine.
# t = np.asarray(t, order='C')

ctypedef np.float64_t DTYPE_t


cdef check(DTYPE_t[::1] t):
    pass


print(t.strides)
check(t)

Corresponding setup.py:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules = cythonize("broadcast.pyx")
)

Compile and run with:

python3 setup.py build_ext --inplace
python3 -c 'import broadcast'

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions