Change name of misc.ensure_dict to misc.safe_copy_dict
Change-Id: I2ff3b9bb4d0b8143163e4bf370f10c74e79dbd31
This commit is contained in:

committed by
Thomas Goirand

parent
989aafb160
commit
1ba18d81ad
@@ -46,7 +46,7 @@ class Conductor(object):
|
|||||||
self._name = name
|
self._name = name
|
||||||
self._jobboard = jobboard
|
self._jobboard = jobboard
|
||||||
self._engine = engine
|
self._engine = engine
|
||||||
self._engine_options = misc.ensure_dict(engine_options)
|
self._engine_options = misc.safe_copy_dict(engine_options)
|
||||||
self._persistence = persistence
|
self._persistence = persistence
|
||||||
self._lock = threading.RLock()
|
self._lock = threading.RLock()
|
||||||
self._notifier = notifier.Notifier()
|
self._notifier = notifier.Notifier()
|
||||||
|
@@ -49,7 +49,7 @@ class Runtime(object):
|
|||||||
self._storage = storage
|
self._storage = storage
|
||||||
self._compilation = compilation
|
self._compilation = compilation
|
||||||
self._atom_cache = {}
|
self._atom_cache = {}
|
||||||
self._options = misc.ensure_dict(options)
|
self._options = misc.safe_copy_dict(options)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _walk_edge_deciders(graph, atom):
|
def _walk_edge_deciders(graph, atom):
|
||||||
|
@@ -42,7 +42,7 @@ class Engine(object):
|
|||||||
self._flow = flow
|
self._flow = flow
|
||||||
self._flow_detail = flow_detail
|
self._flow_detail = flow_detail
|
||||||
self._backend = backend
|
self._backend = backend
|
||||||
self._options = misc.ensure_dict(options)
|
self._options = misc.safe_copy_dict(options)
|
||||||
self._notifier = notifier.Notifier()
|
self._notifier = notifier.Notifier()
|
||||||
self._atom_notifier = notifier.Notifier()
|
self._atom_notifier = notifier.Notifier()
|
||||||
|
|
||||||
|
@@ -342,7 +342,7 @@ class TestIterable(test.TestCase):
|
|||||||
self.assertTrue(misc.is_iterable(dict()))
|
self.assertTrue(misc.is_iterable(dict()))
|
||||||
|
|
||||||
|
|
||||||
class TestEnsureDict(testscenarios.TestWithScenarios):
|
class TestSafeCopyDict(testscenarios.TestWithScenarios):
|
||||||
scenarios = [
|
scenarios = [
|
||||||
('none', {'original': None, 'expected': {}}),
|
('none', {'original': None, 'expected': {}}),
|
||||||
('empty_dict', {'original': {}, 'expected': {}}),
|
('empty_dict', {'original': {}, 'expected': {}}),
|
||||||
@@ -351,11 +351,18 @@ class TestEnsureDict(testscenarios.TestWithScenarios):
|
|||||||
]
|
]
|
||||||
|
|
||||||
def test_expected(self):
|
def test_expected(self):
|
||||||
self.assertEqual(self.expected, misc.ensure_dict(self.original))
|
self.assertEqual(self.expected, misc.safe_copy_dict(self.original))
|
||||||
self.assertFalse(self.expected is misc.ensure_dict(self.original))
|
self.assertFalse(self.expected is misc.safe_copy_dict(self.original))
|
||||||
|
|
||||||
|
def test_mutated_post_copy(self):
|
||||||
|
a = {"a": "b"}
|
||||||
|
a_2 = misc.safe_copy_dict(a)
|
||||||
|
a['a'] = 'c'
|
||||||
|
self.assertEqual("b", a_2['a'])
|
||||||
|
self.assertEqual("c", a['a'])
|
||||||
|
|
||||||
|
|
||||||
class TestEnsureDictRaises(testscenarios.TestWithScenarios):
|
class TestSafeCopyDictRaises(testscenarios.TestWithScenarios):
|
||||||
scenarios = [
|
scenarios = [
|
||||||
('list', {'original': [1, 2], 'exception': TypeError}),
|
('list', {'original': [1, 2], 'exception': TypeError}),
|
||||||
('tuple', {'original': (1, 2), 'exception': TypeError}),
|
('tuple', {'original': (1, 2), 'exception': TypeError}),
|
||||||
@@ -363,4 +370,4 @@ class TestEnsureDictRaises(testscenarios.TestWithScenarios):
|
|||||||
]
|
]
|
||||||
|
|
||||||
def test_exceptions(self):
|
def test_exceptions(self):
|
||||||
self.assertRaises(self.exception, misc.ensure_dict, self.original)
|
self.assertRaises(self.exception, misc.safe_copy_dict, self.original)
|
||||||
|
@@ -584,8 +584,13 @@ def is_iterable(obj):
|
|||||||
isinstance(obj, collections.Iterable))
|
isinstance(obj, collections.Iterable))
|
||||||
|
|
||||||
|
|
||||||
def ensure_dict(obj):
|
def safe_copy_dict(obj):
|
||||||
"""Copy an existing dictionary or default to empty dict...."""
|
"""Copy an existing dictionary or default to empty dict...
|
||||||
|
|
||||||
|
This will return a empty dict if given object is falsey, otherwise it
|
||||||
|
will create a dict of the given object (which if provided a dictionary
|
||||||
|
object will make a shallow copy of that object).
|
||||||
|
"""
|
||||||
if not obj:
|
if not obj:
|
||||||
return {}
|
return {}
|
||||||
# default to a shallow copy to avoid most ownership issues
|
# default to a shallow copy to avoid most ownership issues
|
||||||
|
Reference in New Issue
Block a user