Skip to content

Commit

Permalink
BUG: Fix maximum/minimum for object arrays in Python 3.
Browse files Browse the repository at this point in the history
Use PyObject_RichCompareBool instead of PyObject_Cmp.
  • Loading branch information
charris committed Oct 23, 2011
1 parent d7b59fe commit 4daf949
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 26 deletions.
30 changes: 30 additions & 0 deletions numpy/core/src/umath/funcs.inc.src
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,34 @@ npy_ObjectPower(PyObject *x, PyObject *y)
return PyNumber_Power(x, y, Py_None);
}


#if defined(NPY_PY3K)
/**begin repeat
* #Kind = Max, Min#
* #OP = Py_GE, Py_LE#
*/
static PyObject *
npy_Object@Kind@(PyObject *i1, PyObject *i2)
{
PyObject *result;
int cmp;

cmp = PyObject_RichCompareBool(i1, i2, @OP@);
if (cmp < 0) {
return NULL;
}
if (cmp == 1) {
result = i1;
}
else {
result = i2;
}
Py_INCREF(result);
return result;
}
/**end repeat**/

#else
/**begin repeat
* #Kind = Max, Min#
* #OP = >=, <=#
Expand All @@ -77,6 +105,8 @@ npy_Object@Kind@(PyObject *i1, PyObject *i2)
return result;
}
/**end repeat**/
#endif


/* Emulates Python's 'a or b' behavior */
static PyObject *
Expand Down
50 changes: 24 additions & 26 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,25 +929,24 @@ class TestArgmax(TestCase):
([complex(1, 0), complex(0, 2), complex(0, 1)], 0),
([complex(1, 0), complex(0, 2), complex(1, 1)], 2),

# Fails on 32-bit systems (haven't tested 64-bit) due to y2.038k bug
#([np.datetime64('1923-04-14T12:43:12'),
# np.datetime64('1994-06-21T14:43:15'),
# np.datetime64('2001-10-15T04:10:32'),
# np.datetime64('1995-11-25T16:02:16'),
# np.datetime64('2005-01-04T03:14:12'),
# np.datetime64('2041-12-03T14:05:03')], 5),
([np.datetime64('1923-04-14T12:43:12'),
np.datetime64('1994-06-21T14:43:15'),
np.datetime64('2001-10-15T04:10:32'),
np.datetime64('1995-11-25T16:02:16'),
np.datetime64('2005-01-04T03:14:12'),
np.datetime64('2041-12-03T14:05:03')], 5),
([np.datetime64('1935-09-14T04:40:11'),
np.datetime64('1949-10-12T12:32:11'),
np.datetime64('2010-01-03T05:14:12'),
np.datetime64('2015-11-20T12:20:59'),
np.datetime64('1932-09-23T10:10:13'),
np.datetime64('2014-10-10T03:50:30')], 3),
#([np.datetime64('2059-03-14T12:43:12'),
# np.datetime64('1996-09-21T14:43:15'),
# np.datetime64('2001-10-15T04:10:32'),
# np.datetime64('2022-12-25T16:02:16'),
# np.datetime64('1963-10-04T03:14:12'),
# np.datetime64('2013-05-08T18:15:23')], 0),
([np.datetime64('2059-03-14T12:43:12'),
np.datetime64('1996-09-21T14:43:15'),
np.datetime64('2001-10-15T04:10:32'),
np.datetime64('2022-12-25T16:02:16'),
np.datetime64('1963-10-04T03:14:12'),
np.datetime64('2013-05-08T18:15:23')], 0),

([timedelta(days=5, seconds=14), timedelta(days=2, seconds=35),
timedelta(days=-1, seconds=23)], 0),
Expand Down Expand Up @@ -998,25 +997,24 @@ class TestArgmin(TestCase):
([complex(1, 0), complex(0, 2), complex(0, 1)], 2),
([complex(1, 0), complex(0, 2), complex(1, 1)], 1),

# Fails on 32-bit systems (haven't tested 64-bit) due to y2.038k bug
#([np.datetime64('1923-04-14T12:43:12'),
# np.datetime64('1994-06-21T14:43:15'),
# np.datetime64('2001-10-15T04:10:32'),
# np.datetime64('1995-11-25T16:02:16'),
# np.datetime64('2005-01-04T03:14:12'),
# np.datetime64('2041-12-03T14:05:03')], 0),
([np.datetime64('1923-04-14T12:43:12'),
np.datetime64('1994-06-21T14:43:15'),
np.datetime64('2001-10-15T04:10:32'),
np.datetime64('1995-11-25T16:02:16'),
np.datetime64('2005-01-04T03:14:12'),
np.datetime64('2041-12-03T14:05:03')], 0),
([np.datetime64('1935-09-14T04:40:11'),
np.datetime64('1949-10-12T12:32:11'),
np.datetime64('2010-01-03T05:14:12'),
np.datetime64('2014-11-20T12:20:59'),
np.datetime64('2015-09-23T10:10:13'),
np.datetime64('1932-10-10T03:50:30')], 5),
#([np.datetime64('2059-03-14T12:43:12'),
# np.datetime64('1996-09-21T14:43:15'),
# np.datetime64('2001-10-15T04:10:32'),
# np.datetime64('2022-12-25T16:02:16'),
# np.datetime64('1963-10-04T03:14:12'),
# np.datetime64('2013-05-08T18:15:23')], 4),
([np.datetime64('2059-03-14T12:43:12'),
np.datetime64('1996-09-21T14:43:15'),
np.datetime64('2001-10-15T04:10:32'),
np.datetime64('2022-12-25T16:02:16'),
np.datetime64('1963-10-04T03:14:12'),
np.datetime64('2013-05-08T18:15:23')], 4),

([timedelta(days=5, seconds=14), timedelta(days=2, seconds=35),
timedelta(days=-1, seconds=23)], 2),
Expand Down
10 changes: 10 additions & 0 deletions numpy/core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ def test_complex_nans(self):
out = np.array([nan, nan, nan], dtype=np.complex)
assert_equal(np.maximum(arg1, arg2), out)

def test_object_array(self):
arg1 = np.arange(5, dtype=np.object)
arg2 = arg1 + 1
assert_equal(np.maximum(arg1, arg2), arg2)


class TestMinimum(TestCase):
def test_reduce(self):
Expand Down Expand Up @@ -512,6 +517,11 @@ def test_complex_nans(self):
out = np.array([nan, nan, nan], dtype=np.complex)
assert_equal(np.minimum(arg1, arg2), out)

def test_object_array(self):
arg1 = np.arange(5, dtype=np.object)
arg2 = arg1 + 1
assert_equal(np.minimum(arg1, arg2), arg1)


class TestFmax(TestCase):
def test_reduce(self):
Expand Down

0 comments on commit 4daf949

Please sign in to comment.