APIDictWrapper getitem lookups fail for non str
The APIDictWrapper class within openstack-dashboards/api/base.py can't handle the "in" operator correctly. Also, it will fail if a non-string is passed into it's __getitem__ method. (Which is what is called from a dict lookup.) The get method also fails on non-strings. Change-Id: Iaad66d3c6deb81d0570b1a8c6ddf580024fa46e6 Closes-Bug: 1326512
This commit is contained in:
parent
c28c221f77
commit
e3bf9c4061
@ -119,14 +119,20 @@ class APIDictWrapper(object):
|
||||
def __getitem__(self, item):
|
||||
try:
|
||||
return getattr(self, item)
|
||||
except AttributeError as e:
|
||||
except (AttributeError, TypeError) as e:
|
||||
# caller is expecting a KeyError
|
||||
raise KeyError(e)
|
||||
|
||||
def __contains__(self, item):
|
||||
try:
|
||||
return hasattr(self, item)
|
||||
except TypeError:
|
||||
return False
|
||||
|
||||
def get(self, item, default=None):
|
||||
try:
|
||||
return getattr(self, item)
|
||||
except AttributeError:
|
||||
except (AttributeError, TypeError):
|
||||
return default
|
||||
|
||||
def __repr__(self):
|
||||
|
@ -112,6 +112,38 @@ class APIDictWrapperTests(test.TestCase):
|
||||
|
||||
self.assertEqual('retValue', resource.get('baz', 'retValue'))
|
||||
|
||||
def test_get_with_non_str(self):
|
||||
resource = APIDict.get_instance()
|
||||
self.assertNotIn(0, resource._attrs,
|
||||
msg="Test assumption broken. "
|
||||
"Find new missing attribute.")
|
||||
self.assertIsNone(resource.get(0))
|
||||
self.assertEqual('retValue', resource.get(0, 'retValue'))
|
||||
|
||||
def test_get_item_non_str(self):
|
||||
resource = APIDict.get_instance()
|
||||
self.assertNotIn(0, resource._attrs,
|
||||
msg="Test assumption broken. "
|
||||
"Find new missing attribute.")
|
||||
with self.assertRaises(KeyError):
|
||||
resource[0]
|
||||
|
||||
def test_in_not_there_str(self):
|
||||
resource = APIDict.get_instance()
|
||||
self.assertNotIn('missing', resource._attrs,
|
||||
msg="Test assumption broken. "
|
||||
"Find new missing attribute.")
|
||||
# We're primarily interested in this test NOT raising a TypeError.
|
||||
self.assertFalse('missing' in resource)
|
||||
|
||||
def test_in_not_there_non_str(self):
|
||||
resource = APIDict.get_instance()
|
||||
self.assertNotIn(0, resource._attrs,
|
||||
msg="Test assumption broken. "
|
||||
"Find new missing attribute.")
|
||||
# We're primarily interested in this test NOT raising a TypeError.
|
||||
self.assertFalse(0 in resource)
|
||||
|
||||
|
||||
class ApiHelperTests(test.TestCase):
|
||||
"""Tests for functions that don't use one of the api objects."""
|
||||
|
Loading…
Reference in New Issue
Block a user