From 4f31882ea25685af4c159650f3c1aa4009916487 Mon Sep 17 00:00:00 2001 From: Guido Trotter <ultrotter@google.com> Date: Fri, 26 Jun 2009 17:33:10 +0100 Subject: [PATCH] _SplitKeyVal with no data return an empty dict If an empty string is passed to _SplitKeyVal, we should return {}, rather than {'': True}. Also test for the correct behavior. Signed-off-by: Guido Trotter <ultrotter@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/cli.py | 27 ++++++++++++++------------- test/ganeti.cli_unittest.py | 4 ++++ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/cli.py b/lib/cli.py index f8abe44ff..f4f32db5f 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -259,20 +259,21 @@ def _SplitKeyVal(opt, data): NO_PREFIX = "no_" UN_PREFIX = "-" kv_dict = {} - for elem in data.split(","): - if "=" in elem: - key, val = elem.split("=", 1) - else: - if elem.startswith(NO_PREFIX): - key, val = elem[len(NO_PREFIX):], False - elif elem.startswith(UN_PREFIX): - key, val = elem[len(UN_PREFIX):], None + if data: + for elem in data.split(","): + if "=" in elem: + key, val = elem.split("=", 1) else: - key, val = elem, True - if key in kv_dict: - raise errors.ParameterError("Duplicate key '%s' in option %s" % - (key, opt)) - kv_dict[key] = val + if elem.startswith(NO_PREFIX): + key, val = elem[len(NO_PREFIX):], False + elif elem.startswith(UN_PREFIX): + key, val = elem[len(UN_PREFIX):], None + else: + key, val = elem, True + if key in kv_dict: + raise errors.ParameterError("Duplicate key '%s' in option %s" % + (key, opt)) + kv_dict[key] = val return kv_dict diff --git a/test/ganeti.cli_unittest.py b/test/ganeti.cli_unittest.py index 29734ae0c..b86486bf4 100755 --- a/test/ganeti.cli_unittest.py +++ b/test/ganeti.cli_unittest.py @@ -75,6 +75,10 @@ class TestSplitKeyVal(unittest.TestCase): self.failUnlessRaises(ParameterError, cli._SplitKeyVal, "option", data) + def testEmptyData(self): + """Test how we handle splitting an empty string""" + self.failUnlessEqual(cli._SplitKeyVal("option", ""), {}) + class TestToStream(unittest.TestCase): """Thes the ToStream functions""" -- GitLab