Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guard for writing to read only arrays from ufunc calls #3605

Merged
merged 1 commit into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion numba/tests/npyufunc/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import numpy as np

from numba import unittest_support as unittest
from numba import float32
from numba import float32, jit
from numba.npyufunc import Vectorize
from numba.errors import TypingError
from ..support import tag, TestCase


Expand Down Expand Up @@ -100,6 +101,26 @@ def test_implicit_broadcasting(self):
self.assertPreciseEqual(ufunc(a, broadcasting_b),
a + broadcasting_b)

def test_ufunc_exception_on_write_to_readonly(self):
z = np.ones(10)
z.flags.writeable = False # flip write bit

tests = []
expect = "ufunc 'sin' called with an explicit output that is read-only"
tests.append((jit(nopython=True), TypingError, expect))
tests.append((jit(nopython=False), ValueError,
"output array is read-only"))

for dec, exc, msg in tests:
def test(x):
a = np.ones(x.shape, x.dtype) # do not copy RO attribute from x
np.sin(a, x)

with self.assertRaises(exc) as raises:
dec(test)(z)

self.assertIn(msg, str(raises.exception))


if __name__ == '__main__':
unittest.main()
4 changes: 4 additions & 0 deletions numba/typing/npydecl.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def _handle_inputs(cls, ufunc, args, kws):
msg = "ufunc '{0}' called with an explicit output that is not an array"
raise TypingError(msg=msg.format(ufunc.__name__))

if not all(output.mutable for output in explicit_outputs):
msg = "ufunc '{0}' called with an explicit output that is read-only"
raise TypingError(msg=msg.format(ufunc.__name__))

# find the kernel to use, based only in the input types (as does NumPy)
base_types = [x.dtype if isinstance(x, types.ArrayCompatible) else x
for x in args]
Expand Down