Skip to content

Commit

Permalink
Fix mutable flag transmission in .astype
Browse files Browse the repository at this point in the history
As title. `np.ndarray.astype` always returns a mutable array.

Fixes numba#3844
  • Loading branch information
stuartarchibald committed Mar 8, 2019
1 parent ccb5705 commit 203c459
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions numba/tests/test_array_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ def check(arr, dtype):
arr = np.arange(16, dtype=np.int32)[::2]
check(arr, np.uint64)

# check read only attr does not get copied
arr = np.arange(16, dtype=np.int32)
arr.flags.writeable = False
check(arr, np.int32)

# Invalid conversion
dt = np.dtype([('x', np.int8)])
with self.assertTypingError() as raises:
Expand Down
4 changes: 3 additions & 1 deletion numba/typing/arraydecl.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,9 @@ def resolve_astype(self, ary, args, kws):
"cannot convert from %s to %s"
% (dtype, ary, ary.dtype, dtype))
layout = ary.layout if ary.layout in 'CF' else 'C'
retty = ary.copy(dtype=dtype, layout=layout)
# reset the write bit irrespective of whether the cast type is the same
# as the current dtype, this replicates numpy
retty = ary.copy(dtype=dtype, layout=layout, readonly=False)
return signature(retty, *args)

@bound_function("array.ravel")
Expand Down

0 comments on commit 203c459

Please sign in to comment.