Merge "Change name of misc.ensure_dict to misc.safe_copy_dict"

This commit is contained in:
Jenkins
2015-12-15 05:38:04 +00:00
committed by Gerrit Code Review
5 changed files with 22 additions and 10 deletions

View File

@@ -46,7 +46,7 @@ class Conductor(object):
self._name = name
self._jobboard = jobboard
self._engine = engine
self._engine_options = misc.ensure_dict(engine_options)
self._engine_options = misc.safe_copy_dict(engine_options)
self._persistence = persistence
self._lock = threading.RLock()
self._notifier = notifier.Notifier()

View File

@@ -49,7 +49,7 @@ class Runtime(object):
self._storage = storage
self._compilation = compilation
self._atom_cache = {}
self._options = misc.ensure_dict(options)
self._options = misc.safe_copy_dict(options)
@staticmethod
def _walk_edge_deciders(graph, atom):

View File

@@ -42,7 +42,7 @@ class Engine(object):
self._flow = flow
self._flow_detail = flow_detail
self._backend = backend
self._options = misc.ensure_dict(options)
self._options = misc.safe_copy_dict(options)
self._notifier = notifier.Notifier()
self._atom_notifier = notifier.Notifier()

View File

@@ -342,7 +342,7 @@ class TestIterable(test.TestCase):
self.assertTrue(misc.is_iterable(dict()))
class TestEnsureDict(testscenarios.TestWithScenarios):
class TestSafeCopyDict(testscenarios.TestWithScenarios):
scenarios = [
('none', {'original': None, 'expected': {}}),
('empty_dict', {'original': {}, 'expected': {}}),
@@ -351,11 +351,18 @@ class TestEnsureDict(testscenarios.TestWithScenarios):
]
def test_expected(self):
self.assertEqual(self.expected, misc.ensure_dict(self.original))
self.assertFalse(self.expected is misc.ensure_dict(self.original))
self.assertEqual(self.expected, misc.safe_copy_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 = [
('list', {'original': [1, 2], 'exception': TypeError}),
('tuple', {'original': (1, 2), 'exception': TypeError}),
@@ -363,4 +370,4 @@ class TestEnsureDictRaises(testscenarios.TestWithScenarios):
]
def test_exceptions(self):
self.assertRaises(self.exception, misc.ensure_dict, self.original)
self.assertRaises(self.exception, misc.safe_copy_dict, self.original)

View File

@@ -584,8 +584,13 @@ def is_iterable(obj):
isinstance(obj, collections.Iterable))
def ensure_dict(obj):
"""Copy an existing dictionary or default to empty dict...."""
def safe_copy_dict(obj):
"""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:
return {}
# default to a shallow copy to avoid most ownership issues