From a1776b9c1f23fbd604114b377434e4269d2f1a2e Mon Sep 17 00:00:00 2001 From: Alistair Coles <alistair.coles@hpe.com> Date: Thu, 28 Jan 2016 16:47:37 +0000 Subject: [PATCH] Let equal Timestamps not be unequal Make the result of Timestamp(x) != Timestamp(x) be False. In python 2.7 this requires the __ne__ method to be defined [1]. "The truth of x==y does not imply that x!=y is false." The functools.total_ordering decorator does not autocreate a __ne__ method. In python 3 the __ne__ method is not required [2]. "By default, __ne__() delegates to __eq__() and inverts the result". This patch puts back the __ne__ method removed in [3]. Whilst no tests fail on master with python2.7, they do on this patch [4] and it seems dangerous to have this absurd behaviour lurking. [1] https://docs.python.org/2/reference/datamodel.html#object.__ne__ [2] https://docs.python.org/3.4/reference/datamodel.html#object.__ne__ [3] Change-Id: Id26777ac2c780316ff10ef7d954c48cc1fd480b5 [4] Change-Id: Ia597cd460bb5fd40aa92e886e3e18a7542603d01 Change-Id: I01fbfa310df3c74390f8e8c2e9ffff81bbf05e47 --- swift/common/utils.py | 5 +++++ test/unit/common/test_utils.py | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/swift/common/utils.py b/swift/common/utils.py index 9f3f5fd289..72da228f42 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -847,6 +847,11 @@ class Timestamp(object): other = Timestamp(other) return self.internal == other.internal + def __ne__(self, other): + if not isinstance(other, Timestamp): + other = Timestamp(other) + return self.internal != other.internal + def __lt__(self, other): if not isinstance(other, Timestamp): other = Timestamp(other) diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index d697ce5d53..f9baa42e9e 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -287,6 +287,10 @@ class TestTimestamp(unittest.TestCase): for value in test_values: self.assertTrue(value != ts) + self.assertIs(True, utils.Timestamp(ts) == ts) # sanity + self.assertIs(False, utils.Timestamp(ts) != utils.Timestamp(ts)) + self.assertIs(False, utils.Timestamp(ts) != ts) + def test_no_force_internal_no_offset(self): """Test that internal is the same as normal with no offset""" with mock.patch('swift.common.utils.FORCE_INTERNAL', new=False):