Modify VO so that obj.get always defaults to None

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
This commit is contained in:
Ryan McNair 2015-12-16 04:10:37 +00:00
parent b353731d5c
commit 90cf88d5c9
2 changed files with 8 additions and 1 deletions

View File

@ -238,7 +238,13 @@ class CinderObjectDictCompat(base.VersionedObjectDictCompat):
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:

View File

@ -559,6 +559,7 @@ class TestCinderDictObject(test_objects.BaseObjectsTestCase):
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'))