Index: datetime.py =================================================================== --- datetime.py (revision 82424) +++ datetime.py (working copy) @@ -19,9 +19,6 @@ import time as _time import math as _math -def _cmp(x, y): - return 0 if x == y else 1 if x > y else -1 - MINYEAR = 1 MAXYEAR = 9999 _MAXORDINAL = 3652059 # date.max.toordinal() @@ -570,43 +567,49 @@ def __eq__(self, other): if isinstance(other, timedelta): - return self.__cmp(other) == 0 + x, y = self._normalize(other) + return x == y else: return False def __ne__(self, other): if isinstance(other, timedelta): - return self.__cmp(other) != 0 + x, y = self._normalize(other) + return x != y else: return True def __le__(self, other): if isinstance(other, timedelta): - return self.__cmp(other) <= 0 + x, y = self._normalize(other) + return x <= y else: _cmperror(self, other) def __lt__(self, other): if isinstance(other, timedelta): - return self.__cmp(other) < 0 + x, y = self._normalize(other) + return x < y else: _cmperror(self, other) def __ge__(self, other): if isinstance(other, timedelta): - return self.__cmp(other) >= 0 + x, y = self._normalize(other) + return x >= y else: _cmperror(self, other) def __gt__(self, other): if isinstance(other, timedelta): - return self.__cmp(other) > 0 + x, y = self._normalize(other) + return x > y else: _cmperror(self, other) - def __cmp(self, other): + def _normalize(self, other): assert isinstance(other, timedelta) - return _cmp(self.__getstate(), other.__getstate()) + return self.__getstate(), other.__getstate() def __hash__(self): return hash(self.__getstate()) @@ -792,39 +795,45 @@ def __eq__(self, other): if isinstance(other, date): - return self.__cmp(other) == 0 + x, y = self._normalize(other) + return x == y return NotImplemented def __ne__(self, other): if isinstance(other, date): - return self.__cmp(other) != 0 + x, y = self._normalize(other) + return x != y return NotImplemented def __le__(self, other): if isinstance(other, date): - return self.__cmp(other) <= 0 + x, y = self._normalize(other) + return x <= y return NotImplemented def __lt__(self, other): if isinstance(other, date): - return self.__cmp(other) < 0 + x, y = self._normalize(other) + return x < y return NotImplemented def __ge__(self, other): if isinstance(other, date): - return self.__cmp(other) >= 0 + x, y = self._normalize(other) + return x >= y return NotImplemented def __gt__(self, other): if isinstance(other, date): - return self.__cmp(other) > 0 + x, y = self._normalize(other) + return x > y return NotImplemented - def __cmp(self, other): + def _normalize(self, other): assert isinstance(other, date) 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)) + return (y, m, d), (y2, m2, d2) def __hash__(self): "Hash." @@ -1059,41 +1068,50 @@ def __eq__(self, other): if isinstance(other, time): - return self.__cmp(other) == 0 + x, y = self._normalize(other) + return x == y else: return False def __ne__(self, other): if isinstance(other, time): - return self.__cmp(other) != 0 + x, y = self._normalize(other) + return x != y else: return True def __le__(self, other): if isinstance(other, time): - return self.__cmp(other) <= 0 + x, y = self._normalize(other) + return x <= y else: _cmperror(self, other) def __lt__(self, other): if isinstance(other, time): - return self.__cmp(other) < 0 + x, y = self._normalize(other) + return x < y else: _cmperror(self, other) def __ge__(self, other): if isinstance(other, time): - return self.__cmp(other) >= 0 + x, y = self._normalize(other) + return x >= y else: _cmperror(self, other) def __gt__(self, other): if isinstance(other, time): - return self.__cmp(other) > 0 + x, y = self._normalize(other) + return x > y else: _cmperror(self, other) - def __cmp(self, other): + def _cmpkey(self): + return self.__hour, self.__minute, self.__second, self.__microsecond + + def _normalize(self, other): assert isinstance(other, time) mytz = self._tzinfo ottz = other._tzinfo @@ -1107,17 +1125,13 @@ base_compare = myoff == otoff if base_compare: - return _cmp((self.__hour, self.__minute, self.__second, - self.__microsecond), - (other.__hour, other.__minute, other.__second, - other.__microsecond)) + return self._cmpkey(), other._cmpkey() if myoff is None or otoff is None: - # XXX Buggy in 2.2.2. raise TypeError("cannot compare naive and aware times") myhhmm = self.__hour * 60 + self.__minute - myoff othhmm = other.__hour * 60 + other.__minute - otoff - return _cmp((myhhmm, self.__second, self.__microsecond), - (othhmm, other.__second, other.__microsecond)) + return ((myhhmm, self.__second, self.__microsecond), + (othhmm, other.__second, other.__microsecond)) def __hash__(self): """Hash.""" @@ -1598,7 +1612,8 @@ def __eq__(self, other): if isinstance(other, datetime): - return self.__cmp(other) == 0 + x, y = self._normalize(other) + return x == y elif not isinstance(other, date): return NotImplemented else: @@ -1606,7 +1621,8 @@ def __ne__(self, other): if isinstance(other, datetime): - return self.__cmp(other) != 0 + x, y = self._normalize(other) + return x != y elif not isinstance(other, date): return NotImplemented else: @@ -1614,7 +1630,8 @@ def __le__(self, other): if isinstance(other, datetime): - return self.__cmp(other) <= 0 + x, y = self._normalize(other) + return x <= y elif not isinstance(other, date): return NotImplemented else: @@ -1622,7 +1639,8 @@ def __lt__(self, other): if isinstance(other, datetime): - return self.__cmp(other) < 0 + x, y = self._normalize(other) + return x < y elif not isinstance(other, date): return NotImplemented else: @@ -1630,7 +1648,8 @@ def __ge__(self, other): if isinstance(other, datetime): - return self.__cmp(other) >= 0 + x, y = self._normalize(other) + return x >= y elif not isinstance(other, date): return NotImplemented else: @@ -1638,13 +1657,20 @@ def __gt__(self, other): if isinstance(other, datetime): - return self.__cmp(other) > 0 + x, y = self._normalize(other) + return x > y elif not isinstance(other, date): return NotImplemented else: _cmperror(self, other) - def __cmp(self, other): + def _cmpkey(self): + return (self.__year, self.__month, self.__day, + self.__hour, self.__minute, self.__second, + self.__microsecond) + + def _normalize(self, other): + """returns a pair of objects that compare the same as self and other""" assert isinstance(other, datetime) mytz = self._tzinfo ottz = other._tzinfo @@ -1659,22 +1685,15 @@ otoff = other._utcoffset() base_compare = myoff == otoff - if base_compare: - return _cmp((self.__year, self.__month, self.__day, - self.__hour, self.__minute, self.__second, - self.__microsecond), - (other.__year, other.__month, other.__day, - other.__hour, other.__minute, other.__second, - other.__microsecond)) + if base_compare: + return self._cmpkey(), other._cmpkey() if myoff is None or otoff is None: - # XXX Buggy in 2.2.2. raise TypeError("cannot compare naive and aware datetimes") - # XXX What follows could be done more efficiently... - diff = self - other # this will take offsets into account - if diff.days < 0: - return -1 - return diff and 1 or 0 + # We cannot simply convert self and other to UTC + # because that can overflow datetime + return (self - other), timedelta(0) + def __add__(self, other): "Add a datetime and a timedelta." if not isinstance(other, timedelta):