Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
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
23 changes: 6 additions & 17 deletions numpy/core/src/multiarray/getset.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ static int
array_descr_set(PyArrayObject *self, PyObject *arg)
{
PyArray_Descr *newtype = NULL;
npy_intp newdim;
npy_intp nbytes;
int i;
char *msg = "new type not compatible with array.";
PyObject *safe;
Expand Down Expand Up @@ -493,28 +493,17 @@ array_descr_set(PyArrayObject *self, PyObject *arg)
else {
i = 0;
}
if (newtype->elsize < PyArray_DESCR(self)->elsize) {
/*
* if it is compatible increase the size of the
* dimension at end (or at the front for NPY_ARRAY_F_CONTIGUOUS)
*/
if (PyArray_DESCR(self)->elsize % newtype->elsize != 0) {
goto fail;
}
newdim = PyArray_DESCR(self)->elsize / newtype->elsize;
PyArray_DIMS(self)[i] *= newdim;
PyArray_STRIDES(self)[i] = newtype->elsize;
}
else if (newtype->elsize > PyArray_DESCR(self)->elsize) {

if (newtype->elsize != PyArray_DESCR(self)->elsize) {
/*
* Determine if last (or first if NPY_ARRAY_F_CONTIGUOUS) dimension
* is compatible
*/
newdim = PyArray_DIMS(self)[i] * PyArray_DESCR(self)->elsize;
if ((newdim % newtype->elsize) != 0) {
nbytes = PyArray_DIMS(self)[i] * PyArray_DESCR(self)->elsize;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use PyArray_DIM(self, i) here.

if ((nbytes % newtype->elsize) != 0) {
goto fail;
}
PyArray_DIMS(self)[i] = newdim / newtype->elsize;
PyArray_DIMS(self)[i] = nbytes / newtype->elsize;
PyArray_STRIDES(self)[i] = newtype->elsize;
}

Expand Down
10 changes: 10 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -5707,6 +5707,16 @@ def scanView(d1, otype):
'itemsize': 4*psize})
assert_equal(scanView(overlapped, 'p'), [0, 1, 3*psize-1, 3*psize])

def test_coprime_itemsizes(self):
# test we can view using coprime itemsizes, as long as the array length
# is suitable

np.zeros(2, 'p,p,p').view('p,p')
assert_raises(ValueError, np.zeros(3, 'p,p,p').view, 'p,p')

np.zeros(3, 'p,p').view('p,p,p')
assert_raises(ValueError, np.zeros(2, 'p,p').view, 'p,p,p')


class TestArrayPriority(TestCase):
# This will go away when __array_priority__ is settled, meanwhile
Expand Down