[Zope3-checkins] CVS: Zope3/src/datetime/tests - test_datetime.py:1.24

Tim Peters tim.one@comcast.net
Fri, 24 Jan 2003 14:05:28 -0500


Update of /cvs-repository/Zope3/src/datetime/tests
In directory cvs.zope.org:/tmp/cvs-serv9455/src/datetime/tests

Modified Files:
	test_datetime.py 
Log Message:
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.


=== Zope3/src/datetime/tests/test_datetime.py 1.23 => 1.24 ===
--- Zope3/src/datetime/tests/test_datetime.py:1.23	Thu Jan 23 16:38:07 2003
+++ Zope3/src/datetime/tests/test_datetime.py	Fri Jan 24 14:05:25 2003
@@ -878,6 +878,38 @@
             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)
+        self.assertEqual(cmp(their, our), 0)
+
+        # But date and datetime comparison aise NotImplemented instead if the
+        # other object has a timetuple attr.
+        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)