diff --git a/Lib/datetime.py b/Lib/datetime.py --- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -1065,16 +1065,30 @@ def __eq__(self, other): if isinstance(other, time): - return self._cmp(other) == 0 + mytz = self._tzinfo + ottz = other._tzinfo + myoff = otoff = None + if mytz is ottz: + base_compare = True + else: + myoff = self.utcoffset() + otoff = other.utcoffset() + base_compare = myoff == otoff + + if base_compare: + return (self._hour == other._hour and + self._minute == other._minute and + self._second == other._second and + self._microsecond == other._microsecond) + if myoff is None or otoff is None: + return False + myhhmm = self._hour * 60 + self._minute - myoff//timedelta(minutes=1) + othhmm = other._hour * 60 + other._minute - otoff//timedelta(minutes=1) + return (myhhmm == othhmm and self._second == other._second and + self._microsecond == other._microsecond) else: return False - def __ne__(self, other): - if isinstance(other, time): - return self._cmp(other) != 0 - else: - return True - def __le__(self, other): if isinstance(other, time): return self._cmp(other) <= 0 @@ -1606,7 +1620,30 @@ def __eq__(self, other): if isinstance(other, datetime): - return self._cmp(other) == 0 + mytz = self._tzinfo + ottz = other._tzinfo + myoff = otoff = None + + if mytz is ottz: + base_compare = True + else: + if mytz is not None: + myoff = self.utcoffset() + if ottz is not None: + otoff = other.utcoffset() + base_compare = myoff == otoff + + if base_compare: + return ((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 myoff is None or otoff is None: + return False + # XXX What follows could be done more efficiently... + return self - other == timedelta(0) elif not isinstance(other, date): return NotImplemented else: @@ -1614,7 +1651,7 @@ def __ne__(self, other): if isinstance(other, datetime): - return self._cmp(other) != 0 + return not self == other elif not isinstance(other, date): return NotImplemented else: diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -2508,7 +2508,7 @@ self.assertEqual(t1, t2) self.assertEqual(t1, t3) self.assertEqual(t2, t3) - self.assertRaises(TypeError, lambda: t4 == t5) # mixed tz-aware & naive + self.assertNotEqual(t4, t5) # mixed tz-aware & naive self.assertRaises(TypeError, lambda: t4 < t5) # mixed tz-aware & naive self.assertRaises(TypeError, lambda: t5 < t4) # mixed tz-aware & naive @@ -2660,7 +2660,7 @@ t2 = t2.replace(tzinfo=FixedOffset(None, "")) self.assertEqual(t1, t2) t2 = t2.replace(tzinfo=FixedOffset(0, "")) - self.assertRaises(TypeError, lambda: t1 == t2) + self.assertNotEqual(t1, t2) # In time w/ identical tzinfo objects, utcoffset is ignored. class Varies(tzinfo): @@ -2765,16 +2765,16 @@ microsecond=1) self.assertTrue(t1 > t2) - # Make t2 naive and it should fail. + # Make t2 naive and it should differ. t2 = self.theclass.min - self.assertRaises(TypeError, lambda: t1 == t2) + self.assertNotEqual(t1, t2) self.assertEqual(t2, t2) # It's also naive if it has tzinfo but tzinfo.utcoffset() is None. class Naive(tzinfo): def utcoffset(self, dt): return None t2 = self.theclass(5, 6, 7, tzinfo=Naive()) - self.assertRaises(TypeError, lambda: t1 == t2) + self.assertNotEqual(t1, t2) self.assertEqual(t2, t2) # OTOH, it's OK to compare two of these mixing the two ways of being @@ -3291,7 +3291,7 @@ t2 = t2.replace(tzinfo=FixedOffset(None, "")) self.assertEqual(t1, t2) t2 = t2.replace(tzinfo=FixedOffset(0, "")) - self.assertRaises(TypeError, lambda: t1 == t2) + self.assertNotEqual(t1, t2) # In datetime w/ identical tzinfo objects, utcoffset is ignored. class Varies(tzinfo): diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -3705,6 +3705,14 @@ TIME_GET_MICROSECOND(other); result = diff_to_bool(diff, op); } + else if (op == Py_EQ) { + result = Py_False; + Py_INCREF(result); + } + else if (op == Py_NE) { + result = Py_True; + Py_INCREF(result); + } else { PyErr_SetString(PyExc_TypeError, "can't compare offset-naive and " @@ -4582,6 +4590,14 @@ Py_DECREF(delta); result = diff_to_bool(diff, op); } + else if (op == Py_EQ) { + result = Py_False; + Py_INCREF(result); + } + else if (op == Py_NE) { + result = Py_True; + Py_INCREF(result); + } else { PyErr_SetString(PyExc_TypeError, "can't compare offset-naive and "