Fix ComparableVersionedObject in python 3.4
There is a difference when __eq__ is called and not called in python 2.7 and 3.4. In python 3.4, when an object is compared to something else, e.g. obj != None, the __eq__ is called to do the comparison, whereas in python 2.7, it does not. The following patch allows ComparableVersionedObject to be used in python 3.4 by checking if the object has obj_to_primitive() as an attribute. Change-Id: I9b989bb07505842651bc73c2ccc1552b14307a68 Closes-Bug: #1469864
This commit is contained in:
parent
cccbcf9fbd
commit
91184e1047
|
@ -678,7 +678,14 @@ class ComparableVersionedObject(object):
|
|||
def __eq__(self, obj):
|
||||
# FIXME(inc0): this can return incorrect value if we consider partially
|
||||
# loaded objects from db and fields which are dropped out differ
|
||||
return self.obj_to_primitive() == obj.obj_to_primitive()
|
||||
if hasattr(obj, 'obj_to_primitive'):
|
||||
return self.obj_to_primitive() == obj.obj_to_primitive()
|
||||
return NotImplemented
|
||||
|
||||
def __ne__(self, obj):
|
||||
if hasattr(obj, 'obj_to_primitive'):
|
||||
return self.obj_to_primitive() != obj.obj_to_primitive()
|
||||
return NotImplemented
|
||||
|
||||
|
||||
class VersionedObjectDictCompat(object):
|
||||
|
|
|
@ -1322,11 +1322,17 @@ class _TestObject(object):
|
|||
version_manifest=None)
|
||||
|
||||
def test_comparable_objects(self):
|
||||
class NonVersionedObject(object):
|
||||
pass
|
||||
|
||||
obj1 = MyComparableObj(foo=1)
|
||||
obj2 = MyComparableObj(foo=1)
|
||||
obj3 = MyComparableObj(foo=2)
|
||||
obj4 = NonVersionedObject()
|
||||
self.assertTrue(obj1 == obj2)
|
||||
self.assertFalse(obj1 == obj3)
|
||||
self.assertFalse(obj1 == obj4)
|
||||
self.assertNotEqual(obj1, None)
|
||||
|
||||
def test_compound_clone(self):
|
||||
obj = MyCompoundObject()
|
||||
|
|
Loading…
Reference in New Issue