Commit 4f31882e authored by Guido Trotter's avatar Guido Trotter
Browse files

_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: default avatarGuido Trotter <>
Reviewed-by: default avatarIustin Pop <>
parent d1a7d66f
...@@ -259,20 +259,21 @@ def _SplitKeyVal(opt, data): ...@@ -259,20 +259,21 @@ def _SplitKeyVal(opt, data):
NO_PREFIX = "no_" NO_PREFIX = "no_"
kv_dict = {} kv_dict = {}
for elem in data.split(","): if data:
if "=" in elem: for elem in data.split(","):
key, val = elem.split("=", 1) if "=" in elem:
else: key, val = elem.split("=", 1)
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: else:
key, val = elem, True if elem.startswith(NO_PREFIX):
if key in kv_dict: key, val = elem[len(NO_PREFIX):], False
raise errors.ParameterError("Duplicate key '%s' in option %s" % elif elem.startswith(UN_PREFIX):
(key, opt)) key, val = elem[len(UN_PREFIX):], None
kv_dict[key] = val 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 return kv_dict
...@@ -75,6 +75,10 @@ class TestSplitKeyVal(unittest.TestCase): ...@@ -75,6 +75,10 @@ class TestSplitKeyVal(unittest.TestCase):
self.failUnlessRaises(ParameterError, cli._SplitKeyVal, self.failUnlessRaises(ParameterError, cli._SplitKeyVal,
"option", data) "option", data)
def testEmptyData(self):
"""Test how we handle splitting an empty string"""
self.failUnlessEqual(cli._SplitKeyVal("option", ""), {})
class TestToStream(unittest.TestCase): class TestToStream(unittest.TestCase):
"""Thes the ToStream functions""" """Thes the ToStream functions"""
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment