From 69eb1e1b73bdb5bec437d10acb923564bc5610a6 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Mon, 14 Dec 2015 16:16:04 -0800 Subject: [PATCH] Change name of misc.ensure_dict to misc.safe_copy_dict Change-Id: I2ff3b9bb4d0b8143163e4bf370f10c74e79dbd31 --- taskflow/conductors/base.py | 2 +- taskflow/engines/action_engine/runtime.py | 2 +- taskflow/engines/base.py | 2 +- taskflow/tests/unit/test_utils.py | 17 ++++++++++++----- taskflow/utils/misc.py | 9 +++++++-- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/taskflow/conductors/base.py b/taskflow/conductors/base.py index 694b213b..6cf84852 100644 --- a/taskflow/conductors/base.py +++ b/taskflow/conductors/base.py @@ -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() diff --git a/taskflow/engines/action_engine/runtime.py b/taskflow/engines/action_engine/runtime.py index 719f7563..7f4cf899 100644 --- a/taskflow/engines/action_engine/runtime.py +++ b/taskflow/engines/action_engine/runtime.py @@ -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): diff --git a/taskflow/engines/base.py b/taskflow/engines/base.py index 5330fc18..c479b6fb 100644 --- a/taskflow/engines/base.py +++ b/taskflow/engines/base.py @@ -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() diff --git a/taskflow/tests/unit/test_utils.py b/taskflow/tests/unit/test_utils.py index 6ea9f4fb..cf435003 100644 --- a/taskflow/tests/unit/test_utils.py +++ b/taskflow/tests/unit/test_utils.py @@ -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) diff --git a/taskflow/utils/misc.py b/taskflow/utils/misc.py index e837a426..3d748761 100644 --- a/taskflow/utils/misc.py +++ b/taskflow/utils/misc.py @@ -597,8 +597,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