From 4cdf18dc8f5ea139aba0984e2e24e24050b30e91 Mon Sep 17 00:00:00 2001 From: Barry Pederson Date: Sun, 17 Jul 2011 23:45:50 -0500 Subject: [PATCH] Fix Issue 35 , comparing a Message to something else (such as None), raises an AttributeError instead of returning false. Thanks to Tarrant....@gmail.com for reporting this. --- amqplib/client_0_8/basic_message.py | 3 ++- amqplib/client_0_8/serialization.py | 3 ++- tests/client_0_8/test_basic_message.py | 18 ++++++++++++++++++ tests/client_0_8/test_serialization.py | 15 ++++++++++++++- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/amqplib/client_0_8/basic_message.py b/amqplib/client_0_8/basic_message.py index 8e0db2a..e7d7dfd 100644 --- a/amqplib/client_0_8/basic_message.py +++ b/amqplib/client_0_8/basic_message.py @@ -128,4 +128,5 @@ def __eq__(self, other): which isn't compared. """ - return super(Message, self).__eq__(other) and (self.body == other.body) + return super(Message, self).__eq__(other) \ + and hasattr(other, 'body') and (self.body == other.body) diff --git a/amqplib/client_0_8/serialization.py b/amqplib/client_0_8/serialization.py index 5a1843c..83f4bfc 100644 --- a/amqplib/client_0_8/serialization.py +++ b/amqplib/client_0_8/serialization.py @@ -440,7 +440,8 @@ def __eq__(self, other): content object. """ - return (self.properties == other.properties) + return hasattr(other, 'properties') \ + and (self.properties == other.properties) def __getattr__(self, name): diff --git a/tests/client_0_8/test_basic_message.py b/tests/client_0_8/test_basic_message.py index 71d2fab..9406f1b 100755 --- a/tests/client_0_8/test_basic_message.py +++ b/tests/client_0_8/test_basic_message.py @@ -50,6 +50,24 @@ def check_proplist(self, msg): self.assertEqual(msg, new_msg) + def test_eq(self): + msg = Message('hello', content_type='text/plain') + self.assertNotEqual(msg, None) + + # + # Make sure that something that looks vaguely + # like a Message doesn't raise an Attribute + # error when compared to a Message, and instead + # returns False + # + class FakeMsg(object): pass + + fake_msg = FakeMsg() + fake_msg.properties = {'content_type': 'text/plain'} + + self.assertNotEqual(msg, fake_msg) + + def test_pickle(self): msg = Message( 'some body' * 200000, diff --git a/tests/client_0_8/test_serialization.py b/tests/client_0_8/test_serialization.py index 7c799fc..2b3116e 100755 --- a/tests/client_0_8/test_serialization.py +++ b/tests/client_0_8/test_serialization.py @@ -34,7 +34,7 @@ import settings -from amqplib.client_0_8.serialization import AMQPReader, AMQPWriter +from amqplib.client_0_8.serialization import AMQPReader, AMQPWriter, GenericContent class TestSerialization(unittest.TestCase): if sys.version_info[0] >= 3: @@ -347,6 +347,19 @@ def test_table_multi(self): r = AMQPReader(s) self.assertEqual(r.read_table(), val) + # + # GenericContent + # + def test_generic_content_eq(self): + msg_1 = GenericContent(dummy='foo') + msg_2 = GenericContent(dummy='foo') + msg_3 = GenericContent(dummy='bar') + + self.assertEqual(msg_1, msg_1) + self.assertEqual(msg_1, msg_2) + self.assertNotEqual(msg_1, msg_3) + self.assertNotEqual(msg_1, None) + def main(): suite = unittest.TestLoader().loadTestsFromTestCase(TestSerialization)