[Zope3-checkins] CVS: Zope3/src/datetime - _datetime.py:1.29

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


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

Modified Files:
	_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/_datetime.py 1.28 => 1.29 ===
--- Zope3/src/datetime/_datetime.py:1.28	Thu Jan 23 21:40:09 2003
+++ Zope3/src/datetime/_datetime.py	Fri Jan 24 14:05:23 2003
@@ -761,6 +761,8 @@
             y, m, d = self.__year, self.__month, self.__day
             y2, m2, d2 = other.__year, other.__month, other.__day
             return cmp((y, m, d), (y2, m2, d2))
+        elif hasattr(other, "timetuple"):
+            return NotImplemented
         else:
             raise TypeError, ("can't compare date to %s instance" %
                               type(other).__name__)
@@ -1433,9 +1435,12 @@
 
     def __cmp__(self, other):
         if not isinstance(other, datetime):
-            # XXX Buggy in 2.2.2.
-            raise TypeError("can't compare '%s' to '%s'" % (
-                            type(self).__name__, type(other).__name__))
+            if hasattr(other, "timetuple"):
+                return NotImplemented
+            else:
+                # XXX Buggy in 2.2.2.
+                raise TypeError("can't compare '%s' to '%s'" % (
+                                type(self).__name__, type(other).__name__))
         mytz = self._tzinfo
         ottz = other._tzinfo
         myoff = otoff = None