]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Modify VO so that obj.get always defaults to None
authorRyan McNair <rdmcnair@us.ibm.com>
Wed, 16 Dec 2015 04:10:37 +0000 (04:10 +0000)
committerRyan McNair <rdmcnair@us.ibm.com>
Wed, 16 Dec 2015 04:32:38 +0000 (04:32 +0000)
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

cinder/objects/base.py
cinder/tests/unit/objects/test_base.py

index 61498370d8af87dea5d207e151a85f4127105035..2d012848d870d37c3253aed06bd238fff3207982 100644 (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:
index 6e95124c334de9e7bf0684bc3fa601bcfd0f5501..cb14df5f5e5dee0db3120f4e896dec08ec672f1a 100644 (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'))