mirror of https://github.com/python/cpython
Comparison for timedelta, time, date and datetime objects: __eq__ and
__ne__ no longer complain if they don't know how to compare to the other thing. If no meaningful way to compare is known, saying "not equal" is sensible. This allows things like if adatetime in some_sequence: and somedict[adatetime] = whatever to work as expected even if some_sequence contains non-datetime objects, or somedict non-datetime keys, because they only call __eq__. It still complains (raises TypeError) for mixed-type comparisons in contexts that require a total ordering, such as list.sort(), use as a key in a BTree-based data structure, and cmp().
This commit is contained in:
parent
275666fd50
commit
07534a607b
|
@ -257,6 +257,11 @@ support certain additions and subtractions with \class{date} and
|
|||
Comparisons of \class{timedelta} objects are supported with the
|
||||
\class{timedelta} object representing the smaller duration considered
|
||||
to be the smaller timedelta.
|
||||
In order to stop mixed-type comparisons from falling back to the
|
||||
default comparison by object address, when a \class{timedelta} object is
|
||||
compared to an object of a different type, \exception{TypeError} is
|
||||
raised unless the comparison is \code{==} or \code{!=}. The latter
|
||||
cases return \constant{False} or \constant{True}, respectively.
|
||||
|
||||
\class{timedelta} objects are hashable (usable as dictionary keys),
|
||||
support efficient pickling, and in Boolean contexts, a \class{timedelta}
|
||||
|
@ -402,6 +407,10 @@ isn't also a \class{date} object. However, \code{NotImplemented}
|
|||
is returned instead if the other comparand has a
|
||||
\method{timetuple} attribute. This hook gives other kinds of
|
||||
date objects a chance at implementing mixed-type comparison.
|
||||
If not, when a \class{date} object is
|
||||
compared to an object of a different type, \exception{TypeError} is
|
||||
raised unless the comparison is \code{==} or \code{!=}. The latter
|
||||
cases return \constant{False} or \constant{True}, respectively.
|
||||
|
||||
\end{description}
|
||||
|
||||
|
@ -743,7 +752,11 @@ the other is aware, \exception{TypeError} is raised. If both
|
|||
\code{NotImplemented} is returned instead if the other comparand
|
||||
has a \method{timetuple} attribute. This hook gives other
|
||||
kinds of date objects a chance at implementing mixed-type
|
||||
comparison.}
|
||||
comparison. If not, when a \class{datetime} object is
|
||||
compared to an object of a different type, \exception{TypeError}
|
||||
is raised unless the comparison is \code{==} or \code{!=}. The
|
||||
latter cases return \constant{False} or \constant{True},
|
||||
respectively.}
|
||||
|
||||
\end{description}
|
||||
|
||||
|
@ -1023,6 +1036,11 @@ Supported operations:
|
|||
comparands are aware and have different \member{tzinfo} members,
|
||||
the comparands are first adjusted by subtracting their UTC offsets
|
||||
(obtained from \code{self.utcoffset()}).
|
||||
In order to stop mixed-type comparisons from falling back to the
|
||||
default comparison by object address, when a \class{time} object is
|
||||
compared to an object of a different type, \exception{TypeError} is
|
||||
raised unless the comparison is \code{==} or \code{!=}. The latter
|
||||
cases return \constant{False} or \constant{True}, respectively.
|
||||
|
||||
\item
|
||||
hash, use as dict key
|
||||
|
|
|
@ -133,10 +133,52 @@ class TestTZInfo(unittest.TestCase):
|
|||
self.assertEqual(derived.utcoffset(None), offset)
|
||||
self.assertEqual(derived.tzname(None), 'cookie')
|
||||
|
||||
#############################################################################
|
||||
# Base clase for testing a particular aspect of timedelta, time, date and
|
||||
# datetime comparisons.
|
||||
|
||||
class HarmlessMixedComparison(unittest.TestCase):
|
||||
# Test that __eq__ and __ne__ don't complain for mixed-type comparisons.
|
||||
|
||||
# Subclasses must define 'theclass', and theclass(1, 1, 1) must be a
|
||||
# legit constructor.
|
||||
|
||||
def test_harmless_mixed_comparison(self):
|
||||
me = self.theclass(1, 1, 1)
|
||||
|
||||
self.failIf(me == ())
|
||||
self.failUnless(me != ())
|
||||
self.failIf(() == me)
|
||||
self.failUnless(() != me)
|
||||
|
||||
self.failUnless(me in [1, 20L, [], me])
|
||||
self.failIf(me not in [1, 20L, [], me])
|
||||
|
||||
self.failUnless([] in [me, 1, 20L, []])
|
||||
self.failIf([] not in [me, 1, 20L, []])
|
||||
|
||||
def test_harmful_mixed_comparison(self):
|
||||
me = self.theclass(1, 1, 1)
|
||||
|
||||
self.assertRaises(TypeError, lambda: me < ())
|
||||
self.assertRaises(TypeError, lambda: me <= ())
|
||||
self.assertRaises(TypeError, lambda: me > ())
|
||||
self.assertRaises(TypeError, lambda: me >= ())
|
||||
|
||||
self.assertRaises(TypeError, lambda: () < me)
|
||||
self.assertRaises(TypeError, lambda: () <= me)
|
||||
self.assertRaises(TypeError, lambda: () > me)
|
||||
self.assertRaises(TypeError, lambda: () >= me)
|
||||
|
||||
self.assertRaises(TypeError, cmp, (), me)
|
||||
self.assertRaises(TypeError, cmp, me, ())
|
||||
|
||||
#############################################################################
|
||||
# timedelta tests
|
||||
|
||||
class TestTimeDelta(unittest.TestCase):
|
||||
class TestTimeDelta(HarmlessMixedComparison):
|
||||
|
||||
theclass = timedelta
|
||||
|
||||
def test_constructor(self):
|
||||
eq = self.assertEqual
|
||||
|
@ -301,15 +343,18 @@ class TestTimeDelta(unittest.TestCase):
|
|||
self.assertEqual(cmp(t1, t2), -1)
|
||||
self.assertEqual(cmp(t2, t1), 1)
|
||||
|
||||
for badarg in 10, 10L, 34.5, "abc", {}, [], ():
|
||||
self.assertRaises(TypeError, lambda: t1 == badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 != badarg)
|
||||
badargs = 10, 10L, 34.5, "abc", {}, [], ()
|
||||
for badarg in badargs:
|
||||
self.assertEqual(t1 == badarg, False)
|
||||
self.assertEqual(t1 != badarg, True)
|
||||
self.assertEqual(badarg == t1, False)
|
||||
self.assertEqual(badarg != t1, True)
|
||||
|
||||
for badarg in badargs:
|
||||
self.assertRaises(TypeError, lambda: t1 <= badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 < badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 > badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 >= badarg)
|
||||
self.assertRaises(TypeError, lambda: badarg == t1)
|
||||
self.assertRaises(TypeError, lambda: badarg != t1)
|
||||
self.assertRaises(TypeError, lambda: badarg <= t1)
|
||||
self.assertRaises(TypeError, lambda: badarg < t1)
|
||||
self.assertRaises(TypeError, lambda: badarg > t1)
|
||||
|
@ -446,7 +491,7 @@ class TestDateOnly(unittest.TestCase):
|
|||
dt2 = dt - delta
|
||||
self.assertEqual(dt2, dt - days)
|
||||
|
||||
class TestDate(unittest.TestCase):
|
||||
class TestDate(HarmlessMixedComparison):
|
||||
# Tests here should pass for both dates and datetimes, except for a
|
||||
# few tests that TestDateTime overrides.
|
||||
|
||||
|
@ -853,15 +898,17 @@ class TestDate(unittest.TestCase):
|
|||
self.assertEqual(cmp(t1, t2), -1)
|
||||
self.assertEqual(cmp(t2, t1), 1)
|
||||
|
||||
for badarg in 10, 10L, 34.5, "abc", {}, [], ():
|
||||
self.assertRaises(TypeError, lambda: t1 == badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 != badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 <= badarg)
|
||||
badargs = 10, 10L, 34.5, "abc", {}, [], ()
|
||||
for badarg in badargs:
|
||||
self.assertEqual(t1 == badarg, False)
|
||||
self.assertEqual(t1 != badarg, True)
|
||||
self.assertEqual(badarg == t1, False)
|
||||
self.assertEqual(badarg != t1, True)
|
||||
|
||||
for badarg in badargs:
|
||||
self.assertRaises(TypeError, lambda: t1 < badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 > badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 >= badarg)
|
||||
self.assertRaises(TypeError, lambda: badarg == t1)
|
||||
self.assertRaises(TypeError, lambda: badarg != t1)
|
||||
self.assertRaises(TypeError, lambda: badarg <= t1)
|
||||
self.assertRaises(TypeError, lambda: badarg < t1)
|
||||
self.assertRaises(TypeError, lambda: badarg > t1)
|
||||
|
@ -1361,7 +1408,7 @@ class TestDateTime(TestDate):
|
|||
alsobog = AlsoBogus()
|
||||
self.assertRaises(ValueError, dt.astimezone, alsobog) # also naive
|
||||
|
||||
class TestTime(unittest.TestCase):
|
||||
class TestTime(HarmlessMixedComparison):
|
||||
|
||||
theclass = time
|
||||
|
||||
|
@ -1431,15 +1478,18 @@ class TestTime(unittest.TestCase):
|
|||
badargs = (10, 10L, 34.5, "abc", {}, [], ())
|
||||
if CMP_BUG_FIXED:
|
||||
badargs += (date(1, 1, 1), datetime(1, 1, 1, 1, 1), timedelta(9))
|
||||
|
||||
for badarg in badargs:
|
||||
self.assertEqual(t1 == badarg, False)
|
||||
self.assertEqual(t1 != badarg, True)
|
||||
self.assertEqual(badarg == t1, False)
|
||||
self.assertEqual(badarg != t1, True)
|
||||
|
||||
for badarg in badargs:
|
||||
self.assertRaises(TypeError, lambda: t1 == badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 != badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 <= badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 < badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 > badarg)
|
||||
self.assertRaises(TypeError, lambda: t1 >= badarg)
|
||||
self.assertRaises(TypeError, lambda: badarg == t1)
|
||||
self.assertRaises(TypeError, lambda: badarg != t1)
|
||||
self.assertRaises(TypeError, lambda: badarg <= t1)
|
||||
self.assertRaises(TypeError, lambda: badarg < t1)
|
||||
self.assertRaises(TypeError, lambda: badarg > t1)
|
||||
|
|
17
Misc/NEWS
17
Misc/NEWS
|
@ -140,6 +140,21 @@ Extension modules
|
|||
datetime objects (e.g., mxDateTime) a chance to intercept the
|
||||
comparison.
|
||||
|
||||
date, time, datetime and timedelta comparison: When the exception
|
||||
for mixed-type comparisons in the last paragraph doesn't apply, if
|
||||
the comparison is == then False is returned, and if the comparison is
|
||||
!= then True is returned. Because dict lookup and the "in" operator
|
||||
only invoke __eq__, this allows, for example,
|
||||
|
||||
if some_datetime in some_sequence:
|
||||
and
|
||||
some_dict[some_timedelta] = whatever
|
||||
|
||||
to work as expected, without raising TypeError just because the
|
||||
sequence is heterogeneous, or the dict has mixed-type keys. [This
|
||||
seems like a good idea to implement for all mixed-type comparisons
|
||||
that don't want to allow falling back to address comparison.]
|
||||
|
||||
The constructors building a datetime from a timestamp could raise
|
||||
ValueError if the platform C localtime()/gmtime() inserted "leap
|
||||
seconds". Leap seconds are ignored now. On such platforms, it's
|
||||
|
@ -271,7 +286,7 @@ Mac
|
|||
|
||||
- There are new dialogs EasyDialogs.AskFileForOpen, AskFileForSave
|
||||
and AskFolder. The old macfs.StandardGetFile and friends are deprecated.
|
||||
|
||||
|
||||
- Most of the standard library now uses pathnames or FSRefs in preference
|
||||
of FSSpecs, and use the underlying Carbon.File and Carbon.Folder modules
|
||||
in stead of macfs. macfs will probably be deprecated in the future.
|
||||
|
|
|
@ -1224,6 +1224,16 @@ diff_to_bool(int diff, int op)
|
|||
return result;
|
||||
}
|
||||
|
||||
/* Raises a "can't compare" TypeError and returns NULL. */
|
||||
static PyObject *
|
||||
cmperror(PyObject *a, PyObject *b)
|
||||
{
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"can't compare %s to %s",
|
||||
a->ob_type->tp_name, b->ob_type->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* Basic object allocation. These allocate Python objects of the right
|
||||
* size and type, and do the Python object-initialization bit. If there's
|
||||
|
@ -1654,21 +1664,23 @@ delta_subtract(PyObject *left, PyObject *right)
|
|||
static PyObject *
|
||||
delta_richcompare(PyDateTime_Delta *self, PyObject *other, int op)
|
||||
{
|
||||
int diff;
|
||||
int diff = 42; /* nonsense */
|
||||
|
||||
if (! PyDelta_CheckExact(other)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"can't compare %s to %s instance",
|
||||
self->ob_type->tp_name, other->ob_type->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
diff = GET_TD_DAYS(self) - GET_TD_DAYS(other);
|
||||
if (diff == 0) {
|
||||
diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other);
|
||||
if (diff == 0)
|
||||
diff = GET_TD_MICROSECONDS(self) -
|
||||
GET_TD_MICROSECONDS(other);
|
||||
if (PyDelta_CheckExact(other)) {
|
||||
diff = GET_TD_DAYS(self) - GET_TD_DAYS(other);
|
||||
if (diff == 0) {
|
||||
diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other);
|
||||
if (diff == 0)
|
||||
diff = GET_TD_MICROSECONDS(self) -
|
||||
GET_TD_MICROSECONDS(other);
|
||||
}
|
||||
}
|
||||
else if (op == Py_EQ || op == Py_NE)
|
||||
diff = 1; /* any non-zero value will do */
|
||||
|
||||
else /* stop this from falling back to address comparison */
|
||||
return cmperror((PyObject *)self, other);
|
||||
|
||||
return diff_to_bool(diff, op);
|
||||
}
|
||||
|
||||
|
@ -2443,23 +2455,23 @@ date_isocalendar(PyDateTime_Date *self)
|
|||
static PyObject *
|
||||
date_richcompare(PyDateTime_Date *self, PyObject *other, int op)
|
||||
{
|
||||
int diff;
|
||||
int diff = 42; /* nonsense */
|
||||
|
||||
if (! PyDate_Check(other)) {
|
||||
if (PyObject_HasAttrString(other, "timetuple")) {
|
||||
/* A hook for other kinds of date objects. */
|
||||
Py_INCREF(Py_NotImplemented);
|
||||
return Py_NotImplemented;
|
||||
}
|
||||
/* Stop this from falling back to address comparison. */
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"can't compare '%s' to '%s'",
|
||||
self->ob_type->tp_name,
|
||||
other->ob_type->tp_name);
|
||||
return NULL;
|
||||
if (PyDate_Check(other))
|
||||
diff = memcmp(self->data, ((PyDateTime_Date *)other)->data,
|
||||
_PyDateTime_DATE_DATASIZE);
|
||||
|
||||
else if (PyObject_HasAttrString(other, "timetuple")) {
|
||||
/* A hook for other kinds of date objects. */
|
||||
Py_INCREF(Py_NotImplemented);
|
||||
return Py_NotImplemented;
|
||||
}
|
||||
diff = memcmp(self->data, ((PyDateTime_Date *)other)->data,
|
||||
_PyDateTime_DATE_DATASIZE);
|
||||
else if (op == Py_EQ || op == Py_NE)
|
||||
diff = 1; /* any non-zero value will do */
|
||||
|
||||
else /* stop this from falling back to address comparison */
|
||||
return cmperror((PyObject *)self, other);
|
||||
|
||||
return diff_to_bool(diff, op);
|
||||
}
|
||||
|
||||
|
@ -3173,12 +3185,13 @@ time_richcompare(PyDateTime_Time *self, PyObject *other, int op)
|
|||
int offset1, offset2;
|
||||
|
||||
if (! PyTime_Check(other)) {
|
||||
if (op == Py_EQ || op == Py_NE) {
|
||||
PyObject *result = op == Py_EQ ? Py_False : Py_True;
|
||||
Py_INCREF(result);
|
||||
return result;
|
||||
}
|
||||
/* Stop this from falling back to address comparison. */
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"can't compare '%s' to '%s'",
|
||||
self->ob_type->tp_name,
|
||||
other->ob_type->tp_name);
|
||||
return NULL;
|
||||
return cmperror((PyObject *)self, other);
|
||||
}
|
||||
if (classify_two_utcoffsets((PyObject *)self, &offset1, &n1, Py_None,
|
||||
other, &offset2, &n2, Py_None) < 0)
|
||||
|
@ -4011,12 +4024,13 @@ datetime_richcompare(PyDateTime_DateTime *self, PyObject *other, int op)
|
|||
Py_INCREF(Py_NotImplemented);
|
||||
return Py_NotImplemented;
|
||||
}
|
||||
if (op == Py_EQ || op == Py_NE) {
|
||||
PyObject *result = op == Py_EQ ? Py_False : Py_True;
|
||||
Py_INCREF(result);
|
||||
return result;
|
||||
}
|
||||
/* Stop this from falling back to address comparison. */
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"can't compare '%s' to '%s'",
|
||||
self->ob_type->tp_name,
|
||||
other->ob_type->tp_name);
|
||||
return NULL;
|
||||
return cmperror((PyObject *)self, other);
|
||||
}
|
||||
|
||||
if (classify_two_utcoffsets((PyObject *)self, &offset1, &n1,
|
||||
|
|
Loading…
Reference in New Issue