Currently, CinderObjectDictCompat.get will default to None if a field
is not in the object, however if a field is present in the object
but the value is not yet set we get an error instead of None. To be
consistent with dict.get() we should default to None if the field
is not present or not set.
Change-Id: Id87efeaaeb2fb44960d8d0df9aa854dd156bff45
not self.obj_attr_is_set(key)):
return value
else:
- return getattr(self, key)
+ try:
+ return getattr(self, key)
+ except (exception.ObjectActionError, NotImplementedError):
+ # Exception when haven't set a value for non-lazy
+ # loadable attribute, but to mimic typical dict 'get'
+ # behavior we should still return None
+ return None
def __contains__(self, name):
try:
obj = self.TestDictObject()
self.assertIsNone(obj.get('non_existing'))
self.assertEqual('val', obj.get('abc', 'val'))
+ self.assertIsNone(obj.get('abc'))
obj.abc = 'val2'
self.assertEqual('val2', obj.get('abc', 'val'))
self.assertEqual(42, obj.get('foo'))