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

disallow truncation in types.arraydata #890

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions nutils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,12 @@
def __new__(cls, arg):
if isinstance(arg, cls):
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())
orig = numpy.asarray(arg)
dtype = dict(b=bool, u=int, i=int, f=float, c=complex)[orig.dtype.kind]
array = orig.astype(dtype, copy=False)
if array.dtype != orig.dtype and not numpy.equal(array, orig).all():
raise ValueError('cannot cast array with dtype {orig.dtype} to native dtype {array.dtype} without truncation')

Check warning on line 394 in nutils/types.py

View workflow job for this annotation

GitHub Actions / Test coverage

Line not covered

Line 394 of `nutils/types.py` is not covered by tests.
return super().__new__(cls, dtype, array.shape, array.tobytes())

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