From 90cf88d5c9f722f7a679a6c50744384b11e82362 Mon Sep 17 00:00:00 2001 From: Ryan McNair Date: Wed, 16 Dec 2015 04:10:37 +0000 Subject: [PATCH] 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 --- cinder/objects/base.py | 8 +++++++- cinder/tests/unit/objects/test_base.py | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/cinder/objects/base.py b/cinder/objects/base.py index 61498370d..2d012848d 100644 --- a/cinder/objects/base.py +++ b/cinder/objects/base.py @@ -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: diff --git a/cinder/tests/unit/objects/test_base.py b/cinder/tests/unit/objects/test_base.py index 6e95124c3..cb14df5f5 100644 --- a/cinder/tests/unit/objects/test_base.py +++ b/cinder/tests/unit/objects/test_base.py @@ -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')) -- 2.45.2