summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_datetime.py
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2003-01-24 22:36:34 (GMT)
committerTim Peters <tim.peters@gmail.com>2003-01-24 22:36:34 (GMT)
commit8d81a012efa7fde5e43b8ea7275c7fc995cf74fa (patch)
tree4fad231a65954388e45221d5ca5a3013897e7efa /Lib/test/test_datetime.py
parentcd63e619b4703ed5701589d367cccb7357d67aa8 (diff)
downloadcpython-8d81a012efa7fde5e43b8ea7275c7fc995cf74fa.zip
cpython-8d81a012efa7fde5e43b8ea7275c7fc995cf74fa.tar.gz
cpython-8d81a012efa7fde5e43b8ea7275c7fc995cf74fa.tar.bz2
date and datetime comparison: when we don't know how to
compare against "the other" argument, we raise TypeError, in order to prevent comparison from falling back to the default (and worse than useless, in this case) comparison by object address. That's fine so far as it goes, but leaves no way for another date/datetime object to make itself comparable to our objects. For example, it leaves Marc-Andre no way to teach mxDateTime dates how to compare against Python dates. Discussion on Python-Dev raised a number of impractical ideas, and the simple one implemented here: when we don't know how to compare against "the other" argument, we raise TypeError *unless* the other object has a timetuple attr. In that case, we return NotImplemented instead, and Python will give the other object a shot at handling the comparison then. Note that comparisons of time and timedelta objects still suffer the original problem, though.
Diffstat (limited to 'Lib/test/test_datetime.py')
-rw-r--r--Lib/test/test_datetime.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py
index 0b9597a..995b6a0 100644
--- a/Lib/test/test_datetime.py
+++ b/Lib/test/test_datetime.py
@@ -880,6 +880,44 @@ class TestDate(unittest.TestCase):
self.assertRaises(TypeError, lambda: badarg > t1)
self.assertRaises(TypeError, lambda: badarg >= t1)
+ def test_mixed_compare(self):
+ our = self.theclass(2000, 4, 5)
+ self.assertRaises(TypeError, cmp, our, 1)
+ self.assertRaises(TypeError, cmp, 1, our)
+
+ class AnotherDateTimeClass(object):
+ def __cmp__(self, other):
+ # Return "equal" so calling this can't be confused with
+ # compare-by-address (which never says "equal" for distinct
+ # objects).
+ return 0
+
+ # This still errors, because date and datetime comparison raise
+ # TypeError instead of NotImplemented when they don't know what to
+ # do, in order to stop comparison from falling back to the default
+ # compare-by-address.
+ their = AnotherDateTimeClass()
+ self.assertRaises(TypeError, cmp, our, their)
+ # Oops: The next stab raises TypeError in the C implementation,
+ # but not in the Python implementation of datetime. The difference
+ # is due to that the Python implementation defines __cmp__ but
+ # the C implementation defines tp_richcompare. This is more pain
+ # to fix than it's worth, so commenting out the test.
+ # self.assertEqual(cmp(their, our), 0)
+
+ # But date and datetime comparison return NotImplemented instead if the
+ # other object has a timetuple attr. This gives the other object a
+ # chance to do the comparison.
+ class Comparable(AnotherDateTimeClass):
+ def timetuple(self):
+ return ()
+
+ their = Comparable()
+ self.assertEqual(cmp(our, their), 0)
+ self.assertEqual(cmp(their, our), 0)
+ self.failUnless(our == their)
+ self.failUnless(their == our)
+
def test_bool(self):
# All dates are considered true.
self.failUnless(self.theclass.min)