Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
forked fromnumpy/numpy

Commitc53b0e4

Browse files
committed
BUG: Compare struct arrays with different endian.
Fixes two places where dtypes with fields are compared for *exact*equality where they should be compared for *equivalency*.
1 parentfd0d7d2 commitc53b0e4

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

‎numpy/core/src/multiarray/arrayobject.c‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,16 +1325,16 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
13251325
returnPy_NotImplemented;
13261326
}
13271327

1328-
_res=PyObject_RichCompareBool
1329-
((PyObject*)PyArray_DESCR(self),
1330-
(PyObject*)PyArray_DESCR(array_other),
1331-
Py_EQ);
1332-
if (_res<0) {
1328+
_res=PyArray_CanCastTypeTo(PyArray_DESCR(self),
1329+
PyArray_DESCR(array_other),
1330+
NPY_EQUIV_CASTING);
1331+
if (_res==0) {
13331332
Py_DECREF(result);
13341333
Py_DECREF(array_other);
1335-
returnNULL;
1334+
Py_INCREF(Py_False);
1335+
returnPy_False;
13361336
}
1337-
if (_res) {
1337+
else {
13381338
Py_DECREF(result);
13391339
result=_void_compare(self,array_other,cmp_op);
13401340
}
@@ -1386,16 +1386,16 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
13861386
returnPy_NotImplemented;
13871387
}
13881388

1389-
_res=PyObject_RichCompareBool(
1390-
(PyObject*)PyArray_DESCR(self),
1391-
(PyObject*)PyArray_DESCR(array_other),
1392-
Py_EQ);
1393-
if (_res<0) {
1389+
_res=PyArray_CanCastTypeTo(PyArray_DESCR(self),
1390+
PyArray_DESCR(array_other),
1391+
NPY_EQUIV_CASTING);
1392+
if (_res==0) {
13941393
Py_DECREF(result);
13951394
Py_DECREF(array_other);
1396-
returnNULL;
1395+
Py_INCREF(Py_True);
1396+
returnPy_True;
13971397
}
1398-
if (_res) {
1398+
else {
13991399
Py_DECREF(result);
14001400
result=_void_compare(self,array_other,cmp_op);
14011401
Py_DECREF(array_other);

‎numpy/core/src/multiarray/multiarraymodule.c‎

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,28 +1400,44 @@ array_putmask(PyObject *NPY_UNUSED(module), PyObject *args, PyObject *kwds)
14001400
staticint
14011401
_equivalent_fields(PyObject*field1,PyObject*field2) {
14021402

1403-
intsame,val;
1403+
intval;
1404+
Py_ssize_tppos;
1405+
PyObject*keys,*key;
1406+
PyObject*tuple1,*tuple2;
14041407

14051408
if (field1==field2) {
14061409
return1;
14071410
}
14081411
if (field1==NULL||field2==NULL) {
14091412
return0;
14101413
}
1411-
#if defined(NPY_PY3K)
1412-
val=PyObject_RichCompareBool(field1,field2,Py_EQ);
1413-
if (val!=1||PyErr_Occurred()) {
1414-
#else
1415-
val=PyObject_Compare(field1,field2);
1416-
if (val!=0||PyErr_Occurred()) {
1417-
#endif
1418-
same=0;
1414+
1415+
if (PyDict_Size(field1)!=PyDict_Size(field2)) {
1416+
return0;
14191417
}
1420-
else {
1421-
same=1;
1418+
1419+
/* Iterate over all the fields and compare for equivalency */
1420+
ppos=0;
1421+
while (PyDict_Next(field1,&ppos,&key,&tuple1)) {
1422+
if ((tuple2=PyDict_GetItem(field2,key))==NULL) {
1423+
return0;
1424+
}
1425+
/* Compare the dtype of the field for equivalency */
1426+
if (!PyArray_CanCastTypeTo((PyArray_Descr*)PyTuple_GET_ITEM(tuple1,0),
1427+
(PyArray_Descr*)PyTuple_GET_ITEM(tuple2,0),
1428+
NPY_EQUIV_CASTING)) {
1429+
return0;
1430+
}
1431+
/* Compare the byte position of the field */
1432+
if (PyObject_RichCompareBool(PyTuple_GET_ITEM(tuple1,1),
1433+
PyTuple_GET_ITEM(tuple2,1),
1434+
Py_EQ)!=1) {
1435+
PyErr_Clear();
1436+
return0;
1437+
}
14221438
}
1423-
PyErr_Clear();
1424-
returnsame;
1439+
1440+
return1;
14251441
}
14261442

14271443
/*
@@ -2097,7 +2113,7 @@ array_matrixproduct(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject* kwds)
20972113
PyObject*v,*a,*o=NULL;
20982114
char*kwlist[]= {"a","b","out",NULL };
20992115
PyObject*module;
2100-
2116+
21012117
if (cached_npy_dot==NULL) {
21022118
module=PyImport_ImportModule("numpy.core.multiarray");
21032119
cached_npy_dot= (PyUFuncObject*)PyDict_GetItemString(

‎numpy/core/tests/test_multiarray.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,12 @@ def test_subarray_comparison(self):
623623
y=np.zeros((1,),dtype=[('a', ('f4', (2,))), ('b','i1')])
624624
assert_equal(x==y,False)
625625

626+
# Check that structured arrays that are different only in
627+
# byte-order work
628+
a=np.array([(5,42), (10,1)],dtype=[('a','>i8'), ('b','<f8')])
629+
b=np.array([(5,43), (10,1)],dtype=[('a','<i8'), ('b','>f8')])
630+
assert_equal(a==b, [False,True])
631+
626632

627633
classTestBool(TestCase):
628634
deftest_test_interning(self):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp