summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_datetime.py
diff options
context:
space:
mode:
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)