]> granicus.if.org Git - python/commitdiff
Issue #15973: Fixed segmentation fault on timezone comparison to other types.
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>
Thu, 20 Sep 2012 20:39:33 +0000 (16:39 -0400)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>
Thu, 20 Sep 2012 20:39:33 +0000 (16:39 -0400)
Lib/datetime.py
Lib/test/datetimetester.py
Modules/_datetimemodule.c

index 65f95d203081445018b6c1c8a418bae749243455..bf23e5002d9ad336b2cc38b3f5ddeb87ac50f565 100644 (file)
@@ -1821,6 +1821,8 @@ class timezone(tzinfo):
         return (self._offset, self._name)
 
     def __eq__(self, other):
+        if type(other) != timezone:
+            return False
         return self._offset == other._offset
 
     def __hash__(self):
index 3fd6799b5798597a35cfcb70c84f0e8eae802a3f..bb18630339d5f6b7d9978af0b8a2d61e0105d829 100644 (file)
@@ -235,6 +235,8 @@ class TestTimeZone(unittest.TestCase):
         self.assertEqual(timezone(-5 * HOUR), timezone(-5 * HOUR, 'EST'))
         with self.assertRaises(TypeError): timezone(ZERO) < timezone(ZERO)
         self.assertIn(timezone(ZERO), {timezone(ZERO)})
+        self.assertTrue(timezone(ZERO) != None)
+        self.assertFalse(timezone(ZERO) ==  None)
 
     def test_aware_datetime(self):
         # test that timezone instances can be used by datetime
index 8f571adf3716599cf28955511cb5e8104d045dd7..444cc00d2c543fe85739dd81550814d2b2263348 100644 (file)
@@ -3243,9 +3243,13 @@ static PyObject *
 timezone_richcompare(PyDateTime_TimeZone *self,
                      PyDateTime_TimeZone *other, int op)
 {
-    if (op != Py_EQ && op != Py_NE) {
-        Py_INCREF(Py_NotImplemented);
-        return Py_NotImplemented;
+    if (op != Py_EQ && op != Py_NE)
+        Py_RETURN_NOTIMPLEMENTED;
+    if (Py_TYPE(other) != &PyDateTime_TimeZoneType) {
+       if (op == Py_EQ)
+           Py_RETURN_FALSE;
+       else
+           Py_RETURN_TRUE;
     }
     return delta_richcompare(self->offset, other->offset, op);
 }