diff --git a/pymongo/binary.py b/pymongo/binary.py index 4fa020021..3a9ccfacb 100644 --- a/pymongo/binary.py +++ b/pymongo/binary.py @@ -45,12 +45,19 @@ class Binary(str): raise TypeError("subtype must be an instance of int") if subtype >= 256 or subtype < 0: raise ValueError("subtype must be contained in [0, 256)") - binary = str.__new__(cls, data) - binary.__subtype = subtype - return binary + self = str.__new__(cls, data) + self.__subtype = subtype + return self def subtype(self): + """Get the subtype of this binary data. + """ return self.__subtype + def __eq__(self, other): + if isinstance(other, Binary): + return (self.__subtype, str(self)) == (other.__subtype, str(other)) + return NotImplemented + def __repr__(self): return "Binary(%s, %s)" % (str.__repr__(self), self.__subtype) diff --git a/test/test_binary.py b/test/test_binary.py index 5d0dc2f7e..0647418d0 100644 --- a/test/test_binary.py +++ b/test/test_binary.py @@ -50,6 +50,14 @@ class TestBinary(unittest.TestCase): c = Binary("hello", 100) self.assertEqual(c.subtype(), 100) + def test_equality(self): + b = Binary("hello") + c = Binary("hello", 100) + self.assertNotEqual(b, c) + self.assertEqual(c, Binary("hello", 100)) + self.assertEqual(b, Binary("hello")) + self.assertNotEqual(b, Binary("hello ")) + def test_repr(self): b = Binary("hello world") self.assertEqual(repr(b), "Binary('hello world', 2)")