Skip to content

Commit

Permalink
disallow truncation in types.arraydata
Browse files Browse the repository at this point in the history
If `types.arraydata` is passed a `numpy.array` with dtype `numpy.int64` and the
platform default int type is 32 bit, then `types.arraydata` truncates the array
to `numpy.int32` without warning. This behavior is inherited from
`numpy.astype`. Since this may lead to unexpected behavior, this patch
disallows truncation by passing `casting='safe'` to `numpy.astype`. Deliberate
truncation should now be done before passing the array to `types.arraydata`.
  • Loading branch information
joostvanzwieten committed Nov 21, 2024
1 parent f2a895b commit 41aaeab
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion nutils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def __new__(cls, arg):
return arg
array = numpy.asarray(arg)
dtype = dict(b=bool, u=int, i=int, f=float, c=complex)[array.dtype.kind]
return super().__new__(cls, dtype, array.shape, array.astype(dtype, copy=False).tobytes())
return super().__new__(cls, dtype, array.shape, array.astype(dtype, copy=False, casting='safe').tobytes())

def reshape(self, *shape):
if numpy.prod(shape) != numpy.prod(self.shape):
Expand Down

0 comments on commit 41aaeab

Please sign in to comment.